mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-14 04:04:23 +00:00
WIP: Split out admin angularjs
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
Admin.value "blankOption", ->
|
||||
{ id: "0", name: "All" }
|
||||
@@ -0,0 +1,24 @@
|
||||
Admin.factory "ofnConfirmHandler", (pendingChanges, $compile, $q) ->
|
||||
return (scope, callback) ->
|
||||
template = "<div id='dialog-div' style='padding: 10px'><h6>Unsaved changes currently exist, save now or ignore?</h6></div>"
|
||||
dialogDiv = $compile(template)(scope)
|
||||
return ->
|
||||
if pendingChanges.changeCount(pendingChanges.pendingChanges) > 0
|
||||
dialogDiv.dialog
|
||||
dialogClass: "no-close"
|
||||
resizable: false
|
||||
height: 140
|
||||
modal: true
|
||||
buttons:
|
||||
"SAVE": ->
|
||||
dialogDiv = $(this)
|
||||
$q.all(pendingChanges.submitAll()).then ->
|
||||
callback()
|
||||
dialogDiv.dialog "close"
|
||||
"IGNORE": ->
|
||||
callback()
|
||||
$(this).dialog "close"
|
||||
scope.$apply()
|
||||
dialogDiv.dialog "open"
|
||||
else
|
||||
callback()
|
||||
12
app/assets/javascripts/admin/services/data_fetcher.js.coffee
Normal file
12
app/assets/javascripts/admin/services/data_fetcher.js.coffee
Normal file
@@ -0,0 +1,12 @@
|
||||
Admin.factory "dataFetcher", [
|
||||
"$http", "$q"
|
||||
($http, $q) ->
|
||||
return (dataLocation) ->
|
||||
deferred = $q.defer()
|
||||
$http.get(dataLocation).success((data) ->
|
||||
deferred.resolve data
|
||||
).error ->
|
||||
deferred.reject()
|
||||
|
||||
deferred.promise
|
||||
]
|
||||
@@ -0,0 +1,13 @@
|
||||
Admin.factory "dataSubmitter", [
|
||||
"$http", "$q", "switchClass"
|
||||
($http, $q, switchClass) ->
|
||||
return (changeObj) ->
|
||||
deferred = $q.defer()
|
||||
$http.put(changeObj.url).success((data) ->
|
||||
switchClass changeObj.element, "update-success", ["update-pending", "update-error"], 3000
|
||||
deferred.resolve data
|
||||
).error ->
|
||||
switchClass changeObj.element, "update-error", ["update-pending", "update-success"], false
|
||||
deferred.reject()
|
||||
deferred.promise
|
||||
]
|
||||
@@ -0,0 +1,32 @@
|
||||
Admin.factory "pendingChanges",[
|
||||
"dataSubmitter"
|
||||
(dataSubmitter) ->
|
||||
pendingChanges: {}
|
||||
|
||||
add: (id, attrName, changeObj) ->
|
||||
@pendingChanges["#{id}"] = {} unless @pendingChanges.hasOwnProperty("#{id}")
|
||||
@pendingChanges["#{id}"]["#{attrName}"] = changeObj
|
||||
|
||||
removeAll: ->
|
||||
@pendingChanges = {}
|
||||
|
||||
remove: (id, attrName) ->
|
||||
if @pendingChanges.hasOwnProperty("#{id}")
|
||||
delete @pendingChanges["#{id}"]["#{attrName}"]
|
||||
delete @pendingChanges["#{id}"] if @changeCount( @pendingChanges["#{id}"] ) < 1
|
||||
|
||||
submitAll: ->
|
||||
all = []
|
||||
for id,lineItem of @pendingChanges
|
||||
for attrName,changeObj of lineItem
|
||||
all.push @submit(id, attrName, changeObj)
|
||||
all
|
||||
|
||||
submit: (id, attrName, change) ->
|
||||
dataSubmitter(change).then (data) =>
|
||||
@remove id, attrName
|
||||
change.element.dbValue = data["#{attrName}"]
|
||||
|
||||
changeCount: (lineItem) ->
|
||||
Object.keys(lineItem).length
|
||||
]
|
||||
13
app/assets/javascripts/admin/services/switch_class.js.coffee
Normal file
13
app/assets/javascripts/admin/services/switch_class.js.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
Admin.factory "switchClass", [
|
||||
"$timeout"
|
||||
($timeout) ->
|
||||
return (element,classToAdd,removeClasses,timeout) ->
|
||||
$timeout.cancel element.timeout if element.timeout
|
||||
element.removeClass className for className in removeClasses
|
||||
element.addClass classToAdd
|
||||
intRegex = /^\d+$/
|
||||
if timeout && intRegex.test(timeout)
|
||||
element.timeout = $timeout(->
|
||||
element.removeClass classToAdd
|
||||
, timeout, true)
|
||||
]
|
||||
13
app/assets/javascripts/admin/services/taxons.js.coffee
Normal file
13
app/assets/javascripts/admin/services/taxons.js.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
Admin.factory "Taxons", ($resource) ->
|
||||
resource = $resource "/admin/taxons/search"
|
||||
|
||||
return {
|
||||
findByIDs: (ids) ->
|
||||
resource.get { ids: ids }
|
||||
|
||||
findByTerm: (term) ->
|
||||
resource.get { q: term }
|
||||
|
||||
cleanTaxons: (data) ->
|
||||
data['taxons'].map (result) -> result
|
||||
}
|
||||
Reference in New Issue
Block a user