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