Commit 71439428 authored by Randall Leeds's avatar Randall Leeds

Break out flash provider

I'm starting to think we should start separating modules in our
angular application by function, rather than having a monolithic
module for each category (filter, service, controller, etc).
parent 4faeae9c
......@@ -4,6 +4,7 @@ imports = [
'h.directives'
'h.app_directives'
'h.displayer'
'h.flash'
'h.filters'
'h.services'
]
......
class FlashProvider
queues:
'': []
info: []
error: []
success: []
notice: null
timeout: null
constructor: ->
# Configure notification classes
angular.extend Annotator.Notification,
INFO: 'info'
ERROR: 'error'
SUCCESS: 'success'
_process: ->
@timeout = null
for q, msgs of @queues
if msgs.length
msg = msgs.shift()
unless q then [q, msg] = msg
notice = Annotator.showNotification msg, q
@timeout = this._wait =>
# work around Annotator.Notification not removing classes
for _, klass of notice.options.classes
notice.element.removeClass klass
this._process()
break
_flash: (queue, messages) ->
if @queues[queue]?
@queues[queue] = @queues[queue]?.concat messages
this._process() unless @timeout?
$get: ['$timeout', ($timeout) ->
this._wait = (cb) -> $timeout cb, 5000
angular.bind this, this._flash
]
angular.module('h.flash', ['ngResource'])
.provider('flash', FlashProvider)
.config(['$httpProvider', ($httpProvider) ->
$httpProvider.responseInterceptors.push ['$q', 'flash', ($q, flash) ->
(promise) ->
promise.then (response) ->
data = response.data
format = response.headers 'content-type'
if format?.match /^application\/json/
if data.flash?
flash q, msgs for q, msgs of data.flash
if data.status is 'failure'
flash 'error', data.reason
$q.reject(data.reason)
else if data.status is 'okay'
response.data = data.model
response
else
response
]
])
\ No newline at end of file
......@@ -411,70 +411,7 @@ class DraftProvider
false
class FlashProvider
queues:
'': []
info: []
error: []
success: []
notice: null
timeout: null
this.$inject = ['$httpProvider']
constructor: ($httpProvider) ->
# Configure notification classes
angular.extend Annotator.Notification,
INFO: 'info'
ERROR: 'error'
SUCCESS: 'success'
# Configure the response interceptor
$httpProvider.responseInterceptors.push ['$q', ($q) =>
(promise) =>
promise.then (response) =>
data = response.data
format = response.headers 'content-type'
if format?.match /^application\/json/
if data.flash?
this._flash q, msgs for q, msgs of data.flash
if data.status is 'failure'
this._flash 'error', data.reason
$q.reject(data.reason)
else if data.status is 'okay'
response.data = data.model
response
else
response
]
_process: ->
@timeout = null
for q, msgs of @queues
if msgs.length
msg = msgs.shift()
unless q then [q, msg] = msg
notice = Annotator.showNotification msg, q
@timeout = this._wait =>
# work around Annotator.Notification not removing classes
for _, klass of notice.options.classes
notice.element.removeClass klass
this._process()
break
$get: ['$timeout', 'annotator', ($timeout, annotator) ->
this._wait = (cb) -> $timeout cb, 5000
angular.bind this, this._flash
]
_flash: (queue, messages) ->
if @queues[queue]?
@queues[queue] = @queues[queue]?.concat messages
this._process() unless @timeout?
angular.module('h.services', ['ngResource'])
.provider('authentication', AuthenticationProvider)
.provider('drafts', DraftProvider)
.provider('flash', FlashProvider)
.service('annotator', Hypothesis)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment