add utils angular module and navigation check factory

This commit is contained in:
Rafael Schouten
2014-10-14 00:25:46 +11:00
parent 5dd9879b96
commit 5e41865871
5 changed files with 47 additions and 4 deletions

View File

@@ -22,6 +22,7 @@
//= require ./payment_methods/payment_methods
//= require ./products/products
//= require ./shipping_methods/shipping_methods
//= require ./utils/utils
//= require ./users/users
//= require textAngular.min.js
//= require textAngular-sanitize.min.js

View File

@@ -1,11 +1,15 @@
angular.module("admin.enterprises")
.controller "enterpriseCtrl", ($scope, Enterprise, longDescription, PaymentMethods, ShippingMethods) ->
.controller "enterpriseCtrl", ($scope, longDescription, Enterprise, PaymentMethods, ShippingMethods, NavigationCheck) ->
$scope.Enterprise = Enterprise.enterprise
$scope.PaymentMethods = PaymentMethods.paymentMethods
$scope.ShippingMethods = ShippingMethods.shippingMethods
# htmlVariable is used by textAngular wysiwyg for the long descrtiption.
$scope.htmlVariable = longDescription
$scope.$on "$routeChangeStart", (event, newUrl, oldUrl) ->
event.preventDefault()
# Provide a callback for a warning message displayed when leaving the page.
navigationCallback = ->
"You are editing an enterprise!"
NavigationCheck.register navigationCallback
for payment_method in $scope.PaymentMethods
payment_method.selected = payment_method.id in $scope.Enterprise.payment_method_ids

View File

@@ -1 +1 @@
angular.module("admin.enterprises", ["admin.payment_methods", "admin.shipping_methods", "admin.users", "textAngular"])
angular.module("admin.enterprises", ["admin.payment_methods", "admin.utils", "admin.shipping_methods", "admin.users", "textAngular"])

View File

@@ -0,0 +1,37 @@
angular.module("admin.utils")
.factory "NavigationCheck", ($window, $rootScope) ->
callbacks = []
# Action for regular browser navigation.
onBeforeUnloadHandler = ($event) ->
message = getMessage()
if message
($event or $window.event).preventDefault()
message
# Action for angular navigation.
locationChangeStartHandler = ($event) ->
message = getMessage()
if message and not $window.confirm(message)
$event.stopPropagation() if $event.stopPropagation
$event.preventDefault() if $event.preventDefault
$event.cancelBubble = true
$event.returnValue = false
# Runs callback functions to retreive most recently added non-empty message.
getMessage = ->
message = null
message = callback() ? message for callback in callbacks
message
register = (callback) ->
callbacks.push callback
if $window.addEventListener
$window.addEventListener "beforeunload", onBeforeUnloadHandler
else
$window.onbeforeunload = onBeforeUnloadHandler
$rootScope.$on "$locationChangeStart", locationChangeStartHandler
return register: register

View File

@@ -0,0 +1 @@
angular.module("admin.utils", [])