[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/yui/src/lockscroll/js/ -> lockscroll.js (source)

   1  /**
   2   * Provides the ability to lock the scroll for a page, allowing nested
   3   * locking.
   4   *
   5   * @module moodle-core-lockscroll
   6   */
   7  
   8  /**
   9   * Provides the ability to lock the scroll for a page.
  10   *
  11   * This is achieved by applying the class 'lockscroll' to the body Node.
  12   *
  13   * Nested widgets are also supported and the scroll lock is only removed
  14   * when the final plugin instance is disabled.
  15   *
  16   * @class M.core.LockScroll
  17   * @extends Plugin.Base
  18   */
  19  Y.namespace('M.core').LockScroll = Y.Base.create('lockScroll', Y.Plugin.Base, [], {
  20  
  21      /**
  22       * Whether the LockScroll has been activated.
  23       *
  24       * @property _enabled
  25       * @type Boolean
  26       * @protected
  27       */
  28      _enabled: false,
  29  
  30      /**
  31       * Handle destruction of the lockScroll instance, including disabling
  32       * of the current instance.
  33       *
  34       * @method destructor
  35       */
  36      destructor: function() {
  37          this.disableScrollLock();
  38      },
  39  
  40      /**
  41       * Start locking the page scroll.
  42       *
  43       * This is achieved by applying the lockscroll class to the body Node.
  44       *
  45       * A count of the total number of active, and enabled, lockscroll instances is also kept on
  46       * the body to ensure that premature disabling does not occur.
  47       *
  48       * @method enableScrollLock
  49       * @param {Boolean} forceOnSmallWindow Whether to enable the scroll lock, even for small window sizes.
  50       * @chainable
  51       */
  52      enableScrollLock: function(forceOnSmallWindow) {
  53          if (this.isActive()) {
  54              Y.log('LockScroll already active. Ignoring enable request', 'warn', 'moodle-core-lockscroll');
  55              return;
  56          }
  57  
  58          var dialogueHeight = this.get('host').get('boundingBox').get('region').height,
  59              // Most modern browsers use win.innerHeight, but some older versions of IE use documentElement.clientHeight.
  60              // We fall back to 0 if neither can be found which has the effect of disabling scroll locking.
  61              windowHeight = Y.config.win.innerHeight || Y.config.doc.documentElement.clientHeight || 0;
  62  
  63          if (!forceOnSmallWindow && dialogueHeight > (windowHeight - 10)) {
  64              Y.log('Dialogue height greater than window height. Ignoring enable request.', 'warn', 'moodle-core-lockscroll');
  65              return;
  66          }
  67  
  68          Y.log('Enabling LockScroll.', 'debug', 'moodle-core-lockscroll');
  69          this._enabled = true;
  70          var body = Y.one(Y.config.doc.body);
  71  
  72          // We use a CSS class on the body to handle the actual locking.
  73          body.addClass('lockscroll');
  74  
  75          // Increase the count of active instances - this is used to ensure that we do not
  76          // remove the locking when parent windows are still open.
  77          // Note: We cannot use getData here because data attributes are sandboxed to the instance that created them.
  78          var currentCount = parseInt(body.getAttribute('data-activeScrollLocks'), 10) || 0,
  79              newCount = currentCount + 1;
  80          body.setAttribute('data-activeScrollLocks', newCount);
  81          Y.log("Setting the activeScrollLocks count from " + currentCount + " to " + newCount,
  82                  'debug', 'moodle-core-lockscroll');
  83  
  84          return this;
  85      },
  86  
  87      /**
  88       * Stop locking the page scroll.
  89       *
  90       * The instance may be disabled but the scroll lock not removed if other instances of the
  91       * plugin are also active.
  92       *
  93       * @method disableScrollLock
  94       * @chainable
  95       */
  96      disableScrollLock: function() {
  97          if (this.isActive()) {
  98              Y.log('Disabling LockScroll.', 'debug', 'moodle-core-lockscroll');
  99              this._enabled = false;
 100  
 101              var body = Y.one(Y.config.doc.body);
 102  
 103              // Decrease the count of active instances.
 104              // Note: We cannot use getData here because data attributes are sandboxed to the instance that created them.
 105              var currentCount = parseInt(body.getAttribute('data-activeScrollLocks'), 10) || 1,
 106                  newCount = currentCount - 1;
 107  
 108              if (currentCount === 1) {
 109                  body.removeClass('lockscroll');
 110              }
 111  
 112              body.setAttribute('data-activeScrollLocks', currentCount - 1);
 113              Y.log("Setting the activeScrollLocks count from " + currentCount + " to " + newCount,
 114                      'debug', 'moodle-core-lockscroll');
 115          }
 116  
 117          return this;
 118      },
 119  
 120      /**
 121       * Return whether scroll locking is active.
 122       *
 123       * @method isActive
 124       * @return Boolean
 125       */
 126      isActive: function() {
 127          return this._enabled;
 128      }
 129  
 130  }, {
 131      NS: 'lockScroll',
 132      ATTRS: {
 133      }
 134  });


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1