Debian Codegraph @

lemonldap-ng-manager/site/htdocs/static/bwr/angular-ui-tree/dist/angular-ui-tree.js

raw symbols SCIP
1 /**
2 * @license Angular UI Tree v2.22.6
3 * (c) 2010-2017. https://github.com/angular-ui-tree/angular-ui-tree
4 * License: MIT
5 */
6 (function () {
7 'use strict';
8
9 angular.module('ui.tree', [])
10 .constant('treeConfig', {
11 treeClass: 'angular-ui-tree',
12 emptyTreeClass: 'angular-ui-tree-empty',
13 dropzoneClass: 'angular-ui-tree-dropzone',
14 hiddenClass: 'angular-ui-tree-hidden',
15 nodesClass: 'angular-ui-tree-nodes',
16 nodeClass: 'angular-ui-tree-node',
17 handleClass: 'angular-ui-tree-handle',
18 placeholderClass: 'angular-ui-tree-placeholder',
19 dragClass: 'angular-ui-tree-drag',
20 dragThreshold: 3,
21 defaultCollapsed: false,
22 appendChildOnHover: true
23 });
24
25 })();
26
27 (function () {
28 'use strict';
29
30 angular.module('ui.tree')
31
32 .controller('TreeHandleController', ['$scope', '$element',
33 function ($scope, $element) {
34 this.scope = $scope;
35
36 $scope.$element = $element;
37 $scope.$nodeScope = null;
38 $scope.$type = 'uiTreeHandle';
39
40 }
41 ]);
42 })();
43
44 (function () {
45 'use strict';
46
47 angular.module('ui.tree')
48 .controller('TreeNodeController', ['$scope', '$element',
49 function ($scope, $element) {
50 this.scope = $scope;
51
52 $scope.$element = $element;
53 $scope.$modelValue = null; // Model value for node;
54 $scope.$parentNodeScope = null; // uiTreeNode Scope of parent node;
55 $scope.$childNodesScope = null; // uiTreeNodes Scope of child nodes.
56 $scope.$parentNodesScope = null; // uiTreeNodes Scope of parent nodes.
57 $scope.$treeScope = null; // uiTree scope
58 $scope.$handleScope = null; // it's handle scope
59 $scope.$type = 'uiTreeNode';
60 $scope.$$allowNodeDrop = false;
61 $scope.collapsed = false;
62 $scope.expandOnHover = false;
63
64 //Called by uiTreeNode Directive on load.
65 $scope.init = function (controllersArr) {
66 var treeNodesCtrl = controllersArr[0];
67 $scope.$treeScope = controllersArr[1] ? controllersArr[1].scope : null;
68
69 //Find the scope of it's parent node.
70 $scope.$parentNodeScope = treeNodesCtrl.scope.$nodeScope;
71
72 //modelValue for current node.
73 $scope.$modelValue = treeNodesCtrl.scope.$modelValue[$scope.$index];
74 $scope.$parentNodesScope = treeNodesCtrl.scope;
75
76 //Init sub nodes.
77 treeNodesCtrl.scope.initSubNode($scope);
78
79 $element.on('$destroy', function () {
80
81 //Destroy sub nodes.
82 treeNodesCtrl.scope.destroySubNode($scope);
83 });
84 };
85
86 //Return the index of child node in parent node (nodesScope).
87 $scope.index = function () {
88 return $scope.$parentNodesScope.$modelValue.indexOf($scope.$modelValue);
89 };
90
91 $scope.dragEnabled = function () {
92 return !($scope.$treeScope && !$scope.$treeScope.dragEnabled);
93 };
94
95 $scope.isSibling = function (targetNode) {
96 return $scope.$parentNodesScope == targetNode.$parentNodesScope;
97 };
98
99 $scope.isChild = function (targetNode) {
100 var nodes = $scope.childNodes();
101 return nodes && nodes.indexOf(targetNode) > -1;
102 };
103
104 //TODO(jcarter): This method is on uiTreeHelper already.
105 $scope.prev = function () {
106 var index = $scope.index();
107 if (index > 0) {
108 return $scope.siblings()[index - 1];
109 }
110 return null;
111 };
112
113 //Calls childNodes on parent.
114 $scope.siblings = function () {
115 return $scope.$parentNodesScope.childNodes();
116 };
117
118 $scope.childNodesCount = function () {
119 return $scope.childNodes() ? $scope.childNodes().length : 0;
120 };
121
122 $scope.hasChild = function () {
123 return $scope.childNodesCount() > 0;
124 };
125
126 $scope.childNodes = function () {
127 return $scope.$childNodesScope && $scope.$childNodesScope.$modelValue ?
128 $scope.$childNodesScope.childNodes() :
129 null;
130 };
131
132 $scope.accept = function (sourceNode, destIndex) {
133 return $scope.$childNodesScope &&
134 $scope.$childNodesScope.$modelValue &&
135 $scope.$childNodesScope.accept(sourceNode, destIndex);
136 };
137
138 $scope.remove = function () {
139 return $scope.$parentNodesScope.removeNode($scope);
140 };
141
142 $scope.toggle = function () {
143 $scope.collapsed = !$scope.collapsed;
144 $scope.$treeScope.$callbacks.toggle($scope.collapsed, $scope);
145 };
146
147 $scope.collapse = function () {
148 $scope.collapsed = true;
149 };
150
151 $scope.expand = function () {
152 $scope.collapsed = false;
153 };
154
155 $scope.depth = function () {
156 var parentNode = $scope.$parentNodeScope;
157 if (parentNode) {
158 return parentNode.depth() + 1;
159 }
160 return 1;
161 };
162
163 /**
164 * Returns the depth of the deepest subtree under this node
165 * @param scope a TreeNodesController scope object
166 * @returns Depth of all nodes *beneath* this node. If scope belongs to a leaf node, the
167 * result is 0 (it has no subtree).
168 */
169 function countSubTreeDepth(scope) {
170 if (!scope) {
171 return 0;
172 }
173 var thisLevelDepth = 0,
174 childNodes = scope.childNodes(),
175 childNode,
176 childDepth,
177 i;
178 if (!childNodes || childNodes.length === 0) {
179 return 0;
180 }
181 for (i = childNodes.length - 1; i >= 0 ; i--) {
182 childNode = childNodes[i],
183 childDepth = 1 + countSubTreeDepth(childNode);
184 thisLevelDepth = Math.max(thisLevelDepth, childDepth);
185 }
186 return thisLevelDepth;
187 }
188
189 $scope.maxSubDepth = function () {
190 return $scope.$childNodesScope ? countSubTreeDepth($scope.$childNodesScope) : 0;
191 };
192 }
193 ]);
194 })();
195
196 (function () {
197 'use strict';
198
199 angular.module('ui.tree')
200
201 .controller('TreeNodesController', ['$scope', '$element', '$timeout',
202 function ($scope, $element, $timeout) {
203 this.scope = $scope;
204
205 $scope.$element = $element;
206 $scope.$modelValue = null;
207 $scope.$nodeScope = null; // the scope of node which the nodes belongs to
208 $scope.$treeScope = null;
209 $scope.$type = 'uiTreeNodes';
210 $scope.$nodesMap = {};
211
212 $scope.nodropEnabled = false;
213 $scope.maxDepth = 0;
214 $scope.cloneEnabled = false;
215
216 $scope.initSubNode = function (subNode) {
217 if (!subNode.$modelValue) {
218 return null;
219 }
220 $scope.$nodesMap[subNode.$modelValue.$$hashKey] = subNode;
221 };
222
223 $scope.destroySubNode = function (subNode) {
224 if (!subNode.$modelValue) {
225 return null;
226 }
227 $scope.$nodesMap[subNode.$modelValue.$$hashKey] = null;
228 };
229
230 $scope.accept = function (sourceNode, destIndex) {
231 return $scope.$treeScope.$callbacks.accept(sourceNode, $scope, destIndex);
232 };
233
234 $scope.beforeDrag = function (sourceNode) {
235 return $scope.$treeScope.$callbacks.beforeDrag(sourceNode);
236 };
237
238 $scope.isParent = function (node) {
239 return node.$parentNodesScope == $scope;
240 };
241
242 $scope.hasChild = function () {
243 return $scope.$modelValue.length > 0;
244 };
245
246 //Called in apply method of UiTreeHelper.dragInfo.
247 $scope.removeNode = function (node) {
248 var index = $scope.$modelValue.indexOf(node.$modelValue);
249 if (index > -1) {
250 $timeout(function () {
251 $scope.$modelValue.splice(index, 1)[0];
252 });
253 return $scope.$treeScope.$callbacks.removed(node);
254 }
255 return null;
256 };
257
258 //Called in apply method of UiTreeHelper.dragInfo.
259 $scope.insertNode = function (index, nodeData) {
260 $timeout(function () {
261 $scope.$modelValue.splice(index, 0, nodeData);
262 });
263 };
264
265 $scope.childNodes = function () {
266 var i, nodes = [];
267 if ($scope.$modelValue) {
268 for (i = 0; i < $scope.$modelValue.length; i++) {
269 nodes.push($scope.$nodesMap[$scope.$modelValue[i].$$hashKey]);
270 }
271 }
272 return nodes;
273 };
274
275 $scope.depth = function () {
276 if ($scope.$nodeScope) {
277 return $scope.$nodeScope.depth();
278 }
279 return 0; // if it has no $nodeScope, it's root
280 };
281
282 // check if depth limit has reached
283 $scope.outOfDepth = function (sourceNode) {
284 var maxDepth = $scope.maxDepth || $scope.$treeScope.maxDepth;
285 if (maxDepth > 0) {
286 return $scope.depth() + sourceNode.maxSubDepth() + 1 > maxDepth;
287 }
288 return false;
289 };
290
291 }
292 ]);
293 })();
294
295 (function () {
296 'use strict';
297
298 angular.module('ui.tree')
299
300 .controller('TreeController', ['$scope', '$element',
301 function ($scope, $element) {
302 this.scope = $scope;
303
304 $scope.$element = $element;
305 $scope.$nodesScope = null; // root nodes
306 $scope.$type = 'uiTree';
307 $scope.$emptyElm = null;
308 $scope.$dropzoneElm = null;
309 $scope.$callbacks = null;
310
311 $scope.dragEnabled = true;
312 $scope.emptyPlaceholderEnabled = true;
313 $scope.maxDepth = 0;
314 $scope.dragDelay = 0;
315 $scope.cloneEnabled = false;
316 $scope.nodropEnabled = false;
317 $scope.dropzoneEnabled = false;
318
319 // Check if it's a empty tree
320 $scope.isEmpty = function () {
321 return ($scope.$nodesScope && $scope.$nodesScope.$modelValue
322 && $scope.$nodesScope.$modelValue.length === 0);
323 };
324
325 // add placeholder to empty tree
326 $scope.place = function (placeElm) {
327 $scope.$nodesScope.$element.append(placeElm);
328 $scope.$emptyElm.remove();
329 };
330
331 this.resetEmptyElement = function () {
332 if ((!$scope.$nodesScope.$modelValue || $scope.$nodesScope.$modelValue.length === 0) &&
333 $scope.emptyPlaceholderEnabled) {
334 $element.append($scope.$emptyElm);
335 } else {
336 $scope.$emptyElm.remove();
337 }
338 };
339
340 this.resetDropzoneElement = function () {
341 if ((!$scope.$nodesScope.$modelValue || $scope.$nodesScope.$modelValue.length !== 0) &&
342 $scope.dropzoneEnabled) {
343 $element.append($scope.$dropzoneElm);
344 } else {
345 $scope.$dropzoneElm.remove();
346 }
347 };
348
349 $scope.resetEmptyElement = this.resetEmptyElement;
350 $scope.resetDropzoneElement = this.resetDropzoneElement;
351 }
352 ]);
353 })();
354
355 (function () {
356 'use strict';
357
358 angular.module('ui.tree')
359 .directive('uiTree', ['treeConfig', '$window',
360 function (treeConfig, $window) {
361 return {
362 restrict: 'A',
363 scope: true,
364 controller: 'TreeController',
365 link: function (scope, element, attrs, ctrl) {
366 var callbacks = {
367 accept: null,
368 beforeDrag: null
369 },
370 config = {},
371 tdElm,
372 $trElm,
373 emptyElmColspan;
374
375 //Adding configured class to uiTree.
376 angular.extend(config, treeConfig);
377
378 if (config.treeClass) {
379 element.addClass(config.treeClass);
380 }
381
382 //Determining if uiTree is on a table.
383 if (element.prop('tagName').toLowerCase() === 'table') {
384 scope.$emptyElm = angular.element($window.document.createElement('tr'));
385 $trElm = element.find('tr');
386
387 //If we can find a tr, then we can use its td children as the empty element colspan.
388 if ($trElm.length > 0) {
389 emptyElmColspan = angular.element($trElm).children().length;
390 } else {
391
392 //If not, by setting a huge colspan we make sure it takes full width.
393 //TODO(jcarter): Check for negative side effects.
394 emptyElmColspan = 1000000;
395 }
396 tdElm = angular.element($window.document.createElement('td'))
397 .attr('colspan', emptyElmColspan);
398 scope.$emptyElm.append(tdElm);
399 } else {
400 scope.$emptyElm = angular.element($window.document.createElement('div'));
401 scope.$dropzoneElm = angular.element($window.document.createElement('div'));
402 }
403
404 if (config.emptyTreeClass) {
405 scope.$emptyElm.addClass(config.emptyTreeClass);
406 }
407 if (config.dropzoneClass) {
408 scope.$dropzoneElm.addClass(config.dropzoneClass);
409 }
410
411 scope.$watch('$nodesScope.$modelValue.length', function (val) {
412 if (!angular.isNumber(val)) {
413 return;
414 }
415
416 ctrl.resetEmptyElement();
417 ctrl.resetDropzoneElement();
418 }, true);
419
420 scope.$watch(attrs.dragEnabled, function (val) {
421 if ((typeof val) == 'boolean') {
422 scope.dragEnabled = val;
423 }
424 });
425
426 scope.$watch(attrs.emptyPlaceholderEnabled, function (val) {
427 if ((typeof val) == 'boolean') {
428 scope.emptyPlaceholderEnabled = val;
429 ctrl.resetEmptyElement();
430 }
431 });
432
433 scope.$watch(attrs.nodropEnabled, function (val) {
434 if ((typeof val) == 'boolean') {
435 scope.nodropEnabled = val;
436 }
437 });
438
439 scope.$watch(attrs.dropzoneEnabled, function (val) {
440 if ((typeof val) == 'boolean') {
441 scope.dropzoneEnabled = val;
442 ctrl.resetDropzoneElement();
443 }
444 });
445
446 scope.$watch(attrs.cloneEnabled, function (val) {
447 if ((typeof val) == 'boolean') {
448 scope.cloneEnabled = val;
449 }
450 });
451
452 scope.$watch(attrs.maxDepth, function (val) {
453 if ((typeof val) == 'number') {
454 scope.maxDepth = val;
455 }
456 });
457
458 scope.$watch(attrs.dragDelay, function (val) {
459 if ((typeof val) == 'number') {
460 scope.dragDelay = val;
461 }
462 });
463
464 /**
465 * Callback checks if the destination node can accept the dragged node.
466 * By default, ui-tree will check that 'data-nodrop-enabled' is not set for the
467 * destination ui-tree-nodes, and that the 'max-depth' attribute will not be exceeded
468 * if it is set on the ui-tree or ui-tree-nodes.
469 * This callback can be overridden, but callers must manually enforce nodrop and max-depth
470 * themselves if they need those to be enforced.
471 * @param sourceNodeScope Scope of the ui-tree-node being dragged
472 * @param destNodesScope Scope of the ui-tree-nodes where the node is hovering
473 * @param destIndex Index in the destination nodes array where the source node will drop
474 * @returns {boolean} True if the node is permitted to be dropped here
475 */
476 callbacks.accept = function (sourceNodeScope, destNodesScope, destIndex) {
477 return !(destNodesScope.nodropEnabled || destNodesScope.$treeScope.nodropEnabled || destNodesScope.outOfDepth(sourceNodeScope));
478 };
479
480 callbacks.beforeDrag = function (sourceNodeScope) {
481 return true;
482 };
483
484 callbacks.expandTimeoutStart = function()
485 {
486
487 };
488
489 callbacks.expandTimeoutCancel = function()
490 {
491
492 };
493
494 callbacks.expandTimeoutEnd = function()
495 {
496
497 };
498
499 callbacks.removed = function (node) {
500
501 };
502
503 /**
504 * Callback is fired when a node is successfully dropped in a new location
505 * @param event
506 */
507 callbacks.dropped = function (event) {
508
509 };
510
511 /**
512 * Callback is fired each time the user starts dragging a node
513 * @param event
514 */
515 callbacks.dragStart = function (event) {
516
517 };
518
519 /**
520 * Callback is fired each time a dragged node is moved with the mouse/touch.
521 * @param event
522 */
523 callbacks.dragMove = function (event) {
524
525 };
526
527 /**
528 * Callback is fired when the tree exits drag mode. If the user dropped a node, the drop may have been
529 * accepted or reverted.
530 * @param event
531 */
532 callbacks.dragStop = function (event) {
533
534 };
535
536 /**
537 * Callback is fired when a user drops a node (but prior to processing the drop action)
538 * beforeDrop can return a Promise, truthy, or falsy (returning nothing is falsy).
539 * If it returns falsy, or a resolve Promise, the node move is accepted
540 * If it returns truthy, or a rejected Promise, the node move is reverted
541 * @param event
542 * @returns {Boolean|Promise} Truthy (or rejected Promise) to cancel node move; falsy (or resolved promise)
543 */
544 callbacks.beforeDrop = function (event) {
545
546 };
547
548 /**
549 * Callback is fired when a user toggles node (but after processing the toggle action)
550 * @param sourceNodeScope
551 * @param collapsed
552 */
553 callbacks.toggle = function (collapsed, sourceNodeScope) {
554
555 };
556
557 scope.$watch(attrs.uiTree, function (newVal, oldVal) {
558 angular.forEach(newVal, function (value, key) {
559 if (callbacks[key]) {
560 if (typeof value === 'function') {
561 callbacks[key] = value;
562 }
563 }
564 });
565
566 scope.$callbacks = callbacks;
567 }, true);
568
569
570 }
571 };
572 }
573 ]);
574 })();
575
576 (function () {
577 'use strict';
578
579 angular.module('ui.tree')
580 .directive('uiTreeHandle', ['treeConfig',
581 function (treeConfig) {
582 return {
583 require: '^uiTreeNode',
584 restrict: 'A',
585 scope: true,
586 controller: 'TreeHandleController',
587 link: function (scope, element, attrs, treeNodeCtrl) {
588 var config = {};
589 angular.extend(config, treeConfig);
590 if (config.handleClass) {
591 element.addClass(config.handleClass);
592 }
593 // connect with the tree node.
594 if (scope != treeNodeCtrl.scope) {
595 scope.$nodeScope = treeNodeCtrl.scope;
596 treeNodeCtrl.scope.$handleScope = scope;
597 }
598 }
599 };
600 }
601 ]);
602 })();
603
604 (function () {
605 'use strict';
606
607 angular.module('ui.tree')
608
609 .directive('uiTreeNode', ['treeConfig', 'UiTreeHelper', '$window', '$document', '$timeout', '$q',
610 function (treeConfig, UiTreeHelper, $window, $document, $timeout, $q) {
611 return {
612 require: ['^uiTreeNodes', '^uiTree'],
613 restrict: 'A',
614 controller: 'TreeNodeController',
615 link: function (scope, element, attrs, controllersArr) {
616 var config = {},
617 hasTouch = 'ontouchstart' in window,
618 firstMoving,
619 dragInfo,
620 pos,
621 placeElm,
622 hiddenPlaceElm,
623 dragElm,
624 scrollContainerElm,
625 unhover,
626 treeScope = null,
627 elements, // As a parameter for callbacks
628 dragDelaying = true,
629 dragStarted = false,
630 dragTimer = null,
631 body = document.body,
632 html = document.documentElement,
633 document_height,
634 document_width,
635 dragStart,
636 tagName,
637 dragMove,
638 dragEnd,
639 dragStartEvent,
640 dragMoveEvent,
641 dragEndEvent,
642 dragCancelEvent,
643 dragDelay,
644 bindDragStartEvents,
645 bindDragMoveEvents,
646 unbindDragMoveEvents,
647 keydownHandler,
648 isHandleChild,
649 el,
650 isUiTreeRoot,
651 treeOfOrigin;
652
653 //Adding configured class to ui-tree-node.
654 angular.extend(config, treeConfig);
655
656 if (config.nodeClass) {
657 element.addClass(config.nodeClass);
658 }
659
660 //Call init function in nodeCtrl, sets parent node and sets up sub nodes.
661 scope.init(controllersArr);
662
663 scope.collapsed = !!UiTreeHelper.getNodeAttribute(scope, 'collapsed') || treeConfig.defaultCollapsed;
664 scope.expandOnHover = !!UiTreeHelper.getNodeAttribute(scope, 'expandOnHover');
665 scope.scrollContainer = UiTreeHelper.getNodeAttribute(scope, 'scrollContainer') || attrs.scrollContainer || null;
666 scope.sourceOnly = scope.nodropEnabled || scope.$treeScope.nodropEnabled;
667
668 scope.$watch(attrs.collapsed, function (val) {
669 if ((typeof val) == 'boolean') {
670 scope.collapsed = val;
671 }
672 });
673
674 //Watches to trigger behavior based on actions and settings.
675 scope.$watch('collapsed', function (val) {
676 UiTreeHelper.setNodeAttribute(scope, 'collapsed', val);
677 attrs.$set('collapsed', val);
678 });
679
680 scope.$watch(attrs.expandOnHover, function(val) {
681 if ((typeof val) === 'boolean' || (typeof val) === 'number') {
682 scope.expandOnHover = val;
683 }
684 });
685
686 scope.$watch('expandOnHover', function (val) {
687 UiTreeHelper.setNodeAttribute(scope, 'expandOnHover', val);
688 attrs.$set('expandOnHover', val);
689 });
690
691 attrs.$observe('scrollContainer', function(val) {
692 if ((typeof val) === 'string') {
693 scope.scrollContainer = val;
694 }
695 });
696
697 scope.$watch('scrollContainer', function(val) {
698 UiTreeHelper.setNodeAttribute(scope, 'scrollContainer', val);
699 attrs.$set('scrollContainer', val);
700 scrollContainerElm = document.querySelector(val);
701 });
702
703 scope.$on('angular-ui-tree:collapse-all', function () {
704 scope.collapsed = true;
705 });
706
707 scope.$on('angular-ui-tree:expand-all', function () {
708 scope.collapsed = false;
709 });
710
711 /**
712 * Called when the user has grabbed a node and started dragging it.
713 *
714 * @param {MouseEvent} e event that is triggered by DOM.
715 * @return undefined?
716 */
717 dragStart = function (e) {
718
719 //Disable right click.
720 if (!hasTouch && (e.button === 2 || e.which === 3)) {
721 return;
722 }
723
724 //Event has already fired in other scope.
725 if (e.uiTreeDragging || (e.originalEvent && e.originalEvent.uiTreeDragging)) {
726 return;
727 }
728
729 //The node being dragged.
730 var eventElm = angular.element(e.target),
731 isHandleChild,
732 cloneElm,
733 eventElmTagName,
734 tagName,
735 eventObj,
736 tdElm,
737 hStyle,
738 isTreeNode,
739 isTreeNodeHandle;
740
741 //If the target element is a child element of a ui-tree-handle,
742 // use the containing handle element as target element.
743 isHandleChild = UiTreeHelper.treeNodeHandlerContainerOfElement(eventElm);
744 if (isHandleChild) {
745 eventElm = angular.element(isHandleChild);
746 }
747
748 cloneElm = element.clone();
749 isTreeNode = UiTreeHelper.elementIsTreeNode(eventElm);
750 isTreeNodeHandle = UiTreeHelper.elementIsTreeNodeHandle(eventElm);
751
752 //If we are not triggering mousedown on our uiTree or any of it's parts, return.
753 if (!isTreeNode && !isTreeNodeHandle) {
754 return;
755 }
756
757 //If we are not triggering mousedown on our uiTree or any of it's parts, return.
758 if (isTreeNode && UiTreeHelper.elementContainsTreeNodeHandler(eventElm)) {
759 return;
760 }
761
762 //Dragging not allowed on inputs or buttons.
763 eventElmTagName = eventElm.prop('tagName').toLowerCase();
764 if (eventElmTagName == 'input' ||
765 eventElmTagName == 'textarea' ||
766 eventElmTagName == 'button' ||
767 eventElmTagName == 'select') {
768 return;
769 }
770
771 //Check if it or it's parents has a 'data-nodrag' attribute
772 el = angular.element(e.target);
773 isUiTreeRoot = el[0].attributes['ui-tree'];
774 while (el && el[0] && el[0] !== element && !isUiTreeRoot) {
775
776 //Checking that I can access attributes.
777 if (el[0].attributes) {
778 isUiTreeRoot = el[0].attributes['ui-tree'];
779 }
780
781 //If the node mark as `nodrag`, DONOT drag it.
782 if (UiTreeHelper.nodrag(el)) {
783 return;
784 }
785 el = el.parent();
786 }
787
788 //If users beforeDrag calback returns falsey, do not initiate.
789 if (!scope.beforeDrag(scope)) {
790 return;
791 }
792
793 //Set property checked at start of function to prevent running logic again.
794 e.uiTreeDragging = true;
795 if (e.originalEvent) {
796 e.originalEvent.uiTreeDragging = true;
797 }
798 e.preventDefault();
799
800 //Get original event if TouchEvent.
801 eventObj = UiTreeHelper.eventObj(e);
802
803 //Set boolean used to specify beginning of move.
804 firstMoving = true;
805
806 //Setting drag info properties and methods in scope of node being moved.
807 dragInfo = UiTreeHelper.dragInfo(scope);
808
809 //Setting original tree to adjust horizontal behavior in drag move.
810 treeOfOrigin = dragInfo.source.$treeScope.$id;
811
812 //Determine tage name of element ui-tree-node is on.
813 tagName = element.prop('tagName');
814
815 if (tagName.toLowerCase() === 'tr') {
816
817 //Create a new table column as placeholder.
818 placeElm = angular.element($window.document.createElement(tagName));
819
820 //Create a column placeholder and set colspan to whole row length.
821 tdElm = angular.element($window.document.createElement('td'))
822 .addClass(config.placeholderClass)
823 .attr('colspan', element[0].children.length);
824 placeElm.append(tdElm);
825 } else {
826
827 //If not a table just duplicate element and add placeholder class.
828 placeElm = angular.element($window.document.createElement(tagName))
829 .addClass(config.placeholderClass);
830 }
831
832 //Create a hidden placeholder and add class from config.
833 hiddenPlaceElm = angular.element($window.document.createElement(tagName));
834 if (config.hiddenClass) {
835 hiddenPlaceElm.addClass(config.hiddenClass);
836 }
837
838 //Getting starting position of element being moved.
839 pos = UiTreeHelper.positionStarted(eventObj, element);
840 placeElm.css('height', element.prop('offsetHeight') + 'px');
841
842 //Creating drag element to represent node.
843 dragElm = angular.element($window.document.createElement(scope.$parentNodesScope.$element.prop('tagName')))
844 .addClass(scope.$parentNodesScope.$element.attr('class')).addClass(config.dragClass);
845 dragElm.css('width', UiTreeHelper.width(element) + 'px');
846 dragElm.css('z-index', 9999);
847
848 //Prevents cursor to change rapidly in Opera 12.16 and IE when dragging an element.
849 hStyle = (element[0].querySelector('.angular-ui-tree-handle') || element[0]).currentStyle;
850 if (hStyle) {
851 document.body.setAttribute('ui-tree-cursor', $document.find('body').css('cursor') || '');
852 $document.find('body').css({'cursor': hStyle.cursor + '!important'});
853 }
854
855 //If tree is sourceOnly (noDragDrop) don't show placeholder when moving about it.
856 if (scope.sourceOnly) {
857 placeElm.css('display', 'none');
858 }
859
860 //Insert placeholder.
861 element.after(placeElm);
862 element.after(hiddenPlaceElm);
863 if (dragInfo.isClone() && scope.sourceOnly) {
864 dragElm.append(cloneElm);
865 } else {
866 dragElm.append(element);
867 }
868
869 //Create drag element.
870 $document.find('body').append(dragElm);
871
872 //Set drag elements position on screen.
873 dragElm.css({
874 'left': eventObj.pageX - pos.offsetX + 'px',
875 'top': eventObj.pageY - pos.offsetY + 'px'
876 });
877 elements = {
878 placeholder: placeElm,
879 dragging: dragElm
880 };
881
882 //Create all drag/move bindings.
883 bindDragMoveEvents();
884
885 //Fire dragStart callback.
886 scope.$apply(function () {
887 scope.$treeScope.$callbacks.dragStart(dragInfo.eventArgs(elements, pos));
888 });
889
890 //Get bounds of document.
891 document_height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
892 document_width = Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);
893 };
894
895 dragMove = function (e) {
896 var eventObj = UiTreeHelper.eventObj(e),
897 prev,
898 next,
899 leftElmPos,
900 topElmPos,
901 top_scroll,
902 bottom_scroll,
903 scrollContainerElmRect,
904 target,
905 targetX,
906 targetY,
907 displayElm,
908 targetNode,
909 targetElm,
910 isEmpty,
911 scrollDownBy,
912 scrollUpBy,
913 targetOffset,
914 targetBefore,
915 moveWithinTree,
916 targetBeforeBuffer,
917 targetHeight,
918 targetChildElm,
919 targetChildHeight,
920 isDropzone;
921
922 //If check ensures that drag element was created.
923 if (dragElm) {
924 e.preventDefault();
925
926 //Deselect anything (text, etc.) that was selected when move began.
927 if ($window.getSelection) {
928 $window.getSelection().removeAllRanges();
929 } else if ($window.document.selection) {
930 $window.document.selection.empty();
931 }
932
933 //Get top left positioning of element being moved.
934 leftElmPos = eventObj.pageX - pos.offsetX;
935 topElmPos = eventObj.pageY - pos.offsetY;
936
937 //dragElm can't leave the screen on the left.
938 if (leftElmPos < 0) {
939 leftElmPos = 0;
940 }
941
942 //dragElm can't leave the screen on the top.
943 if (topElmPos < 0) {
944 topElmPos = 0;
945 }
946
947 //dragElm can't leave the screen on the bottom.
948 if ((topElmPos + 10) > document_height) {
949 topElmPos = document_height - 10;
950 }
951
952 //dragElm can't leave the screen on the right.
953 if ((leftElmPos + 10) > document_width) {
954 leftElmPos = document_width - 10;
955 }
956
957 //Updating element being moved css.
958 dragElm.css({
959 'left': leftElmPos + 'px',
960 'top': topElmPos + 'px'
961 });
962
963 if (scrollContainerElm) {
964 //Getting position to top and bottom of container element.
965 scrollContainerElmRect = scrollContainerElm.getBoundingClientRect();
966 top_scroll = scrollContainerElm.scrollTop;
967 bottom_scroll = top_scroll + scrollContainerElm.clientHeight;
968
969 //To scroll down if cursor y-position is greater than the bottom position of the container vertical scroll
970 if (scrollContainerElmRect.bottom < eventObj.clientY && bottom_scroll < scrollContainerElm.scrollHeight) {
971 scrollDownBy = Math.min(scrollContainerElm.scrollHeight - bottom_scroll, 10);
972 scrollContainerElm.scrollTop += scrollDownBy;
973 }
974
975 //To scroll top if cursor y-position is less than the top position of the container vertical scroll
976 if (scrollContainerElmRect.top > eventObj.clientY && top_scroll > 0) {
977 scrollUpBy = Math.min(top_scroll, 10);
978 scrollContainerElm.scrollTop -= scrollUpBy;
979 }
980 } else {
981 //Getting position to top and bottom of page.
982 top_scroll = window.pageYOffset || $window.document.documentElement.scrollTop;
983 bottom_scroll = top_scroll + (window.innerHeight || $window.document.clientHeight || $window.document.clientHeight);
984
985 //To scroll down if cursor y-position is greater than the bottom position of the window vertical scroll
986 if (bottom_scroll < eventObj.pageY && bottom_scroll < document_height) {
987 scrollDownBy = Math.min(document_height - bottom_scroll, 10);
988 window.scrollBy(0, scrollDownBy);
989 }
990
991 //To scroll top if cursor y-position is less than the top position of the window vertical scroll
992 if (top_scroll > eventObj.pageY) {
993 scrollUpBy = Math.min(top_scroll, 10);
994 window.scrollBy(0, -scrollUpBy);
995 }
996 }
997
998 //Calling service to update position coordinates based on move.
999 UiTreeHelper.positionMoved(e, pos, firstMoving);
1000 if (firstMoving) {
1001 firstMoving = false;
1002 return;
1003 }
1004
1005 //Setting X point for elementFromPoint.
1006 targetX = eventObj.pageX - ($window.pageXOffset ||
1007 $window.document.body.scrollLeft ||
1008 $window.document.documentElement.scrollLeft) -
1009 ($window.document.documentElement.clientLeft || 0);
1010
1011 targetY = eventObj.pageY - ($window.pageYOffset ||
1012 $window.document.body.scrollTop ||
1013 $window.document.documentElement.scrollTop) -
1014 ($window.document.documentElement.clientTop || 0);
1015
1016 //Select the drag target. Because IE does not support CSS 'pointer-events: none', it will always
1017 // pick the drag element itself as the target. To prevent this, we hide the drag element while
1018 // selecting the target.
1019 if (angular.isFunction(dragElm.hide)) {
1020 dragElm.hide();
1021 } else {
1022 displayElm = dragElm[0].style.display;
1023 dragElm[0].style.display = 'none';
1024 }
1025
1026 //When using elementFromPoint() inside an iframe, you have to call
1027 // elementFromPoint() twice to make sure IE8 returns the correct value
1028 //MDN: The elementFromPoint() method of the Document interface returns the topmost element at the specified coordinates.
1029 $window.document.elementFromPoint(targetX, targetY);
1030
1031 //Set target element (element in specified x/y coordinates).
1032 targetElm = angular.element($window.document.elementFromPoint(targetX, targetY));
1033
1034 //If the target element is a child element of a ui-tree-handle,
1035 // use the containing handle element as target element
1036 isHandleChild = UiTreeHelper.treeNodeHandlerContainerOfElement(targetElm);
1037 if (isHandleChild) {
1038 targetElm = angular.element(isHandleChild);
1039 }
1040
1041 if (angular.isFunction(dragElm.show)) {
1042 dragElm.show();
1043 } else {
1044 dragElm[0].style.display = displayElm;
1045 }
1046
1047 //Assigning scope to target you are moving draggable over.
1048 if (UiTreeHelper.elementIsTree(targetElm)) {
1049 targetNode = targetElm.controller('uiTree').scope;
1050 } else if (UiTreeHelper.elementIsTreeNodeHandle(targetElm)) {
1051 targetNode = targetElm.controller('uiTreeHandle').scope;
1052 } else if (UiTreeHelper.elementIsTreeNode(targetElm)) {
1053 targetNode = targetElm.controller('uiTreeNode').scope;
1054 } else if (UiTreeHelper.elementIsTreeNodes(targetElm)) {
1055 targetNode = targetElm.controller('uiTreeNodes').scope;
1056 } else if (UiTreeHelper.elementIsPlaceholder(targetElm)) {
1057 targetNode = targetElm.controller('uiTreeNodes').scope;
1058 } else if (UiTreeHelper.elementIsDropzone(targetElm)) {
1059 targetNode = targetElm.controller('uiTree').scope;
1060 isDropzone = true;
1061 } else if (targetElm.controller('uiTreeNode')) {
1062 //Is a child element of a node.
1063 targetNode = targetElm.controller('uiTreeNode').scope;
1064 }
1065
1066 moveWithinTree = (targetNode && targetNode.$treeScope && targetNode.$treeScope.$id && targetNode.$treeScope.$id === treeOfOrigin);
1067
1068 /* (jcarter) Notes to developers:
1069 * pos.dirAx is either 0 or 1
1070 * 1 means horizontal movement is happening
1071 * 0 means vertical movement is happening
1072 */
1073
1074 // Move nodes up and down in nesting level.
1075 if (moveWithinTree && pos.dirAx) {
1076
1077 // increase horizontal level if previous sibling exists and is not collapsed
1078 // example 1.1.1 becomes 1.2
1079 if (pos.distX > 0) {
1080 prev = dragInfo.prev();
1081 if (prev && !prev.collapsed
1082 && prev.accept(scope, prev.childNodesCount())) {
1083 prev.$childNodesScope.$element.append(placeElm);
1084 dragInfo.moveTo(prev.$childNodesScope, prev.childNodes(), prev.childNodesCount());
1085 }
1086 }
1087
1088 // decrease horizontal level
1089 // example 1.2 become 1.1.1
1090 if (pos.distX < 0) {
1091 // we can't decrease a level if an item preceeds the current one
1092 next = dragInfo.next();
1093 if (!next) {
1094 target = dragInfo.parentNode(); // As a sibling of it's parent node
1095 if (target
1096 && target.$parentNodesScope.accept(scope, target.index() + 1)) {
1097 target.$element.after(placeElm);
1098 dragInfo.moveTo(target.$parentNodesScope, target.siblings(), target.index() + 1);
1099 }
1100 }
1101 }
1102 } else { //Either in origin tree and moving horizontally OR you are moving within a new tree.
1103
1104 //Check it's new position.
1105 isEmpty = false;
1106
1107 //Exit if target is not a uiTree or child of one.
1108 if (!targetNode) {
1109 return;
1110 }
1111
1112 //Show the placeholder if it was hidden for nodrop-enabled and this is a new tree
1113 if (targetNode.$treeScope && !targetNode.$parent.nodropEnabled && !targetNode.$treeScope.nodropEnabled) {
1114 placeElm.css('display', '');
1115 }
1116
1117 //Set whether target tree is empty or not.
1118 if (targetNode.$type === 'uiTree' && targetNode.dragEnabled) {
1119 isEmpty = targetNode.isEmpty();
1120 }
1121
1122 //If target is a handle set new target to handle's node.
1123 if (targetNode.$type === 'uiTreeHandle') {
1124 targetNode = targetNode.$nodeScope;
1125 }
1126
1127 //Check if it is a uiTreeNode or it's an empty tree or it's a dropzone.
1128 if (targetNode.$type !== 'uiTreeNode' && !isEmpty && !isDropzone) {
1129
1130 // Allow node to return to its original position if no longer hovering over target
1131 if (config.appendChildOnHover) {
1132 next = dragInfo.next();
1133 if (!next && unhover) {
1134 target = dragInfo.parentNode();
1135 target.$element.after(placeElm);
1136 dragInfo.moveTo(target.$parentNodesScope, target.siblings(), target.index() + 1);
1137 unhover = false;
1138 }
1139 }
1140 return;
1141 }
1142
1143 //If placeholder move from empty tree, reset it.
1144 if (treeScope && placeElm.parent()[0] != treeScope.$element[0]) {
1145 treeScope.resetEmptyElement();
1146 treeScope.resetDropzoneElement();
1147 treeScope = null;
1148 }
1149
1150 //It's an empty tree
1151 if (isEmpty) {
1152 treeScope = targetNode;
1153 if (targetNode.$nodesScope.accept(scope, 0)) {
1154 dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), 0);
1155 }
1156 //It's a dropzone
1157 } else if (isDropzone) {
1158 treeScope = targetNode;
1159 if (targetNode.$nodesScope.accept(scope, targetNode.$nodesScope.childNodes().length)) {
1160 dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), targetNode.$nodesScope.childNodes().length);
1161 }
1162 //Not empty and drag enabled.
1163 } else if (targetNode.dragEnabled()) {
1164
1165 //Setting/Resetting data for exanding on hover.
1166 if (angular.isDefined(scope.expandTimeoutOn) && scope.expandTimeoutOn !== targetNode.id) {
1167 $timeout.cancel(scope.expandTimeout);
1168 delete scope.expandTimeout;
1169 delete scope.expandTimeoutOn;
1170
1171 scope.$callbacks.expandTimeoutCancel();
1172 }
1173
1174 //Determining if expansion is needed.
1175 if (targetNode.collapsed) {
1176 if (scope.expandOnHover === true || (angular.isNumber(scope.expandOnHover) && scope.expandOnHover === 0)) {
1177 targetNode.collapsed = false;
1178 targetNode.$treeScope.$callbacks.toggle(false, targetNode);
1179 } else if (scope.expandOnHover !== false && angular.isNumber(scope.expandOnHover) && scope.expandOnHover > 0) {
1180
1181 //Triggering expansion.
1182 if (angular.isUndefined(scope.expandTimeoutOn)) {
1183 scope.expandTimeoutOn = targetNode.$id;
1184
1185 scope.$callbacks.expandTimeoutStart();
1186 scope.expandTimeout = $timeout(function()
1187 {
1188 scope.$callbacks.expandTimeoutEnd();
1189 targetNode.collapsed = false;
1190 targetNode.$treeScope.$callbacks.toggle(false, targetNode);
1191 }, scope.expandOnHover);
1192 }
1193 }
1194 }
1195
1196 //Get the element of ui-tree-node
1197 targetElm = targetNode.$element;
1198 targetOffset = UiTreeHelper.offset(targetElm);
1199 targetHeight = UiTreeHelper.height(targetElm);
1200 targetChildElm = targetNode.$childNodesScope ? targetNode.$childNodesScope.$element : null;
1201 targetChildHeight = targetChildElm ? UiTreeHelper.height(targetChildElm) : 0;
1202 targetHeight -= targetChildHeight;
1203 targetBeforeBuffer = config.appendChildOnHover ? targetHeight * 0.25 : UiTreeHelper.height(targetElm) / 2;
1204 targetBefore = eventObj.pageY < (targetOffset.top + targetBeforeBuffer);
1205
1206 if (targetNode.$parentNodesScope.accept(scope, targetNode.index())) {
1207 if (targetBefore) {
1208 targetElm[0].parentNode.insertBefore(placeElm[0], targetElm[0]);
1209 dragInfo.moveTo(targetNode.$parentNodesScope, targetNode.siblings(), targetNode.index());
1210 } else {
1211 // Try to append as a child if dragged upwards onto targetNode
1212 if (config.appendChildOnHover && targetNode.accept(scope, targetNode.childNodesCount())) {
1213 targetNode.$childNodesScope.$element.prepend(placeElm);
1214 dragInfo.moveTo(targetNode.$childNodesScope, targetNode.childNodes(), 0);
1215 unhover = true;
1216 } else {
1217 targetElm.after(placeElm);
1218 dragInfo.moveTo(targetNode.$parentNodesScope, targetNode.siblings(), targetNode.index() + 1);
1219 }
1220 }
1221
1222 //We have to check if it can add the dragging node as a child.
1223 } else if (!targetBefore && targetNode.accept(scope, targetNode.childNodesCount())) {
1224 targetNode.$childNodesScope.$element.append(placeElm);
1225 dragInfo.moveTo(targetNode.$childNodesScope, targetNode.childNodes(), targetNode.childNodesCount());
1226 }
1227 }
1228 }
1229
1230 //Triggering dragMove callback.
1231 scope.$apply(function () {
1232 scope.$treeScope.$callbacks.dragMove(dragInfo.eventArgs(elements, pos));
1233 });
1234 }
1235 };
1236
1237 dragEnd = function (e) {
1238
1239 var dragEventArgs = dragInfo.eventArgs(elements, pos);
1240
1241 e.preventDefault();
1242
1243 //TODO(jcarter): Is dragStart need to be unbound?
1244 unbindDragMoveEvents();
1245
1246 //This cancel the collapse/expand login running.
1247 $timeout.cancel(scope.expandTimeout);
1248
1249 scope.$treeScope.$apply(function () {
1250 $q.when(scope.$treeScope.$callbacks.beforeDrop(dragEventArgs))
1251
1252 //Promise resolved (or callback didn't return false)
1253 .then(function (allowDrop) {
1254 if (allowDrop !== false && scope.$$allowNodeDrop) {
1255 //Node drop accepted.
1256 dragInfo.apply();
1257
1258 //Fire the dropped callback only if the move was successful.
1259 scope.$treeScope.$callbacks.dropped(dragEventArgs);
1260 } else {
1261 //Drop canceled - revert the node to its original position.
1262 bindDragStartEvents();
1263 }
1264 })
1265
1266 //Promise rejected - revert the node to its original position.
1267 .catch(function () {
1268 bindDragStartEvents();
1269 })
1270 .finally(function () {
1271
1272 //Replace placeholder with newly dropped element.
1273 hiddenPlaceElm.replaceWith(scope.$element);
1274 placeElm.remove();
1275
1276 //Remove drag element if still in DOM.
1277 if (dragElm) {
1278 dragElm.remove();
1279 dragElm = null;
1280 }
1281
1282 //Fire dragStope callback.
1283 scope.$treeScope.$callbacks.dragStop(dragEventArgs);
1284 scope.$$allowNodeDrop = false;
1285 dragInfo = null;
1286
1287 //Restore cursor in Opera 12.16 and IE
1288 var oldCur = document.body.getAttribute('ui-tree-cursor');
1289 if (oldCur !== null) {
1290 $document.find('body').css({'cursor': oldCur});
1291 document.body.removeAttribute('ui-tree-cursor');
1292 }
1293 });
1294 });
1295 };
1296
1297 dragStartEvent = function (e) {
1298 if (scope.dragEnabled()) {
1299 dragStart(e);
1300 }
1301 };
1302
1303 dragMoveEvent = function (e) {
1304 dragMove(e);
1305 };
1306
1307 dragEndEvent = function (e) {
1308 scope.$$allowNodeDrop = true;
1309 dragEnd(e);
1310 };
1311
1312 dragCancelEvent = function (e) {
1313 dragEnd(e);
1314 };
1315
1316 dragDelay = (function () {
1317 var to;
1318
1319 return {
1320 exec: function (fn, ms) {
1321 if (!ms) {
1322 ms = 0;
1323 }
1324 this.cancel();
1325 to = $timeout(fn, ms);
1326 },
1327 cancel: function () {
1328 $timeout.cancel(to);
1329 }
1330 };
1331 })();
1332
1333 keydownHandler = function (e) {
1334 if (e.keyCode === 27) {
1335 dragEndEvent(e);
1336 }
1337 };
1338
1339 /**
1340 * Binds the mouse/touch events to enable drag start for this node.
1341 */
1342 //This is outside of bindDragMoveEvents because of the potential for a delay setting.
1343 bindDragStartEvents = function () {
1344 element.bind('touchstart mousedown', function (e) {
1345 //Don't call drag delay if no delay was specified.
1346 if (scope.dragDelay > 0) {
1347 dragDelay.exec(function () {
1348 dragStartEvent(e);
1349 }, scope.dragDelay);
1350 } else {
1351 dragStartEvent(e);
1352 }
1353 });
1354 element.bind('touchend touchcancel mouseup', function () {
1355 if (scope.dragDelay > 0) {
1356 dragDelay.cancel();
1357 }
1358 });
1359 };
1360 bindDragStartEvents();
1361
1362 /**
1363 * Binds mouse/touch events that handle moving/dropping this dragged node
1364 */
1365 bindDragMoveEvents = function () {
1366 angular.element($document).bind('touchend', dragEndEvent);
1367 angular.element($document).bind('touchcancel', dragEndEvent);
1368 angular.element($document).bind('touchmove', dragMoveEvent);
1369 angular.element($document).bind('mouseup', dragEndEvent);
1370 angular.element($document).bind('mousemove', dragMoveEvent);
1371 angular.element($document).bind('mouseleave', dragCancelEvent);
1372 angular.element($document).bind('keydown', keydownHandler);
1373 };
1374
1375 /**
1376 * Unbinds mouse/touch events that handle moving/dropping this dragged node.
1377 */
1378 unbindDragMoveEvents = function () {
1379 angular.element($document).unbind('touchend', dragEndEvent);
1380 angular.element($document).unbind('touchcancel', dragEndEvent);
1381 angular.element($document).unbind('touchmove', dragMoveEvent);
1382 angular.element($document).unbind('mouseup', dragEndEvent);
1383 angular.element($document).unbind('mousemove', dragMoveEvent);
1384 angular.element($document).unbind('mouseleave', dragCancelEvent);
1385 angular.element($document).unbind('keydown', keydownHandler);
1386 };
1387 }
1388 };
1389 }
1390 ]);
1391 })();
1392
1393 (function () {
1394 'use strict';
1395
1396 angular.module('ui.tree')
1397 .directive('uiTreeNodes', ['treeConfig', '$window',
1398 function (treeConfig) {
1399 return {
1400 require: ['ngModel', '?^uiTreeNode', '^uiTree'],
1401 restrict: 'A',
1402 scope: true,
1403 controller: 'TreeNodesController',
1404 link: function (scope, element, attrs, controllersArr) {
1405
1406 var config = {},
1407 ngModel = controllersArr[0],
1408 treeNodeCtrl = controllersArr[1],
1409 treeCtrl = controllersArr[2];
1410
1411 angular.extend(config, treeConfig);
1412 if (config.nodesClass) {
1413 element.addClass(config.nodesClass);
1414 }
1415
1416 if (treeNodeCtrl) {
1417 treeNodeCtrl.scope.$childNodesScope = scope;
1418 scope.$nodeScope = treeNodeCtrl.scope;
1419 } else {
1420 // find the root nodes if there is no parent node and have a parent ui-tree
1421 treeCtrl.scope.$nodesScope = scope;
1422 }
1423 scope.$treeScope = treeCtrl.scope;
1424
1425 if (ngModel) {
1426 ngModel.$render = function () {
1427 scope.$modelValue = ngModel.$modelValue;
1428 };
1429 }
1430
1431 scope.$watch(function () {
1432 return attrs.maxDepth;
1433 }, function (val) {
1434 if ((typeof val) == 'number') {
1435 scope.maxDepth = val;
1436 }
1437 });
1438
1439 scope.$watch(function () {
1440 return attrs.nodropEnabled;
1441 }, function (newVal) {
1442 if ((typeof newVal) != 'undefined') {
1443 scope.nodropEnabled = true;
1444 }
1445 }, true);
1446
1447 }
1448 };
1449 }
1450 ]);
1451 })();
1452
1453 (function () {
1454 'use strict';
1455
1456 angular.module('ui.tree')
1457
1458 /**
1459 * @ngdoc service
1460 * @name ui.tree.service:UiTreeHelper
1461 * @requires ng.$document
1462 * @requires ng.$window
1463 *
1464 * @description
1465 * angular-ui-tree.
1466 */
1467 .factory('UiTreeHelper', ['$document', '$window', 'treeConfig',
1468 function ($document, $window, treeConfig) {
1469 return {
1470
1471 /**
1472 * A hashtable used to storage data of nodes
1473 * @type {Object}
1474 */
1475 nodesData: {},
1476
1477 setNodeAttribute: function (scope, attrName, val) {
1478 if (!scope.$modelValue) {
1479 return null;
1480 }
1481 var data = this.nodesData[scope.$modelValue.$$hashKey];
1482 if (!data) {
1483 data = {};
1484 this.nodesData[scope.$modelValue.$$hashKey] = data;
1485 }
1486 data[attrName] = val;
1487 },
1488
1489 getNodeAttribute: function (scope, attrName) {
1490 if (!scope.$modelValue) {
1491 return null;
1492 }
1493 var data = this.nodesData[scope.$modelValue.$$hashKey];
1494 if (data) {
1495 return data[attrName];
1496 }
1497 return null;
1498 },
1499
1500 /**
1501 * @ngdoc method
1502 * @methodOf ui.tree.service:$nodrag
1503 * @param {Object} targetElm angular element
1504 * @return {Bool} check if the node can be dragged.
1505 */
1506 nodrag: function (targetElm) {
1507 if (typeof targetElm.attr('data-nodrag') != 'undefined') {
1508 return targetElm.attr('data-nodrag') !== 'false';
1509 }
1510 return false;
1511 },
1512
1513 /**
1514 * Get the event object for touches.
1515 *
1516 * @param {MouseEvent|TouchEvent} e MouseEvent or TouchEvent that kicked off dragX method.
1517 * @return {MouseEvent|TouchEvent} Object returned as original event object.
1518 */
1519 eventObj: function (e) {
1520 var obj = e;
1521 if (e.targetTouches !== undefined) {
1522 //Set obj equal to the first Touch object in the TouchList.
1523 obj = e.targetTouches.item(0);
1524 //Logic to set obj to original TouchEvent.
1525 } else if (e.originalEvent !== undefined && e.originalEvent.targetTouches !== undefined) {
1526 obj = e.originalEvent.targetTouches.item(0);
1527 }
1528 return obj;
1529 },
1530
1531 /**
1532 * Generate object used to store data about node being moved.
1533 *
1534 * {angular.$scope} node Scope of the node that is being moved.
1535 */
1536 dragInfo: function (node) {
1537 return {
1538 source: node,
1539 sourceInfo: {
1540 cloneModel: node.$treeScope.cloneEnabled === true ? angular.copy(node.$modelValue) : undefined,
1541 nodeScope: node,
1542 index: node.index(),
1543 nodesScope: node.$parentNodesScope
1544 },
1545 index: node.index(),
1546
1547 //Slice(0) just duplicates an array.
1548 siblings: node.siblings().slice(0),
1549 parent: node.$parentNodesScope,
1550
1551 //Reset parent to source parent.
1552 resetParent: function() {
1553 this.parent = node.$parentNodesScope;
1554 },
1555
1556 //Move the node to a new position, determining where the node will be inserted to when dropped happens here.
1557 moveTo: function (parent, siblings, index) {
1558 this.parent = parent;
1559
1560 //Duplicate siblings array.
1561 this.siblings = siblings.slice(0);
1562
1563 //If source node is in the target nodes
1564 var i = this.siblings.indexOf(this.source);
1565 if (i > -1) {
1566 this.siblings.splice(i, 1);
1567 if (this.source.index() < index) {
1568 index--;
1569 }
1570 }
1571
1572 this.siblings.splice(index, 0, this.source);
1573 this.index = index;
1574 },
1575
1576 //Get parent nodes nodeScope.
1577 parentNode: function () {
1578 return this.parent.$nodeScope;
1579 },
1580
1581 //Get previous sibling node.
1582 prev: function () {
1583 if (this.index > 0) {
1584 return this.siblings[this.index - 1];
1585 }
1586
1587 return null;
1588 },
1589
1590 //Get next sibling node.
1591 next: function () {
1592 if (this.index < this.siblings.length - 1) {
1593 return this.siblings[this.index + 1];
1594 }
1595
1596 return null;
1597 },
1598
1599 //Return what cloneEnabled is set to on uiTree.
1600 isClone: function () {
1601 return this.source.$treeScope.cloneEnabled === true;
1602 },
1603
1604 //Returns a copy of node passed in.
1605 clonedNode: function (node) {
1606 return angular.copy(node);
1607 },
1608
1609 //Returns true if parent or index have changed (move happened within any uiTree).
1610 isDirty: function () {
1611 return this.source.$parentNodesScope != this.parent ||
1612 this.source.index() != this.index;
1613 },
1614
1615 //Return whether node has a new parent (set on moveTo method).
1616 isForeign: function () {
1617 return this.source.$treeScope !== this.parent.$treeScope;
1618 },
1619
1620 //Sets arguments passed to user callbacks.
1621 eventArgs: function (elements, pos) {
1622 return {
1623 source: this.sourceInfo,
1624 dest: {
1625 index: this.index,
1626 nodesScope: this.parent
1627 },
1628 elements: elements,
1629 pos: pos
1630 };
1631 },
1632
1633 //Method that actually manipulates the node being moved.
1634 apply: function () {
1635
1636 var nodeData = this.source.$modelValue;
1637
1638 //Nodrop enabled on tree or parent
1639 if (this.parent.nodropEnabled || this.parent.$treeScope.nodropEnabled) {
1640 return;
1641 }
1642
1643 //Node was dropped in the same place - do nothing.
1644 if (!this.isDirty()) {
1645 return;
1646 }
1647
1648 //CloneEnabled and cross-tree so copy and do not remove from source.
1649 if (this.isClone() && this.isForeign()) {
1650 this.parent.insertNode(this.index, this.sourceInfo.cloneModel);
1651 //Any other case, remove and reinsert.
1652 } else {
1653 this.source.remove();
1654 this.parent.insertNode(this.index, nodeData);
1655 }
1656 }
1657 };
1658 },
1659
1660 /**
1661 * @ngdoc method
1662 * @name ui.tree#height
1663 * @methodOf ui.tree.service:UiTreeHelper
1664 *
1665 * @description
1666 * Get the height of an element.
1667 *
1668 * @param {Object} element Angular element.
1669 * @returns {String} Height
1670 */
1671 height: function (element) {
1672 return element.prop('scrollHeight');
1673 },
1674
1675 /**
1676 * @ngdoc method
1677 * @name ui.tree#width
1678 * @methodOf ui.tree.service:UiTreeHelper
1679 *
1680 * @description
1681 * Get the width of an element.
1682 *
1683 * @param {Object} element Angular element.
1684 * @returns {String} Width
1685 */
1686 width: function (element) {
1687 return element.prop('scrollWidth');
1688 },
1689
1690 /**
1691 * @ngdoc method
1692 * @name ui.tree#offset
1693 * @methodOf ui.nestedSortable.service:UiTreeHelper
1694 *
1695 * @description
1696 * Get the offset values of an element.
1697 *
1698 * @param {Object} element Angular element.
1699 * @returns {Object} Object with properties width, height, top and left
1700 */
1701 offset: function (element) {
1702 var boundingClientRect = element[0].getBoundingClientRect();
1703
1704 return {
1705 width: element.prop('offsetWidth'),
1706 height: element.prop('offsetHeight'),
1707 top: boundingClientRect.top + ($window.pageYOffset || $document[0].body.scrollTop || $document[0].documentElement.scrollTop),
1708 left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft)
1709 };
1710 },
1711
1712 /**
1713 * @ngdoc method
1714 * @name ui.tree#positionStarted
1715 * @methodOf ui.tree.service:UiTreeHelper
1716 *
1717 * @description
1718 * Get the start position of the target element according to the provided event properties.
1719 *
1720 * @param {Object} e Event
1721 * @param {Object} target Target element
1722 * @returns {Object} Object with properties offsetX, offsetY, startX, startY, nowX and dirX.
1723 */
1724 positionStarted: function (e, target) {
1725 var pos = {},
1726 pageX = e.pageX,
1727 pageY = e.pageY;
1728
1729 //Check to set correct data for TouchEvents
1730 if (e.originalEvent && e.originalEvent.touches && (e.originalEvent.touches.length > 0)) {
1731 pageX = e.originalEvent.touches[0].pageX;
1732 pageY = e.originalEvent.touches[0].pageY;
1733 }
1734 pos.offsetX = pageX - this.offset(target).left;
1735 pos.offsetY = pageY - this.offset(target).top;
1736 pos.startX = pos.lastX = pageX;
1737 pos.startY = pos.lastY = pageY;
1738 pos.nowX = pos.nowY = pos.distX = pos.distY = pos.dirAx = 0;
1739 pos.dirX = pos.dirY = pos.lastDirX = pos.lastDirY = pos.distAxX = pos.distAxY = 0;
1740 return pos;
1741 },
1742
1743 positionMoved: function (e, pos, firstMoving) {
1744
1745 var pageX = e.pageX,
1746 pageY = e.pageY,
1747 newAx;
1748
1749 //If there are multiple touch points, choose one to use as X and Y.
1750 if (e.originalEvent && e.originalEvent.touches && (e.originalEvent.touches.length > 0)) {
1751 pageX = e.originalEvent.touches[0].pageX;
1752 pageY = e.originalEvent.touches[0].pageY;
1753 }
1754
1755 //Mouse position last event.
1756 pos.lastX = pos.nowX;
1757 pos.lastY = pos.nowY;
1758
1759 //Mouse position this event.
1760 pos.nowX = pageX;
1761 pos.nowY = pageY;
1762
1763 //Distance mouse moved between events.
1764 pos.distX = pos.nowX - pos.lastX;
1765 pos.distY = pos.nowY - pos.lastY;
1766
1767 //Direction mouse was moving.
1768 pos.lastDirX = pos.dirX;
1769 pos.lastDirY = pos.dirY;
1770
1771 //Direction mouse is now moving (on both axis).
1772 pos.dirX = pos.distX === 0 ? 0 : pos.distX > 0 ? 1 : -1;
1773 pos.dirY = pos.distY === 0 ? 0 : pos.distY > 0 ? 1 : -1;
1774
1775 //Axis mouse is now moving on.
1776 newAx = Math.abs(pos.distX) > Math.abs(pos.distY) ? 1 : 0;
1777
1778 //Do nothing on first move.
1779 if (firstMoving) {
1780 pos.dirAx = newAx;
1781 pos.moving = true;
1782 return;
1783 }
1784
1785 //Calc distance moved on this axis (and direction).
1786 if (pos.dirAx !== newAx) {
1787 pos.distAxX = 0;
1788 pos.distAxY = 0;
1789 } else {
1790 pos.distAxX += Math.abs(pos.distX);
1791 if (pos.dirX !== 0 && pos.dirX !== pos.lastDirX) {
1792 pos.distAxX = 0;
1793 }
1794 pos.distAxY += Math.abs(pos.distY);
1795 if (pos.dirY !== 0 && pos.dirY !== pos.lastDirY) {
1796 pos.distAxY = 0;
1797 }
1798 }
1799 pos.dirAx = newAx;
1800 },
1801
1802 elementIsTreeNode: function (element) {
1803 return typeof element.attr('ui-tree-node') !== 'undefined';
1804 },
1805
1806 elementIsTreeNodeHandle: function (element) {
1807 return typeof element.attr('ui-tree-handle') !== 'undefined';
1808 },
1809 elementIsTree: function (element) {
1810 return typeof element.attr('ui-tree') !== 'undefined';
1811 },
1812 elementIsTreeNodes: function (element) {
1813 return typeof element.attr('ui-tree-nodes') !== 'undefined';
1814 },
1815 elementIsPlaceholder: function (element) {
1816 return element.hasClass(treeConfig.placeholderClass);
1817 },
1818 elementIsDropzone: function (element) {
1819 return element.hasClass(treeConfig.dropzoneClass);
1820 },
1821 elementContainsTreeNodeHandler: function (element) {
1822 return element[0].querySelectorAll('[ui-tree-handle]').length >= 1;
1823 },
1824 treeNodeHandlerContainerOfElement: function (element) {
1825 return findFirstParentElementWithAttribute('ui-tree-handle', element[0]);
1826 }
1827 };
1828 }
1829 ]);
1830
1831 // TODO: optimize this loop
1832 //(Jcarter): Suggest adding a parent element property on uiTree, then all these bubble
1833 // to <html> can trigger to stop when they reach the parent.
1834 function findFirstParentElementWithAttribute(attributeName, childObj) {
1835 //Undefined if the mouse leaves the browser window
1836 if (childObj === undefined) {
1837 return null;
1838 }
1839 var testObj = childObj.parentNode,
1840 count = 1,
1841 //Check for setAttribute due to exception thrown by Firefox when a node is dragged outside the browser window
1842 res = (typeof testObj.setAttribute === 'function' && testObj.hasAttribute(attributeName)) ? testObj : null;
1843 while (testObj && typeof testObj.setAttribute === 'function' && !testObj.hasAttribute(attributeName)) {
1844 testObj = testObj.parentNode;
1845 res = testObj;
1846 //Stop once we reach top of page.
1847 if (testObj === document.documentElement) {
1848 res = null;
1849 break;
1850 }
1851 count++;
1852 }
1853 return res;
1854 }
1855
1856 })();