[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 /** 2 * A collection of utility classes for use with pages. 3 * 4 * @module moodle-mod_quiz-util 5 * @submodule moodle-mod_quiz-util-page 6 */ 7 8 Y.namespace('Moodle.mod_quiz.util.page'); 9 10 /** 11 * A collection of utility classes for use with pages. 12 * 13 * @class Moodle.mod_quiz.util.page 14 * @static 15 */ 16 Y.Moodle.mod_quiz.util.page = { 17 CSS: { 18 PAGE : 'page' 19 }, 20 CONSTANTS: { 21 ACTIONMENUIDPREFIX: 'action-menu-', 22 ACTIONMENUBARIDSUFFIX: '-menubar', 23 ACTIONMENUMENUIDSUFFIX: '-menu', 24 PAGEIDPREFIX: 'page-', 25 PAGENUMBERPREFIX: M.util.get_string('page', 'moodle') + ' ' 26 }, 27 SELECTORS: { 28 ACTIONMENU: 'div.moodle-actionmenu', 29 ACTIONMENUBAR: 'ul.menubar', 30 ACTIONMENUMENU: 'ul.menu', 31 PAGE: 'li.page', 32 INSTANCENAME: '.instancename', 33 NUMBER: 'span.text' 34 }, 35 36 /** 37 * Retrieve the page item from one of it's child Nodes. 38 * 39 * @method getPageFromComponent 40 * @param pagecomponent {Node} The component Node. 41 * @return {Node|null} The Page Node. 42 */ 43 getPageFromComponent: function(pagecomponent) { 44 return Y.one(pagecomponent).ancestor(this.SELECTORS.PAGE, true); 45 }, 46 47 /** 48 * Retrieve the page item from one of it's previous siblings. 49 * 50 * @method getPageFromSlot 51 * @param pagecomponent {Node} The component Node. 52 * @return {Node|null} The Page Node. 53 */ 54 getPageFromSlot: function(slot) { 55 return Y.one(slot).previous(this.SELECTORS.PAGE); 56 }, 57 58 /** 59 * Returns the page ID for the provided page. 60 * 61 * @method getId 62 * @param page {Node} The page to find an ID for. 63 * @return {Number|false} The ID of the page in question or false if no ID was found. 64 */ 65 getId: function(page) { 66 // We perform a simple substitution operation to get the ID. 67 var id = page.get('id').replace( 68 this.CONSTANTS.PAGEIDPREFIX, ''); 69 70 // Attempt to validate the ID. 71 id = parseInt(id, 10); 72 if (typeof id === 'number' && isFinite(id)) { 73 return id; 74 } 75 return false; 76 }, 77 78 /** 79 * Updates the page id for the provided page. 80 * 81 * @method setId 82 * @param page {Node} The page to update the number for. 83 * @param id int The id value. 84 * @return void 85 */ 86 setId: function(page, id) { 87 page.set('id', this.CONSTANTS.PAGEIDPREFIX + id); 88 }, 89 90 /** 91 * Determines the page name for the provided page. 92 * 93 * @method getName 94 * @param page {Node} The page to find a name for. 95 * @return {string|false} The name of the page in question or false if no ID was found. 96 */ 97 getName: function(page) { 98 var instance = page.one(this.SELECTORS.INSTANCENAME); 99 if (instance) { 100 return instance.get('firstChild').get('data'); 101 } 102 return null; 103 }, 104 105 /** 106 * Determines the page number for the provided page. 107 * 108 * @method getNumber 109 * @param page {Node} The page to find a number for. 110 * @return {Number|false} The number of the page in question or false if no number was found. 111 */ 112 getNumber: function(page) { 113 // We perform a simple substitution operation to get the number. 114 var number = page.one(this.SELECTORS.NUMBER).get('text').replace( 115 this.CONSTANTS.PAGENUMBERPREFIX, ''); 116 117 // Attempt to validate the ID. 118 number = parseInt(number, 10); 119 if (typeof number === 'number' && isFinite(number)) { 120 return number; 121 } 122 return false; 123 }, 124 125 /** 126 * Updates the page number for the provided page. 127 * 128 * @method setNumber 129 * @param page {Node} The page to update the number for. 130 * @return void 131 */ 132 setNumber: function(page, number) { 133 page.one(this.SELECTORS.NUMBER).set('text', this.CONSTANTS.PAGENUMBERPREFIX + number); 134 }, 135 136 /** 137 * Returns a list of all page elements. 138 * 139 * @method getPages 140 * @return {node[]} An array containing page nodes. 141 */ 142 getPages: function() { 143 return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.PAGE); 144 }, 145 146 /** 147 * Is the given element a page element? 148 * 149 * @method isPage 150 * @param page Page node 151 * @return boolean 152 */ 153 isPage: function(page) { 154 if (!page) { 155 return false; 156 } 157 return page.hasClass(this.CSS.PAGE); 158 }, 159 160 /** 161 * Does the page have atleast one slot? 162 * 163 * @method isEmpty 164 * @param page Page node 165 * @return boolean 166 */ 167 isEmpty: function(page) { 168 var activity = page.next('li.activity'); 169 if (!activity) { 170 return true; 171 } 172 return !activity.hasClass('slot'); 173 }, 174 175 /** 176 * Add a page and related elements to the list of slots. 177 * 178 * @method add 179 * @param beforenode Int | Node | HTMLElement | String to add 180 * @return page Page node 181 */ 182 add: function(beforenode) { 183 var pagenumber = this.getNumber(this.getPageFromSlot(beforenode)) + 1; 184 var pagehtml = M.mod_quiz.resource_toolbox.get('config').pagehtml; 185 186 // Normalise the page number. 187 pagehtml = pagehtml.replace(/%%PAGENUMBER%%/g, pagenumber); 188 189 // Create the page node. 190 var page = Y.Node.create(pagehtml); 191 192 // Assign is as a drop target. 193 YUI().use('dd-drop', function(Y) { 194 var drop = new Y.DD.Drop({ 195 node: page, 196 groups: M.mod_quiz.dragres.groups 197 }); 198 page.drop = drop; 199 }); 200 201 // Insert in the correct place. 202 beforenode.insert(page, 'after'); 203 204 // Enhance the add menu to make if fully visible and clickable. 205 M.core.actionmenu.newDOMNode(page); 206 return page; 207 }, 208 209 /** 210 * Remove a page and related elements from the list of slots. 211 * 212 * @method remove 213 * @param page Page node 214 * @return void 215 */ 216 remove: function(page, keeppagebreak) { 217 // Remove page break from previous slot. 218 var previousslot = page.previous(Y.Moodle.mod_quiz.util.slot.SELECTORS.SLOT); 219 if (!keeppagebreak && previousslot) { 220 Y.Moodle.mod_quiz.util.slot.removePageBreak(previousslot); 221 } 222 page.remove(); 223 }, 224 225 /** 226 * Reset the order of the numbers given to each page. 227 * 228 * @method reorderPages 229 * @return void 230 */ 231 reorderPages: function() { 232 // Get list of page nodes. 233 var pages = this.getPages(), currentpagenumber = 0; 234 // Loop through pages incrementing the number each time. 235 pages.each(function(page) { 236 // Is the page empty? 237 if (this.isEmpty(page)) { 238 var keeppagebreak = page.next('li.slot') ? true : false; 239 this.remove(page, keeppagebreak); 240 return; 241 } 242 243 currentpagenumber++; 244 // Set page number. 245 this.setNumber(page, currentpagenumber); 246 this.setId(page, currentpagenumber); 247 }, this); 248 249 // Reorder action menus 250 this.reorderActionMenus(); 251 }, 252 253 /** 254 * Reset the order of the numbers given to each action menu. 255 * 256 * @method reorderActionMenus 257 * @return void 258 */ 259 reorderActionMenus: function() { 260 // Get list of action menu nodes. 261 var actionmenus = this.getActionMenus(); 262 // Loop through pages incrementing the number each time. 263 actionmenus.each(function(actionmenu, key) { 264 var previousActionMenu = actionmenus.item(key - 1); 265 previousActionMenunumber = 0; 266 if (previousActionMenu) { 267 previousActionMenunumber = this.getActionMenuId(previousActionMenu); 268 } 269 var id = previousActionMenunumber + 1; 270 271 // Set menu id. 272 this.setActionMenuId(actionmenu, id); 273 274 // Update action-menu-1-menubar 275 var menubar = actionmenu.one(this.SELECTORS.ACTIONMENUBAR); 276 menubar.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUBARIDSUFFIX); 277 // Update action-menu-1-menu 278 var menumenu = actionmenu.one(this.SELECTORS.ACTIONMENUMENU); 279 menumenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUMENUIDSUFFIX); 280 }, this); 281 }, 282 283 /** 284 * Returns a list of all page elements. 285 * 286 * @method getActionMenus 287 * @return {node[]} An array containing page nodes. 288 */ 289 getActionMenus: function() { 290 return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.ACTIONMENU); 291 }, 292 293 /** 294 * Returns the ID for the provided action menu. 295 * 296 * @method getId 297 * @param actionmenu {Node} The actionmenu to find an ID for. 298 * @return {Number|false} The ID of the actionmenu in question or false if no ID was found. 299 */ 300 getActionMenuId: function(actionmenu) { 301 // We perform a simple substitution operation to get the ID. 302 var id = actionmenu.get('id').replace( 303 this.CONSTANTS.ACTIONMENUIDPREFIX, ''); 304 305 // Attempt to validate the ID. 306 id = parseInt(id, 10); 307 if (typeof id === 'number' && isFinite(id)) { 308 return id; 309 } 310 return false; 311 }, 312 313 /** 314 * Updates the page id for the provided page. 315 * 316 * @method setId 317 * @param page {Node} The page to update the number for. 318 * @param id int The id value. 319 * @return void 320 */ 321 setActionMenuId: function(actionmenu, id) { 322 actionmenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id); 323 } 324 };
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |