Use only one method for filters and without
[ajatus.git] / js / ajatus.toolbar.js
blob91e6fda43f6dec542b87062355a3530ccdb3cb26
1 (function($){
2     $.ajatus = $.ajatus || {};
4     $.ajatus.toolbar = {
5         settings: {
6             enable_clone: false
7         },
8         visible: false,
9         objects: {},
10         items: []
11     };
12     $.extend($.ajatus.toolbar, {
13         init: function(settings) {
14             $.ajatus.toolbar.settings = $.extend($.ajatus.toolbar.settings, settings || {});
15             $.ajatus.toolbar.objects = {};
16             $.ajatus.toolbar.items = [];
17             
18             var main_tb = $('#main-page_toolbar', $.ajatus.application_element).hide();
19             if (! main_tb[0]) {
20                 main_tb = $.ajatus.toolbar.create_main();
21             } else {
22                 $.ajatus.toolbar.prepare_main(main_tb);
23             }
24             
25             $.ajatus.toolbar.objects['main'] = main_tb;
26             
27             if ($.ajatus.toolbar.settings.enable_clone) {
28                 var clone_tb = $('#clone-page_toolbar', $.ajatus.application_element).hide();
29                 if (! clone_tb[0]) {
30                     clone_tb = $.ajatus.toolbar.create_clone(main_tb);
31                 } else {
32                     $.ajatus.toolbar.prepare_clone();
33                 }
34                 $.ajatus.toolbar.objects['clone'] = clone_tb;
35             }
36             
37             $.ajatus.toolbar.clear();
38         },
39         show: function(index) {
40             if (typeof index == 'undefined') {
41                 $.each($.ajatus.toolbar.objects, function(i,o){
42                     o.show();
43                 });
44             } else {
45                 if (typeof($.ajatus.toolbar.objects[index]) != 'undefined') {
46                     $.ajatus.toolbar.objects[index].show();
47                 }
48             }
49             $.ajatus.toolbar.visible = true;
50         },
51         hide: function(index) {
52             if (typeof index == 'undefined') {
53                 $.each($.ajatus.toolbar.objects, function(i,o){
54                     o.hide();
55                 });
56             } else {
57                 if (typeof($.ajatus.toolbar.objects[index]) != 'undefined') {
58                     $.ajatus.toolbar.objects[index].hide();
59                 }
60             }
61             $.ajatus.toolbar.visible = false;
62         },
63         add_item: function() { // title, settings/icon/action, action, action args, insert after
64             if (arguments.length <= 0) {
65                 return;
66             }
67             var item_obj = {
68                 id: $.ajatus.utils.generate_id(),
69                 title: arguments[0],
70                 icon: null,
71                 content: [],
72                 action: null,
73                 extra_args: [],
74                 insert_after: null,
75                 access_key: null
76             };            
77             var item_settings = {};
78             var extra_args = [];
79             
80             var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['main']);
81             
82             if (arguments.length == 2) {
83                 if (typeof arguments[1] == 'object') {
84                     $.each($.ajatus.toolbar._parse_settings(arguments[1]), function(key,value){
85                         item_obj[key] = value;
86                     });
87                     extra_args = [item_obj];
88                     $.each(item_obj['extra_args'], function(i,n){
89                         extra_args.push(n);
90                     });
91                 } else {
92                     item_obj['action'] = arguments[1];
93                     item_obj['content'] = [
94                         'div', {}, title
95                     ];                    
96                 }
97             }
98             
99             if (arguments.length >= 3) {
100                 item_obj['icon'] = $.ajatus.preferences.client.theme_icons_url + arguments[1];
101                 item_obj['action'] = arguments[2];
102                 item_obj['content'] = [
103                     'div', {}, [
104                         'img', { src: item_obj['icon'], alt: item_obj['title'], title: item_obj['title'] }, ''
105                     ]
106                 ];
107             } else if (item_obj['icon'] != null) {
108                 item_obj['content'] = [
109                     'div', {}, [
110                         'img', { src: item_obj['icon'], alt: item_obj['title'], title: item_obj['title'] }, ''
111                     ]
112                 ];
113             }
114             
115             if (   arguments.length >= 4
116                 && typeof arguments[3] == 'object'
117                 && arguments[3].length > 0)
118             {
119                 item_obj['extra_args'] = arguments[3];
120                 extra_args = [item_obj];
121             }
122             
123             if (extra_args.length > 0) {
124                 $.each(item_obj['extra_args'], function(i,n){
125                     extra_args.push(n);                    
126                 });
127             }
129             if (arguments.length == 5) {
130                 item_obj['insert_after'] = arguments[4];
131             }
133             if (item_obj['insert_after'] != null) {         
134                 var item_elem = $('<li class="item" />').attr({
135                     id: item_obj.id
136                 });                
137                 item_elem.insertAfter($('#'+item_obj['insert_after'], item_holder));                
138                 $('#'+item_obj.id, item_holder).createAppend(item_obj['content'][0], item_obj['content'][1], item_obj['content'][2]);
139             } else {
140                 item_holder.createAppend(
141                     'li', { className: 'item', id: item_obj.id }, item_obj['content']
142                 );
143             }
144             
145             $('#'+item_obj.id, item_holder).bind('click', function(e){
146                 item_obj['action'].apply(item_obj['action'], extra_args);
147             });
148             
149             if (item_obj['access_key'] != null) {
150                 $.hotkeys.add(item_obj['access_key'], function(){ item_obj['action'].apply(item_obj['action'], extra_args); });
151             }
152             
153             if ($.ajatus.toolbar.settings.enable_clone) {
154                 var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['clone']);
155                 
156                 if (item_obj['insert_after']) {
157                     var item_elem = $('<li class="item" />').attr({
158                         id: item_obj.id+'_clone'
159                     });
160                     item_elem.insertAfter($('#'+item_obj['insert_after']+'_clone', item_holder));
161                     $('#'+item_obj.id+'_clone', item_holder).createAppend(item_obj['content'][0], item_obj['content'][1], item_obj['content'][2]);
162                 } else {
163                     item_holder.createAppend(
164                         'li', { className: 'item', id: item_obj.id+'_clone' }, item_obj['content']
165                     );                
166                 }
168                 $('#'+item_obj.id, item_holder).bind('click', function(e){
169                     item_obj['action'].apply(item_obj['action'], extra_args);
170                 });
171             }
172             
173             $.ajatus.toolbar.items.push(item_obj);
174             
175             return item_obj.id;
176         },
177         remove_item: function(id) {
178             var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['main']);
179             $('#'+id, item_holder).remove();
180             if ($.ajatus.toolbar.settings.enable_clone) {
181                 var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['clone']);
182                 $('#'+id+'_clone', item_holder).remove();
183             }
184             $.ajatus.toolbar.items = $.grep($.ajatus.toolbar.items, function(n,i){
185                 if (n.id == id) {
186                     if (n['access_key'] != null) {
187                         $.hotkeys.remove(n['access_key']);
188                     }
189                     return false;
190                 }
191                 return true;
192             });
193         },
194         hide_item: function(id) {
195             var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['main']);
196             $('#'+id, item_holder).hide();
198             if ($.ajatus.toolbar.settings.enable_clone) {
199                 var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['clone']);
200                 $('#'+id+'_clone', item_holder).hide();
201             }
202         },
203         show_item: function(id) {
204             var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['main']);
205             $('#'+id, item_holder).show();
206             
207             if ($.ajatus.toolbar.settings.enable_clone) {
208                 var item_holder = $('ul.item_holder', $.ajatus.toolbar.objects['clone']);
209                 $('#'+id+'_clone', item_holder).show();
210             }  
211         },
212         clear: function() {
213             $('ul.item_holder', $.ajatus.toolbar.objects['main']).html('');
214             if ($.ajatus.toolbar.settings.enable_clone) {
215                 $('ul.item_holder', $.ajatus.toolbar.objects['clone']).html('');
216             }
217             $.ajatus.toolbar.items = [];
218         },
219         create_main: function() {
220             
221         },
222         create_clone: function() {
223             
224         },
225         prepare_main: function(tb) {
226             var tb_content = $('.content', tb);
227             tb_content.html('');
228             var items_holder = $('<ul class="item_holder"/>');
229             items_holder.appendTo(tb_content);
230         },
231         prepare_clone: function(tb) {
232             var tb_content = $('.content', tb);
233             tb_content.html('');
234             var items_holder = $('<ul class="item_holder"/>');
235             items_holder.appendTo(tb_content);            
236         },
237         clone: function() {
238             var main_tb_content = $('.content', $.ajatus.toolbar.objects['main']);
239             var clone_tb_content = $('.content', $.ajatus.toolbar.objects['clone']);
240             var main_items = $('ul.item_holder', main_tb_content).html();
241             $('ul.item_holder', clone_tb_content).html(main_items);            
242         },
243         _parse_settings: function(s) {
244             var settings = {};
245             $.each(s, function(k, v){
246                 if ($.inArray(k, ['icon','action','insert_after','access_key','extra_args']) != -1) {
247                     if (k == 'icon') {
248                         v = $.ajatus.preferences.client.theme_icons_url + v;
249                     }
250                     settings[k] = v;
251                 }
252                 return;
253             });
255             return settings;
256         }
257     });
258     
259 })(jQuery);