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.toggle = this.head.childNodes[1];
16 this.list = e.childNodes[0];
20 k.ActionBarCombobox.prototype = {
23 $(obj.toggle).on('click', function(event){
24 $('div.' + CLASSES.combobox + '[id!=' + obj.combobox.id + ']').removeClass(CLASSES.active);
25 $(obj.combobox).toggleClass(CLASSES.active);
26 event.stopPropagation();
31 k.ActionBarAccesskeys = {
39 add_accesskey: function (target, keystring, action) {
40 if (target === undefined) {
44 var normalized = $.map(String.prototype.split.call(keystring, '+'), function(val, i) {
45 console.log(keystring)
48 case 'alt': return val;
49 case 'enter': return 13;
51 if (val.length == 1) {
52 return val.charChodeAt(0)
53 } else if (val % 1 === 0) {
56 console.log('can not normalize access key token: ' + val);
61 if (!(target in this.actions))
62 this.actions[target] = {};
63 this.actions[target][normalized] = action;
66 bind_targets: function(){
67 for (var target in this.actions) {
68 if (target in this.bound_targets) continue;
69 $(target).on('keypress', null, { 'target': target }, this.handle_accesskey);
70 this.bound_targets[target] = 1;
74 handle_accesskey: function(e,t) {
75 var target = e.data.target;
78 if (e.ctrlKey) accesskey += 'crtl+'
79 if (e.altKey) accesskey += 'alt+'
82 // special case. HTML elements that make legitimate use of enter will also trigger the enter accesskey.
83 // so. if accesskey is '13' and the event source is one of these (currently only textarea) ignore it.
84 // higher level widgets will usually prevent their key events from bubbling if used.
85 if (accesskey == 13 && e.target.tagName == 'TEXTAREA') return true;
87 if ((target in k.ActionBarAccesskeys.actions) && (accesskey in k.ActionBarAccesskeys.actions[target])) {
89 k.ActionBarAccesskeys.actions[target][accesskey].click();
91 // and another special case.
92 // if the form contains submit buttons the default action will click them instead.
94 if (accesskey == 13) return false;
100 k.ActionBarAction = function(e) {
101 var data = $(e).data('action');
102 if (undefined === data) return;
105 $(e).addClass(CLASSES.disabled);
108 if (data.accesskey) {
110 k.ActionBarAccesskeys.add_accesskey(data.submit[0], data.accesskey, $(e));
113 k.ActionBarAccesskeys.add_accesskey(undefined, data.accesskey, $(e));
118 $(e).tooltipster({ content: data.tooltip, theme: 'tooltipster-light' });
121 if (data.call || data.submit) {
122 $(e).click(function(event) {
123 var $hidden, key, func, check;
124 if ($(e).hasClass(CLASSES.disabled)) {
125 event.stopPropagation();
129 for (var i=0; i < data.checks.length; i++) {
130 check = data.checks[i];
131 func = kivi.get_function_by_name(check);
132 if (!func) console.log('Cannot find check function: ' + check);
136 if (data.confirm && !confirm(data.confirm)) return;
138 func = kivi.get_function_by_name(data.call[0]);
139 func.apply(document, data.call.slice(1))
142 var form = data.submit[0];
143 var params = data.submit[1];
144 for (key in params) {
145 $hidden = $('<input type=hidden>')
146 $hidden.attr('name', key)
147 $hidden.attr('value', params[key])
148 $(form).append($hidden)
158 $('div.layout-actionbar .layout-actionbar-action').each(function(_, e) {
159 kivi.ActionBarAction(e)
161 $('div.layout-actionbar-combobox').each(function(_, e) {
162 $(e).data('combobox', new kivi.ActionBarCombobox(e));
164 $(document).click(function() {
165 $('div.layout-actionbar-combobox').removeClass('active');
167 kivi.ActionBarAccesskeys.bind_targets();