angular.module('agileApp').directive('taskList', ['$filter', '$modal', 'BackendService', '$timeout', '$rootScope', function($filter, $modal, backendService, $timeout, $rootScope) { return { restrict: 'E', scope: { obj:'=', list:'=?', addtodo:'&', reorderfinished:'&' }, link: function($scope, element, attrs){ $scope.newTitle = ''; $scope.newDescription = ''; $scope.addMode = false; $scope.focusMe = false; var modalInstance; var deleteAllInList = function() { for(var c = 0; c < $scope.list.length; c++) { var object = $scope.list[c]; object.deleted = true; backendService.upsertIntoDirtyObjects(object); } } $scope.getDescriptionDisplayText = function(todo, show) { if(todo.data.description) { return todo.data.description; } else if(show) { return 'Description'; } else { return ''; } } $scope.getTitleDisplayText = function(todo, show) { if(todo.data.title) { return todo.data.title; } else if(show) { return 'Title'; } else { return ''; } } $scope.sortHelperDirective = function(taskIn) { return parseInt(taskIn.data.position); } $scope.$watch('list', function () { $scope.list = $filter('orderBy')($scope.list, $scope.sortHelperDirective); }, true); $scope.deleteAll = function() { modalInstance = $modal.open({ animation: $scope.animationsEnabled, windowClass: 'agile-modal', templateUrl: 'partials/modals/delete-all-modal-partial.html', size: "m" }); modalInstance.result.then(function(reason) { if(reason == 1) { deleteAllInList(); } }); } $scope.delete = function(task) { task.softDeleted = true; $timeout(function() { task.deleted = true; backendService.upsertIntoDirtyObjects(task);},200) } $scope.onTaskEdited = function(task) { backendService.upsertIntoDirtyObjects(task); } $scope.deleteModalCancel = function() { modalInstance.close(); } $scope.toggleAddMode = function ($event) { $scope.addMode = !$scope.addMode; $scope.focusMe = true; } $scope.newTask = function() { //validation here... var arg = {type:$scope.obj.type, title:$scope.newTitle, description:$scope.newDescription}; $scope.addtodo(arg); $scope.newTitle = ''; $scope.newDescription = ''; } var movedToNewListHandler = function(ui) { var sortable = ui.item.sortable; var item = sortable.model; //always set the type item.data.taskType = $scope.obj.type; //new item is alone if($scope.list.length == 0) { item.data.position = 0; $scope.list.splice(0,0,item); backendService.upsertIntoDirtyObjects(item); return; } //new item is at the top of the list if(sortable.dropindex == 0) { $scope.list.splice(sortable.dropindex, 0,item); var positionOfObjectWeAreMovingTo = String(parseInt($scope.list[1].data.position)- 1); item.data.position = positionOfObjectWeAreMovingTo; backendService.upsertIntoDirtyObjects(item); return; } //otherwise new item is somewhere below that $scope.list.splice(sortable.dropindex, 0, item); item.data.position = $scope.list[sortable.dropindex - 1].data.position; backendService.upsertIntoDirtyObjects(item); for(var c = 0; c < sortable.dropindex; c++) { $scope.list[c].data.position = String(parseInt($scope.list[c].data.position)- 1); backendService.upsertIntoDirtyObjects($scope.list[c]); } }; $scope.dragOptions = { placeholder: "sortable-placeholder", connectWith: ".agile-task-list", start: function(e, ui){ ui.placeholder.height(ui.item.outerHeight()); }, receive: function(event, ui) {movedToNewListHandler(ui);}, update: function(event, ui) { var sortable = ui.item.sortable; var item = sortable.model; var movedToNewList = $scope.movedToNewList(item); var targetThisList = sortable.droptargetModel == sortable.sourceModel; if(movedToNewList) { //this is handled in the received method } else if(targetThisList) { var positionOfObjectWeAreMovingTo = parseInt($scope.list[sortable.dropindex].data.position); console.log("thelist",$scope.list); //this is called when the source and dest list are the same, basically a reorder //grab the position of the item we are moving into if(sortable.index > sortable.dropindex) { console.log("moved up"); item.data.position = String(positionOfObjectWeAreMovingTo - 1); backendService.upsertIntoDirtyObjects(item); for(var c = 0; c < sortable.dropindex; c++) { $scope.list[c].data.position = String(parseInt($scope.list[c].data.position)- 1); backendService.upsertIntoDirtyObjects($scope.list[c]); } } else { //moved down item.data.position = String(positionOfObjectWeAreMovingTo + 1); backendService.upsertIntoDirtyObjects(item); for(var c = sortable.dropindex + 1; c < $scope.list.length; c++) { $scope.list[c].data.position = String(parseInt($scope.list[c].data.position) + 1); backendService.upsertIntoDirtyObjects($scope.list[c]); } } } else { /*this is the update called on the source list when an item is moved to another list */ } console.log(ui); console.log($scope.list); } } $scope.movedToNewList = function(item) { console.log($scope.obj.type); console.log(item); return !($scope.obj.type == item.data.taskType); } }, templateUrl: 'partials/task-list-partial.html' }; }])