kivitendo/js/kivi.ActionBar.js @ 5c859d64
f63a4593 | Moritz Bunkus | namespace('kivi.ActionBar', function(k){
|
||
e0a3b19e | Sven Schöling | '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',
|
||||
41ec1a25 | Moritz Bunkus | default: 'layout-actionbar-default-action',
|
||
0d270789 | Moritz Bunkus | };
|
||
6ce40ffc | Sven Schöling | |||
f63a4593 | Moritz Bunkus | k.Combobox = 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 | |||
f63a4593 | Moritz Bunkus | k.Combobox.prototype = {
|
||
121f8ff1 | Sven Schöling | 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 | };
|
||
f63a4593 | Moritz Bunkus | k.Accesskeys = {
|
||
72d64df1 | Sven Schöling | 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 | |||
f63a4593 | Moritz Bunkus | if ((target in kivi.ActionBar.Accesskeys.actions) && (accesskey in kivi.ActionBar.Accesskeys.actions[target])) {
|
||
72d64df1 | Sven Schöling | e.stopPropagation();
|
||
f63a4593 | Moritz Bunkus | kivi.ActionBar.Accesskeys.actions[target][accesskey].click();
|
||
72d64df1 | Sven Schöling | |||
// 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 | |||
3c13f327 | Moritz Bunkus | k.removeTooltip = function($e) {
|
||
if ($e.hasClass('tooltipstered'))
|
||||
$e.tooltipster('destroy');
|
||||
$e.prop('title', '');
|
||||
};
|
||||
k.setTooltip = function($e, tooltip) {
|
||||
if ($e.hasClass('tooltipstered'))
|
||||
$e.tooltipster('content', tooltip);
|
||||
else
|
||||
$e.tooltipster({ content: tooltip, theme: 'tooltipster-light' });
|
||||
};
|
||||
k.setDisabled = function($e, tooltip) {
|
||||
var data = $e.data('action');
|
||||
$e.addClass(CLASSES.disabled);
|
||||
if (tooltip && (tooltip != '1'))
|
||||
kivi.ActionBar.setTooltip($e, tooltip);
|
||||
else
|
||||
kivi.ActionBar.removeTooltip($e);
|
||||
};
|
||||
k.setEnabled = function($e) {
|
||||
var data = $e.data('action');
|
||||
$e.removeClass(CLASSES.disabled);
|
||||
if (data.tooltip)
|
||||
kivi.ActionBar.setTooltip($e, data.tooltip);
|
||||
else
|
||||
kivi.ActionBar.removeTooltip($e);
|
||||
};
|
||||
k.Action = function(e) {
|
||||
var $e = $(e);
|
||||
var instance = $e.data('instance');
|
||||
if (instance)
|
||||
return instance;
|
||||
var data = $e.data('action');
|
||||
121f8ff1 | Sven Schöling | if (undefined === data) return;
|
||
6ce40ffc | Sven Schöling | |||
3c13f327 | Moritz Bunkus | data.originalTooltip = data.tooltip;
|
||
if (data.disabled && (data.disabled != '0'))
|
||||
kivi.ActionBar.setDisabled($e, data.disabled);
|
||||
else if (data.tooltip)
|
||||
kivi.ActionBar.setTooltip($e, data.tooltip);
|
||||
3dc29e42 | Sven Schöling | |||
72d64df1 | Sven Schöling | if (data.accesskey) {
|
||
if (data.submit) {
|
||||
3c13f327 | Moritz Bunkus | kivi.ActionBar.Accesskeys.add_accesskey(data.submit[0], data.accesskey, $e);
|
||
72d64df1 | Sven Schöling | }
|
||
if (data.call) {
|
||||
484b8c43 | Moritz Bunkus | kivi.ActionBar.Accesskeys.add_accesskey('body', data.accesskey, $e);
|
||
72d64df1 | Sven Schöling | }
|
||
41ec1a25 | Moritz Bunkus | if (data.accesskey == 'enter') {
|
||
$e.addClass(CLASSES.default);
|
||||
}
|
||||
72d64df1 | Sven Schöling | }
|
||
412f76c9 | Moritz Bunkus | if (data.call || data.submit || data.link) {
|
||
3c13f327 | Moritz Bunkus | $e.click(function(event) {
|
||
121f8ff1 | Sven Schöling | var $hidden, key, func, check;
|
||
3c13f327 | Moritz Bunkus | if ($e.hasClass(CLASSES.disabled)) {
|
||
121f8ff1 | Sven Schöling | event.stopPropagation();
|
||
return;
|
||||
}
|
||||
if (data.checks) {
|
||||
for (var i=0; i < data.checks.length; i++) {
|
||||
check = data.checks[i];
|
||||
ef26fcde | Moritz Bunkus | if (check.constructor !== Array)
|
||
check = [ check ];
|
||||
func = kivi.get_function_by_name(check[0]);
|
||||
if (!func)
|
||||
console.log('Cannot find check function: ' + check);
|
||||
if (!func.apply(document, check.slice(1)))
|
||||
return;
|
||||
121f8ff1 | Sven Schöling | }
|
||
}
|
||||
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) {
|
||||
c2aaf253 | Moritz Bunkus | $('[name=' + key + ']').remove();
|
||
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;
|
||||
}
|
||||
72613664 | Moritz Bunkus | if ((data.only_once !== undefined) && (data.only_once !== 0)) {
|
||
3c13f327 | Moritz Bunkus | $e.addClass(CLASSES.disabled);
|
||
$e.tooltipster({ content: kivi.t8("The action can only be executed once."), theme: 'tooltipster-light' });
|
||||
72613664 | Moritz Bunkus | }
|
||
121f8ff1 | Sven Schöling | });
|
||
}
|
||||
3c13f327 | Moritz Bunkus | |||
instance = {
|
||||
removeTooltip: function() { kivi.ActionBar.removeTooltip($e); },
|
||||
setTooltip: function(tooltip) { kivi.ActionBar.setTooltip($e, tooltip); },
|
||||
disable: function(tooltip) { kivi.ActionBar.setDisabled($e, tooltip); },
|
||||
enable: function() { kivi.ActionBar.setEnabled($e, $e.data('action').tooltip); },
|
||||
};
|
||||
$e.data('instance', instance);
|
||||
return instance;
|
||||
0d270789 | Moritz Bunkus | };
|
||
e0a3b19e | Sven Schöling | });
|
||
$(function(){
|
||||
$('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
|
||||
3c13f327 | Moritz Bunkus | kivi.ActionBar.Action(e);
|
||
121f8ff1 | Sven Schöling | });
|
||
$('div.layout-actionbar-combobox').each(function(_, e) {
|
||||
f63a4593 | Moritz Bunkus | $(e).data('combobox', new kivi.ActionBar.Combobox(e));
|
||
121f8ff1 | Sven Schöling | });
|
||
72d64df1 | Sven Schöling | $(document).click(function() {
|
||
121f8ff1 | Sven Schöling | $('div.layout-actionbar-combobox').removeClass('active');
|
||
e0a3b19e | Sven Schöling | });
|
||
f63a4593 | Moritz Bunkus | kivi.ActionBar.Accesskeys.bind_targets();
|
||
e0a3b19e | Sven Schöling | });
|