JAVASCRIPT   42
Component
Guest on 29th April 2022 01:18:58 AM


  1. (function (global, factory) {
  2.   if (typeof define === "function" && define.amd) {
  3.     define('/Component', ['exports', 'jquery', 'State'], factory);
  4.   } else if (typeof exports !== "undefined") {
  5.     factory(exports, require('jquery'), require('State'));
  6.   } else {
  7.     var mod = {
  8.       exports: {}
  9.     };
  10.     factory(mod.exports, global.jQuery, global.State);
  11.     global.Component = mod.exports;
  12.   }
  13. })(this, function (exports, _jquery, _State) {
  14.   'use strict';
  15.  
  16.   Object.defineProperty(exports, "__esModule", {
  17.     value: true
  18.   });
  19.  
  20.   var _jquery2 = babelHelpers.interopRequireDefault(_jquery);
  21.  
  22.   var _State2 = babelHelpers.interopRequireDefault(_State);
  23.  
  24.   if (typeof Object.assign === 'undefined') Object.assign = _jquery2.default.extend;
  25.  
  26.   var _class = function () {
  27.     function _class() {
  28.       var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  29.       babelHelpers.classCallCheck(this, _class);
  30.  
  31.       this.$el = options.$el ? options.$el : (0, _jquery2.default)(document);
  32.       this.el = this.$el[0];
  33.       delete options.$el;
  34.  
  35.       this.children = this.getDefaultChildren();
  36.       this.actions = this.getDefaultActions();
  37.       this.initialState = this.getDefaultState();
  38.  
  39.       this._willProcess = _jquery2.default.Callbacks();
  40.       this._processed = _jquery2.default.Callbacks();
  41.  
  42.       if (this.willProcess) this._willProcess.add(this.willProcess);
  43.       if (this.processed) this._processed.add(this.processed);
  44.  
  45.       this.isProcessed = false;
  46.  
  47.       this.mix(options);
  48.  
  49.       this.state = null;
  50.     }
  51.  
  52.     babelHelpers.createClass(_class, [{
  53.       key: '_combineInitialState',
  54.       value: function _combineInitialState() {
  55.         var childrenInitialState = {};
  56.         for (var i = 0, l = this.children.length; i < l; i++) {
  57.           var child = this.children[i];
  58.  
  59.           Object.assign(childrenInitialState, child.initialState);
  60.         }
  61.         return Object.assign(childrenInitialState, this.initialState);
  62.       }
  63.     }, {
  64.       key: '_process',
  65.       value: function _process(state) {
  66.         this._willProcess.fireWith(this);
  67.  
  68.         this.state = state ? state : new _State2.default(this.initialState);
  69.  
  70.         this._registerActions();
  71.  
  72.         for (var i = 0, l = this.children.length; i < l; i++) {
  73.           this.children[i]._process(this.state);
  74.           this.children[i].isProcessed = true;
  75.         }
  76.  
  77.         this._processed.fireWith(this);
  78.       }
  79.     }, {
  80.       key: '_registerActions',
  81.       value: function _registerActions() {
  82.         var _this = this;
  83.  
  84.         var actions = this.actions;
  85.  
  86.         var _loop = function _loop(state) {
  87.           var action = actions[state];
  88.           if (typeof action === 'function') {
  89.             _this.state.on(state, function () {
  90.               var _actions$state;
  91.  
  92.               for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
  93.                 params[_key] = arguments[_key];
  94.               }
  95.  
  96.               (_actions$state = actions[state]).apply.apply(_actions$state, [_this].concat(params));
  97.             });
  98.           } else if (typeof action === 'string' && typeof _this[action] === 'function') {
  99.             _this.state.on(state, function () {
  100.               var _action;
  101.  
  102.               for (var _len2 = arguments.length, params = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  103.                 params[_key2] = arguments[_key2];
  104.               }
  105.  
  106.               (_action = _this[action]).apply.apply(_action, [_this].concat(params));
  107.             });
  108.           }
  109.         };
  110.  
  111.         for (var state in actions) {
  112.           _loop(state);
  113.         }
  114.       }
  115.     }, {
  116.       key: 'run',
  117.       value: function run() {
  118.         if (!this.isProcessed) {
  119.           this._process();
  120.           this.isProcessed = true;
  121.         }
  122.  
  123.         this.setState.apply(this, arguments);
  124.       }
  125.     }, {
  126.       key: 'setState',
  127.       value: function setState() {
  128.         if (this.state) {
  129.           var _state;
  130.  
  131.           (_state = this.state).set.apply(_state, arguments);
  132.         }
  133.       }
  134.     }, {
  135.       key: 'getState',
  136.       value: function getState() {
  137.         if (this.state) {
  138.           var _state2;
  139.  
  140.           return (_state2 = this.state).get.apply(_state2, arguments);
  141.         } else {
  142.           return null;
  143.         }
  144.       }
  145.     }, {
  146.       key: 'getDefaultState',
  147.       value: function getDefaultState() {
  148.         return {};
  149.       }
  150.     }, {
  151.       key: 'getDefaultChildren',
  152.       value: function getDefaultChildren() {
  153.         return [];
  154.       }
  155.     }, {
  156.       key: 'getDefaultActions',
  157.       value: function getDefaultActions() {
  158.         return {};
  159.       }
  160.     }, {
  161.       key: 'mix',
  162.       value: function mix() {
  163.         var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  164.  
  165.         if (this.isInit) return;
  166.  
  167.         var _options$children = options.children;
  168.         var children = _options$children === undefined ? [] : _options$children;
  169.         var _options$actions = options.actions;
  170.         var actions = _options$actions === undefined ? {} : _options$actions;
  171.         var _options$state = options.state;
  172.         var state = _options$state === undefined ? {} : _options$state;
  173.         var _options$willProcess = options.willProcess;
  174.         var willProcess = _options$willProcess === undefined ? false : _options$willProcess;
  175.         var _options$processed = options.processed;
  176.         var processed = _options$processed === undefined ? false : _options$processed;
  177.  
  178.  
  179.         children = children.filter(function (child) {
  180.           return child instanceof Component;
  181.         });
  182.         this.children = [].concat(babelHelpers.toConsumableArray(this.children), babelHelpers.toConsumableArray(children));
  183.  
  184.         this.actions = Object.assign({}, this.actions, actions);
  185.  
  186.         this.initialState = Object.assign({}, this.initialState, state);
  187.         this.initialState = this._combineInitialState();
  188.  
  189.         if (typeof willProcess !== 'function') this._willProcess.add(willProcess);
  190.         if (typeof processed !== 'function') this._processed.add(processed);
  191.  
  192.         delete options.children;
  193.         delete options.actions;
  194.         delete options.state;
  195.         delete options.willProcess;
  196.         delete options.processed;
  197.  
  198.         Object.assign(this, options);
  199.         return this;
  200.       }
  201.     }, {
  202.       key: 'triggerResize',
  203.       value: function triggerResize() {
  204.         if (document.createEvent) {
  205.           var ev = document.createEvent('Event');
  206.           ev.initEvent('resize', true, true);
  207.           window.dispatchEvent(ev);
  208.         } else {
  209.           element = document.documentElement;
  210.           var event = document.createEventObject();
  211.           element.fireEvent('onresize', event);
  212.         }
  213.       }
  214.     }]);
  215.     return _class;
  216.   }();
  217.  
  218.   exports.default = _class;
  219. });

Raw Paste

Login or Register to edit or fork this paste. It's free.