ActionBar: Unterstützung für Tooltips für deaktivierte Actions
[kivitendo-erp.git] / js / kivi.ActionBar.js
1 namespace('kivi', function(k){
2   'use strict';
3
4   var CLASSES = {
5     active:   'active',
6     actionbar: 'layout-actionbar',
7     disabled: 'layout-actionbar-action-disabled',
8     action:   'layout-actionbar-action',
9     combobox: 'layout-actionbar-combobox',
10   }
11
12   k.ActionBarCombobox = function(e) {
13     this.combobox = e;
14     this.head     = e.childNodes[0];
15     this.toggle   = this.head.childNodes[1];
16     this.list     = e.childNodes[0];
17     this.init();
18   }
19
20   k.ActionBarCombobox.prototype = {
21     init: function() {
22       var obj = this;
23       $(obj.toggle).on('click', function(event){
24         $('div.' + CLASSES.combobox + '[id!=' + obj.combobox.id + ']').removeClass(CLASSES.active);
25         $(obj.combobox).toggleClass(CLASSES.active);
26         event.stopPropagation();
27       });
28     }
29   };
30
31   k.ActionBarAccesskeys = {
32     known_keys: {
33      'enter': 13,
34      'esc': 27,
35     },
36     actions: {},
37     bound_targets: {},
38
39     add_accesskey: function (target, keystring, action) {
40       if (target === undefined) {
41         target = 'document';
42       }
43
44       var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) {
45         console.log(keystring)
46         switch (val) {
47           case 'ctrl':
48           case 'alt':  return val;
49           case 'enter': return 13;
50           default:
51             if (val.length == 1) {
52               return val.charChodeAt(0)
53             } else if (val % 1 === 0) {
54               return val;
55             } else {
56               console.log('can not normalize access key token: ' + val);
57             }
58         }
59       }).join('+');
60
61       if (!(target in this.actions))
62         this.actions[target] = {};
63       this.actions[target][normalized] = action;
64     },
65
66     bind_targets: function(){
67       for (var target in this.actions) {
68         if (target in this.bound_targets) continue;
69         $(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
70         this.bound_targets[target] = 1;
71       }
72     },
73
74     handle_accesskey: function(e,t) {
75       var target = e.data.target;
76       var key = e.which;
77       var accesskey = '';
78       if (e.ctrlKey) accesskey += 'crtl+'
79       if (e.altKey)  accesskey += 'alt+'
80       accesskey += e.which;
81
82       // special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
83       // so. if accesskey is '13' and the event source is one of these (currently only textarea) ignore it.
84       // higher level widgets will usually prevent their key events from bubbling if used.
85       if (accesskey == 13 && e.target.tagName == 'TEXTAREA') return true;
86
87       if ((target in k.ActionBarAccesskeys.actions) && (accesskey in k.ActionBarAccesskeys.actions[target])) {
88         e.stopPropagation();
89         k.ActionBarAccesskeys.actions[target][accesskey].click();
90
91         // and another special case.
92         // if the form contains submit buttons the default action will click them instead.
93         // prevent that
94         if (accesskey == 13) return false;
95       }
96       return true;
97     }
98   };
99
100   k.ActionBarAction = function(e) {
101     var data = $(e).data('action');
102     if (undefined === data) return;
103
104     if (data.disabled) {
105       $(e).addClass(CLASSES.disabled);
106       if (!data.tooltip && (data.disabled != '1'))
107         data.tooltip = data.disabled;
108     }
109
110     if (data.accesskey) {
111       if (data.submit) {
112         k.ActionBarAccesskeys.add_accesskey(data.submit[0], data.accesskey, $(e));
113       }
114       if (data.call) {
115         k.ActionBarAccesskeys.add_accesskey(undefined, data.accesskey, $(e));
116       }
117     }
118
119     if (data.tooltip) {
120       $(e).tooltipster({ content: data.tooltip, theme: 'tooltipster-light' });
121     }
122
123     if (data.call || data.submit) {
124       $(e).click(function(event) {
125         var $hidden, key, func, check;
126         if ($(e).hasClass(CLASSES.disabled)) {
127           event.stopPropagation();
128           return;
129         }
130         if (data.checks) {
131           for (var i=0; i < data.checks.length; i++) {
132             check = data.checks[i];
133             func = kivi.get_function_by_name(check);
134             if (!func) console.log('Cannot find check function: ' + check);
135             if (!func()) return;
136           }
137         }
138         if (data.confirm && !confirm(data.confirm)) return;
139         if (data.call) {
140           func = kivi.get_function_by_name(data.call[0]);
141           func.apply(document, data.call.slice(1))
142         }
143         if (data.submit) {
144           var form   = data.submit[0];
145           var params = data.submit[1];
146           for (key in params) {
147             $hidden = $('<input type=hidden>')
148             $hidden.attr('name', key)
149             $hidden.attr('value', params[key])
150             $(form).append($hidden)
151           }
152           $(form).submit();
153         }
154       });
155     }
156   }
157 });
158
159 $(function(){
160   $('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
161     kivi.ActionBarAction(e)
162   });
163   $('div.layout-actionbar-combobox').each(function(_, e) {
164     $(e).data('combobox', new kivi.ActionBarCombobox(e));
165   });
166   $(document).click(function() {
167     $('div.layout-actionbar-combobox').removeClass('active');
168   });
169   kivi.ActionBarAccesskeys.bind_targets();
170 });