Skip to content

Commit

Permalink
New build
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Dequeker committed Dec 20, 2015
1 parent daafdec commit a7057b1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 66 deletions.
189 changes: 125 additions & 64 deletions dist/angular-ui-tree.js
@@ -1,5 +1,5 @@
/**
* @license Angular UI Tree v2.11.0
* @license Angular UI Tree v2.12.0
* (c) 2010-2015. https://github.com/angular-ui-tree/angular-ui-tree
* License: MIT
*/
Expand Down Expand Up @@ -55,7 +55,7 @@
$scope.$treeScope = null; // uiTree scope
$scope.$handleScope = null; // it's handle scope
$scope.$type = 'uiTreeNode';
$scope.$$apply = false;
$scope.$$allowNodeDrop = false;
$scope.collapsed = false;

$scope.init = function (controllersArr) {
Expand Down Expand Up @@ -451,12 +451,20 @@
}
});

// check if the dest node can accept the dragging node
// by default, we check the 'data-nodrop-enabled' attribute in `ui-tree-nodes`
// and the 'max-depth' attribute in `ui-tree` or `ui-tree-nodes`.
// the method can be overrided
/**
* Callback checks if the destination node can accept the dragged node.
* By default, ui-tree will check that 'data-nodrop-enabled' is not set for the
* destination ui-tree-nodes, and that the 'max-depth' attribute will not be exceeded
* if it is set on the ui-tree or ui-tree-nodes.
* This callback can be overridden, but callers must manually enforce nodrop and max-depth
* themselves if they need those to be enforced.
* @param sourceNodeScope Scope of the ui-tree-node being dragged
* @param destNodesScope Scope of the ui-tree-nodes where the node is hovering
* @param destIndex Index in the destination nodes array where the source node will drop
* @returns {boolean} True if the node is permitted to be dropped here
*/
callbacks.accept = function (sourceNodeScope, destNodesScope, destIndex) {
return !(destNodesScope.nodropEnabled || destNodesScope.outOfDepth(sourceNodeScope));
return !(destNodesScope.nodropEnabled || destNodesScope.$treeScope.nodropEnabled || destNodesScope.outOfDepth(sourceNodeScope));
};

callbacks.beforeDrag = function (sourceNodeScope) {
Expand All @@ -467,22 +475,47 @@

};

/**
* Callback is fired when a node is successfully dropped in a new location
* @param event
*/
callbacks.dropped = function (event) {

};

/**
* Callback is fired each time the user starts dragging a node
* @param event
*/
callbacks.dragStart = function (event) {

};

/**
* Callback is fired each time a dragged node is moved with the mouse/touch.
* @param event
*/
callbacks.dragMove = function (event) {

};

/**
* Callback is fired when the tree exits drag mode. If the user dropped a node, the drop may have been
* accepted or reverted.
* @param event
*/
callbacks.dragStop = function (event) {

};

/**
* Callback is fired when a user drops a node (but prior to processing the drop action)
* beforeDrop can return a Promise, truthy, or falsy (returning nothing is falsy).
* If it returns falsy, or a resolve Promise, the node move is accepted
* If it returns truthy, or a rejected Promise, the node move is reverted
* @param event
* @returns {Boolean|Promise} Truthy (or rejected Promise) to cancel node move; falsy (or resolved promise)
*/
callbacks.beforeDrop = function (event) {

};
Expand Down Expand Up @@ -539,8 +572,8 @@

angular.module('ui.tree')

