X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=js%2Fautocomplete_part.js;h=cae7f01af179694a732cd39eb873b38ee5619f2a;hb=6fc16dd1a7da25e9b728bdeb454a5e844850f809;hp=de8f6e535681b0ca2a65e1c20a946e8c9f0b853e;hpb=2313b5225dff4d76ca58dae83829cd04ad84c3b3;p=kivitendo-erp.git diff --git a/js/autocomplete_part.js b/js/autocomplete_part.js index de8f6e535..cae7f01af 100644 --- a/js/autocomplete_part.js +++ b/js/autocomplete_part.js @@ -1,4 +1,6 @@ namespace('kivi', function(k){ + "use strict"; + k.PartPicker = function($real, options) { // short circuit in case someone double inits us if ($real.data("part_picker")) @@ -12,6 +14,9 @@ namespace('kivi', function(k){ RIGHT: 39, PAGE_UP: 33, PAGE_DOWN: 34, + SHIFT: 16, + CTRL: 17, + ALT: 18, }; var CLASSES = { PICKED: 'partpicker-picked', @@ -28,11 +33,11 @@ namespace('kivi', function(k){ UNDEFINED: CLASSES.UNDEFINED } 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 $dummy = $('#' + real_id + '_name'); + var $part_type = $('#' + real_id + '_part_type'); + var $classification_id = $('#' + real_id + '_classification_id'); + var $unit = $('#' + real_id + '_unit'); + var $convertible_unit = $('#' + real_id + '_convertible_unit'); var state = STATES.PICKED; var last_real = $real.val(); var last_dummy = $dummy.val(); @@ -61,12 +66,14 @@ namespace('kivi', function(k){ 'filter.obsolete': 0, 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '', no_paginate: $('#no_paginate').prop('checked') ? 1 : 0, - column: $column && $column.val() ? $column.val() : '', current: $real.val(), }; - if ($type && $type.val()) - data['filter.type'] = $type.val().split(','); + if ($part_type && $part_type.val()) + data['filter.part_type'] = $part_type.val().split(','); + + if ($classification_id && $classification_id.val()) + data['filter.classification_id'] = $classification_id.val().split(','); if ($unit && $unit.val()) data['filter.unit'] = $unit.val().split(','); @@ -77,8 +84,8 @@ namespace('kivi', function(k){ function set_item (item) { if (item.id) { $real.val(item.id); - // autocomplete ui has name, ajax items have description - $dummy.val(item.name ? item.name : item.description); + // autocomplete ui has name, use the value for ajax items, which contains displayable_name + $dummy.val(item.name ? item.name : item.value); } else { $real.val(''); $dummy.val(''); @@ -86,7 +93,6 @@ namespace('kivi', function(k){ state = STATES.PICKED; last_real = $real.val(); last_dummy = $dummy.val(); - last_unverified_dummy = $dummy.val(); $real.trigger('change'); if (o.fat_set_item && item.id) { @@ -107,10 +113,9 @@ namespace('kivi', function(k){ if (state == STATES.PICKED) { annotate_state(); return true - } else if (state == STATES.UNDEFINED && $dummy.val() == '') + } else if (state == STATES.UNDEFINED && $dummy.val() === '') set_item({}) else { - last_unverified_dummy = $dummy.val(); set_item({ id: last_real, name: last_dummy }) } annotate_state(); @@ -119,10 +124,9 @@ namespace('kivi', function(k){ function annotate_state () { if (state == STATES.PICKED) $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED); - else if (state == STATES.UNDEFINED && $dummy.val() == '') + else if (state == STATES.UNDEFINED && $dummy.val() === '') $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED); else { - last_unverified_dummy = $dummy.val(); $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED); } } @@ -135,7 +139,7 @@ namespace('kivi', function(k){ }, 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) { if (!$('no_paginate').prop('checked')) { @@ -154,7 +158,28 @@ namespace('kivi', function(k){ function close_popup() { $('#part_selection').dialog('close'); - }; + } + + function handle_changed_text(callbacks) { + $.ajax({ + url: 'controller.pl?action=Part/ajax_autocomplete', + dataType: "json", + data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ), + success: function (data) { + if (data.length == 1) { + set_item(data[0]); + if (callbacks && callbacks.match_one) callbacks.match_one(data[0]); + } else if (data.length > 1) { + state = STATES.UNDEFINED; + if (callbacks && callbacks.match_many) callbacks.match_many(data); + } else { + state = STATES.UNDEFINED; + if (callbacks &&callbacks.match_none) callbacks.match_none(); + } + annotate_state(); + } + }); + } $dummy.autocomplete({ source: function(req, rsp) { @@ -168,6 +193,10 @@ namespace('kivi', function(k){ select: function(event, ui) { set_item(ui.item); }, + search: function(event, ui) { + if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT)) + event.preventDefault(); + } }); /* In case users are impatient and want to skip ahead: * Capture key events and check if it's a unique hit. @@ -184,56 +213,51 @@ namespace('kivi', function(k){ $dummy.keydown(function(event){ if (event.which == KEY.ENTER || event.which == KEY.TAB) { // if string is empty assume they want to delete - if ($dummy.val() == '') { + if ($dummy.val() === '') { set_item({}); return true; } else if (state == STATES.PICKED) { return true; } - if (event.which == KEY.TAB) event.preventDefault(); - $.ajax({ - url: 'controller.pl?action=Part/ajax_autocomplete', - dataType: "json", - data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ), - success: function (data) { - if (data.length == 1) { - set_item(data[0]); - if (event.which == KEY.ENTER) - $('#update_button').click(); - } else if (data.length > 1) { - if (event.which == KEY.ENTER) - open_dialog(); - } else { - } - annotate_state(); - } - }); - if (event.which == KEY.ENTER) + if (event.which == KEY.TAB) { + event.preventDefault(); + handle_changed_text(); + } + if (event.which == KEY.ENTER) { + handle_changed_text({ + match_one: function(){$('#update_button').click();}, + match_many: function(){open_dialog();} + }); return false; - } else { + } + } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) { state = STATES.UNDEFINED; } }); + $dummy.on('paste', function(){ + setTimeout(function() { + handle_changed_text(); + }, 1); + }); + $dummy.blur(function(){ window.clearTimeout(timer); timer = window.setTimeout(annotate_state, 100); }); // now add a picker div after the original input - var pcont = $('').addClass('position-absolute'); - var picker = $('
'); - $dummy.after(pcont); - pcont.append(picker); - picker.addClass('icon16 crm--search').click(open_dialog); + var popup_button = $('').addClass('ppp_popup_button'); + $dummy.after(popup_button); + popup_button.click(open_dialog); 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 }, + real: function() { return $real }, + dummy: function() { return $dummy }, + part_type: function() { return $part_type }, + classification_id: function() { return $classification_id }, + unit: function() { return $unit }, + convertible_unit: function() { return $convertible_unit }, update_results: update_results, result_timer: result_timer, set_item: set_item, @@ -245,7 +269,10 @@ namespace('kivi', function(k){ set_item({ id: $(this).children('input.part_picker_id').val(), name: $(this).children('input.part_picker_description').val(), + classification_id: $(this).children('input.part_picker_classification_id').val(), unit: $(this).children('input.part_picker_unit').val(), + partnumber: $(this).children('input.part_picker_partnumber').val(), + description: $(this).children('input.part_picker_description').val(), }); close_popup(); $dummy.focus();