WIP: Split out admin angularjs

This commit is contained in:
Rob H
2014-05-02 21:38:39 +10:00
parent 5a2a43a060
commit 6ba0d6c5f9
29 changed files with 270 additions and 298 deletions

View File

@@ -0,0 +1,2 @@
Admin.value "blankOption", ->
{ id: "0", name: "All" }

View File

@@ -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()

View 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
]

View File

@@ -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
]

View File

@@ -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
]

View 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)
]

View 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
}