[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yui/build/moodle-core-dock-loader/ -> moodle-core-dock-loader.js (source)

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


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