Skip to content

Commit

Permalink
chore: partially upgrade angular-bootstrap to 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassus committed Sep 16, 2013
1 parent 16e5f42 commit a3a1bc4
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 71 deletions.
4 changes: 2 additions & 2 deletions app/scripts/controllers/listCtrl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions app/scripts/controllers/users/listCtrl.coffee
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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) ->
"""
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/controllers/users/massUpdateFormCtrl.coffee
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 4 additions & 3 deletions app/scripts/controllers/usersListCtrl.coffee
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
63 changes: 28 additions & 35 deletions app/scripts/modules/forms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
22 changes: 11 additions & 11 deletions test/unit/controllers/users/listCtrlSpec.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
10 changes: 5 additions & 5 deletions test/unit/controllers/users/massUpdateFormCtrlSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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", ->
Expand All @@ -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", ->

Expand All @@ -45,4 +45,4 @@ describe "controller: users.MassUpdateFormCtrl", ->
$scope.closeDialog()

# Then
expect(dialogStub.close.called).toBeTruthy()
expect(modalInstanceStub.close.called).toBeTruthy()
2 changes: 1 addition & 1 deletion test/unit/controllers/usersListCtrlSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
8 changes: 6 additions & 2 deletions test/unit/modules/formsSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit a3a1bc4

Please sign in to comment.