From a4e0d50e8a185c74ab79121a2fa482557e61fd6c Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Wed, 11 Jan 2017 18:06:42 +0100 Subject: [PATCH] make focusing edit-on-click less complicated --- app/scripts/edit-on-click/edit-on-click-d.js | 13 +++---------- app/scripts/task-list/task-list-d.html | 2 +- app/scripts/task-list/task-list-d.js | 19 ++++++------------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/app/scripts/edit-on-click/edit-on-click-d.js b/app/scripts/edit-on-click/edit-on-click-d.js index 341e99cd8c..81f2b26fb0 100644 --- a/app/scripts/edit-on-click/edit-on-click-d.js +++ b/app/scripts/edit-on-click/edit-on-click-d.js @@ -22,8 +22,8 @@ controllerAs: 'vm', restrict: 'A', scope: { + editOnClickEvId: '@', editOnClick: '=', - editOnClickToggle: '=', editOnClickType: '@', editOnClickOnChange: '&' } @@ -48,15 +48,8 @@ } }; - $scope.$on(EDIT_ON_CLICK_TOGGLE_EV, (ev, data) => { - console.log(data); - - // for some weird reason we have to wait for vm.editOnClick to be ready - if (!angular.isUndefined(vm.editOnClick)) { - if (ev.defaultPrevented) { - return; - } - ev.preventDefault(); + $scope.$on(EDIT_ON_CLICK_TOGGLE_EV, (ev, eventId) => { + if (eventId === vm.editOnClickEvId) { vm.toggleShowEdit(); } }); diff --git a/app/scripts/task-list/task-list-d.html b/app/scripts/task-list/task-list-d.html index 8664b104aa..41aa968b9f 100644 --- a/app/scripts/task-list/task-list-d.html +++ b/app/scripts/task-list/task-list-d.html @@ -32,7 +32,7 @@ {{ task.title }} diff --git a/app/scripts/task-list/task-list-d.js b/app/scripts/task-list/task-list-d.js index 3e507bf8f6..01685f0112 100644 --- a/app/scripts/task-list/task-list-d.js +++ b/app/scripts/task-list/task-list-d.js @@ -37,7 +37,7 @@ } /* @ngInject */ - function TaskListCtrl(Dialogs, $mdToast, $timeout, $window, Tasks, EDIT_ON_CLICK_TOGGLE_EV) { + function TaskListCtrl(Dialogs, $mdToast, $timeout, $window, Tasks, EDIT_ON_CLICK_TOGGLE_EV, $scope) { let vm = this; vm.estimateTime = (task) => { @@ -146,8 +146,7 @@ } // enter if ($event.keyCode === 13) { - let taskScope = angular.element($event.target).scope(); - taskScope.$broadcast(EDIT_ON_CLICK_TOGGLE_EV); + $scope.$broadcast(EDIT_ON_CLICK_TOGGLE_EV, task.id); } // moving items via shift+ctrl+keyUp/keyDown @@ -177,7 +176,7 @@ } }; - vm.addSubTask = (task, $event) => { + vm.addSubTask = (task) => { if (!task.subTasks) { task.subTasks = []; // save original values for potential later re-initialization @@ -190,16 +189,10 @@ // edit title right away task.subTasks.push(subTask); - // super complicated way of focusing the new element to edit it right away - // TODO fix this super complication by using the task id instead and passing it to edit-on-click + // focus the new element to edit it right away + // timeout is needed to wait for dom to update $timeout(() => { - let buttonEl = $event.currentTarget || $event.srcElement || $event.originalTarget; - buttonEl = angular.element(buttonEl); - let taskEl = buttonEl.parent().parent().parent().parent(); - let subTaskEls = taskEl.find('li'); - let lastSubTasksEl = subTaskEls[subTaskEls.length - 1]; - let subTaskScope = angular.element(lastSubTasksEl).scope(); - subTaskScope.$broadcast(EDIT_ON_CLICK_TOGGLE_EV); + $scope.$broadcast(EDIT_ON_CLICK_TOGGLE_EV, subTask.id); }); // if parent was current task, mark sub task as current now