[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/yui/src/util/js/ -> page.js (source)

   1  /* global YUI */
   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: 'h4'
  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 + ' ' +
 146                       Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' +
 147                      this.SELECTORS.PAGE);
 148      },
 149  
 150      /**
 151       * Is the given element a page element?
 152       *
 153       * @method isPage
 154       * @param page Page node
 155       * @return boolean
 156       */
 157      isPage: function(page) {
 158          if (!page) {
 159              return false;
 160          }
 161          return page.hasClass(this.CSS.PAGE);
 162      },
 163  
 164      /**
 165       * Does the page have atleast one slot?
 166       *
 167       * @method isEmpty
 168       * @param page Page node
 169       * @return boolean
 170       */
 171      isEmpty: function(page) {
 172          var activity = page.next('li.activity');
 173          if (!activity) {
 174              return true;
 175          }
 176          return !activity.hasClass('slot');
 177      },
 178  
 179      /**
 180       * Add a page and related elements to the list of slots.
 181       *
 182       * @method add
 183       * @param beforenode Int | Node | HTMLElement | String to add
 184       * @return page Page node
 185       */
 186      add: function(beforenode) {
 187          var pagenumber = this.getNumber(this.getPageFromSlot(beforenode)) + 1;
 188          var pagehtml = M.mod_quiz.resource_toolbox.get('config').pagehtml;
 189  
 190          // Normalise the page number.
 191          pagehtml = pagehtml.replace(/%%PAGENUMBER%%/g, pagenumber);
 192  
 193          // Create the page node.
 194          var page = Y.Node.create(pagehtml);
 195  
 196          // Assign is as a drop target.
 197          YUI().use('dd-drop', function(Y) {
 198              var drop = new Y.DD.Drop({
 199                  node: page,
 200                  groups: M.mod_quiz.dragres.groups
 201              });
 202              page.drop = drop;
 203          });
 204  
 205          // Insert in the correct place.
 206          beforenode.insert(page, 'after');
 207  
 208          // Enhance the add menu to make if fully visible and clickable.
 209          M.core.actionmenu.newDOMNode(page);
 210          return page;
 211      },
 212  
 213      /**
 214       * Remove a page and related elements from the list of slots.
 215       *
 216       * @method remove
 217       * @param page Page node
 218       * @return void
 219       */
 220      remove: function(page, keeppagebreak) {
 221          // Remove page break from previous slot.
 222          var previousslot = page.previous(Y.Moodle.mod_quiz.util.slot.SELECTORS.SLOT);
 223          if (!keeppagebreak && previousslot) {
 224              Y.Moodle.mod_quiz.util.slot.removePageBreak(previousslot);
 225          }
 226          page.remove();
 227      },
 228  
 229      /**
 230       * Reset the order of the numbers given to each page.
 231       *
 232       * @method reorderPages
 233       * @return void
 234       */
 235      reorderPages: function() {
 236          // Get list of page nodes.
 237          var pages = this.getPages();
 238          var currentpagenumber = 0;
 239          // Loop through pages incrementing the number each time.
 240          pages.each(function(page) {
 241              // Is the page empty?
 242              if (this.isEmpty(page)) {
 243                  var keeppagebreak = page.next('li.slot') ? true : false;
 244                  this.remove(page, keeppagebreak);
 245                  return;
 246              }
 247  
 248              currentpagenumber++;
 249              // Set page number.
 250              this.setNumber(page, currentpagenumber);
 251              this.setId(page, currentpagenumber);
 252          }, this);
 253  
 254          // Reorder action menus
 255          this.reorderActionMenus();
 256      },
 257  
 258      /**
 259       * Reset the order of the numbers given to each action menu.
 260       *
 261       * @method reorderActionMenus
 262       * @return void
 263       */
 264      reorderActionMenus: function() {
 265          // Get list of action menu nodes.
 266          var actionmenus = this.getActionMenus();
 267          // Loop through pages incrementing the number each time.
 268          actionmenus.each(function(actionmenu, key) {
 269              var previousActionMenu = actionmenus.item(key - 1),
 270                  previousActionMenunumber = 0;
 271              if (previousActionMenu) {
 272                  previousActionMenunumber = this.getActionMenuId(previousActionMenu);
 273              }
 274              var id = previousActionMenunumber + 1;
 275  
 276              // Set menu id.
 277              this.setActionMenuId(actionmenu, id);
 278  
 279              // Update action-menu-1-menubar
 280              var menubar = actionmenu.one(this.SELECTORS.ACTIONMENUBAR);
 281              menubar.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUBARIDSUFFIX);
 282  
 283              // Update action-menu-1-menu
 284              var menumenu = actionmenu.one(this.SELECTORS.ACTIONMENUMENU);
 285              menumenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUMENUIDSUFFIX);
 286  
 287              // Update the URL of the add-section action.
 288              menumenu.one('a.addasection').set('href',
 289                      menumenu.one('a.addasection').get('href').replace(/\baddsectionatpage=\d/, 'addsectionatpage=' + id));
 290  
 291          }, this);
 292      },
 293  
 294      /**
 295       * Returns a list of all page elements.
 296       *
 297       * @method getActionMenus
 298       * @return {node[]} An array containing page nodes.
 299       */
 300      getActionMenus: function() {
 301          return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' +
 302                       Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' +
 303                       this.SELECTORS.ACTIONMENU);
 304      },
 305  
 306      /**
 307       * Returns the ID for the provided action menu.
 308       *
 309       * @method getId
 310       * @param actionmenu {Node} The actionmenu to find an ID for.
 311       * @return {Number|false} The ID of the actionmenu in question or false if no ID was found.
 312       */
 313      getActionMenuId: function(actionmenu) {
 314          // We perform a simple substitution operation to get the ID.
 315          var id = actionmenu.get('id').replace(
 316                  this.CONSTANTS.ACTIONMENUIDPREFIX, '');
 317  
 318          // Attempt to validate the ID.
 319          id = parseInt(id, 10);
 320          if (typeof id === 'number' && isFinite(id)) {
 321              return id;
 322          }
 323          return false;
 324      },
 325  
 326      /**
 327       * Updates the page id for the provided page.
 328       *
 329       * @method setId
 330       * @param page {Node} The page to update the number for.
 331       * @param id int The id value.
 332       * @return void
 333       */
 334      setActionMenuId: function(actionmenu, id) {
 335          actionmenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id);
 336      }
 337  };


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1