Fix flash messages

This commit is contained in:
Piotr Sarnacki 2015-09-09 10:36:12 +02:00
parent a8d585cb74
commit ca54a64211
5 changed files with 64 additions and 47 deletions

View File

@ -1,58 +1,15 @@
`import Ember from 'ember'`
`import LimitedArray from 'travis/utils/limited-array'`
FlashDisplayComponent = Ember.Component.extend
auth: Ember.inject.service()
store: Ember.inject.service()
currentUserBinding: 'auth.currentUser'
flashes: Ember.inject.service()
classNames: ['flash']
tagName: 'ul'
init: ->
@_super.apply this, arguments
@set('flashes', LimitedArray.create(limit: 1, content: []))
messages: (->
broadcasts = @get('unseenBroadcasts')
flashes = @get('flashes')
model = []
model.pushObjects(broadcasts) if broadcasts
model.pushObjects(flashes.toArray().reverse()) if flashes
model.uniq()
).property('unseenBroadcasts.[]', 'flashes.[]', 'unseenBroadcasts.length', 'flashes.length')
unseenBroadcasts: (->
@get('broadcasts').filter (broadcast) ->
!broadcast.get('isSeen')
).property('broadcasts.[]', 'broadcasts.length')
broadcasts: (->
broadcasts = Ember.ArrayProxy.create(content: [])
if @get('currentUser.id')
@get('store').find('broadcast').then (result) ->
broadcasts.pushObjects(result.toArray())
broadcasts
).property('currentUser.id')
loadFlashes: (msgs) ->
for msg in msgs
type = Object.keys(msg)[0]
msg = { type: type, message: msg[type] }
@get('flashes').unshiftObject(msg)
Ember.run.later(this, (-> @get('flashes.content').removeObject(msg)), 15000)
close: (msg) ->
if msg.constructor.modelName == "broadcast"
msg.setSeen()
@notifyPropertyChange('unseenBroadcasts')
else
@get('flashes').removeObject(msg)
messagesBinding: 'flashes.messages'
actions:
closeMessage: (msg) ->
@close(msg)
@get('flashes').close(msg)
`export default FlashDisplayComponent`

9
app/controllers/flash.js Normal file
View File

@ -0,0 +1,9 @@
import Ember from 'ember';
export default Ember.Controller.extend({
flashes: Ember.inject.service(),
loadFlashes() {
return this.get('flashes').loadFlashes(...arguments);
}
});

View File

@ -8,6 +8,7 @@ initialize = (container, app) ->
app.inject('controller', 'auth', 'auth:main')
app.inject('application', 'auth', 'auth:main')
app.inject('component', 'auth', 'auth:main')
app.inject('service:flashes', 'auth', 'auth:main')
app.inject('auth', 'store', 'service:store')

View File

@ -0,0 +1,51 @@
`import Ember from 'ember'`
`import LimitedArray from 'travis/utils/limited-array'`
FlashesService = Ember.Service.extend
store: Ember.inject.service()
currentUserBinding: 'auth.currentUser'
init: ->
@_super.apply this, arguments
@set('flashes', LimitedArray.create(limit: 1, content: []))
messages: (->
broadcasts = @get('unseenBroadcasts')
flashes = @get('flashes')
model = []
model.pushObjects(broadcasts) if broadcasts
model.pushObjects(flashes.toArray().reverse()) if flashes
model.uniq()
).property('unseenBroadcasts.[]', 'flashes.[]', 'unseenBroadcasts.length', 'flashes.length')
unseenBroadcasts: (->
@get('broadcasts').filter (broadcast) ->
!broadcast.get('isSeen')
).property('broadcasts.[]', 'broadcasts.length')
broadcasts: (->
broadcasts = Ember.ArrayProxy.create(content: [])
if @get('currentUser.id')
@get('store').find('broadcast').then (result) ->
broadcasts.pushObjects(result.toArray())
broadcasts
).property('currentUser.id')
loadFlashes: (msgs) ->
for msg in msgs
type = Object.keys(msg)[0]
msg = { type: type, message: msg[type] }
@get('flashes').unshiftObject(msg)
Ember.run.later(this, (-> @get('flashes.content').removeObject(msg)), 15000)
close: (msg) ->
if msg.constructor.modelName == "broadcast"
msg.setSeen()
@notifyPropertyChange('unseenBroadcasts')
else
@get('flashes').removeObject(msg)
`export default FlashesService`

View File

@ -1 +0,0 @@