Fixes #273.
[redakcja.git] / platforma / static / js / messages.js
1 /*global Editor*/
2 Editor.MessageCenter = Editor.Object.extend({
3     init: function() {
4         this.messages = [];
5         this.flashMessages = [];
6         this.firstFlashMessage = null;
7         this.timeout = null;
8         console.log("MSC-init:", Date(), this);
9     },
10   
11     addMessage: function(type, tag, text, flash)
12     {
13         if (!tag) tag = '#default'
14         
15         if (!flash) {
16             flash = text;
17         }
18
19         this.messages.push({
20             type: type,
21             text: text
22         });
23
24         this.flashMessages.push({
25             type: type,
26             text: flash,
27             tag: tag
28         });
29
30         if(this.timeout) {
31             if(this.flashMessages[0] && (this.flashMessages[0].tag == tag))
32             {
33                 clearTimeout(this.timeout);
34                 this.timeout = null;
35                 this.changeFlashMessage();
36             }
37         }       
38         
39         else {
40             /* queue was empty at the start */
41             if (this.flashMessages.length == 1) {
42                 console.log("MSC-added-fisrt", Date(), this);
43                 this.set('firstFlashMessage', this.flashMessages[0]);
44                 this.timeout = setTimeout(this.changeFlashMessage.bind(this), 3000);
45             }
46
47         }
48         
49     },
50   
51     changeFlashMessage: function() 
52     {
53         console.log("MSC-change", Date(), this);
54         var previous = this.flashMessages.splice(0, 1);
55         
56         if (this.flashMessages.length > 0) 
57         {
58             console.log("MSC-chaning-first", Date(), this);
59             this.set('firstFlashMessage', this.flashMessages[0]);            
60             this.timeout = setTimeout(this.changeFlashMessage.bind(this), 3000);
61         } else {
62             console.log("MSC-emptying", Date(), this);
63             this.set('firstFlashMessage', null);
64         }
65     }
66   
67 });
68
69
70 var messageCenter = new Editor.MessageCenter();
71