1 namespace('kivi', function(k){
6 actionbar: 'layout-actionbar',
7 disabled: 'layout-actionbar-action-disabled',
8 action: 'layout-actionbar-action',
9 combobox: 'layout-actionbar-combobox',
12 k.ActionBarCombobox = function(e) {
14 this.head = e.childNodes[0];
15 this.topAction = this.head.childNodes[0];
16 this.toggle = this.head.childNodes[1];
17 this.list = e.childNodes[0];
21 k.ActionBarCombobox.prototype = {
24 var toggler = function(event){
25 $('div.' + CLASSES.combobox + '[id!=' + obj.combobox.id + ']').removeClass(CLASSES.active);
26 $(obj.combobox).toggleClass(CLASSES.active);
27 event.stopPropagation();
30 $(obj.toggle).on('click', toggler);
32 var data = $(this.topAction).data('action') || {};
33 if (!data.call && !data.submit)
34 $(this.topAction).on('click', toggler);
38 k.ActionBarAccesskeys = {
46 add_accesskey: function (target, keystring, action) {
47 if (target === undefined) {
51 var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) {
54 case 'alt': return val;
55 case 'enter': return 13;
57 if (val.length == 1) {
58 return val.charChodeAt(0);
59 } else if (val % 1 === 0) {
62 console.log('can not normalize access key token: ' + val);
67 if (!(target in this.actions))
68 this.actions[target] = {};
69 this.actions[target][normalized] = action;
72 bind_targets: function(){
73 for (var target in this.actions) {
74 if (target in this.bound_targets) continue;
75 $(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
76 this.bound_targets[target] = 1;
80 handle_accesskey: function(e,t) {
81 var target = e.data.target;
84 if (e.ctrlKey) accesskey += 'crtl+'
85 if (e.altKey) accesskey += 'alt+'
88 // special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
89 // so. if accesskey is '13' and the event source is one of these (currently only textareas & combo boxes) ignore it.
90 // higher level widgets will usually prevent their key events from bubbling if used.
91 if ( (accesskey == 13)
92 && ( (e.target.tagName == 'TEXTAREA')
93 || (e.target.tagName == 'SELECT')))
96 if ((target in k.ActionBarAccesskeys.actions) && (accesskey in k.ActionBarAccesskeys.actions[target])) {
98 k.ActionBarAccesskeys.actions[target][accesskey].click();
100 // and another special case.
101 // if the form contains submit buttons the default action will click them instead.
103 if (accesskey == 13) return false;
109 k.ActionBarAction = function(e) {
110 var data = $(e).data('action');
111 if (undefined === data) return;
113 if (data.disabled && (data.disabled != '0')) {
114 $(e).addClass(CLASSES.disabled);
115 if (data.disabled != '1')
116 data.tooltip = data.disabled;
119 if (data.accesskey) {
121 k.ActionBarAccesskeys.add_accesskey(data.submit[0], data.accesskey, $(e));
124 k.ActionBarAccesskeys.add_accesskey(undefined, data.accesskey, $(e));
129 $(e).tooltipster({ content: data.tooltip, theme: 'tooltipster-light' });
132 if (data.call || data.submit || data.link) {
133 $(e).click(function(event) {
134 var $hidden, key, func, check;
135 if ($(e).hasClass(CLASSES.disabled)) {
136 event.stopPropagation();
140 for (var i=0; i < data.checks.length; i++) {
141 check = data.checks[i];
142 if (check.constructor !== Array)
144 func = kivi.get_function_by_name(check[0]);
146 console.log('Cannot find check function: ' + check);
147 if (!func.apply(document, check.slice(1)))
151 if (data.confirm && !confirm(data.confirm)) return;
153 func = kivi.get_function_by_name(data.call[0]);
154 func.apply(document, data.call.slice(1));
157 var form = data.submit[0];
158 var params = data.submit[1];
159 for (key in params) {
160 $('[name=' + key + ']').remove();
161 $hidden = $('<input type=hidden>');
162 $hidden.attr('name', key);
163 $hidden.attr('value', params[key]);
164 $(form).append($hidden);
169 window.location.href = data.link;
171 if ((data.only_once !== undefined) && (data.only_once !== 0)) {
172 $(e).addClass(CLASSES.disabled);
173 $(e).tooltipster({ content: kivi.t8("The action can only be executed once."), theme: 'tooltipster-light' });
181 $('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
182 kivi.ActionBarAction(e)
184 $('div.layout-actionbar-combobox').each(function(_, e) {
185 $(e).data('combobox', new kivi.ActionBarCombobox(e));
187 $(document).click(function() {
188 $('div.layout-actionbar-combobox').removeClass('active');
190 kivi.ActionBarAccesskeys.bind_targets();