mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
add utils angular module and navigation check factory
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"])
|
||||
@@ -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
|
||||
1
app/assets/javascripts/admin/utils/utils.js.coffee
Normal file
1
app/assets/javascripts/admin/utils/utils.js.coffee
Normal file
@@ -0,0 +1 @@
|
||||
angular.module("admin.utils", [])
|
||||
Reference in New Issue
Block a user