X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=js%2Fautocomplete_part.js;h=b89751e3b076dd41bd1c25ff3143a9fe74e5311f;hb=54f00ed88d7ed5fcd286ac72b9a9052243153c57;hp=90e4312f0211fef7a4237ca6e7000b6754add9a6;hpb=eff6af2885966e0bbe4b614ef03525012cff92aa;p=kivitendo-erp.git diff --git a/js/autocomplete_part.js b/js/autocomplete_part.js index 90e4312f0..b89751e3b 100644 --- a/js/autocomplete_part.js +++ b/js/autocomplete_part.js @@ -1,6 +1,14 @@ namespace('kivi', function(k){ - k.PartPickerCache = { } k.PartPicker = function($real, options) { + // short circuit in case someone double inits us + if ($real.data("part_picker")) + return $real.data("part_picker"); + + var KEY = { + ESCAPE: 27, + ENTER: 13, + TAB: 9, + }; var o = $.extend({ limit: 20, delay: 50, @@ -12,33 +20,42 @@ namespace('kivi', function(k){ var real_id = $real.attr('id'); var $dummy = $('#' + real_id + '_name'); var $type = $('#' + real_id + '_type'); + var $unit = $('#' + real_id + '_unit'); + var $convertible_unit = $('#' + real_id + '_convertible_unit'); var $column = $('#' + real_id + '_column'); var state = STATES.PICKED; var last_real = $real.val(); var last_dummy = $dummy.val(); - var open_dialog = function(){ + var timer; + + function open_dialog () { open_jqm_window({ - url: 'controller.pl', - data: { - action: 'Part/part_picker_search', + url: 'controller.pl?action=Part/part_picker_search', + data: $.extend({ real_id: real_id, - 'filter.all:substr::ilike': function(){ return $dummy.val() }, - 'filter.type': function(){ return $type.val() }, - 'column': function(){ return $column.val() }, - }, + }, ajax_data($dummy.val())), id: 'part_selection', }); + window.clearTimeout(timer); return true; - }; + } function ajax_data(term) { - return { - term: term, - type: function() { return $type.val() }, - column: function() { return $column.val()===undefined ? '' : $column.val() }, - current: function() { return $real.val() }, - obsolete: 0, - } + var data = { + 'filter.all:substr::ilike': term, + 'filter.obsolete': 0, + 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '', + column: $column && $column.val() ? $column.val() : '', + current: $real.val(), + }; + + if ($type && $type.val()) + data['filter.type'] = $type.val().split(','); + + if ($unit && $unit.val()) + data['filter.unit'] = $unit.val().split(','); + + return data; } function set_item (item) { @@ -68,16 +85,18 @@ namespace('kivi', function(k){ function update_results () { $.ajax({ url: 'controller.pl?action=Part/part_picker_result', - data: { - 'filter.all:substr::ilike': function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val }, - 'filter.type': $type.val(), - 'column': $column.val(), - 'real_id': $real.val, - }, + data: $.extend({ + 'real_id': $real.val(), + }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })), success: function(data){ $('#part_picker_result').html(data) } }); }; + function result_timer (event) { + window.clearTimeout(timer); + timer = window.setTimeout(update_results, 100); + } + function close_popup() { $('#part_selection').jqmClose() }; @@ -103,8 +122,14 @@ namespace('kivi', function(k){ * to fire a tab event later on, so we'd have to reimplement the "find * next active element in tabindex order and focus it". */ - $dummy.keypress(function(event){ - if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab + /* note: + * event.which does not contain tab events in keypressed in firefox but will report 0 + * chrome does not fire keypressed at all on tab or escape + * TODO: users expect tab to work on keydown but enter to trigger on keyup, + * should be handled seperately + */ + $dummy.keydown(function(event){ + if (event.which == KEY.ENTER || event.which == KEY.TAB) { // enter or tab or tab // if string is empty assume they want to delete if ($dummy.val() == '') { set_item({}); @@ -119,27 +144,30 @@ namespace('kivi', function(k){ success: function (data){ if (data.length == 1) { set_item(data[0]); - if (event.keyCode == 13) + if (event.which == KEY.ENTER) $('#update_button').click(); } else if (data.length > 1) { - if (event.keyCode == 13) + if (event.which == KEY.ENTER) open_dialog(); else make_defined_state(); } else { - if (event.keyCode == 9) + if (event.which == KEY.TAB) make_defined_state(); } } }); - if (event.keyCode == 13) + if (event.which == KEY.ENTER) return false; } else { state = STATES.UNDEFINED; } }); -// $dummy.blur(make_defined_state); // blur triggers also on open_jqm_dialog + $dummy.blur(function(){ + window.clearTimeout(timer); + timer = window.setTimeout(make_defined_state, 100); + }); // now add a picker div after the original input var pcont = $('').addClass('position-absolute'); @@ -148,12 +176,15 @@ namespace('kivi', function(k){ pcont.append(picker); picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog); - return { + var pp = { real: function() { return $real }, dummy: function() { return $dummy }, type: function() { return $type }, + unit: function() { return $unit }, + convertible_unit: function() { return $convertible_unit }, column: function() { return $column }, update_results: update_results, + result_timer: result_timer, set_item: set_item, reset: make_defined_state, init_results: function () { @@ -167,19 +198,21 @@ namespace('kivi', function(k){ return true; }); }); - $('#part_selection').keypress(function(e){ - if (e.keyCode == 27) { // escape + $('#part_selection').keydown(function(e){ + if (e.which == KEY.ESCAPE) { close_popup(); $dummy.focus(); } }); } } + $real.data('part_picker', pp); + return pp; } }); $(function(){ $('input.part_autocomplete').each(function(i,real){ - kivi.PartPickerCache[real.id] = new kivi.PartPicker($(real)); + kivi.PartPicker($(real)); }) });