UNPKG

7.84 kBJavaScriptView Raw
1import { Component, Input, Output, EventEmitter, forwardRef } from '@angular/core';
2import { NG_VALUE_ACCESSOR } from '@angular/forms';
3import { DraggableItemService } from './draggable-item.service';
4/* tslint:disable */
5/* tslint:enable */
6export var SortableComponent = (function () {
7 function SortableComponent(transfer) {
8 var _this = this;
9 /** class name for items wrapper */
10 this.wrapperClass = '';
11 /** style object for items wrapper */
12 this.wrapperStyle = {};
13 /** class name for item */
14 this.itemClass = '';
15 /** style object for item */
16 this.itemStyle = {};
17 /** class name for active item */
18 this.itemActiveClass = '';
19 /** style object for active item */
20 this.itemActiveStyle = {};
21 /** class name for placeholder */
22 this.placeholderClass = '';
23 /** style object for placeholder */
24 this.placeholderStyle = {};
25 /** placeholder item which will be shown if collection is empty */
26 this.placeholderItem = '';
27 /** fired on array change (reordering, insert, remove), same as <code>ngModelChange</code>.
28 * Returns new items collection as a payload.
29 */
30 this.onChange = new EventEmitter();
31 this.showPlaceholder = false;
32 this.activeItem = -1;
33 this.onTouched = Function.prototype;
34 this.onChanged = Function.prototype;
35 this.transfer = transfer;
36 this.currentZoneIndex = SortableComponent.globalZoneIndex++;
37 this.transfer.onCaptureItem()
38 .subscribe(function (item) { return _this.onDrop(item); });
39 }
40 Object.defineProperty(SortableComponent.prototype, "items", {
41 get: function () {
42 return this._items;
43 },
44 set: function (value) {
45 this._items = value;
46 var out = this.items.map(function (x) { return x.initData; });
47 this.onChanged(out);
48 this.onChange.emit(out);
49 },
50 enumerable: true,
51 configurable: true
52 });
53 SortableComponent.prototype.onItemDragstart = function (event, item, i) {
54 this.initDragstartEvent(event);
55 this.onTouched();
56 this.transfer.dragStart({
57 event: event,
58 item: item,
59 i: i,
60 initialIndex: i,
61 lastZoneIndex: this.currentZoneIndex,
62 overZoneIndex: this.currentZoneIndex
63 });
64 };
65 SortableComponent.prototype.onItemDragover = function (event, i) {
66 if (!this.transfer.getItem()) {
67 return;
68 }
69 event.preventDefault();
70 var dragItem = this.transfer.captureItem(this.currentZoneIndex, this.items.length);
71 var newArray = [];
72 if (!this.items.length) {
73 newArray = [dragItem.item];
74 }
75 else if (dragItem.i > i) {
76 newArray = this.items.slice(0, i).concat([
77 dragItem.item
78 ], this.items.slice(i, dragItem.i), this.items.slice(dragItem.i + 1));
79 }
80 else {
81 newArray = this.items.slice(0, dragItem.i).concat(this.items.slice(dragItem.i + 1, i + 1), [
82 dragItem.item
83 ], this.items.slice(i + 1));
84 }
85 this.items = newArray;
86 dragItem.i = i;
87 this.activeItem = i;
88 this.updatePlaceholderState();
89 };
90 SortableComponent.prototype.cancelEvent = function (event) {
91 if (!this.transfer.getItem() || !event) {
92 return;
93 }
94 event.preventDefault();
95 };
96 SortableComponent.prototype.onDrop = function (item) {
97 if (item &&
98 item.overZoneIndex !== this.currentZoneIndex &&
99 item.lastZoneIndex === this.currentZoneIndex) {
100 this.items = this.items.filter(function (x, i) { return i !== item.i; });
101 this.updatePlaceholderState();
102 }
103 this.resetActiveItem(undefined);
104 };
105 SortableComponent.prototype.resetActiveItem = function (event) {
106 this.cancelEvent(event);
107 this.activeItem = -1;
108 };
109 SortableComponent.prototype.registerOnChange = function (callback) {
110 this.onChanged = callback;
111 };
112 SortableComponent.prototype.registerOnTouched = function (callback) {
113 this.onTouched = callback;
114 };
115 SortableComponent.prototype.writeValue = function (value) {
116 var _this = this;
117 if (value) {
118 this.items = value.map(function (x, i) { return ({
119 id: i,
120 initData: x,
121 value: _this.fieldName ? x[_this.fieldName] : x
122 }); });
123 }
124 else {
125 this.items = [];
126 }
127 this.updatePlaceholderState();
128 };
129 SortableComponent.prototype.updatePlaceholderState = function () {
130 this.showPlaceholder = !this._items.length;
131 };
132 SortableComponent.prototype.getItemStyle = function (isActive) {
133 return isActive
134 ? Object.assign({}, this.itemStyle, this.itemActiveStyle)
135 : this.itemStyle;
136 };
137 SortableComponent.prototype.initDragstartEvent = function (event) {
138 // it is necessary for mozilla
139 // data type should be 'Text' instead of 'text/plain' to keep compatibility
140 // with IE
141 event.dataTransfer.setData('Text', 'placeholder');
142 };
143 SortableComponent.globalZoneIndex = 0;
144 SortableComponent.decorators = [
145 { type: Component, args: [{
146 selector: 'bs-sortable',
147 exportAs: 'bs-sortable',
148 template: "\n<div\n [ngClass]=\"wrapperClass\"\n [ngStyle]=\"wrapperStyle\"\n [ngStyle]=\"wrapperStyle\"\n (dragover)=\"cancelEvent($event)\"\n (dragenter)=\"cancelEvent($event)\"\n (drop)=\"resetActiveItem($event)\"\n (mouseleave)=\"resetActiveItem($event)\">\n <div\n *ngIf=\"showPlaceholder\"\n [ngClass]=\"placeholderClass\"\n [ngStyle]=\"placeholderStyle\"\n (dragover)=\"onItemDragover($event, 0)\"\n (dragenter)=\"cancelEvent($event)\"\n >{{placeholderItem}}</div>\n <div\n *ngFor=\"let item of items; let i=index;\"\n [ngClass]=\"[ itemClass, i === activeItem ? itemActiveClass : '' ]\"\n [ngStyle]=\"getItemStyle(i === activeItem)\"\n draggable=\"true\"\n (dragstart)=\"onItemDragstart($event, item, i)\"\n (dragend)=\"resetActiveItem($event)\"\n (dragover)=\"onItemDragover($event, i)\"\n (dragenter)=\"cancelEvent($event)\"\n ><template [ngTemplateOutlet]=\"itemTemplate || defItemTemplate\"\n [ngOutletContext]=\"{item:item, index: i}\"></template></div>\n</div>\n\n<template #defItemTemplate let-item=\"item\">{{item.value}}</template> \n",
149 providers: [{
150 provide: NG_VALUE_ACCESSOR,
151 useExisting: forwardRef(function () { return SortableComponent; }),
152 multi: true
153 }],
154 },] },
155 ];
156 /** @nocollapse */
157 SortableComponent.ctorParameters = function () { return [
158 { type: DraggableItemService, },
159 ]; };
160 SortableComponent.propDecorators = {
161 'fieldName': [{ type: Input },],
162 'wrapperClass': [{ type: Input },],
163 'wrapperStyle': [{ type: Input },],
164 'itemClass': [{ type: Input },],
165 'itemStyle': [{ type: Input },],
166 'itemActiveClass': [{ type: Input },],
167 'itemActiveStyle': [{ type: Input },],
168 'placeholderClass': [{ type: Input },],
169 'placeholderStyle': [{ type: Input },],
170 'placeholderItem': [{ type: Input },],
171 'itemTemplate': [{ type: Input },],
172 'onChange': [{ type: Output },],
173 };
174 return SortableComponent;
175}());
176//# sourceMappingURL=sortable.component.js.map
\No newline at end of file