UNPKG

aurelia-bootstrap

Version:
316 lines (261 loc) 11.7 kB
/* */ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-templating', './repeat-strategy-locator', './repeat-utilities', './analyze-view-factory', './abstract-repeater'], function (exports, _aureliaDependencyInjection, _aureliaBinding, _aureliaTemplating, _repeatStrategyLocator, _repeatUtilities, _analyzeViewFactory, _abstractRepeater) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Repeat = undefined; function _initDefineProp(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } 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; } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.'); } var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; var Repeat = exports.Repeat = (_dec = (0, _aureliaTemplating.customAttribute)('repeat'), _dec2 = (0, _aureliaDependencyInjection.inject)(_aureliaTemplating.BoundViewFactory, _aureliaTemplating.TargetInstruction, _aureliaTemplating.ViewSlot, _aureliaTemplating.ViewResources, _aureliaBinding.ObserverLocator, _repeatStrategyLocator.RepeatStrategyLocator), _dec(_class = (0, _aureliaTemplating.templateController)(_class = _dec2(_class = (_class2 = function (_AbstractRepeater) { _inherits(Repeat, _AbstractRepeater); function Repeat(viewFactory, instruction, viewSlot, viewResources, observerLocator, strategyLocator) { var _this = _possibleConstructorReturn(this, _AbstractRepeater.call(this, { local: 'item', viewsRequireLifecycle: (0, _analyzeViewFactory.viewsRequireLifecycle)(viewFactory) })); _initDefineProp(_this, 'items', _descriptor, _this); _initDefineProp(_this, 'local', _descriptor2, _this); _initDefineProp(_this, 'key', _descriptor3, _this); _initDefineProp(_this, 'value', _descriptor4, _this); _this.viewFactory = viewFactory; _this.instruction = instruction; _this.viewSlot = viewSlot; _this.lookupFunctions = viewResources.lookupFunctions; _this.observerLocator = observerLocator; _this.key = 'key'; _this.value = 'value'; _this.strategyLocator = strategyLocator; _this.ignoreMutation = false; _this.sourceExpression = (0, _repeatUtilities.getItemsSourceExpression)(_this.instruction, 'repeat.for'); _this.isOneTime = (0, _repeatUtilities.isOneTime)(_this.sourceExpression); _this.viewsRequireLifecycle = (0, _analyzeViewFactory.viewsRequireLifecycle)(viewFactory); return _this; } Repeat.prototype.call = function call(context, changes) { this[context](this.items, changes); }; Repeat.prototype.bind = function bind(bindingContext, overrideContext) { this.scope = { bindingContext: bindingContext, overrideContext: overrideContext }; this.matcherBinding = this._captureAndRemoveMatcherBinding(); this.itemsChanged(); }; Repeat.prototype.unbind = function unbind() { this.scope = null; this.items = null; this.matcherBinding = null; this.viewSlot.removeAll(true); this._unsubscribeCollection(); }; Repeat.prototype._unsubscribeCollection = function _unsubscribeCollection() { if (this.collectionObserver) { this.collectionObserver.unsubscribe(this.callContext, this); this.collectionObserver = null; this.callContext = null; } }; Repeat.prototype.itemsChanged = function itemsChanged() { this._unsubscribeCollection(); if (!this.scope) { return; } var items = this.items; this.strategy = this.strategyLocator.getStrategy(items); if (!this.strategy) { throw new Error('Value for \'' + this.sourceExpression + '\' is non-repeatable'); } if (!this.isOneTime && !this._observeInnerCollection()) { this._observeCollection(); } this.strategy.instanceChanged(this, items); }; Repeat.prototype._getInnerCollection = function _getInnerCollection() { var expression = (0, _repeatUtilities.unwrapExpression)(this.sourceExpression); if (!expression) { return null; } return expression.evaluate(this.scope, null); }; Repeat.prototype.handleCollectionMutated = function handleCollectionMutated(collection, changes) { if (!this.collectionObserver) { return; } this.strategy.instanceMutated(this, collection, changes); }; Repeat.prototype.handleInnerCollectionMutated = function handleInnerCollectionMutated(collection, changes) { var _this2 = this; if (!this.collectionObserver) { return; } if (this.ignoreMutation) { return; } this.ignoreMutation = true; var newItems = this.sourceExpression.evaluate(this.scope, this.lookupFunctions); this.observerLocator.taskQueue.queueMicroTask(function () { return _this2.ignoreMutation = false; }); if (newItems === this.items) { this.itemsChanged(); } else { this.items = newItems; } }; Repeat.prototype._observeInnerCollection = function _observeInnerCollection() { var items = this._getInnerCollection(); var strategy = this.strategyLocator.getStrategy(items); if (!strategy) { return false; } this.collectionObserver = strategy.getCollectionObserver(this.observerLocator, items); if (!this.collectionObserver) { return false; } this.callContext = 'handleInnerCollectionMutated'; this.collectionObserver.subscribe(this.callContext, this); return true; }; Repeat.prototype._observeCollection = function _observeCollection() { var items = this.items; this.collectionObserver = this.strategy.getCollectionObserver(this.observerLocator, items); if (this.collectionObserver) { this.callContext = 'handleCollectionMutated'; this.collectionObserver.subscribe(this.callContext, this); } }; Repeat.prototype._captureAndRemoveMatcherBinding = function _captureAndRemoveMatcherBinding() { if (this.viewFactory.viewFactory) { var instructions = this.viewFactory.viewFactory.instructions; var instructionIds = Object.keys(instructions); for (var i = 0; i < instructionIds.length; i++) { var expressions = instructions[instructionIds[i]].expressions; if (expressions) { for (var ii = 0; i < expressions.length; i++) { if (expressions[ii].targetProperty === 'matcher') { var matcherBinding = expressions[ii]; expressions.splice(ii, 1); return matcherBinding; } } } } } return undefined; }; Repeat.prototype.viewCount = function viewCount() { return this.viewSlot.children.length; }; Repeat.prototype.views = function views() { return this.viewSlot.children; }; Repeat.prototype.view = function view(index) { return this.viewSlot.children[index]; }; Repeat.prototype.matcher = function matcher() { return this.matcherBinding ? this.matcherBinding.sourceExpression.evaluate(this.scope, this.matcherBinding.lookupFunctions) : null; }; Repeat.prototype.addView = function addView(bindingContext, overrideContext) { var view = this.viewFactory.create(); view.bind(bindingContext, overrideContext); this.viewSlot.add(view); }; Repeat.prototype.insertView = function insertView(index, bindingContext, overrideContext) { var view = this.viewFactory.create(); view.bind(bindingContext, overrideContext); this.viewSlot.insert(index, view); }; Repeat.prototype.moveView = function moveView(sourceIndex, targetIndex) { this.viewSlot.move(sourceIndex, targetIndex); }; Repeat.prototype.removeAllViews = function removeAllViews(returnToCache, skipAnimation) { return this.viewSlot.removeAll(returnToCache, skipAnimation); }; Repeat.prototype.removeViews = function removeViews(viewsToRemove, returnToCache, skipAnimation) { return this.viewSlot.removeMany(viewsToRemove, returnToCache, skipAnimation); }; Repeat.prototype.removeView = function removeView(index, returnToCache, skipAnimation) { return this.viewSlot.removeAt(index, returnToCache, skipAnimation); }; Repeat.prototype.updateBindings = function updateBindings(view) { var j = view.bindings.length; while (j--) { (0, _repeatUtilities.updateOneTimeBinding)(view.bindings[j]); } j = view.controllers.length; while (j--) { var k = view.controllers[j].boundProperties.length; while (k--) { var binding = view.controllers[j].boundProperties[k].binding; (0, _repeatUtilities.updateOneTimeBinding)(binding); } } }; return Repeat; }(_abstractRepeater.AbstractRepeater), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'items', [_aureliaTemplating.bindable], { enumerable: true, initializer: null }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'local', [_aureliaTemplating.bindable], { enumerable: true, initializer: null }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, 'key', [_aureliaTemplating.bindable], { enumerable: true, initializer: null }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'value', [_aureliaTemplating.bindable], { enumerable: true, initializer: null })), _class2)) || _class) || _class) || _class); });