UNPKG

react-blades

Version:
393 lines (345 loc) 12 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 14); /******/ }) /************************************************************************/ /******/ ({ /***/ 14: /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _EventEmitter2 = __webpack_require__(6); var _EventEmitter3 = _interopRequireDefault(_EventEmitter2); var _fn = __webpack_require__(2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint-disable no-underscore-dangle */ var defaultBladeProps = { isVisible: true, isActive: true, width: 300, depth: 0, orientation: 'horizontal' }; var BladeManager = function (_EventEmitter) { _inherits(BladeManager, _EventEmitter); function BladeManager(options) { _classCallCheck(this, BladeManager); var _this = _possibleConstructorReturn(this, (BladeManager.__proto__ || Object.getPrototypeOf(BladeManager)).call(this)); _this.options = Object.assign({}, options); _this.blades = []; _this.bladesPreventingNavigation = []; return _this; } _createClass(BladeManager, [{ key: 'add', value: function () { function add(blade) { if (!blade || !blade.id) { throw new Error('A blade with an ID is mandatory.'); } if (this._findById(blade.id)) { throw new Error('Blade with ID=' + String(blade.id) + ' already exists.'); } if (blade.width && !(0, _fn.isNumber)(blade.width)) { throw new Error('Blade width must be a numerical value'); } this._addToCollection(blade); this._resetBladeVisibility(); this._activateById(blade.id); this.trigger('render'); } return add; }() /** * Removes all blades following and including the provided ID. * @param {*string} id The Blade ID. */ }, { key: 'remove', value: function () { function remove(id, successCb) { var _this2 = this; if (!this._findById(id)) return; if (this._shouldPreventNavigation()) { this._notifyNavigationPrevented(function () { _this2.remove(id); if (successCb) successCb(); }); return; } this._removeFromCollection(id); if (this.blades.length > 0) { this._resetBladeVisibility(); this._activateById(this._last().id); } this.trigger('render'); if (successCb) successCb(); } return remove; }() }, { key: 'back', value: function () { function back(id, successCb) { var _this3 = this; if (this._shouldPreventNavigation()) { this._notifyNavigationPrevented(function () { _this3.back(id); if (successCb) successCb(); }); return; } if (id) { var end = Math.min(this.blades.findIndex(function (b) { return b.id === id; }) + 1, this.blades.length); this.blades = this.blades.slice(0, end); } else { this.blades = this.blades.slice(0, this.blades.length - 1); } this._resetBladeVisibility(); this._activateById(id); this.trigger('render'); if (successCb) successCb(); } return back; }() }, { key: 'activate', value: function () { function activate(id) { if (!id) throw new Error('Parameter "id" must be defined.'); this._activateById(id); this.trigger('render'); } return activate; }() }, { key: 'preventNavigation', value: function () { function preventNavigation(id) { if (!id) throw new Error('Parameter "id" must be defined.'); if (this.bladesPreventingNavigation.indexOf(id) <= 0) { this.bladesPreventingNavigation.splice(0, 0, id); } } return preventNavigation; }() }, { key: 'allowNavigation', value: function () { function allowNavigation(id) { if (!id) throw new Error('Parameter "id" must be defined.'); var indexOfBladeToRemove = this.bladesPreventingNavigation.indexOf(id); if (indexOfBladeToRemove > -1) { this.bladesPreventingNavigation.splice(indexOfBladeToRemove, 1); } } return allowNavigation; }() }, { key: 'canNavigate', value: function () { function canNavigate() { return this.bladesPreventingNavigation.length > 0; } return canNavigate; }() }, { key: 'getAll', value: function () { function getAll() { return this.blades.slice(0); } return getAll; }() }, { key: 'getVisible', value: function () { function getVisible() { return this.blades.filter(function (x) { return x.isVisible; }); } return getVisible; }() }, { key: '_last', value: function () { function _last() { return this.blades[this.blades.length - 1]; } return _last; }() }, { key: '_findById', value: function () { function _findById(id) { return this.blades.find(function (b) { return b.id === id; }); } return _findById; }() }, { key: '_addToCollection', value: function () { function _addToCollection(blade) { var tmpBlade = Object.assign({}, defaultBladeProps, blade, { index: Object.keys(this.blades).length }); if (this.options.orientation === 'vertical') { tmpBlade.depth = 1; tmpBlade.width = '100%'; } this.blades = this.blades.concat(tmpBlade); } return _addToCollection; }() }, { key: '_removeFromCollection', value: function () { function _removeFromCollection(id) { this.blades = this.blades.slice(0, this.blades.findIndex(function (b) { return b.id === id; })); } return _removeFromCollection; }() }, { key: '_activateById', value: function () { function _activateById(id) { this.blades = this.blades.map(function (b) { return Object.assign({}, b, { isActive: b.id === id }); }); } return _activateById; }() }, { key: '_resetBladeVisibility', value: function () { function _resetBladeVisibility() { var bladeWithDepth = [].concat(this.blades).reverse().find(function (b) { return b.depth > 0; }); if (bladeWithDepth) { this.blades.forEach(function (b) { return b.isVisible = false; }); //eslint-disable-line this.blades.slice(this.blades.indexOf(bladeWithDepth)).forEach(function (b) { return b.isVisible = true; }); //eslint-disable-line } else { this.blades.forEach(function (b) { return b.isVisible = true; }); //eslint-disable-line } } return _resetBladeVisibility; }() }, { key: '_shouldPreventNavigation', value: function () { function _shouldPreventNavigation() { return this.bladesPreventingNavigation.length > 0; } return _shouldPreventNavigation; }() }, { key: '_notifyNavigationPrevented', value: function () { function _notifyNavigationPrevented(navigateFn) { var bladeIdToBeRemoved = this.bladesPreventingNavigation[0]; this.trigger('navigationPrevented', { id: bladeIdToBeRemoved, navigate: navigateFn }); } return _notifyNavigationPrevented; }() }]); return BladeManager; }(_EventEmitter3['default']); exports['default'] = BladeManager; /***/ }), /***/ 2: /***/ (function(module, exports) { module.exports = require("../utils/fn"); /***/ }), /***/ 6: /***/ (function(module, exports) { module.exports = require("../utils/EventEmitter"); /***/ }) /******/ }); //# sourceMappingURL=BladeManager.js.map