API Docs for: undefined
Show:

File: js/async-pause.js

  1. /**
  2.  * @module gallery-async-pause
  3.  */
  4. (function (Y, moduleName) {
  5.     'use strict';
  6.    
  7.     var _string__args = '_args',
  8.         _string__resumed = '_resumed',
  9.         _string_host = 'host',
  10.         _string_paused = 'paused',
  11.        
  12.         _false = false,
  13.         _true = true,
  14.        
  15.         _DoPrevent = Y.Do.Prevent,
  16.         _Plugin = Y.Plugin;

  17.     /**
  18.      * Asynchronous command runner pause plugin.
  19.      * @class AsyncPause
  20.      * @extends Plugin.Base
  21.      * @namespace Plugin
  22.      * @param {Object} config Configuration Object.
  23.      */
  24.     _Plugin.AsyncPause = Y.Base.create(moduleName, _Plugin.Base, [], {
  25.         initializer: function () {
  26.             var me = this;
  27.                
  28.             if (me.get(_string_host).get('mode') === 'queue') {
  29.                 me.beforeHostMethod('_runQueue', function () {
  30.                     if (me.get(_string_paused)) {
  31.                         me._set(_string__args, arguments);
  32.                         return new _DoPrevent(_string_paused);
  33.                     }

  34.                     return null;
  35.                 });
  36.             }
  37.         },
  38.         /**
  39.          * Pause the run.  Does not stop a command that is currently running,
  40.          * the run will pause before the next command runs.
  41.          * @method pause
  42.          * @chainable
  43.          */
  44.         pause: function () {
  45.             return this._set(_string_paused, _true);
  46.         },
  47.         /**
  48.          * Resumes a paused run.  If a command is currently running, the paused
  49.          * state may not be updated immediately.  Resume does nothing if the run
  50.          * is not paused or not started yet or already complete.
  51.          * @method resume
  52.          * @chainable
  53.          */
  54.         resume: function () {
  55.             var argsChangeListener,
  56.                 completeListener,
  57.                 me = this,
  58.                
  59.                 args = me.get(_string__args),
  60.                 host = me.get(_string_host),
  61.                 runQueue = host._runQueue,
  62.                
  63.                 resume = function (args) {
  64.                     me._setAttrs({
  65.                         paused: _false,
  66.                         _args: null,
  67.                         _resumed: _false
  68.                     });
  69.                     runQueue.apply(host, args);
  70.                 };
  71.            
  72.             if (!me.get(_string_paused) || me.get(_string__resumed)) {
  73.                 return me;
  74.             }
  75.            
  76.             if (!host.get('started') || host.get('completed')) {
  77.                 me._set(_string_paused, _false);
  78.                 return me;
  79.             }

  80.             if (args) {
  81.                 resume(args);
  82.                 return me;
  83.             }
  84.            
  85.             me._set(_string__resumed, _true);
  86.            
  87.             argsChangeListener = me.once('_argsChange', function (eventFacade) {
  88.                 completeListener.detach();
  89.                 resume(eventFacade.newVal);
  90.             });
  91.            
  92.             completeListener = host.on('complete', function () {
  93.                 argsChangeListener.detach();
  94.             });
  95.            
  96.             return me;
  97.         }
  98.     }, {
  99.         ATTRS: {
  100.             /**
  101.              * Boolean value indicating the paused status of the run.
  102.              * @attribute paused
  103.              * @default false
  104.              * @readonly
  105.              * @type Boolean
  106.              */
  107.             paused: {
  108.                 readonly: _true,
  109.                 value: _false
  110.             },
  111.             /**
  112.              * Paused _runQueue arguments.
  113.              * @attribute _args
  114.              * @protected
  115.              * @readonly
  116.              * @type Array
  117.              */
  118.             _args: {
  119.                 readOnly: _true,
  120.                 value: null
  121.             },
  122.             /**
  123.              * Boolean value indicating the resumed status of the run.
  124.              * @attribute _resumed
  125.              * @protected
  126.              * @readonly
  127.              * @type Array
  128.              */
  129.             _resumed: {
  130.                 readOnly: _true,
  131.                 value: _false
  132.             }
  133.         },
  134.         NS: 'pause'
  135.     });
  136. }(Y, arguments[1]));
  137.