ActionBar: accesskeys
[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       if (!(target in this.actions))
44         this.actions[target] = {};
45       this.actions[target][keystring] = action;
46     },
47
48     bind_targets: function(){
49       for (var target in this.actions) {
50         if (target in this.bound_targets) continue;
51         $(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
52         this.bound_targets[target] = 1;
53       }
54     },
55
56     handle_accesskey: function(e,t) {
57       var target = e.data.target;
58       var key = e.which;
59       var accesskey = '';
60       if (e.ctrlKey) accesskey += 'crtl+'
61       if (e.altKey)  accesskey += 'alt+'
62       accesskey += e.which;
63
64       // special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
65       // so. if accesskey is '13' and the event source is one of these (currently only textarea) ignore it.
66       // higher level widgets will usually prevent their key events from bubbling if used.
67       if (accesskey == 13 && e.target.tagName == 'TEXTAREA') return true;
68
69       if ((target in k.ActionBarAccesskeys.actions) && (accesskey in k.ActionBarAccesskeys.actions[target])) {
70         e.stopPropagation();
71         k.ActionBarAccesskeys.actions[target][accesskey].click();
72
73         // and another special case.
74         // if the form contains submit buttons the default action will click them instead.
75         // prevent that
76         if (accesskey == 13) return false;
77       }
78       return true;
79     }
80   };
81
82   k.ActionBarAction = function(e) {
83     var data = $(e).data('action');
84     if (undefined === data) return;
85
86     if (data.disabled) {
87       $(e).addClass(CLASSES.disabled);
88     }
89
90     if (data.accesskey) {
91       if (data.submit) {
92         k.ActionBarAccesskeys.add_accesskey(data.submit[0], data.accesskey, $(e));
93       }
94       if (data.call) {
95         k.ActionBarAccesskeys.add_accesskey(undefined, data.accesskey, $(e));
96       }
97     }
98
99     if (data.call || data.submit) {
100       $(e).click(function(event) {
101         var $hidden, key, func, check;
102         if ($(e).hasClass(CLASSES.disabled)) {
103           event.stopPropagation();
104           return;
105         }
106         if (data.checks) {
107           for (var i=0; i < data.checks.length; i++) {
108             check = data.checks[i];
109             func = kivi.get_function_by_name(check);
110             if (!func) console.log('Cannot find check function: ' + check);
111             if (!func()) return;
112           }
113         }
114         if (data.confirm && !confirm(data.confirm)) return;
115         if (data.call) {
116           func = kivi.get_function_by_name(data.call[0]);
117           func.apply(document, data.call.slice(1))
118         }
119         if (data.submit) {
120           var form   = data.submit[0];
121           var params = data.submit[1];
122           for (key in params) {
123             $hidden = $('<input type=hidden>')
124             $hidden.attr('name', key)
125             $hidden.attr('value', params[key])
126             $(form).append($hidden)
127           }
128           $(form).submit();
129         }
130       });
131     }
132   }
133 });
134
135 $(function(){
136   $('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
137     kivi.ActionBarAction(e)
138   });
139   $('div.layout-actionbar-combobox').each(function(_, e) {
140     $(e).data('combobox', new kivi.ActionBarCombobox(e));
141   });
142   $(document).click(function() {
143     $('div.layout-actionbar-combobox').removeClass('active');
144   });
145   kivi.ActionBarAccesskeys.bind_targets();
146 });