Merge branch 'master' of git+ssh://repo.or.cz/srv/git/ajatus
[ajatus.git] / js / ajatus.elements.js
blobcc7ded22b31a1ca4cceabb1b2a8601ac1099c1a6
1 /*
2 * This file is part of
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
15 (function($){
16 $.ajatus = $.ajatus || {};
17 $.ajatus.elements = $.ajatus.elements || {};
19 /**
20 * Ajatus Dialog
22 $.ajatus.elements.dialog = function(title, msg, options)
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');
27 this.holder = $('#dynamic-elements');
28 this.options = {
29 modal: true,
30 overlay: 30
33 this.id = this.create(title, msg, options);
34 return this;
36 $.extend($.ajatus.elements.dialog.prototype, {
37 create: function(title, msg, options)
39 var closable = true;
40 if (this.options.closable != undefined) {
41 closable = this.options.closable;
44 var id = this._generate_id();
45 var dialog = this._create_dialog_structure(id, closable);
47 if (typeof title != 'undefined') {
48 $('.jqmdTC',dialog).html(title);
50 if (typeof msg != 'undefined') {
51 $('.jqmdMSG',dialog).html(msg);
54 this.holder.html(dialog);
56 var content_target = $('.jqmdMSG',dialog);
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);
65 }, this.options, options);
67 $('#' + id).jqm(options);
69 return id;
72 set_content: function(content) {
73 $('#' + this.id + ' .jqmdMSG').html(content);
76 append_content: function(content)
78 $('#' + this.id + ' .jqmdMSG').append(content);
81 prepend_content: function(content)
83 $('#' + this.id + ' .jqmdMSG').prepend(content);
86 open: function()
88 $('#' + this.id).jqmShow();
91 close: function()
93 $('#' + this.id).jqmHide();
96 destroy: function()
98 $('#' + this.id).jqmHide();
99 $('#' + this.id).remove();
102 _create_dialog_structure: function(id, closable)
104 var body = $('<div />')
105 .attr('id', id)
106 .addClass('jqmDialog')
107 .hide()
108 .css({width: 300, height: 450});
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>');
112 if ( typeof closable == 'undefined'
113 || closable != false)
115 frame.append('<input type="image" src="' + $.ajatus.preferences.client.theme_url + 'css/jquery/plugins/dialog/close.gif" class="jqmdX jqmClose" />');
118 body.html(frame);
120 return body;
123 _generate_id: function()
125 return "ajatus_dialog_" + $.ajatus.utils.generate_id();
129 * Ajatus Dialog ends
132 $.ajatus.elements.messages = {
133 msgs: [],
134 watchers: [],
135 holder: null
137 $.extend($.ajatus.elements.messages, {
138 create: function(title, message, options)
140 if (typeof options == 'undefined') {
141 options = {
142 closable: true,
143 visible: 5
146 if (typeof options.type == 'undefined') {
147 options.type = 'notification';
149 if (typeof options.closable == 'undefined') {
150 options.closable = true;
152 options.holder = $.ajatus.elements.messages.holder;
153 var msg = new $.ajatus.elements.message(title, message, options);
155 $.ajatus.elements.messages.msgs.push(msg);
157 if ( options.visible
158 && options.visible > 0)
160 var watcher_opts = {
161 auto_start: true,
162 safety_runs: 0,
163 interval: 200,
164 timed: (options.visible * 1000)
166 var watcher = new $.ajatus.events.watcher(watcher_opts);
167 watcher.element.bind('on_stop', function(e, watcher_id){
168 msg.fade('out', true);
172 return msg;
174 static: function(title, message, options)
176 var msg = new $.ajatus.elements.message(title, message, options);
177 return msg;
179 remove: function(msg_id)
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;
190 clear: function()
192 $.ajatus.elements.messages.msgs = [];
193 $.ajatus.elements.messages.holder.html('');
195 gen_id: function()
197 var date = new Date();
198 var id = 'msg_' + date.getTime();
199 return id;
201 set_holder: function(holder)
203 if (typeof holder == 'undefined') {
204 var holder = $('#system_messages', $.ajatus.application_element);
206 $.ajatus.elements.messages.holder = holder;
210 $.ajatus.elements.message = function(title, msg, options)
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;
222 if (! this.holder) {
223 this.holder = $('<div />');
224 this.holder.appendTo($.ajatus.application_content_area);
227 this.id = this.create(title, msg);
228 return this;
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);
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);
247 var title_item = jQuery('<h2 />').attr('class', 'title').html(title);
248 title_item.appendTo(this.body_div);
250 var msg_item = jQuery('<div />').attr('class', 'message').html(msg);
251 msg_item.appendTo(this.body_div);
253 if (this.options.auto_show) {
254 this.show();
257 return id;
259 show: function() {
260 this.fade('in');
261 //this.body_div.show();
263 hide: function() {
264 this.fade('out');
265 //this.body_div.hide();
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();
275 } else {
276 this.body_div.fadeIn('normal', function(){
280 remove: function() {
281 this.body_div.remove();
283 destroy: function() {
284 $.ajatus.elements.messages.remove(this.id);
286 bind_in_content: function(action, selector, func) {
287 $(selector, this.body_div).bind(action, func);
291 })(jQuery);