From 15b99b76ea72ae6e70e151fff4a37ccc455e3c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 18 Sep 2014 16:06:07 +0200 Subject: [PATCH] Debounce refactor: remove double clicks problem --- app/coffee/modules/admin/lightboxes.coffee | 3 ++- app/coffee/modules/admin/project-values.coffee | 9 +++++---- app/coffee/modules/admin/roles.coffee | 7 ++++--- app/coffee/modules/backlog/lightboxes.coffee | 3 ++- app/coffee/modules/base/confirm.coffee | 5 +++-- app/coffee/modules/common/history.coffee | 7 ++++--- app/coffee/modules/common/lightboxes.coffee | 11 ++++++----- app/coffee/modules/common/popovers.coffee | 5 +++-- app/coffee/modules/issues/lightboxes.coffee | 5 +++-- app/coffee/modules/projects/lightboxes.coffee | 3 ++- app/coffee/modules/related-tasks.coffee | 7 ++++--- app/coffee/modules/taskboard/lightboxes.coffee | 5 +++-- app/coffee/modules/user-settings/lightboxes.coffee | 3 ++- app/coffee/modules/wiki/main.coffee | 3 ++- app/coffee/modules/wiki/nav.coffee | 3 ++- app/coffee/utils.coffee | 2 +- 16 files changed, 48 insertions(+), 33 deletions(-) diff --git a/app/coffee/modules/admin/lightboxes.coffee b/app/coffee/modules/admin/lightboxes.coffee index 2386e66c..db8a57a2 100644 --- a/app/coffee/modules/admin/lightboxes.coffee +++ b/app/coffee/modules/admin/lightboxes.coffee @@ -20,6 +20,7 @@ ### taiga = @.taiga +debounce = @.taiga.debounce module = angular.module("taigaKanban") @@ -92,7 +93,7 @@ CreateMembersDirective = ($rs, $rootScope, $confirm, lightboxService) -> $el.find("fieldset:last > a").removeClass("icon-plus add-fieldset") .addClass("icon-delete delete-fieldset") - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() onSuccess = (data) -> diff --git a/app/coffee/modules/admin/project-values.coffee b/app/coffee/modules/admin/project-values.coffee index 32c7ccbc..fb544d03 100644 --- a/app/coffee/modules/admin/project-values.coffee +++ b/app/coffee/modules/admin/project-values.coffee @@ -27,6 +27,7 @@ toString = @.taiga.toString joinStr = @.taiga.joinStr groupBy = @.taiga.groupBy bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaAdmin") @@ -85,7 +86,7 @@ class ProjectValuesController extends mixOf(taiga.Controller, taiga.PageMixin) @.loadValues(), ])) - moveValue: (ctx, itemValue, itemIndex) => + moveValue: debounce 2000, (ctx, itemValue, itemIndex) => values = @scope.values r = values.indexOf(itemValue) values.splice(r, 1) @@ -152,7 +153,7 @@ ProjectValuesDirective = ($log, $repo, $confirm, $location, animationFrame) -> if focus $(".new-value input").focus() - submit = => + submit = debounce 2000, => promise = $repo.save($scope.project) promise.then -> $confirm.notify("success") @@ -160,7 +161,7 @@ ProjectValuesDirective = ($log, $repo, $confirm, $location, animationFrame) -> promise.then null, (data) -> $confirm.notify("error", data._error_message) - saveValue = (target)-> + saveValue = debounce 2000, (target) -> form = target.parents("form").checksley() return if not form.validate() @@ -197,7 +198,7 @@ ProjectValuesDirective = ($log, $repo, $confirm, $location, animationFrame) -> goToBottomList(true) - $el.on "click", ".add-new", (event) -> + $el.on "click", ".add-new", debounce 2000, (event) -> event.preventDefault() form = $el.find(".new-value").parents("form").checksley() return if not form.validate() diff --git a/app/coffee/modules/admin/roles.coffee b/app/coffee/modules/admin/roles.coffee index f9847cd4..0cc4b9bc 100644 --- a/app/coffee/modules/admin/roles.coffee +++ b/app/coffee/modules/admin/roles.coffee @@ -23,6 +23,7 @@ taiga = @.taiga mixOf = @.taiga.mixOf bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaAdmin") @@ -108,7 +109,7 @@ class RolesController extends mixOf(taiga.Controller, taiga.PageMixin, taiga.Fil promise.then null, => @confirm.notify('error') - setComputable: -> + setComputable: debounce 2000, -> onSuccess = => @confirm.notify("success") @@ -148,7 +149,7 @@ NewRoleDirective = ($tgrepo, $confirm) -> $el.find(".new").focus() $el.find(".add-button").hide() - $el.on "keyup", ".new", (event) -> + $el.on "keyup", ".new", debounce 2000, (event) -> event.preventDefault() if event.keyCode == 13 # Enter key target = angular.element(event.currentTarget) @@ -305,7 +306,7 @@ RolePermissionsDirective = ($rootscope, $repo, $confirm) -> target = angular.element(event.currentTarget) target.next().toggleClass("open") - $el.on "change", ".category-item input", (event) -> + $el.on "change", ".category-item input", debounce 2000, (event) -> getActivePermissions = -> activePermissions = _.filter($el.find(".category-item input"), (t) -> angular.element(t).is(":checked") diff --git a/app/coffee/modules/backlog/lightboxes.coffee b/app/coffee/modules/backlog/lightboxes.coffee index 6f8dc450..6fd41f6c 100644 --- a/app/coffee/modules/backlog/lightboxes.coffee +++ b/app/coffee/modules/backlog/lightboxes.coffee @@ -21,6 +21,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaBacklog") @@ -143,7 +144,7 @@ CreateEditSprint = ($repo, $confirm, $rs, $rootscope, lightboxService, $loading) else $el.find(".last-sprint-name").removeClass("disappear") - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() submit(event) diff --git a/app/coffee/modules/base/confirm.coffee b/app/coffee/modules/base/confirm.coffee index f7280630..a875ca64 100644 --- a/app/coffee/modules/base/confirm.coffee +++ b/app/coffee/modules/base/confirm.coffee @@ -22,6 +22,7 @@ taiga = @.taiga timeout = @.taiga.timeout cancelTimeout = @.taiga.cancelTimeout +debounce = @.taiga.debounce NOTIFICATION_MSG = { @@ -59,7 +60,7 @@ class ConfirmService extends taiga.Service defered = @q.defer() # Assign event handlers - @.el.on "click.confirm-dialog", "a.button-green", (event) => + @.el.on "click.confirm-dialog", "a.button-green", debounce 2000, (event) => event.preventDefault() target = angular.element(event.currentTarget) @loading.start(target) @@ -89,7 +90,7 @@ class ConfirmService extends taiga.Service defered = @q.defer() # Assign event handlers - @.el.on "click.confirm-dialog", "a.button-green", (event) => + @.el.on "click.confirm-dialog", "a.button-green", debounce 2000, (event) => event.preventDefault() target = angular.element(event.currentTarget) @loading.start(target) diff --git a/app/coffee/modules/common/history.coffee b/app/coffee/modules/common/history.coffee index 91f54c95..c10ee9fd 100644 --- a/app/coffee/modules/common/history.coffee +++ b/app/coffee/modules/common/history.coffee @@ -22,6 +22,7 @@ taiga = @.taiga trim = @.taiga.trim bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaCommon") @@ -437,7 +438,7 @@ HistoryDirective = ($log, $loading) -> # Events - $el.on "click", ".add-comment a.button-green", (event) -> + $el.on "click", ".add-comment a.button-green", debounce 2000, (event) -> event.preventDefault() target = angular.element(event.currentTarget) @@ -492,12 +493,12 @@ HistoryDirective = ($log, $loading) -> $el.find(".history-tabs li a").toggleClass("active") $el.find(".history section").toggleClass("hidden") - $el.on "click", ".comment-delete", (event) -> + $el.on "click", ".comment-delete", debounce 2000, (event) -> target = angular.element(event.currentTarget) activityId = target.data('activity-id') $ctrl.deleteComment(type, objectId, activityId) - $el.on "click", ".comment-restore", (event) -> + $el.on "click", ".comment-restore", debounce 2000, (event) -> target = angular.element(event.currentTarget) activityId = target.data('activity-id') $ctrl.undeleteComment(type, objectId, activityId) diff --git a/app/coffee/modules/common/lightboxes.coffee b/app/coffee/modules/common/lightboxes.coffee index 7fa1c96f..31b36be3 100644 --- a/app/coffee/modules/common/lightboxes.coffee +++ b/app/coffee/modules/common/lightboxes.coffee @@ -23,6 +23,7 @@ module = angular.module("taigaCommon") bindOnce = @.taiga.bindOnce timeout = @.taiga.timeout +debounce = @.taiga.debounce ############################################################################# ## Common Lightbox Services @@ -247,7 +248,7 @@ CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService, lightboxService.open($el) - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() form = $el.find("form").checksley() target = angular.element(event.currentTarget) @@ -306,7 +307,7 @@ CreateBulkUserstoriesDirective = ($repo, $rs, $rootscope, lightboxService, $load } lightboxService.open($el) - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() form = $el.find("form").checksley({ @@ -439,7 +440,7 @@ AssignedToLightboxDirective = (lightboxService, lightboxKeyboardNavigationServic $scope.$watch "usersSearch", (searchingText) -> render(selectedUser, searchingText) if searchingText? - $el.on "click", ".watcher-single", (event) -> + $el.on "click", ".watcher-single", debounce 2000, (event) -> event.preventDefault() target = angular.element(event.currentTarget) @@ -449,7 +450,7 @@ AssignedToLightboxDirective = (lightboxService, lightboxKeyboardNavigationServic $scope.$broadcast("assigned-to:added", target.data("user-id"), selectedItem) $scope.usersSearch = null - $el.on "click", ".remove-assigned-to", (event) -> + $el.on "click", ".remove-assigned-to", debounce 2000, (event) -> event.preventDefault() event.stopPropagation() @@ -534,7 +535,7 @@ WatchersLightboxDirective = ($repo, lightboxService, lightboxKeyboardNavigationS users = getFilteredUsers(searchingText) render(users) - $el.on "click", ".watcher-single", (event) -> + $el.on "click", ".watcher-single", debounce 2000, (event) -> closeLightbox() event.preventDefault() diff --git a/app/coffee/modules/common/popovers.coffee b/app/coffee/modules/common/popovers.coffee index 44f748b2..8a7bc083 100644 --- a/app/coffee/modules/common/popovers.coffee +++ b/app/coffee/modules/common/popovers.coffee @@ -21,6 +21,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaCommon") @@ -73,7 +74,7 @@ UsStatusDirective = ($repo, popoverService) -> # pop = $el.find(".pop-status") # popoverService.open(pop) - $el.on "click", ".status", (event) -> + $el.on "click", ".status", debounce 2000, (event) -> event.preventDefault() event.stopPropagation() target = angular.element(event.currentTarget) @@ -152,7 +153,7 @@ RelatedTaskStatusDirective = ($repo, popoverService) -> # pop = $el.find(".pop-status") # popoverService.open(pop) - $el.on "click", ".status", (event) -> + $el.on "click", ".status", debounce 2000, (event) -> event.preventDefault() event.stopPropagation() target = angular.element(event.currentTarget) diff --git a/app/coffee/modules/issues/lightboxes.coffee b/app/coffee/modules/issues/lightboxes.coffee index 788fcced..4daab182 100644 --- a/app/coffee/modules/issues/lightboxes.coffee +++ b/app/coffee/modules/issues/lightboxes.coffee @@ -21,6 +21,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaIssues") @@ -49,7 +50,7 @@ CreateIssueDirective = ($repo, $confirm, $rootscope, lightboxService) -> $scope.$on "$destroy", -> $el.off() - submit = -> + submit = debounce 2000, -> if not form.validate() return @@ -90,7 +91,7 @@ CreateBulkIssuesDirective = ($repo, $rs, $confirm, $rootscope, lightboxService) bulk: "" } - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() form = $el.find("form").checksley() diff --git a/app/coffee/modules/projects/lightboxes.coffee b/app/coffee/modules/projects/lightboxes.coffee index f8b3d5b2..d08bc8cf 100644 --- a/app/coffee/modules/projects/lightboxes.coffee +++ b/app/coffee/modules/projects/lightboxes.coffee @@ -22,6 +22,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce timeout = @.taiga.timeout +debounce = @.taiga.debounce module = angular.module("taigaProject") @@ -106,7 +107,7 @@ CreateProject = ($rootscope, $repo, $confirm, $location, $navurls, $rs, $project $el.find('.progress-bar').removeClass().addClass('progress-bar').addClass(step) - $el.on "click", ".button-submit", (event) -> + $el.on "click", ".button-submit", debounce 2000, (event) -> event.preventDefault() submit() diff --git a/app/coffee/modules/related-tasks.coffee b/app/coffee/modules/related-tasks.coffee index 7872a6a7..1c98c6cb 100644 --- a/app/coffee/modules/related-tasks.coffee +++ b/app/coffee/modules/related-tasks.coffee @@ -21,6 +21,7 @@ taiga = @.taiga trim = @.taiga.trim +debounce = @.taiga.debounce module = angular.module("taigaRelatedTasks", []) @@ -86,7 +87,7 @@ RelatedTaskRowDirective = ($repo, $compile, $confirm, $rootscope) -> """) link = ($scope, $el, $attrs, $model) -> - saveTask = (task) -> + saveTask = debounce 2000, (task) -> task.subject = $el.find('input').val() promise = $repo.save(task) promise.then => @@ -191,7 +192,7 @@ RelatedTaskCreateFormDirective = ($repo, $compile, $confirm, $tgmodel) -> } link = ($scope, $el, $attrs) -> - createTask = (task) -> + createTask = debounce 2000, (task) -> task.subject = $el.find('input').val() task.assigned_to = $scope.newTask.assigned_to task.status = $scope.newTask.status @@ -330,7 +331,7 @@ RelatedTaskAssignedToInlineEditionDirective = ($repo, $rootscope, popoverService $el.unbind("click") $el.find("a").addClass("not-clickable") - $scope.$on "assigned-to:added", (ctx, userId, updatedRelatedTask) => + $scope.$on "assigned-to:added", debounce 2000, (ctx, userId, updatedRelatedTask) => if updatedRelatedTask.id == task.id updatedRelatedTask.assigned_to = userId if autoSave diff --git a/app/coffee/modules/taskboard/lightboxes.coffee b/app/coffee/modules/taskboard/lightboxes.coffee index 7dfe5f55..9a28e5da 100644 --- a/app/coffee/modules/taskboard/lightboxes.coffee +++ b/app/coffee/modules/taskboard/lightboxes.coffee @@ -21,6 +21,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce CreateEditTaskDirective = ($repo, $model, $rs, $rootscope, lightboxService) -> link = ($scope, $el, attrs) -> @@ -52,7 +53,7 @@ CreateEditTaskDirective = ($repo, $model, $rs, $rootscope, lightboxService) -> $el.find(".title").html("Edit task ") #TODO: i18n lightboxService.open($el) - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() form = $el.find("form").checksley() @@ -85,7 +86,7 @@ CreateBulkTasksDirective = ($repo, $rs, $rootscope, lightboxService) -> lightboxService.open($el) $scope.form = {data: "", sprintId: sprintId, usId: usId} - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() form = $el.find("form").checksley() diff --git a/app/coffee/modules/user-settings/lightboxes.coffee b/app/coffee/modules/user-settings/lightboxes.coffee index 2407f754..c71a9f68 100644 --- a/app/coffee/modules/user-settings/lightboxes.coffee +++ b/app/coffee/modules/user-settings/lightboxes.coffee @@ -21,6 +21,7 @@ taiga = @.taiga bindOnce = @.taiga.bindOnce +debounce = @.taiga.debounce module = angular.module("taigaUserSettings") @@ -53,7 +54,7 @@ DeleteUserDirective = ($repo, $rootscope, $auth, $location, $navUrls, lightboxSe event.preventDefault() lightboxService.close($el) - $el.on "click", ".button-green", (event) -> + $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() submit() diff --git a/app/coffee/modules/wiki/main.coffee b/app/coffee/modules/wiki/main.coffee index 93a8911a..faff5041 100644 --- a/app/coffee/modules/wiki/main.coffee +++ b/app/coffee/modules/wiki/main.coffee @@ -25,6 +25,7 @@ mixOf = @.taiga.mixOf groupBy = @.taiga.groupBy bindOnce = @.taiga.bindOnce unslugify = @.taiga.unslugify +debounce = @.taiga.debounce module = angular.module("taigaWiki") @@ -157,7 +158,7 @@ module.controller("WikiDetailController", WikiDetailController) ############################################################################# class WikiEditController extends WikiDetailController - save: -> + save: debounce 2000, -> onSuccess = => ctx = { project: @scope.projectSlug diff --git a/app/coffee/modules/wiki/nav.coffee b/app/coffee/modules/wiki/nav.coffee index 0d6d8e2e..d31d43ee 100644 --- a/app/coffee/modules/wiki/nav.coffee +++ b/app/coffee/modules/wiki/nav.coffee @@ -26,6 +26,7 @@ groupBy = @.taiga.groupBy bindOnce = @.taiga.bindOnce slugify = @.taiga.slugify unslugify = @.taiga.slugify +debounce = @.taiga.debounce module = angular.module("taigaWiki") @@ -120,7 +121,7 @@ WikiNavDirective = ($tgrepo, $log, $location, $confirm, $navUrls) -> promise.then null, -> $confirm.notify("error") - $el.on "keyup", ".new input", (event) -> + $el.on "keyup", ".new input", debounce 2000, (event) -> event.preventDefault() if event.keyCode == 13 target = angular.element(event.currentTarget) diff --git a/app/coffee/utils.coffee b/app/coffee/utils.coffee index 5f965b22..59c47d11 100644 --- a/app/coffee/utils.coffee +++ b/app/coffee/utils.coffee @@ -104,7 +104,7 @@ joinStr = (str, coll) -> debounce = (wait, func) -> - return _.debounce(func, wait) + return _.debounce(func, wait, {leading: true, trailing: false}) startswith = (str1, str2) -> return _.str.startsWith(str1, str2)