From a3a1bc4483e01eb235d13f29709d2fd2604a8ddc Mon Sep 17 00:00:00 2001 From: Lukasz Bandzarewicz Date: Mon, 16 Sep 2013 16:38:55 +0200 Subject: [PATCH] chore: partially upgrade angular-bootstrap to 0.6.0 --- app/scripts/controllers/listCtrl.coffee | 4 +- app/scripts/controllers/users/listCtrl.coffee | 12 ++-- .../users/massUpdateFormCtrl.coffee | 8 +-- app/scripts/controllers/usersListCtrl.coffee | 7 ++- app/scripts/modules/forms.coffee | 63 +++++++++---------- bower.json | 2 +- .../controllers/users/listCtrlSpec.coffee | 22 +++---- .../users/massUpdateFormCtrlSpec.coffee | 10 +-- .../unit/controllers/usersListCtrlSpec.coffee | 2 +- test/unit/modules/formsSpec.coffee | 8 ++- 10 files changed, 67 insertions(+), 71 deletions(-) diff --git a/app/scripts/controllers/listCtrl.coffee b/app/scripts/controllers/listCtrl.coffee index abe1d1b0b..073e6b8b7 100644 --- a/app/scripts/controllers/listCtrl.coffee +++ b/app/scripts/controllers/listCtrl.coffee @@ -31,8 +31,8 @@ class ListCtrl item.id = generateId() callback.success(this) - editDialog.open("templates/partials/itemForm.html", item, $scope.grid) - .then (item) => @data.push(item) + promise = editDialog.open("templates/partials/itemForm.html", item, $scope.grid).result + promise.then (item) => @data.push(item) $scope.deleteItem = (id) => item = @deleteItemById(id) diff --git a/app/scripts/controllers/users/listCtrl.coffee b/app/scripts/controllers/users/listCtrl.coffee index 7db64523f..a775534cc 100644 --- a/app/scripts/controllers/users/listCtrl.coffee +++ b/app/scripts/controllers/users/listCtrl.coffee @@ -1,6 +1,6 @@ class ListCtrl - @$inject = ["$scope", "$location", "$filter", "$dialog", "confirmationDialog", "Users", "pathWithContext"] - constructor: ($scope, $location, $filter, $dialog, confirmationDialog, Users, pathWithContext) -> + @$inject = ["$scope", "$location", "$filter", "$modal", "confirmationDialog", "Users", "pathWithContext"] + constructor: ($scope, $location, $filter, $modal, confirmationDialog, Users, pathWithContext) -> @$filter = $filter $scope.gridOptions = @@ -28,15 +28,13 @@ class ListCtrl userIds = $scope.usersGrid.getSelectedRowIds() return if userIds.length is 0 - dialog = $dialog.dialog - backdropFade: false - dialogFade: false + $modal.open + templateUrl: pathWithContext("/templates/users/massUpdateForm.html") + controller: "users.MassUpdateFormCtrl" resolve: userIds: -> userIds usersGrid: -> $scope.usersGrid - dialog.open(pathWithContext("/templates/users/massUpdateForm.html"), "users.MassUpdateFormCtrl") - gridColumns: -> showActionLink = (cellVal, options, rowdata) -> """ diff --git a/app/scripts/controllers/users/massUpdateFormCtrl.coffee b/app/scripts/controllers/users/massUpdateFormCtrl.coffee index 19f62a11e..721c12201 100644 --- a/app/scripts/controllers/users/massUpdateFormCtrl.coffee +++ b/app/scripts/controllers/users/massUpdateFormCtrl.coffee @@ -1,17 +1,17 @@ class MassUpdateFormCtrl - @$inject = ["$scope", "Users", "userIds", "dialog", "usersGrid"] - constructor: ($scope, Users, userIds, dialog, usersGrid) -> + @$inject = ["$scope", "Users", "userIds", "$modalInstance", "usersGrid"] + constructor: ($scope, Users, userIds, $modalInstance, usersGrid) -> $scope.users = allowance: 0 $scope.save = (users) -> promise = Users.massUpdate(ids: userIds, data: users).$promise promise.then -> usersGrid.reload() - dialog.close() + $modalInstance.close() $scope.closeDialog = -> - dialog.close() + $modalInstance.close() angular.module("angleGrinder") .controller("users.MassUpdateFormCtrl", MassUpdateFormCtrl) diff --git a/app/scripts/controllers/usersListCtrl.coffee b/app/scripts/controllers/usersListCtrl.coffee index 942e95f99..9bc8fa6e4 100644 --- a/app/scripts/controllers/usersListCtrl.coffee +++ b/app/scripts/controllers/usersListCtrl.coffee @@ -1,6 +1,6 @@ class UsersListCtrl - @$inject = ["$scope", "$log", "$dialog", "$filter", "confirmationDialog", "editDialog", "Users", "pathWithContext"] - constructor: ($scope, $log, $dialog, @$filter, confirmationDialog, editDialog, Users, pathWithContext) -> + @$inject = ["$scope", "$log", "$filter", "confirmationDialog", "editDialog", "Users", "pathWithContext"] + constructor: ($scope, $log, @$filter, confirmationDialog, editDialog, Users, pathWithContext) -> # Intitially show the search form $scope.showSearchForm = true @@ -19,7 +19,8 @@ class UsersListCtrl editDialog.open(pathWithContext("templates/partials/userForm.html"), user, $scope.usersGrid) $scope.deleteItem = (id) -> - confirmationDialog.open().then (confirmed) -> + dialogPromise = confirmationDialog.open().result + dialogPromise.then (confirmed) -> return unless confirmed promise = Users.delete(id: id).$promise diff --git a/app/scripts/modules/forms.coffee b/app/scripts/modules/forms.coffee index d2ccfe5fb..b4e4cc2bd 100644 --- a/app/scripts/modules/forms.coffee +++ b/app/scripts/modules/forms.coffee @@ -12,35 +12,38 @@ forms.value "$strapConfig", autoClose: true forceParse: false -class FormDialogCtrl - @$inject = ["$scope", "$rootScope", "$log", "dialog", "item", "gridCtrl"] - constructor: ($scope, $rootScope, $log, dialog, item, gridCtrl) -> +class EditDialogCtrl + @$inject = ["$scope", "$rootScope", "$log", "$modalInstance", "item", "gridCtrl"] + constructor: ($scope, $rootScope, $log, $modalInstance, item, gridCtrl) -> $scope.item = item $scope.createNew = not item.persisted() # Closes the dialog $scope.closeEditDialog = -> $log.info "Closing the dialog" - dialog.close($scope.item) + $modalInstance.dismiss("cancel click") # If form is valid performs server side update $scope.save = (item) -> - if $scope.editForm.$invalid - $log.warn "The form is invalid", $scope.editForm + # TODO $scope.editForm is undefined due to https://github.com/angular-ui/bootstrap/issues/969 + form = $scope.editForm + + if form.$invalid + $log.warn "The form is invalid", form return onSuccess = (response) -> $log.info "Item has been updated/created", response gridCtrl.saveRow(item.id, response) - $scope.closeEditDialog() + $modalInstance.close(item) onError = (response) -> $log.error "Something went wront", response if response.status is 422 errors = response.data?.errors?[item.resourceName()] - $scope.editForm.$serverError = errors + form.$serverError = errors $log.error "Server side validation errors", errors item.save success: onSuccess, error: onError @@ -51,49 +54,39 @@ class FormDialogCtrl $log.info "Item has been deleted", response gridCtrl.removeRow(item.id) - $scope.closeEditDialog() + $modalInstance.close(item) onError = (response) -> $log.error "Something went wront", response item.delete success: onSuccess, error: onError -forms.controller "FormDialogCtrl", FormDialogCtrl +forms.controller "EditDialogCtrl", EditDialogCtrl class EditDialog - @$inject = ["$dialog"] - constructor: (@$dialog) -> + @$inject = ["$modal"] + constructor: (@$modal) -> open: (templateUrl, item, gridCtrl = null) -> - dialog = @$dialog.dialog - backdropFade: false - dialogFade: false + modalInstance = @$modal.open + templateUrl: templateUrl + controller: "EditDialogCtrl" + backdrop: "static" resolve: item: -> item gridCtrl: -> gridCtrl - # override so we can intercept form dirty and prevent escape - dialog.handledEscapeKey = (e) -> - dialog.handleBackDropClick(e) if e.which is 27 - - # override so we can intercept form dirty and prevent backdrop click - dialog.handleBackDropClick = (e) -> - e.preventDefault() - unless dialog.$scope.editForm.$dirty - dialog.close() - dialog.$scope.$apply() - - dialog.open templateUrl, "FormDialogCtrl" + modalInstance forms.service "editDialog", EditDialog class ConfirmationDialogCtrl - @$inject = ["$scope", "$log", "dialog", "message"] - constructor: ($scope, $log, dialog, message) -> + @$inject = ["$scope", "$log", "$modalInstance", "message"] + constructor: ($scope, $log, $modalInstance, message) -> $scope.message = message $scope.close = (confirmed) -> $log.info "Confirmation dialog closed", confirmed - dialog.close(confirmed) + $modalInstance.close(confirmed) forms.controller "ConfirmationDialogCtrl", ConfirmationDialogCtrl @@ -108,16 +101,16 @@ forms.run ["$templateCache", ($templateCache) -> ] class ConfirmationDialog - @$inject = ["$dialog", "$log"] - constructor: (@$dialog, @$log) -> + @$inject = ["$modal", "$log"] + constructor: (@$modal, @$log) -> open: (message = null) -> @$log.info "Opening confirmation dialog, message:", message - dialog = @$dialog.dialog + @$modal.open + templateUrl: "templates/dialogs/confirmation.html" + controller: "ConfirmationDialogCtrl" resolve: message: -> if message? then message else "Are you sure?" - dialog.open "templates/dialogs/confirmation.html", "ConfirmationDialogCtrl" - forms.service "confirmationDialog", ConfirmationDialog diff --git a/bower.json b/bower.json index ec6f0c609..a0a8bcc44 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,7 @@ "angular": "1.2.0-rc.2", "angular-route": "1.2.0-rc.2", "angular-ui-select2": "~0.0.2", - "angular-bootstrap": "0.5.0", + "angular-bootstrap": "0.6.0", "angular-strap": "0.7.6", "bootstrap-datepicker": "1.1.1" }, diff --git a/test/unit/controllers/users/listCtrlSpec.coffee b/test/unit/controllers/users/listCtrlSpec.coffee index 4e9a22bb3..7ad8a1d42 100644 --- a/test/unit/controllers/users/listCtrlSpec.coffee +++ b/test/unit/controllers/users/listCtrlSpec.coffee @@ -1,9 +1,9 @@ describe "controller: users.ListCtrl", -> beforeEach module("angleGrinder") - # Stub $dialog service + # Stub $modal service beforeEach module "ui.bootstrap", ($provide) -> - $provide.value "$dialog", sinon.stub(dialog: angular.noop) + $provide.value "$modal", sinon.stub(open: angular.noop) return $scope = null @@ -83,25 +83,25 @@ describe "controller: users.ListCtrl", -> beforeEach -> gridStub.getSelectedRowIds.returns([]) - it "does nothing", inject ($dialog) -> + it "does nothing", inject ($modal) -> # when $scope.massUpdate() # Then - expect($dialog.dialog.called).toBeFalsy() + expect($modal.open.called).toBeFalsy() describe "otherwise", -> - dialogStub = null - beforeEach inject ($dialog) -> + beforeEach -> gridStub.getSelectedRowIds.returns([1, 2, 3]) - dialogStub = sinon.stub(open: angular.noop) - $dialog.dialog.returns(dialogStub) - it "invokes a dialog", inject ($dialog) -> + it "invokes a dialog", inject ($modal) -> # When $scope.massUpdate() # Then - expect($dialog.dialog.called).toBeTruthy() - expect(dialogStub.open.called).toBeTruthy() + expect($modal.open.called).toBeTruthy() + + args = $modal.open.args[0][0] + expect(args.templateUrl).toEqual "/templates/users/massUpdateForm.html" + expect(args.controller).toEqual "users.MassUpdateFormCtrl" diff --git a/test/unit/controllers/users/massUpdateFormCtrlSpec.coffee b/test/unit/controllers/users/massUpdateFormCtrlSpec.coffee index 759ec0602..f57d0e3aa 100644 --- a/test/unit/controllers/users/massUpdateFormCtrlSpec.coffee +++ b/test/unit/controllers/users/massUpdateFormCtrlSpec.coffee @@ -3,20 +3,20 @@ describe "controller: users.MassUpdateFormCtrl", -> $scope = null usersStub = null - dialogStub = null + modalInstanceStub = null gridStub = null beforeEach inject ($rootScope, $controller) -> $scope = $rootScope.$new() usersStub = sinon.stub(massUpdate: angular.noop) - dialogStub = sinon.stub(close: angular.noop) + modalInstanceStub = sinon.stub(close: angular.noop) gridStub = sinon.stub(reload: angular.noop) $controller "users.MassUpdateFormCtrl", $scope: $scope + $modalInstance: modalInstanceStub Users: usersStub userIds: [1, 2, 3] - dialog: dialogStub usersGrid: gridStub describe "#save", -> @@ -36,7 +36,7 @@ describe "controller: users.MassUpdateFormCtrl", -> expect(gridStub.reload.called).toBeTruthy() it "closes a dialog", -> - expect(dialogStub.close.called).toBeTruthy() + expect(modalInstanceStub.close.called).toBeTruthy() describe "#closeDialog", -> @@ -45,4 +45,4 @@ describe "controller: users.MassUpdateFormCtrl", -> $scope.closeDialog() # Then - expect(dialogStub.close.called).toBeTruthy() + expect(modalInstanceStub.close.called).toBeTruthy() diff --git a/test/unit/controllers/usersListCtrlSpec.coffee b/test/unit/controllers/usersListCtrlSpec.coffee index 1a7cebc0d..3b23371e1 100644 --- a/test/unit/controllers/usersListCtrlSpec.coffee +++ b/test/unit/controllers/usersListCtrlSpec.coffee @@ -70,7 +70,7 @@ describe "controller: UsersListCtrl", -> describe "when the dialog was confirmed", -> beforeEach inject (confirmationDialog, $httpBackend) -> - sinon.stub(confirmationDialog, "open").returns(then: (fn) -> fn(true)) + sinon.stub(confirmationDialog, "open").returns(result: then: (fn) -> fn(true)) $httpBackend.expectDELETE("/api/users/#{user.id}").respond(id: 123) it "deleates the user", inject ($httpBackend) -> diff --git a/test/unit/modules/formsSpec.coffee b/test/unit/modules/formsSpec.coffee index 0ac4e1458..39c19288e 100644 --- a/test/unit/modules/formsSpec.coffee +++ b/test/unit/modules/formsSpec.coffee @@ -326,12 +326,16 @@ describe "module: angleGrinder.forms", -> itMarksFieldsAsValid() describe "service: confirmationDialog", -> - it "displays the confirmation", inject ($dialog, confirmationDialog) -> + it "displays the confirmation", inject ($modal, confirmationDialog) -> # Given - spy = sinon.spy($dialog, "dialog") + spy = sinon.spy($modal, "open") # When confirmationDialog.open() # Then expect(spy.called).toBeTruthy() + + args = spy.args[0][0] + expect(args.templateUrl).toEqual "templates/dialogs/confirmation.html" + expect(args.controller).toEqual "ConfirmationDialogCtrl"