[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yui/src/dock/js/ -> loader.js (source)

   1  var LOADERNAME = 'moodle-core-dock-loader';
   2  
   3  M.core = M.core || {};
   4  M.core.dock = M.core.dock || {};
   5  
   6  /**
   7   * Creates the move to dock icon for dockable blocks if it doesn't already exist.
   8   *
   9   * @static
  10   * @method M.core.dock.ensureMoveToIconExists
  11   * @param {Node} blocknode The Blocks node (.block[data-instanceid])
  12   */
  13  M.core.dock.ensureMoveToIconExists = function(blocknode) {
  14      if (blocknode.one('.moveto')) {
  15          return true;
  16      }
  17  
  18      var commands,
  19          moveto = Y.Node.create('<input type="image" class="moveto customcommand requiresjs" />'),
  20          blockaction = blocknode.one('.block_action'),
  21          icon = 't/block_to_dock',
  22          titleh2 = blocknode.one('.header .title h2');
  23  
  24      // Must set the image src separately of we get an error with XML strict headers
  25      if (Y.one(document.body).hasClass('dir-rtl')) {
  26          icon = icon + '_rtl';
  27      }
  28      moveto.setAttribute('alt', M.util.get_string('addtodock', 'block'));
  29      if (titleh2) {
  30          moveto.setAttribute('title', Y.Escape.html(M.util.get_string('dockblock', 'block', titleh2.getHTML())));
  31      }
  32      moveto.setAttribute('src', M.util.image_url(icon, 'moodle'));
  33  
  34      if (blockaction) {
  35          blockaction.prepend(moveto);
  36      } else {
  37          commands = blocknode.one('.header .title .commands');
  38          if (!commands && blocknode.one('.header .title')) {
  39              commands = Y.Node.create('<div class="commands"></div>');
  40              blocknode.one('.header .title').append(commands);
  41          }
  42          commands.append(moveto);
  43      }
  44      return true;
  45  };
  46  
  47  /**
  48   * Dock loader.
  49   *
  50   * The dock loader is repsponsible for loading and initialising the dock only when required.
  51   * By doing this we avoid the need to load unnecessary JavaScript into the page for the dock just incase
  52   * it is being used.
  53   *
  54   * @static
  55   * @namespace M.core.dock
  56   * @class Loader
  57   */
  58  M.core.dock.loader = M.core.dock.loader || {};
  59  
  60  /**
  61   * Delegation events
  62   * @property delegationEvents
  63   * @protected
  64   * @type {Array}
  65   */
  66  M.core.dock.loader.delegationEvents = [];
  67  
  68  /**
  69   * Initialises the dock loader.
  70   *
  71   * The dock loader works by either firing the dock immediately if there are already docked blocks.
  72   * Or if there are not any docked blocks delegating two events and then loading and firing the dock when one of
  73   * those delegated events is triggered.
  74   *
  75   * @method initLoader
  76   */
  77  M.core.dock.loader.initLoader = function() {
  78      Y.log('Dock loader initialising', 'debug', LOADERNAME);
  79      var dockedblocks = Y.all('.block[data-instanceid][data-dockable]'),
  80          body = Y.one(document.body),
  81          callback;
  82      dockedblocks.each(function() {
  83          var id = parseInt(this.getData('instanceid'), 10);
  84          Y.log('Dock loader watching block with instance id: ' + id, 'debug', LOADERNAME);
  85          M.core.dock.ensureMoveToIconExists(this);
  86      });
  87      if (dockedblocks.some(function(node) { return node.hasClass('dock_on_load'); })) {
  88          Y.log('Loading dock module', 'debug', LOADERNAME);
  89          Y.use('moodle-core-dock', function() {
  90              M.core.dock.init();
  91          });
  92      } else {
  93          callback = function(e) {
  94              var i,
  95                  block = this.ancestor('.block[data-instanceid]'),
  96                  instanceid = block.getData('instanceid');
  97              e.halt();
  98              for (i in M.core.dock.loader.delegationEvents) {
  99                  if (Y.Lang.isNumber(i) || Y.Lang.isString(i)) {
 100                      M.core.dock.loader.delegationEvents[i].detach();
 101                  }
 102              }
 103              block.addClass('dock_on_load');
 104              Y.log('Loading dock module', 'debug', LOADERNAME);
 105              Y.use('moodle-core-dock', function() {
 106                  M.util.set_user_preference('docked_block_instance_' + instanceid, 1);
 107                  M.core.dock.init();
 108              });
 109          };
 110          M.core.dock.loader.delegationEvents.push(body.delegate('click', callback, '.moveto'));
 111          M.core.dock.loader.delegationEvents.push(body.delegate('key', callback, '.moveto', 'enter'));
 112      }
 113  };


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