1 namespace('kivi.ActionBar', function(k){
6 actionbar: 'layout-actionbar',
7 disabled: 'layout-actionbar-action-disabled',
8 action: 'layout-actionbar-action',
9 combobox: 'layout-actionbar-combobox',
10 default: 'layout-actionbar-default-action',
13 k.Combobox = function(e) {
15 this.head = e.childNodes[0];
16 this.topAction = this.head.childNodes[0];
17 this.toggle = this.head.childNodes[1];
18 this.list = e.childNodes[0];
22 k.Combobox.prototype = {
25 var toggler = function(event){
26 $('div.' + CLASSES.combobox + '[id!=' + obj.combobox.id + ']').removeClass(CLASSES.active);
27 $(obj.combobox).toggleClass(CLASSES.active);
28 event.stopPropagation();
31 $(obj.toggle).on('click', toggler);
33 var data = $(this.topAction).data('action') || {};
34 if (!data.call && !data.submit)
35 $(this.topAction).on('click', toggler);
47 add_accesskey: function (target, keystring, action) {
48 if (target === undefined) {
52 var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) {
55 case 'alt': return val;
56 case 'enter': return 13;
58 if (val.length == 1) {
59 return val.charCodeAt(0);
60 } else if (val % 1 === 0) {
63 console.log('can not normalize access key token: ' + val);
68 if (!(target in this.actions))
69 this.actions[target] = {};
70 this.actions[target][normalized] = action;
73 bind_targets: function(){
74 for (var target in this.actions) {
75 if (target in this.bound_targets) continue;
76 $(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
77 this.bound_targets[target] = 1;
81 handle_accesskey: function(e,t) {
82 var target = e.data.target;
85 if (e.ctrlKey) accesskey += 'crtl+'
86 if (e.altKey) accesskey += 'alt+'
89 // special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
90 // so. if accesskey is '13' and the event source is one of these (currently only textareas & combo boxes) ignore it.
91 // higher level widgets will usually prevent their key events from bubbling if used.
92 if ( (accesskey == 13)
93 && ( (e.target.tagName == 'TEXTAREA')
94 || (e.target.tagName == 'SELECT')))
97 if ((target in kivi.ActionBar.Accesskeys.actions) && (accesskey in kivi.ActionBar.Accesskeys.actions[target])) {
99 kivi.ActionBar.Accesskeys.actions[target][accesskey].click();
101 // and another special case.
102 // if the form contains submit buttons the default action will click them instead.
104 if (accesskey == 13) return false;
110 k.removeTooltip = function(e) {
112 if ($e.hasClass('tooltipstered'))
113 $e.tooltipster('destroy');
114 $e.prop('title', '');
117 k.setTooltip = function(e, tooltip) {
119 if ($e.hasClass('tooltipstered'))
120 $e.tooltipster('content', tooltip);
122 $e.tooltipster({ content: tooltip, theme: 'tooltipster-light' });
125 k.setDisabled = function(e, tooltip) {
128 $e.addClass(CLASSES.disabled);
130 if (tooltip && (tooltip != '1'))
131 kivi.ActionBar.setTooltip($e, tooltip);
133 kivi.ActionBar.removeTooltip($e);
136 k.setEnabled = function(e) {
138 var data = $e.data('action');
140 $e.removeClass(CLASSES.disabled);
143 kivi.ActionBar.setTooltip($e, data.tooltip);
145 kivi.ActionBar.removeTooltip($e);
148 k.Action = function(e) {
150 var instance = $e.data('instance');
154 var data = $e.data('action');
155 if (undefined === data) return;
157 data.originalTooltip = data.tooltip;
159 if (data.disabled && (data.disabled != '0'))
160 kivi.ActionBar.setDisabled($e, data.disabled);
162 else if (data.tooltip)
163 kivi.ActionBar.setTooltip($e, data.tooltip);
165 if (data.accesskey) {
167 kivi.ActionBar.Accesskeys.add_accesskey(data.submit[0], data.accesskey, $e);
170 kivi.ActionBar.Accesskeys.add_accesskey('body', data.accesskey, $e);
172 if (data.accesskey == 'enter') {
173 $e.addClass(CLASSES.default);
177 if (data.call || data.submit || data.link) {
178 $e.click(function(event) {
179 var $hidden, key, func, check;
180 if ($e.hasClass(CLASSES.disabled)) {
181 event.stopPropagation();
185 for (var i=0; i < data.checks.length; i++) {
186 check = data.checks[i];
187 if (check.constructor !== Array)
189 func = kivi.get_function_by_name(check[0]);
191 console.log('Cannot find check function: ' + check);
192 if (!func.apply(document, check.slice(1)))
196 if (data.confirm && !confirm(data.confirm)) return;
198 func = kivi.get_function_by_name(data.call[0]);
199 func.apply(document, data.call.slice(1));
202 var form = data.submit[0];
203 var params = data.submit[1];
204 for (key in params) {
205 $('[name=' + key + ']').remove();
206 $hidden = $('<input type=hidden>');
207 $hidden.attr('name', key);
208 $hidden.attr('value', params[key]);
209 $(form).append($hidden);
214 window.location.href = data.link;
216 if ((data.only_once !== undefined) && (data.only_once !== 0)) {
217 $e.addClass(CLASSES.disabled);
218 $e.tooltipster({ content: kivi.t8("The action can only be executed once."), theme: 'tooltipster-light' });
224 removeTooltip: function() { kivi.ActionBar.removeTooltip($e); },
225 setTooltip: function(tooltip) { kivi.ActionBar.setTooltip($e, tooltip); },
226 disable: function(tooltip) { kivi.ActionBar.setDisabled($e, tooltip); },
227 enable: function() { kivi.ActionBar.setEnabled($e, $e.data('action').tooltip); },
230 $e.data('instance', instance);
237 $('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
238 kivi.ActionBar.Action(e);
240 $('div.layout-actionbar-combobox').each(function(_, e) {
241 $(e).data('combobox', new kivi.ActionBar.Combobox(e));
243 $(document).click(function() {
244 $('div.layout-actionbar-combobox').removeClass('active');
246 kivi.ActionBar.Accesskeys.bind_targets();