Fixed timed event check
[ajatus.git] / js / ajatus.elements.js
blobcc7ded22b31a1ca4cceabb1b2a8601ac1099c1a6
1 /*
2  * This file is part of
3  *
4  * Ajatus - Distributed CRM
5  * @requires jQuery v1.2.1
6  * 
7  * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8  * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9  * Website: http://ajatus.info
10  * Licensed under the GPL license
11  * http://www.gnu.org/licenses/gpl.html
12  * 
13  */
15 (function($){
16     $.ajatus = $.ajatus || {};
17     $.ajatus.elements = $.ajatus.elements || {};
18     
19     /**
20      * Ajatus Dialog
21      */
22     $.ajatus.elements.dialog = function(title, msg, options)
23     {                
24         $.ajatus.layout.styles.load($.ajatus.preferences.client.theme_url + 'css/jquery/plugins/jqModal.css');
25         $.ajatus.layout.styles.load($.ajatus.preferences.client.theme_url + 'css/jquery/plugins/dialog/dialog.css');
26         
27         this.holder = $('#dynamic-elements');
28         this.options = {
29             modal: true,
30             overlay: 30
31         };
32         
33         this.id = this.create(title, msg, options);
34         return this;
35     };
36     $.extend($.ajatus.elements.dialog.prototype, {
37         create: function(title, msg, options)
38         {
39             var closable = true;
40             if (this.options.closable != undefined) {
41                 closable = this.options.closable;
42             }
43             
44             var id = this._generate_id();
45             var dialog = this._create_dialog_structure(id, closable);
46             
47             if (typeof title != 'undefined') {
48                 $('.jqmdTC',dialog).html(title);
49             }
50             if (typeof msg != 'undefined') {
51                 $('.jqmdMSG',dialog).html(msg);
52             }
53             
54             this.holder.html(dialog);
55             
56             var content_target = $('.jqmdMSG',dialog);
57             
58             var options = $.extend({
59                 target: content_target,
60                 onHide: function(h) { 
61                     content_target.html('');
62                     h.o.remove();
63                     h.w.fadeOut(300);
64                 }
65             }, this.options, options);
66             
67             $('#' + id).jqm(options);
68             
69             return id;
70         },
71         
72         set_content: function(content) {
73             $('#' + this.id + ' .jqmdMSG').html(content);
74         },
75         
76         append_content: function(content)
77         {
78             $('#' + this.id + ' .jqmdMSG').append(content);
79         },
80         
81         prepend_content: function(content)
82         {
83             $('#' + this.id + ' .jqmdMSG').prepend(content);
84         },
85         
86         open: function()
87         {
88             $('#' + this.id).jqmShow();
89         },
90         
91         close: function()
92         {
93             $('#' + this.id).jqmHide();
94         },
96         destroy: function()
97         {
98             $('#' + this.id).jqmHide();
99             $('#' + this.id).remove();
100         },
101                 
102         _create_dialog_structure: function(id, closable)
103         {
104             var body = $('<div />')
105             .attr('id', id)
106             .addClass('jqmDialog')
107             .hide()
108             .css({width: 300, height: 450});
109             
110             var frame = $('<div class="jqmdTL"><div class="jqmdTR"><div class="jqmdTC"></div></div></div><div class="jqmdBL"><div class="jqmdBR"><div class="jqmdBC"><div class="jqmdMSG"></div></div></div></div>');
111             
112             if (   typeof closable == 'undefined'
113                 || closable != false)
114             {
115                 frame.append('<input type="image" src="' + $.ajatus.preferences.client.theme_url + 'css/jquery/plugins/dialog/close.gif" class="jqmdX jqmClose" />');
116             }
117             
118             body.html(frame);
119             
120             return body;
121         },
122         
123         _generate_id: function()
124         {
125             return "ajatus_dialog_" + $.ajatus.utils.generate_id();
126         }
127     });
128     /**
129      * Ajatus Dialog ends
130      */
131      
132     $.ajatus.elements.messages = {
133         msgs: [],
134         watchers: [],
135         holder: null
136     };
137     $.extend($.ajatus.elements.messages, {
138         create: function(title, message, options)
139         {
140             if (typeof options == 'undefined') {
141                 options = {
142                     closable: true,
143                     visible: 5
144                 };
145             }
146             if (typeof options.type == 'undefined') {
147                 options.type = 'notification';
148             }
149             if (typeof options.closable == 'undefined') {
150                 options.closable = true;
151             }
152             options.holder = $.ajatus.elements.messages.holder;            
153             var msg = new $.ajatus.elements.message(title, message, options);
155             $.ajatus.elements.messages.msgs.push(msg);
156             
157             if (   options.visible
158                 && options.visible > 0)
159             {
160                 var watcher_opts = {
161                     auto_start: true,
162                     safety_runs: 0,
163                     interval: 200,
164                     timed: (options.visible * 1000)
165                 };
166                 var watcher = new $.ajatus.events.watcher(watcher_opts);
167                 watcher.element.bind('on_stop', function(e, watcher_id){
168                     msg.fade('out', true);
169                 });
170             }
171              
172             return msg;
173         },
174         static: function(title, message, options)
175         {
176             var msg = new $.ajatus.elements.message(title, message, options);
177             return msg;
178         },
179         remove: function(msg_id)
180         {
181             $.ajatus.elements.messages.msgs = $.grep($.ajatus.elements.messages.msgs, function(n,i){
182                 if (n.id == msg_id) {
183                     n.remove();
184                     return false;
185                 } else {
186                     return true;
187                 }
188             });
189         },
190         clear: function()
191         {
192             $.ajatus.elements.messages.msgs = [];
193             $.ajatus.elements.messages.holder.html('');
194         },
195         gen_id: function()
196         {
197             var date = new Date();
198             var id = 'msg_' + date.getTime();
199             return id;
200         },
201         set_holder: function(holder)
202         {
203             if (typeof holder == 'undefined') {
204                 var holder = $('#system_messages', $.ajatus.application_element);
205             }
206             $.ajatus.elements.messages.holder = holder;
207         }
208     });
209      
210     $.ajatus.elements.message = function(title, msg, options)
211     {
212         this.options = $.extend({
213             auto_show: true,
214             disappear: false,
215             holder: false,
216             closable: false,
217             type: 'static'
218         }, options || {});
219         this.holder = this.options.holder;
220         this.body_div = null;
221          
222         if (! this.holder) {
223             this.holder = $('<div />');
224             this.holder.appendTo($.ajatus.application_content_area);
225         }
226          
227         this.id = this.create(title, msg);
228         return this;
229     };
230     $.extend($.ajatus.elements.message.prototype, {
231         create: function(title, msg) {
232             var id = $.ajatus.elements.messages.gen_id();
233             this.body_div = jQuery('<div class="ajatus_elements_message" />').attr({
234                 id: id
235             }).addClass(this.options.type).hide();
236             this.body_div.appendTo(this.holder);
237             
238             if (this.options.closable) {
239                 var self = this;
240                 var close_handle = jQuery('<div class="close_handle" />')
241                 close_handle.appendTo(this.body_div)
242                 close_handle.bind('click', function(){
243                     self.fade('out', true);
244                 });
245             }
247             var title_item = jQuery('<h2 />').attr('class', 'title').html(title);
248             title_item.appendTo(this.body_div);
249             
250             var msg_item = jQuery('<div />').attr('class', 'message').html(msg);
251             msg_item.appendTo(this.body_div);
252             
253             if (this.options.auto_show) {
254                 this.show();
255             }
256             
257             return id;
258         },
259         show: function() {
260             this.fade('in');
261             //this.body_div.show();
262         },
263         hide: function() {
264             this.fade('out');
265             //this.body_div.hide();
266         },
267         fade: function(direction, destroy) {
268             var self = this;
269             if (direction == 'out') {
270                 this.body_div.fadeOut('slow', function(){
271                     if (destroy) {
272                         self.destroy();
273                     }
274                 });
275             } else {
276                 this.body_div.fadeIn('normal', function(){
277                 });
278             }
279         },
280         remove: function() {
281             this.body_div.remove();            
282         },
283         destroy: function() {
284             $.ajatus.elements.messages.remove(this.id);
285         },
286         bind_in_content: function(action, selector, func) {
287             $(selector, this.body_div).bind(action, func);
288         }
289     });
290     
291 })(jQuery);