UNPKG

15.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var core_1 = require("@angular/core");
4var TreeTypes = require("./tree.types");
5var tree_types_1 = require("./tree.types");
6var tree_1 = require("./tree");
7var tree_controller_1 = require("./tree-controller");
8var node_menu_service_1 = require("./menu/node-menu.service");
9var menu_events_1 = require("./menu/menu.events");
10var editable_events_1 = require("./editable/editable.events");
11var tree_service_1 = require("./tree.service");
12var EventUtils = require("./utils/event.utils");
13var fn_utils_1 = require("./utils/fn.utils");
14var TreeInternalComponent = (function () {
15 function TreeInternalComponent(nodeMenuService, treeService, nodeElementRef) {
16 this.nodeMenuService = nodeMenuService;
17 this.treeService = treeService;
18 this.nodeElementRef = nodeElementRef;
19 this.isSelected = false;
20 this.isRightMenuVisible = false;
21 this.isLeftMenuVisible = false;
22 this.isReadOnly = false;
23 this.subscriptions = [];
24 }
25 TreeInternalComponent.prototype.ngAfterViewInit = function () {
26 if (this.tree.checked && !this.tree.firstCheckedFired) {
27 this.tree.firstCheckedFired = true;
28 this.treeService.fireNodeChecked(this.tree);
29 }
30 };
31 TreeInternalComponent.prototype.ngOnInit = function () {
32 var _this = this;
33 var nodeId = fn_utils_1.get(this.tree, 'node.id', '');
34 if (nodeId) {
35 this.controller = new tree_controller_1.TreeController(this);
36 this.treeService.setController(nodeId, this.controller);
37 }
38 this.settings = this.settings || new tree_types_1.Ng2TreeSettings();
39 this.isReadOnly = !fn_utils_1.get(this.settings, 'enableCheckboxes', true);
40 if (this.tree.isRoot() && this.settings.rootIsVisible === false) {
41 this.tree.disableCollapseOnInit();
42 }
43 this.subscriptions.push(this.nodeMenuService.hideMenuStream(this.nodeElementRef).subscribe(function () {
44 _this.isRightMenuVisible = false;
45 _this.isLeftMenuVisible = false;
46 }));
47 this.subscriptions.push(this.treeService.unselectStream(this.tree).subscribe(function () { return (_this.isSelected = false); }));
48 this.subscriptions.push(this.treeService.draggedStream(this.tree, this.nodeElementRef).subscribe(function (e) {
49 if (_this.tree.hasSibling(e.captured.tree)) {
50 _this.swapWithSibling(e.captured.tree, _this.tree);
51 }
52 else if (_this.tree.isBranch()) {
53 _this.moveNodeToThisTreeAndRemoveFromPreviousOne(e, _this.tree);
54 }
55 else {
56 _this.moveNodeToParentTreeAndRemoveFromPreviousOne(e, _this.tree);
57 }
58 }));
59 this.subscriptions.push(this.treeService.nodeChecked$
60 .merge(this.treeService.nodeUnchecked$)
61 .filter(function (e) { return _this.eventContainsId(e) && _this.tree.hasChild(e.node); })
62 .subscribe(function (e) { return _this.updateCheckboxState(); }));
63 };
64 TreeInternalComponent.prototype.ngOnChanges = function (changes) {
65 this.controller = new tree_controller_1.TreeController(this);
66 };
67 TreeInternalComponent.prototype.ngOnDestroy = function () {
68 if (fn_utils_1.get(this.tree, 'node.id', '')) {
69 this.treeService.deleteController(this.tree.node.id);
70 }
71 this.subscriptions.forEach(function (sub) { return sub && sub.unsubscribe(); });
72 };
73 TreeInternalComponent.prototype.swapWithSibling = function (sibling, tree) {
74 tree.swapWithSibling(sibling);
75 this.treeService.fireNodeMoved(sibling, sibling.parent);
76 };
77 TreeInternalComponent.prototype.moveNodeToThisTreeAndRemoveFromPreviousOne = function (e, tree) {
78 this.treeService.fireNodeRemoved(e.captured.tree);
79 var addedChild = tree.addChild(e.captured.tree);
80 this.treeService.fireNodeMoved(addedChild, e.captured.tree.parent);
81 };
82 TreeInternalComponent.prototype.moveNodeToParentTreeAndRemoveFromPreviousOne = function (e, tree) {
83 this.treeService.fireNodeRemoved(e.captured.tree);
84 var addedSibling = tree.addSibling(e.captured.tree, tree.positionInParent);
85 this.treeService.fireNodeMoved(addedSibling, e.captured.tree.parent);
86 };
87 TreeInternalComponent.prototype.onNodeSelected = function (e) {
88 if (!this.tree.selectionAllowed) {
89 return;
90 }
91 if (EventUtils.isLeftButtonClicked(e)) {
92 this.isSelected = true;
93 this.treeService.fireNodeSelected(this.tree);
94 }
95 };
96 TreeInternalComponent.prototype.onNodeUnselected = function (e) {
97 if (!this.tree.selectionAllowed) {
98 return;
99 }
100 if (EventUtils.isLeftButtonClicked(e)) {
101 this.isSelected = false;
102 this.treeService.fireNodeUnselected(this.tree);
103 }
104 };
105 TreeInternalComponent.prototype.showRightMenu = function (e) {
106 if (!this.tree.hasRightMenu()) {
107 return;
108 }
109 if (EventUtils.isRightButtonClicked(e)) {
110 this.isRightMenuVisible = !this.isRightMenuVisible;
111 this.nodeMenuService.hideMenuForAllNodesExcept(this.nodeElementRef);
112 }
113 e.preventDefault();
114 };
115 TreeInternalComponent.prototype.showLeftMenu = function (e) {
116 if (!this.tree.hasLeftMenu()) {
117 return;
118 }
119 if (EventUtils.isLeftButtonClicked(e)) {
120 this.isLeftMenuVisible = !this.isLeftMenuVisible;
121 this.nodeMenuService.hideMenuForAllNodesExcept(this.nodeElementRef);
122 if (this.isLeftMenuVisible) {
123 e.preventDefault();
124 }
125 }
126 };
127 TreeInternalComponent.prototype.onMenuItemSelected = function (e) {
128 switch (e.nodeMenuItemAction) {
129 case menu_events_1.NodeMenuItemAction.NewTag:
130 this.onNewSelected(e);
131 break;
132 case menu_events_1.NodeMenuItemAction.NewFolder:
133 this.onNewSelected(e);
134 break;
135 case menu_events_1.NodeMenuItemAction.Rename:
136 this.onRenameSelected();
137 break;
138 case menu_events_1.NodeMenuItemAction.Remove:
139 this.onRemoveSelected();
140 break;
141 case menu_events_1.NodeMenuItemAction.Custom:
142 this.onCustomSelected();
143 this.treeService.fireMenuItemSelected(this.tree, e.nodeMenuItemSelected);
144 break;
145 default:
146 throw new Error("Chosen menu item doesn't exist");
147 }
148 };
149 TreeInternalComponent.prototype.onNewSelected = function (e) {
150 this.tree.createNode(e.nodeMenuItemAction === menu_events_1.NodeMenuItemAction.NewFolder);
151 this.isRightMenuVisible = false;
152 this.isLeftMenuVisible = false;
153 };
154 TreeInternalComponent.prototype.onRenameSelected = function () {
155 this.tree.markAsBeingRenamed();
156 this.isRightMenuVisible = false;
157 this.isLeftMenuVisible = false;
158 };
159 TreeInternalComponent.prototype.onRemoveSelected = function () {
160 this.treeService.deleteController(fn_utils_1.get(this.tree, 'node.id', ''));
161 this.treeService.fireNodeRemoved(this.tree);
162 };
163 TreeInternalComponent.prototype.onCustomSelected = function () {
164 this.isRightMenuVisible = false;
165 this.isLeftMenuVisible = false;
166 };
167 TreeInternalComponent.prototype.onSwitchFoldingType = function () {
168 this.tree.switchFoldingType();
169 this.treeService.fireNodeSwitchFoldingType(this.tree);
170 };
171 TreeInternalComponent.prototype.applyNewValue = function (e) {
172 if ((e.action === editable_events_1.NodeEditableEventAction.Cancel || this.tree.isNew()) && tree_1.Tree.isValueEmpty(e.value)) {
173 return this.treeService.fireNodeRemoved(this.tree);
174 }
175 if (this.tree.isNew()) {
176 this.tree.value = e.value;
177 this.treeService.fireNodeCreated(this.tree);
178 }
179 if (this.tree.isBeingRenamed()) {
180 var oldValue = this.tree.value;
181 this.tree.value = e.value;
182 this.treeService.fireNodeRenamed(oldValue, this.tree);
183 }
184 this.tree.markAsModified();
185 };
186 TreeInternalComponent.prototype.shouldShowInputForTreeValue = function () {
187 return this.tree.isNew() || this.tree.isBeingRenamed();
188 };
189 TreeInternalComponent.prototype.isRootHidden = function () {
190 return this.tree.isRoot() && !this.settings.rootIsVisible;
191 };
192 TreeInternalComponent.prototype.hasCustomMenu = function () {
193 return this.tree.hasCustomMenu();
194 };
195 TreeInternalComponent.prototype.switchNodeCheckStatus = function () {
196 if (!this.tree.checked) {
197 this.onNodeChecked();
198 }
199 else {
200 this.onNodeUnchecked();
201 }
202 };
203 TreeInternalComponent.prototype.onNodeChecked = function () {
204 if (!this.checkboxElementRef) {
205 return;
206 }
207 this.checkboxElementRef.nativeElement.indeterminate = false;
208 this.treeService.fireNodeChecked(this.tree);
209 this.executeOnChildController(function (controller) { return controller.check(); });
210 this.tree.checked = true;
211 };
212 TreeInternalComponent.prototype.onNodeUnchecked = function () {
213 if (!this.checkboxElementRef) {
214 return;
215 }
216 this.checkboxElementRef.nativeElement.indeterminate = false;
217 this.treeService.fireNodeUnchecked(this.tree);
218 this.executeOnChildController(function (controller) { return controller.uncheck(); });
219 this.tree.checked = false;
220 };
221 TreeInternalComponent.prototype.executeOnChildController = function (executor) {
222 var _this = this;
223 if (this.tree.hasLoadedChildern()) {
224 this.tree.children.forEach(function (child) {
225 var controller = _this.treeService.getController(child.id);
226 if (!fn_utils_1.isNil(controller)) {
227 executor(controller);
228 }
229 });
230 }
231 };
232 TreeInternalComponent.prototype.updateCheckboxState = function () {
233 var _this = this;
234 // Calling setTimeout so the value of isChecked will be updated and after that I'll check the children status.
235 setTimeout(function () {
236 var checkedChildrenAmount = _this.tree.checkedChildrenAmount();
237 if (checkedChildrenAmount === 0) {
238 _this.checkboxElementRef.nativeElement.indeterminate = false;
239 _this.tree.checked = false;
240 _this.treeService.fireNodeUnchecked(_this.tree);
241 }
242 else if (checkedChildrenAmount === _this.tree.loadedChildrenAmount()) {
243 _this.checkboxElementRef.nativeElement.indeterminate = false;
244 _this.tree.checked = true;
245 _this.treeService.fireNodeChecked(_this.tree);
246 }
247 else {
248 _this.tree.checked = false;
249 _this.checkboxElementRef.nativeElement.indeterminate = true;
250 _this.treeService.fireNodeIndetermined(_this.tree);
251 }
252 });
253 };
254 TreeInternalComponent.prototype.eventContainsId = function (event) {
255 if (!event.node.id) {
256 console.warn('"Node with checkbox" feature requires a unique id assigned to every node, please consider to add it.');
257 return false;
258 }
259 return true;
260 };
261 TreeInternalComponent.decorators = [
262 { type: core_1.Component, args: [{
263 selector: 'tree-internal',
264 template: "\n <ul class=\"tree\" *ngIf=\"tree\" [ngClass]=\"{rootless: isRootHidden()}\">\n <li>\n <div class=\"value-container\"\n [ngClass]=\"{rootless: isRootHidden()}\"\n [class.selected]=\"isSelected\"\n (contextmenu)=\"showRightMenu($event)\"\n [nodeDraggable]=\"nodeElementRef\"\n [tree]=\"tree\">\n\n <div class=\"folding\" (click)=\"onSwitchFoldingType()\" [ngClass]=\"tree.foldingCssClass\"></div>\n\n <div class=\"node-checkbox\" *ngIf=\"settings.showCheckboxes\">\n <input checkbox type=\"checkbox\" [disabled]=\"isReadOnly\" [checked]=\"this.tree.checked\" (change)=\"switchNodeCheckStatus()\" #checkbox />\n </div>\n\n <div class=\"node-value\"\n *ngIf=\"!shouldShowInputForTreeValue()\"\n [class.node-selected]=\"isSelected\"\n (click)=\"onNodeSelected($event)\">\n <div *ngIf=\"tree.nodeTemplate\" class=\"node-template\" [innerHTML]=\"tree.nodeTemplate | safeHtml\"></div>\n <span *ngIf=\"!template\" class=\"node-name\" [innerHTML]=\"tree.value | safeHtml\"></span>\n <span class=\"loading-children\" *ngIf=\"tree.childrenAreBeingLoaded()\"></span>\n <ng-template [ngTemplateOutlet]=\"template\" [ngTemplateOutletContext]=\"{ $implicit: tree.node }\"></ng-template>\n </div>\n\n <input type=\"text\" class=\"node-value\"\n *ngIf=\"shouldShowInputForTreeValue()\"\n [nodeEditable]=\"tree.value\"\n (valueChanged)=\"applyNewValue($event)\"/>\n\n <div class=\"node-left-menu\" *ngIf=\"tree.hasLeftMenu()\" (click)=\"showLeftMenu($event)\" [innerHTML]=\"tree.leftMenuTemplate\">\n </div>\n <node-menu *ngIf=\"tree.hasLeftMenu() && isLeftMenuVisible && !hasCustomMenu()\"\n (menuItemSelected)=\"onMenuItemSelected($event)\">\n </node-menu>\n </div>\n\n <node-menu *ngIf=\"isRightMenuVisible && !hasCustomMenu()\"\n (menuItemSelected)=\"onMenuItemSelected($event)\">\n </node-menu>\n\n <node-menu *ngIf=\"hasCustomMenu() && (isRightMenuVisible || isLeftMenuVisible)\"\n [menuItems]=\"tree.menuItems\"\n (menuItemSelected)=\"onMenuItemSelected($event)\">\n </node-menu>\n\n <div *ngIf=\"tree.keepNodesInDOM()\" [ngStyle]=\"{'display': tree.isNodeExpanded() ? 'block' : 'none'}\">\n <tree-internal *ngFor=\"let child of tree.childrenAsync | async\" [tree]=\"child\" [template]=\"template\" [settings]=\"settings\"></tree-internal>\n </div>\n <ng-template [ngIf]=\"tree.isNodeExpanded() && !tree.keepNodesInDOM()\">\n <tree-internal *ngFor=\"let child of tree.childrenAsync | async\" [tree]=\"child\" [template]=\"template\" [settings]=\"settings\"></tree-internal>\n </ng-template>\n </li>\n </ul>\n "
265 },] },
266 ];
267 /** @nocollapse */
268 TreeInternalComponent.ctorParameters = function () { return [
269 { type: node_menu_service_1.NodeMenuService, },
270 { type: tree_service_1.TreeService, },
271 { type: core_1.ElementRef, },
272 ]; };
273 TreeInternalComponent.propDecorators = {
274 "tree": [{ type: core_1.Input },],
275 "settings": [{ type: core_1.Input },],
276 "template": [{ type: core_1.Input },],
277 "checkboxElementRef": [{ type: core_1.ViewChild, args: ['checkbox',] },],
278 };
279 return TreeInternalComponent;
280}());
281exports.TreeInternalComponent = TreeInternalComponent;
282//# sourceMappingURL=tree-internal.component.js.map
\No newline at end of file