X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/3879b1fa99d914256e86ebfab8bc7118e3de2e06..70173b7798fd1df574d81b525e69ff6c1c6741e7:/js/kivi.ActionBar.js diff --git a/js/kivi.ActionBar.js b/js/kivi.ActionBar.js index 940f2f99e..0963d73da 100644 --- a/js/kivi.ActionBar.js +++ b/js/kivi.ActionBar.js @@ -7,7 +7,7 @@ namespace('kivi', function(k){ disabled: 'layout-actionbar-action-disabled', action: 'layout-actionbar-action', combobox: 'layout-actionbar-combobox', - } + }; k.ActionBarCombobox = function(e) { this.combobox = e; @@ -15,7 +15,7 @@ namespace('kivi', function(k){ this.toggle = this.head.childNodes[1]; this.list = e.childNodes[0]; this.init(); - } + }; k.ActionBarCombobox.prototype = { init: function() { @@ -26,7 +26,78 @@ namespace('kivi', function(k){ event.stopPropagation(); }); } - } + }; + + k.ActionBarAccesskeys = { + known_keys: { + 'enter': 13, + 'esc': 27, + }, + actions: {}, + bound_targets: {}, + + add_accesskey: function (target, keystring, action) { + if (target === undefined) { + target = 'document'; + } + + var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) { + switch (val) { + case 'ctrl': + case 'alt': return val; + case 'enter': return 13; + default: + if (val.length == 1) { + return val.charChodeAt(0) + } else if (val % 1 === 0) { + return val; + } else { + console.log('can not normalize access key token: ' + val); + } + } + }).join('+'); + + if (!(target in this.actions)) + this.actions[target] = {}; + this.actions[target][normalized] = action; + }, + + 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. + // so. if accesskey is '13' and the event source is one of these (currently only textareas & combo boxes) ignore it. + // higher level widgets will usually prevent their key events from bubbling if used. + if ( (accesskey == 13) + && ( (e.target.tagName == 'TEXTAREA') + || (e.target.tagName == 'SELECT'))) + return true; + + 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; + } + }; k.ActionBarAction = function(e) { var data = $(e).data('action'); @@ -34,6 +105,21 @@ namespace('kivi', function(k){ if (data.disabled) { $(e).addClass(CLASSES.disabled); + if (!data.tooltip && (data.disabled != '1')) + data.tooltip = data.disabled; + } + + 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)); + } + } + + if (data.tooltip) { + $(e).tooltipster({ content: data.tooltip, theme: 'tooltipster-light' }); } if (data.call || data.submit) { @@ -69,7 +155,7 @@ namespace('kivi', function(k){ } }); } - } + }; }); $(function(){ @@ -79,7 +165,8 @@ $(function(){ $('div.layout-actionbar-combobox').each(function(_, e) { $(e).data('combobox', new kivi.ActionBarCombobox(e)); }); - $(document).click(function() { + $(document).click(function() { $('div.layout-actionbar-combobox').removeClass('active'); }); + kivi.ActionBarAccesskeys.bind_targets(); });