UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

221 lines (187 loc) 5.88 kB
"use strict"; var inArray = require("../core/utils/array").inArray, each = require("../core/utils/iterator").each, Class = require("../core/class"), EventsMixin = require("../core/events_mixin"); /** * @name ViewCache * @publicName ViewCache * @type object * @inherits EventsMixin * @module framework/view_cache * @export default */ var ViewCache = Class.inherit({ /** * @name ViewCacheevents.viewRemoved * @publicName viewRemoved * @type classEventType * @type_function_param1 e:object * @type_function_param1_field1 viewInfo:object */ ctor: function ctor() { this._cache = {}; }, /** * @name ViewCachemethods.setView * @publicName setView(key, viewInfo) * @param1 key:string * @param2 viewInfo:object */ setView: function setView(key, viewInfo) { this._cache[key] = viewInfo; }, /** * @name ViewCachemethods.getView * @publicName getView(key) * @param1 key:string * @return object */ getView: function getView(key) { return this._cache[key]; }, /** * @name ViewCachemethods.removeView * @publicName removeView(key) * @param1 key:string * @return object */ removeView: function removeView(key) { var result = this._cache[key]; if (result) { delete this._cache[key]; this.fireEvent("viewRemoved", [{ viewInfo: result }]); } return result; }, /** * @name ViewCachemethods.clear * @publicName clear() */ clear: function clear() { var that = this; each(this._cache, function (key) { that.removeView(key); }); }, /** * @name ViewCachemethods.hasView * @publicName hasView(key) * @param1 key:string * @return boolean */ hasView: function hasView(key) { return key in this._cache; } }).include(EventsMixin); var NullViewCache = ViewCache.inherit({ setView: function setView(key, viewInfo) { this.callBase(key, viewInfo); this.removeView(key); } }); function delegateEvent(eventName, source, target) { source.on(eventName, function () { target.fireEvent(eventName, arguments); }); } var ConditionalViewCacheDecorator = Class.inherit({ ctor: function ctor(options) { this._filter = options.filter; this._viewCache = options.viewCache; this.viewRemoved = this._viewCache.viewRemoved; delegateEvent("viewRemoved", this._viewCache, this); }, setView: function setView(key, viewInfo) { this._viewCache.setView(key, viewInfo); if (!this._filter(key, viewInfo)) { this._viewCache.removeView(key); } }, getView: function getView(key) { return this._viewCache.getView(key); }, removeView: function removeView(key) { return this._viewCache.removeView(key); }, clear: function clear() { return this._viewCache.clear(); }, hasView: function hasView(key) { return this._viewCache.hasView(key); } }).include(EventsMixin); var DEFAULT_VIEW_CACHE_CAPACITY = 5 /* T317332 */; var CapacityViewCacheDecorator = Class.inherit({ ctor: function ctor(options) { this._keys = []; this._size = options.size || DEFAULT_VIEW_CACHE_CAPACITY; this._viewCache = options.viewCache; this.viewRemoved = this._viewCache.viewRemoved; delegateEvent("viewRemoved", this._viewCache, this); }, setView: function setView(key, viewInfo) { if (!this.hasView(key)) { if (this._keys.length === this._size) { this.removeView(this._keys[0]); } this._keys.push(key); } this._viewCache.setView(key, viewInfo); }, getView: function getView(key) { var index = inArray(key, this._keys); if (index < 0) { return null; } this._keys.push(key); this._keys.splice(index, 1); return this._viewCache.getView(key); }, removeView: function removeView(key) { var index = inArray(key, this._keys); if (index > -1) { this._keys.splice(index, 1); } return this._viewCache.removeView(key); }, clear: function clear() { this._keys = []; return this._viewCache.clear(); }, hasView: function hasView(key) { return this._viewCache.hasView(key); } }).include(EventsMixin); var HistoryDependentViewCacheDecorator = Class.inherit({ ctor: function ctor(options) { this._viewCache = options.viewCache || new ViewCache(); this._navigationManager = options.navigationManager; this._navigationManager.on("itemRemoved", this._onNavigationItemRemoved.bind(this)); this.viewRemoved = this._viewCache.viewRemoved; delegateEvent("viewRemoved", this._viewCache, this); }, _onNavigationItemRemoved: function _onNavigationItemRemoved(item) { this.removeView(item.key); }, setView: function setView(key, viewInfo) { this._viewCache.setView(key, viewInfo); }, getView: function getView(key) { return this._viewCache.getView(key); }, removeView: function removeView(key) { return this._viewCache.removeView(key); }, clear: function clear() { return this._viewCache.clear(); }, hasView: function hasView(key) { return this._viewCache.hasView(key); } }).include(EventsMixin); module.exports = ViewCache; module.exports.NullViewCache = NullViewCache; module.exports.ConditionalViewCacheDecorator = ConditionalViewCacheDecorator; module.exports.CapacityViewCacheDecorator = CapacityViewCacheDecorator; module.exports.HistoryDependentViewCacheDecorator = HistoryDependentViewCacheDecorator;