X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=js%2Fautocomplete_part.js;h=8c304bba3e6719476c70b0a95ea00e43301582d0;hb=c13a2652c90aa3b2282a8ee4dd24df88838ab43e;hp=62c1bf4bfd726a52f0258a54f21a7514fce03501;hpb=46e1177c8b8a14cfef2765e1f7b784116bddfc17;p=kivitendo-erp.git diff --git a/js/autocomplete_part.js b/js/autocomplete_part.js index 62c1bf4bf..8c304bba3 100644 --- a/js/autocomplete_part.js +++ b/js/autocomplete_part.js @@ -13,22 +13,25 @@ namespace('kivi', function(k){ PAGE_UP: 33, PAGE_DOWN: 34, }; + var CLASSES = { + PICKED: 'partpicker-picked', + UNDEFINED: 'partpicker-undefined', + FAT_SET_ITEM: 'partpicker_fat_set_item', + } var o = $.extend({ limit: 20, delay: 50, - fat_set_item: 0, // use this option to get a fat item json with all - // relevant data instead of the guaranteed id + name + fat_set_item: $real.hasClass(CLASSES.FAT_SET_ITEM), }, options); var STATES = { - UNIQUE: 1, - UNDEFINED: 0, + PICKED: CLASSES.PICKED, + 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 state = STATES.PICKED; var last_real = $real.val(); var last_dummy = $dummy.val(); @@ -53,11 +56,10 @@ namespace('kivi', function(k){ function ajax_data(term) { var data = { - 'filter.all:substr::ilike': term, + 'filter.all:substr:multi::ilike': term, '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(), }; @@ -73,8 +75,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(''); @@ -82,9 +84,10 @@ 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) { + if (o.fat_set_item && item.id) { $.ajax({ url: 'controller.pl?action=Part/show.json', data: { id: item.id }, @@ -95,15 +98,31 @@ namespace('kivi', function(k){ } else { $real.trigger('set_item:PartPicker', item); } + annotate_state(); } function make_defined_state () { - if (state == STATES.PICKED) + 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 + else { + last_unverified_dummy = $dummy.val(); set_item({ id: last_real, name: last_dummy }) + } + annotate_state(); + } + + function annotate_state () { + if (state == STATES.PICKED) + $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED); + 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); + } } function update_results () { @@ -159,11 +178,9 @@ namespace('kivi', function(k){ /* 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 (event.which == KEY.ENTER || event.which == KEY.TAB) { // if string is empty assume they want to delete if ($dummy.val() == '') { set_item({}); @@ -171,11 +188,12 @@ namespace('kivi', function(k){ } 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){ + success: function (data) { if (data.length == 1) { set_item(data[0]); if (event.which == KEY.ENTER) @@ -183,12 +201,9 @@ namespace('kivi', function(k){ } else if (data.length > 1) { if (event.which == KEY.ENTER) open_dialog(); - else - make_defined_state(); } else { - if (event.which == KEY.TAB) - make_defined_state(); } + annotate_state(); } }); if (event.which == KEY.ENTER) @@ -200,7 +215,7 @@ namespace('kivi', function(k){ $dummy.blur(function(){ window.clearTimeout(timer); - timer = window.setTimeout(make_defined_state, 100); + timer = window.setTimeout(annotate_state, 100); }); // now add a picker div after the original input @@ -208,7 +223,7 @@ namespace('kivi', function(k){ var picker = $('
'); $dummy.after(pcont); pcont.append(picker); - picker.addClass('icon16 crm--search').click(open_dialog); + picker.addClass('icon16 search').click(open_dialog); var pp = { real: function() { return $real }, @@ -216,11 +231,11 @@ namespace('kivi', function(k){ 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, + is_defined_state: function() { return state == STATES.PICKED }, init_results: function () { $('div.part_picker_part').each(function(){ $(this).click(function(){