UNPKG

angular2-data-table

Version:

angular2-data-table is a Angular2 component for presenting large and complex data.

90 lines 3.68 kB
"use strict"; var core_1 = require('@angular/core'); var draggable_directive_1 = require('./draggable.directive'); var OrderableDirective = (function () { function OrderableDirective(differs) { this.reorder = new core_1.EventEmitter(); this.differ = differs.find({}).create(null); } OrderableDirective.prototype.ngAfterContentInit = function () { // HACK: Investigate Better Way this.updateSubscriptions(); this.draggables.changes.subscribe(this.updateSubscriptions.bind(this)); }; OrderableDirective.prototype.ngOnDestroy = function () { this.draggables.forEach(function (d) { d.dragStart.unsubscribe(); d.dragEnd.unsubscribe(); }); }; OrderableDirective.prototype.updateSubscriptions = function () { var _this = this; var diffs = this.differ.diff(this.draggables.toArray()); if (diffs) { var subscribe = function (_a) { var currentValue = _a.currentValue, previousValue = _a.previousValue; unsubscribe_1({ previousValue: previousValue }); if (currentValue) { currentValue.dragStart.subscribe(_this.onDragStart.bind(_this)); currentValue.dragEnd.subscribe(_this.onDragEnd.bind(_this)); } }; var unsubscribe_1 = function (_a) { var previousValue = _a.previousValue; if (previousValue) { previousValue.dragStart.unsubscribe(); previousValue.dragEnd.unsubscribe(); } }; diffs.forEachAddedItem(subscribe.bind(this)); diffs.forEachChangedItem(subscribe.bind(this)); diffs.forEachRemovedItem(unsubscribe_1.bind(this)); } }; OrderableDirective.prototype.onDragStart = function () { this.positions = {}; var i = 0; for (var _i = 0, _a = this.draggables.toArray(); _i < _a.length; _i++) { var dragger = _a[_i]; var elm = dragger.element; this.positions[dragger.dragModel.prop] = { left: parseInt(elm.offsetLeft.toString(), 0), index: i++ }; } }; OrderableDirective.prototype.onDragEnd = function (_a) { var element = _a.element, model = _a.model; var newPos = parseInt(element.offsetLeft.toString(), 0); var prevPos = this.positions[model.prop]; var i = 0; for (var prop in this.positions) { var pos = this.positions[prop]; var movedLeft = newPos < pos.left && prevPos.left > pos.left; var movedRight = newPos > pos.left && prevPos.left < pos.left; if (movedLeft || movedRight) { this.reorder.emit({ prevIndex: prevPos.index, newIndex: i, model: model }); } i++; } element.style.left = 'auto'; }; OrderableDirective.decorators = [ { type: core_1.Directive, args: [{ selector: '[orderable]' },] }, ]; /** @nocollapse */ OrderableDirective.ctorParameters = function () { return [ { type: core_1.KeyValueDiffers, }, ]; }; OrderableDirective.propDecorators = { 'reorder': [{ type: core_1.Output },], 'draggables': [{ type: core_1.ContentChildren, args: [draggable_directive_1.DraggableDirective, { descendants: true },] },], }; return OrderableDirective; }()); exports.OrderableDirective = OrderableDirective; //# sourceMappingURL=orderable.directive.js.map