11import Ember from 'ember' ;
22
3+ //##Usage Info:
4+ //
5+ //Inject the `flashMessages: Ember.inject.service()` into the
6+ //target Ember Class
7+ //
8+ //##Standard Info Toast:
9+ //
10+ //`this.get('flashMessages').info('Some Message');
11+ //
12+ //##Sticky Toast:
13+ //
14+ //`this.get('flashMessages').add({
15+ // message: 'Some Message',
16+ // sticky: true,
17+ // type: 'info|warning'
18+ //})`
19+ //
20+ //##Progress Toast:
21+ //
22+ //(Note: the callback uses the context of the flash-message component)
23+ //
24+ //`var progress = {
25+ // status: true,
26+ // callback: function(){
27+ // this.set('message', 'completed!');
28+ // this.get('flash')._setTimer('timer', 'destroyMessage', 2000)
29+ // }
30+ //}`
31+ //
32+ //`setTimeout(function(){
33+ // Ember.set(progress, 'status', false);
34+ //}, 10000)`
35+ //
36+ //`this.get('flashMessages').add({
37+ // message: 'Some Message',
38+ // sticky: true,
39+ // progress: progress,
40+ // type: 'info|warning'
41+ //})`
42+
343var HbFlashMessageComponent = Ember . Component . extend ( {
444 classNames : [ 'hb-flash-message' ] ,
545 flashMessages : Ember . inject . service ( ) ,
@@ -19,10 +59,11 @@ var HbFlashMessageComponent = Ember.Component.extend({
1959 } else if ( index > max ) {
2060 _self . resetTimer ( flash , this . get ( 'timer' ) * 2 ) ;
2161 }
22- } )
62+ } ) ;
2363 } . observes ( 'flashMessages.queue.[]' , 'currentFlash.[]' ) ,
2464 addToQueue : function ( flash , current ) {
2565 var _self = this ;
66+ flash . id = _ . uniqueId ( 'flash' ) ;
2667 flash . on ( 'didDestroyMessage' , ( ) => {
2768 _self . scheduleRemove ( flash ) ;
2869 } ) ;
@@ -31,10 +72,10 @@ var HbFlashMessageComponent = Ember.Component.extend({
3172 if ( first && ! first . isDestroying ) {
3273 this . resetTimer ( first , this . get ( 'timer' ) / 2 ) ;
3374 }
34- if ( ! flash . get ( 'sticky ' ) ) {
35- this . resetTimer ( flash , this . get ( 'timer' ) ) ;
36- }
37- current . unshiftObject ( flash ) ;
75+ this . resetTimer ( flash , this . get ( 'timer ' ) ) ;
76+
77+ var index = this . determineIndex ( flash ) ;
78+ current . insertAt ( index , flash ) ;
3879 } ,
3980 scheduleRemove : function ( flash ) {
4081 var current = this . get ( 'currentFlash' ) ;
@@ -51,20 +92,29 @@ var HbFlashMessageComponent = Ember.Component.extend({
5192 } ,
5293 removeFlash : function ( flash , callback ) {
5394 this . set ( "removingFlash" , true ) ;
54- if ( this . get ( "currentFlash" ) . length === 1 ) {
95+ if ( this . get ( "currentFlash" ) . indexOf ( flash ) === 0 ) {
5596 return this . $ ( ".message" ) . first ( ) . animate ( {
5697 'top' : '-=38px'
5798 } , 400 , callback ) ;
5899 }
59- this . $ ( " .message" ) . last ( ) . animate ( {
100+ this . $ ( ` .message. ${ flash . id } ` ) . first ( ) . animate ( {
60101 'top' : '+=8px' ,
61102 'opacity' : 'hide'
62103 } , 400 , callback ) ;
63104 } ,
64105 resetTimer : function ( flash , time ) {
106+ if ( flash . get ( 'sticky' ) ) { return ; }
65107 flash . _cancelTimer ( "timer" ) ;
66108 flash . _cancelTimer ( "exitTimer" ) ;
67109 flash . _setTimer ( "timer" , "destroyMessage" , time ) ;
110+ } ,
111+ determineIndex : function ( flash ) {
112+ var current = this . get ( 'currentFlash' ) ;
113+ var sticky = current . find ( ( f ) => { return f . sticky } ) ;
114+ if ( sticky ) {
115+ return current . indexOf ( sticky ) + 1 ;
116+ }
117+ return 0 ;
68118 }
69119} ) ;
70120
0 commit comments