kivitendo/js/kivi.ActionBar.js @ 87e1fb0d
e0a3b19e | Sven Schöling | namespace('kivi', function(k){
|
||
'use strict';
|
||||
6ce40ffc | Sven Schöling | var CLASSES = {
|
||
121f8ff1 | Sven Schöling | active: 'active',
|
||
actionbar: 'layout-actionbar',
|
||||
disabled: 'layout-actionbar-action-disabled',
|
||||
action: 'layout-actionbar-action',
|
||||
combobox: 'layout-actionbar-combobox',
|
||||
0d270789 | Moritz Bunkus | };
|
||
6ce40ffc | Sven Schöling | |||
121f8ff1 | Sven Schöling | k.ActionBarCombobox = function(e) {
|
||
c4f90397 | Moritz Bunkus | this.combobox = e;
|
||
this.head = e.childNodes[0];
|
||||
this.topAction = this.head.childNodes[0];
|
||||
this.toggle = this.head.childNodes[1];
|
||||
this.list = e.childNodes[0];
|
||||
121f8ff1 | Sven Schöling | this.init();
|
||
0d270789 | Moritz Bunkus | };
|
||
3879b1fa | Sven Schöling | |||
121f8ff1 | Sven Schöling | k.ActionBarCombobox.prototype = {
|
||
init: function() {
|
||||
c4f90397 | Moritz Bunkus | var obj = this;
|
||
var toggler = function(event){
|
||||
3879b1fa | Sven Schöling | $('div.' + CLASSES.combobox + '[id!=' + obj.combobox.id + ']').removeClass(CLASSES.active);
|
||
121f8ff1 | Sven Schöling | $(obj.combobox).toggleClass(CLASSES.active);
|
||
event.stopPropagation();
|
||||
c4f90397 | Moritz Bunkus | };
|
||
$(obj.toggle).on('click', toggler);
|
||||
var data = $(this.topAction).data('action') || {};
|
||||
if (!data.call && !data.submit)
|
||||
$(this.topAction).on('click', toggler);
|
||||
121f8ff1 | Sven Schöling | }
|
||
72d64df1 | Sven Schöling | };
|
||
k.ActionBarAccesskeys = {
|
||||
known_keys: {
|
||||
'enter': 13,
|
||||
'esc': 27,
|
||||
},
|
||||
actions: {},
|
||||
bound_targets: {},
|
||||
add_accesskey: function (target, keystring, action) {
|
||||
if (target === undefined) {
|
||||
target = 'document';
|
||||
}
|
||||
112eb7bb | Sven Schöling | |||
339f80aa | Sven Schöling | var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) {
|
||
112eb7bb | Sven Schöling | switch (val) {
|
||
case 'ctrl':
|
||||
case 'alt': return val;
|
||||
case 'enter': return 13;
|
||||
default:
|
||||
if (val.length == 1) {
|
||||
87e1fb0d | Moritz Bunkus | return val.charChodeAt(0);
|
||
112eb7bb | Sven Schöling | } else if (val % 1 === 0) {
|
||
339f80aa | Sven Schöling | return val;
|
||
112eb7bb | Sven Schöling | } else {
|
||
console.log('can not normalize access key token: ' + val);
|
||||
}
|
||||
}
|
||||
}).join('+');
|
||||
72d64df1 | Sven Schöling | if (!(target in this.actions))
|
||
this.actions[target] = {};
|
||||
112eb7bb | Sven Schöling | this.actions[target][normalized] = action;
|
||
72d64df1 | Sven Schöling | },
|
||
bind_targets: function(){
|
||||
for (var target in this.actions) {
|
||||
if (target in this.bound_targets) continue;
|
||||
$(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
|
||||
this.bound_targets[target] = 1;
|
||||
}
|
||||
},
|
||||
handle_accesskey: function(e,t) {
|
||||
var target = e.data.target;
|
||||
var key = e.which;
|
||||
var accesskey = '';
|
||||
if (e.ctrlKey) accesskey += 'crtl+'
|
||||
if (e.altKey) accesskey += 'alt+'
|
||||
accesskey += e.which;
|
||||
// special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
|
||||
5ef10c85 | Moritz Bunkus | // so. if accesskey is '13' and the event source is one of these (currently only textareas & combo boxes) ignore it.
|
||
72d64df1 | Sven Schöling | // higher level widgets will usually prevent their key events from bubbling if used.
|
||
5ef10c85 | Moritz Bunkus | if ( (accesskey == 13)
|
||
&& ( (e.target.tagName == 'TEXTAREA')
|
||||
|| (e.target.tagName == 'SELECT')))
|
||||
return true;
|
||||
72d64df1 | Sven Schöling | |||
if ((target in k.ActionBarAccesskeys.actions) && (accesskey in k.ActionBarAccesskeys.actions[target])) {
|
||||
e.stopPropagation();
|
||||
k.ActionBarAccesskeys.actions[target][accesskey].click();
|
||||
// and another special case.
|
||||
// if the form contains submit buttons the default action will click them instead.
|
||||
// prevent that
|
||||
if (accesskey == 13) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
121f8ff1 | Sven Schöling | |||
k.ActionBarAction = function(e) {
|
||||
var data = $(e).data('action');
|
||||
if (undefined === data) return;
|
||||
6ce40ffc | Sven Schöling | |||
121f8ff1 | Sven Schöling | if (data.disabled) {
|
||
$(e).addClass(CLASSES.disabled);
|
||||
8817139d | Moritz Bunkus | if (!data.tooltip && (data.disabled != '1'))
|
||
data.tooltip = data.disabled;
|
||||
121f8ff1 | Sven Schöling | }
|
||
3dc29e42 | Sven Schöling | |||
72d64df1 | Sven Schöling | if (data.accesskey) {
|
||
if (data.submit) {
|
||||
k.ActionBarAccesskeys.add_accesskey(data.submit[0], data.accesskey, $(e));
|
||||
}
|
||||
if (data.call) {
|
||||
k.ActionBarAccesskeys.add_accesskey(undefined, data.accesskey, $(e));
|
||||
}
|
||||
}
|
||||
9c337bc8 | Sven Schöling | if (data.tooltip) {
|
||
$(e).tooltipster({ content: data.tooltip, theme: 'tooltipster-light' });
|
||||
}
|
||||
412f76c9 | Moritz Bunkus | if (data.call || data.submit || data.link) {
|
||
121f8ff1 | Sven Schöling | $(e).click(function(event) {
|
||
var $hidden, key, func, check;
|
||||
if ($(e).hasClass(CLASSES.disabled)) {
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
if (data.checks) {
|
||||
for (var i=0; i < data.checks.length; i++) {
|
||||
check = data.checks[i];
|
||||
func = kivi.get_function_by_name(check);
|
||||
if (!func) console.log('Cannot find check function: ' + check);
|
||||
if (!func()) return;
|
||||
}
|
||||
}
|
||||
if (data.confirm && !confirm(data.confirm)) return;
|
||||
if (data.call) {
|
||||
func = kivi.get_function_by_name(data.call[0]);
|
||||
87e1fb0d | Moritz Bunkus | func.apply(document, data.call.slice(1));
|
||
121f8ff1 | Sven Schöling | }
|
||
if (data.submit) {
|
||||
var form = data.submit[0];
|
||||
var params = data.submit[1];
|
||||
for (key in params) {
|
||||
87e1fb0d | Moritz Bunkus | $hidden = $('<input type=hidden>');
|
||
$hidden.attr('name', key);
|
||||
$hidden.attr('value', params[key]);
|
||||
$(form).append($hidden);
|
||||
121f8ff1 | Sven Schöling | }
|
||
$(form).submit();
|
||||
}
|
||||
412f76c9 | Moritz Bunkus | if (data.link) {
|
||
window.location.href = data.link;
|
||||
}
|
||||
121f8ff1 | Sven Schöling | });
|
||
}
|
||||
0d270789 | Moritz Bunkus | };
|
||
e0a3b19e | Sven Schöling | });
|
||
$(function(){
|
||||
$('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
|
||||
121f8ff1 | Sven Schöling | kivi.ActionBarAction(e)
|
||
});
|
||||
$('div.layout-actionbar-combobox').each(function(_, e) {
|
||||
$(e).data('combobox', new kivi.ActionBarCombobox(e));
|
||||
});
|
||||
72d64df1 | Sven Schöling | $(document).click(function() {
|
||
121f8ff1 | Sven Schöling | $('div.layout-actionbar-combobox').removeClass('active');
|
||
e0a3b19e | Sven Schöling | });
|
||
72d64df1 | Sven Schöling | kivi.ActionBarAccesskeys.bind_targets();
|
||
e0a3b19e | Sven Schöling | });
|