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