Extract status message to a service

This commit is contained in:
Rohan Mitchell
2014-12-04 17:07:51 +11:00
parent abf58c0e02
commit bcc3815f6b
3 changed files with 37 additions and 21 deletions

View File

@@ -1,9 +1,7 @@
angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout, $http, BulkProducts, DisplayProperties, dataFetcher, DirtyProducts, VariantUnitManager, producers, Taxons, SpreeApiAuth) ->
angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout, $http, BulkProducts, DisplayProperties, dataFetcher, DirtyProducts, VariantUnitManager, StatusMessage, producers, Taxons, SpreeApiAuth) ->
$scope.loading = true
$scope.updateStatusMessage =
text: ""
style: {}
$scope.StatusMessage = StatusMessage
$scope.columns =
producer: {name: "Producer", visible: true}
@@ -64,7 +62,7 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
$scope.resetProducts = ->
DirtyProducts.clear()
$scope.setMessage $scope.updateStatusMessage, "", {}, false
StatusMessage.clearMessage()
# $scope.matchProducer = (product) ->
# for producer in $scope.producers
@@ -182,7 +180,7 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
if productsToSubmit.length > 0
$scope.updateProducts productsToSubmit # Don't submit an empty list
else
$scope.setMessage $scope.updateStatusMessage, "No changes to save.", color: "grey", 3000
StatusMessage.displayMessage 'No changes to save.', 'alert'
$scope.updateProducts = (productsToSubmit) ->
@@ -250,31 +248,23 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
$scope.displayUpdating = ->
$scope.setMessage $scope.updateStatusMessage, "Saving...",
color: "#FF9906"
, false
StatusMessage.displayMessage 'Saving...', 'progress'
$scope.displaySuccess = ->
$scope.setMessage $scope.updateStatusMessage, "Changes saved.",
color: "#9fc820"
, 3000
StatusMessage.displayMessage 'Changes saved.', 'success'
$scope.displayFailure = (failMessage) ->
$scope.setMessage $scope.updateStatusMessage, "Saving failed. " + failMessage,
color: "#DA5354"
, false
StatusMessage.displayMessage "Saving failed. #{failMessage}", 'failure'
$scope.displayDirtyProducts = ->
if DirtyProducts.count() > 0
message = if DirtyProducts.count() == 1 then "one product" else DirtyProducts.count() + " products"
$scope.setMessage $scope.updateStatusMessage, "Changes to " + message + " remain unsaved.",
color: "gray"
, false
StatusMessage.displayMessage "Changes to #{message} remain unsaved.", 'notice'
else
$scope.setMessage $scope.updateStatusMessage, "", {}, false
StatusMessage.clearMessage()
filterSubmitProducts = (productsToFilter) ->

View File

@@ -0,0 +1,26 @@
angular.module("ofn.admin").factory "StatusMessage", ($timeout) ->
new class StatusMessage
types:
progress: {timeout: false, style: {color: '#ff9906'}}
alert: {timeout: 3000, style: {color: 'grey'}}
notice: {timeout: false, style: {color: 'grey'}}
success: {timeout: 3000, style: {color: '#9fc820'}}
failure: {timeout: false, style: {color: '#da5354'}}
statusMessage:
text: ""
style: {}
displayMessage: (text, type) ->
@statusMessage.text = text
@statusMessage.style = @types[type].style
$timeout.cancel @statusMessage.timeout if @statusMessage.timeout
timeout = @types[type].timeout
if timeout
@statusMessage.timeout = $timeout =>
@clearMessage()
, timeout, true
clearMessage: ->
@statusMessage.text = ''
@statusMessage.style = {}

View File

@@ -2,8 +2,8 @@
%div.four.columns.alpha
%input.four.columns.alpha{ :type => 'button', :value => 'Save Changes', 'ng-click' => 'submitProducts()'}
%div.nine.columns
%h6{ id: "update-status-message", ng: { style: 'updateStatusMessage.style' } }
{{ updateStatusMessage.text || " " }}
%h6{ id: "update-status-message", ng: { style: 'StatusMessage.statusMessage.style' } }
{{ StatusMessage.statusMessage.text || " " }}
%div.three.columns.omega
%div.ofn_drop_down.three.columns.omega{ 'ng-controller' => "DropDownCtrl", :id => "columns_dropdown", 'ofn-drop-down' => true, :style => 'float:right;' }
%span{ :class => 'icon-reorder' }   Columns