.directive('uiTreeNode', ['treeConfig', 'UiTreeHelper', '$window', '$document', '$timeout', '$rootElement',
function (treeConfig, UiTreeHelper, $window, $document, $timeout, $rootElement) {
.directive('uiTreeNode', ['treeConfig', 'UiTreeHelper', '$window', '$document', '$timeout', '$q', '$rootElement',
function (treeConfig, UiTreeHelper, $window, $document, $timeout, $q, $rootElement) {
return {
require: ['^uiTreeNodes', '^uiTree'],
restrict: 'A',
Expand Down Expand Up @@ -569,7 +602,9 @@
dragEndEvent,
dragCancelEvent,
dragDelay,
bindDrag,
bindDragStartEvents,
bindDragMoveEvents,
unbindDragMoveEvents,
keydownHandler,
outOfBounds;
angular.extend(config, treeConfig);
Expand All @@ -592,6 +627,10 @@
attrs.$set('collapsed', val);
});

/**
* Called when the user has grabbed a node and started dragging it
* @param e
*/
dragStart = function (e) {
if (!hasTouch && (e.button == 2 || e.which == 3)) {
// disable right click
Expand All @@ -601,7 +640,7 @@
return;
}

// the element which is clicked.
// the node being dragged
var eventElm = angular.element(e.target),
eventScope = eventElm.scope(),
cloneElm = element.clone(),
Expand Down Expand Up @@ -703,18 +742,12 @@
dragging: dragElm
};

bindDragMoveEvents();
// Fire dragStart callback
scope.$apply(function () {
scope.$treeScope.$callbacks.dragStart(dragInfo.eventArgs(elements, pos));
});

angular.element($document).bind('touchend', dragEndEvent);
angular.element($document).bind('touchcancel', dragEndEvent);
angular.element($document).bind('touchmove', dragMoveEvent);
angular.element($document).bind('mouseup', dragEndEvent);
angular.element($document).bind('mousemove', dragMoveEvent);
angular.element($document).bind('mouseleave', dragCancelEvent);

document_height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
document_width = Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);
};
Expand Down Expand Up @@ -943,47 +976,46 @@
};

dragEnd = function (e) {

var dragEventArgs = dragInfo.eventArgs(elements, pos);
e.preventDefault();

if (dragElm) {
scope.$treeScope.$apply(function () {
scope.$treeScope.$callbacks.beforeDrop(dragInfo.eventArgs(elements, pos));
});
// roll back elements changed
hiddenPlaceElm.replaceWith(element);
placeElm.remove();

dragElm.remove();
dragElm = null;
if (scope.$$apply && !outOfBounds) {
scope.$treeScope.$apply(function () {
dragInfo.apply();
scope.$treeScope.$callbacks.dropped(dragInfo.eventArgs(elements, pos));
});
} else {
bindDrag();
}
scope.$treeScope.$apply(function () {
scope.$treeScope.$callbacks.dragStop(dragInfo.eventArgs(elements, pos));
});
scope.$$apply = false;
dragInfo = null;
}

// Restore cursor in Opera 12.16 and IE
var oldCur = document.body.getAttribute('ui-tree-cursor');
if (oldCur !== null) {
$document.find('body').css({'cursor': oldCur});
document.body.removeAttribute('ui-tree-cursor');
}

angular.element($document).unbind('touchend', dragEndEvent); // Mobile
angular.element($document).unbind('touchcancel', dragEndEvent); // Mobile
angular.element($document).unbind('touchmove', dragMoveEvent); // Mobile
angular.element($document).unbind('mouseup', dragEndEvent);
angular.element($document).unbind('mousemove', dragMoveEvent);
angular.element($window.document.body).unbind('mouseleave', dragCancelEvent);
unbindDragMoveEvents();

scope.$treeScope.$apply(function () {
$q.when(scope.$treeScope.$callbacks.beforeDrop(dragEventArgs))
// promise resolved (or callback returned truthy)
.then(function (allowDrop) {
if (allowDrop && scope.$$allowNodeDrop && !outOfBounds) { // node drop accepted)
dragInfo.apply();
// fire the dropped callback only if the move was successful
scope.$treeScope.$callbacks.dropped(dragEventArgs);
} else { // drop canceled - revert the node to its original position
bindDragStartEvents();
}
})
// promise rejected - revert the node to its original position
.catch(function () {
bindDragStartEvents();
})
.finally(function () {
hiddenPlaceElm.replaceWith(scope.$element);
placeElm.remove();

if (dragElm) { // drag element is attached to the mouse pointer
dragElm.remove();
dragElm = null;
}
scope.$treeScope.$callbacks.dragStop(dragEventArgs);
scope.$$allowNodeDrop = false;
dragInfo = null;

// Restore cursor in Opera 12.16 and IE
var oldCur = document.body.getAttribute('ui-tree-cursor');
if (oldCur !== null) {
$document.find('body').css({'cursor': oldCur});
document.body.removeAttribute('ui-tree-cursor');
}
});
});
};

dragStartEvent = function (e) {
Expand All @@ -997,7 +1029,7 @@
};

dragEndEvent = function (e) {
scope.$$apply = true;
scope.$$allowNodeDrop = true;
dragEnd(e);
};

Expand All @@ -1022,7 +1054,10 @@
};
})();

bindDrag = function () {
/**
* Binds the mouse/touch events to enable drag start for this node
*/
bindDragStartEvents = function () {
element.bind('touchstart mousedown', function (e) {
dragDelay.exec(function () {
dragStartEvent(e);
Expand All @@ -1032,11 +1067,35 @@
dragDelay.cancel();
});
};
bindDrag();
bindDragStartEvents();

/**
* Binds mouse/touch events that handle moving/dropping this dragged node
*/
bindDragMoveEvents = function () {
angular.element($document).bind('touchend', dragEndEvent);
angular.element($document).bind('touchcancel', dragEndEvent);
angular.element($document).bind('touchmove', dragMoveEvent);
angular.element($document).bind('mouseup', dragEndEvent);
angular.element($document).bind('mousemove', dragMoveEvent);
angular.element($document).bind('mouseleave', dragCancelEvent);
};

/**
* Unbinds mouse/touch events that handle moving/dropping this dragged node
*/
unbindDragMoveEvents = function () {
angular.element($document).unbind('touchend', dragEndEvent);
angular.element($document).unbind('touchcancel', dragEndEvent);
angular.element($document).unbind('touchmove', dragMoveEvent);
angular.element($document).unbind('mouseup', dragEndEvent);
angular.element($document).unbind('mousemove', dragMoveEvent);
angular.element($document).unbind('mouseleave', dragCancelEvent);
};

keydownHandler = function (e) {
if (e.keyCode == 27) {
scope.$$apply = false;
scope.$$allowNodeDrop = false;
dragEnd(e);
}
};
Expand Down Expand Up @@ -1092,7 +1151,9 @@
};
}

scope.$watch(attrs.maxDepth, function (val) {
scope.$watch(function () {
return attrs.maxDepth;
}, function (val) {
if ((typeof val) == 'number') {
scope.maxDepth = val;
}
Expand Down Expand Up @@ -1287,7 +1348,7 @@

// cloneEnabled and cross-tree so copy and do not remove from source
if (this.isClone() && this.isForeign()) {
this.parent.insertNode(this.index, angular.copy(nodeData));
this.parent.insertNode(this.index, this.sourceInfo.cloneModel);
} else { // Any other case, remove and reinsert
this.source.remove();
this.parent.insertNode(this.index, nodeData);
Expand Down

0 comments on commit a7057b1

Please sign in to comment.