From 5aec18fed79986d088e3a7ed05a9166c48d05b39 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 13 Feb 2017 16:28:28 +0100 Subject: [PATCH] PartPicker von autocomplete_part.js nach kivi.Part.js verschoben --- SL/Controller/PriceRule.pm | 2 +- SL/Controller/RequirementSpec.pm | 2 +- SL/Presenter/Chart.pm | 2 +- SL/Presenter/Part.pm | 4 +- bin/mozilla/do.pl | 2 +- bin/mozilla/ir.pl | 2 +- bin/mozilla/is.pl | 2 +- bin/mozilla/oe.pl | 2 +- js/autocomplete_part.js | 299 ------------------ js/kivi.CustomerVendor.js | 2 +- js/kivi.Part.js | 295 ++++++++++++++++- js/kivi.js | 4 +- .../webpages/part/_part_picker_result.html | 2 +- .../webpages/part/part_picker_search.html | 2 +- 14 files changed, 308 insertions(+), 314 deletions(-) delete mode 100644 js/autocomplete_part.js diff --git a/SL/Controller/PriceRule.pm b/SL/Controller/PriceRule.pm index d9c397847..2ddccf15b 100644 --- a/SL/Controller/PriceRule.pm +++ b/SL/Controller/PriceRule.pm @@ -236,7 +236,7 @@ sub all_price_rule_item_types { } sub add_javascripts { - $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_customer.js autocomplete_vendor.js autocomplete_part.js)); + $::request->{layout}->add_javascripts(qw(kivi.PriceRule.js autocomplete_customer.js autocomplete_vendor.js kivi.Part.js)); } sub init_price_rule { diff --git a/SL/Controller/RequirementSpec.pm b/SL/Controller/RequirementSpec.pm index 68969e61b..6d9c57b95 100644 --- a/SL/Controller/RequirementSpec.pm +++ b/SL/Controller/RequirementSpec.pm @@ -322,7 +322,7 @@ sub setup { $::auth->assert('requirement_spec_edit'); $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec); - $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery autocomplete_part autocomplete_customer); + $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery kivi.Part.js autocomplete_customer); $self->init_visible_section; return 1; diff --git a/SL/Presenter/Chart.pm b/SL/Presenter/Chart.pm index 025fa8021..58c6b858c 100644 --- a/SL/Presenter/Chart.pm +++ b/SL/Presenter/Chart.pm @@ -159,7 +159,7 @@ C will register its javascript for inclusion in the next header rendering. If you write a standard controller that only calls C once, it will just work. In case the header is generated in a different render call (multiple blocks, ajax, old C style controllers) you need to -include C yourself. +include C yourself. =back diff --git a/SL/Presenter/Part.pm b/SL/Presenter/Part.pm index 4bd45ca41..f84d706d5 100644 --- a/SL/Presenter/Part.pm +++ b/SL/Presenter/Part.pm @@ -42,7 +42,7 @@ sub part_picker { join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(part_type classification_id unit convertible_unit)) . $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params); - $::request->layout->add_javascripts('autocomplete_part.js'); + $::request->layout->add_javascripts('kivi.Part.js'); $::request->presenter->need_reinit_widgets($id); $self->html_tag('span', $ret, class => 'part_picker'); @@ -224,7 +224,7 @@ C will register it's javascript for inclusion in the next header rendering. If you write a standard controller that only call C once, it will just work. In case the header is generated in a different render call (multiple blocks, ajax, old C style controllers) you need to -include C yourself. +include C yourself. =back diff --git a/bin/mozilla/do.pl b/bin/mozilla/do.pl index 6c1222195..437e9e2f6 100644 --- a/bin/mozilla/do.pl +++ b/bin/mozilla/do.pl @@ -302,7 +302,7 @@ sub form_header { $form->{follow_up_trans_info} = $form->{donumber} .'('. $form->{VC_OBJ}->name .')'; - $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.File kivi.MassDeliveryOrderPrint kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part)); + $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.File kivi.MassDeliveryOrderPrint kivi.SalesPurchase kivi.Part ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer)); my @custom_hidden; push @custom_hidden, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) }; diff --git a/bin/mozilla/ir.pl b/bin/mozilla/ir.pl index ad1c20253..2e8c567d8 100644 --- a/bin/mozilla/ir.pl +++ b/bin/mozilla/ir.pl @@ -296,7 +296,7 @@ sub form_header { $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice(); $form->{duedate} = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $TMPL_VAR{payment_terms_obj}; - $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part autocomplete_project client_js)); + $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase kivi.Part ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_project client_js)); $form->header(); diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl index f7d296c42..6d172088d 100644 --- a/bin/mozilla/is.pl +++ b/bin/mozilla/is.pl @@ -354,7 +354,7 @@ sub form_header { ), @custom_hiddens, map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}]; - $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js)); + $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase kivi.Part ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer client_js)); $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice(); $form->{duedate} = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{duedate})->to_kivitendo if $TMPL_VAR{payment_terms_obj}; diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl index 85ec57dea..063059a36 100644 --- a/bin/mozilla/oe.pl +++ b/bin/mozilla/oe.pl @@ -435,7 +435,7 @@ sub form_header { } } - $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase kivi.File show_form_details show_history show_vc_details ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part)); + $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase kivi.File kivi.Part show_form_details show_history show_vc_details ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer)); $form->header; if ($form->{CFDD_shipto} && $form->{CFDD_shipto_id} ) { diff --git a/js/autocomplete_part.js b/js/autocomplete_part.js deleted file mode 100644 index 4ebc5a583..000000000 --- a/js/autocomplete_part.js +++ /dev/null @@ -1,299 +0,0 @@ -namespace('kivi', function(k){ - "use strict"; - - 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, - LEFT: 37, - RIGHT: 39, - PAGE_UP: 33, - PAGE_DOWN: 34, - SHIFT: 16, - CTRL: 17, - ALT: 18, - }; - 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: $real.hasClass(CLASSES.FAT_SET_ITEM), - }, options); - var STATES = { - PICKED: CLASSES.PICKED, - UNDEFINED: CLASSES.UNDEFINED - } - var real_id = $real.attr('id'); - 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(); - var timer; - - function open_dialog () { - k.popup_dialog({ - url: 'controller.pl?action=Part/part_picker_search', - data: $.extend({ - real_id: real_id, - }, ajax_data($dummy.val())), - id: 'part_selection', - dialog: { - title: k.t8('Part picker'), - width: 800, - height: 800, - } - }); - window.clearTimeout(timer); - return true; - } - - function ajax_data(term) { - var data = { - '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, - current: $real.val(), - }; - - 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(','); - - return data; - } - - function set_item (item) { - if (item.id) { - $real.val(item.id); - // 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(''); - } - state = STATES.PICKED; - last_real = $real.val(); - last_dummy = $dummy.val(); - $real.trigger('change'); - - if (o.fat_set_item && item.id) { - $.ajax({ - url: 'controller.pl?action=Part/show.json', - data: { 'part.id': item.id }, - success: function(rsp) { - $real.trigger('set_item:PartPicker', rsp); - }, - }); - } else { - $real.trigger('set_item:PartPicker', item); - } - annotate_state(); - } - - function make_defined_state () { - if (state == STATES.PICKED) { - annotate_state(); - return true - } else if (state == STATES.UNDEFINED && $dummy.val() === '') - set_item({}) - else { - 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 { - $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED); - } - } - - function update_results () { - $.ajax({ - url: 'controller.pl?action=Part/part_picker_result', - 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) { - if (!$('no_paginate').prop('checked')) { - if (event.keyCode == KEY.PAGE_UP) { - $('#part_picker_result a.paginate-prev').click(); - return; - } - if (event.keyCode == KEY.PAGE_DOWN) { - $('#part_picker_result a.paginate-next').click(); - return; - } - } - window.clearTimeout(timer); - timer = window.setTimeout(update_results, 100); - } - - 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) { - $.ajax($.extend(o, { - url: 'controller.pl?action=Part/ajax_autocomplete', - dataType: "json", - data: ajax_data(req.term), - success: function (data){ rsp(data) } - })); - }, - 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. - * If it is, go ahead and assume it was selected. If it wasn't don't do - * anything so that autocompletion kicks in. For don't prevent - * propagation. It would be nice to catch it, but javascript is too stupid - * to fire a tab event later on, so we'd have to reimplement the "find - * next active element in tabindex order and focus it". - */ - /* 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 - */ - $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() === '') { - set_item({}); - return true; - } else if (state == STATES.PICKED) { - return true; - } - 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 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 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 }, - 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, - reset: make_defined_state, - is_defined_state: function() { return state == STATES.PICKED }, - init_results: function () { - $('div.part_picker_part').each(function(){ - $(this).click(function(){ - 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(); - return true; - }); - }); - $('#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.PartPicker($(real)); - }) -}); diff --git a/js/kivi.CustomerVendor.js b/js/kivi.CustomerVendor.js index d6820672d..9efb990f1 100644 --- a/js/kivi.CustomerVendor.js +++ b/js/kivi.CustomerVendor.js @@ -48,7 +48,7 @@ namespace('kivi.CustomerVendor', function(ns) { kivi.CustomerVendorPicker($ctrl).set_item({ id: cvar.id, name: cvar.value }); else if (cvar.type == 'part') - kivi.PartPicker($ctrl).set_item({ id: cvar.id, name: cvar.value }); + kivi.Part.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value }); else $ctrl.val(cvar.value); diff --git a/js/kivi.Part.js b/js/kivi.Part.js index eefd1deef..06f9591fd 100644 --- a/js/kivi.Part.js +++ b/js/kivi.Part.js @@ -263,6 +263,296 @@ namespace('kivi.Part', function(ns) { $.post("controller.pl", { action: 'Part/warehouse_changed', warehouse_id: function(){ return $('#part_warehouse_id').val() } }, kivi.eval_json_result); } + ns.Picker = 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, + LEFT: 37, + RIGHT: 39, + PAGE_UP: 33, + PAGE_DOWN: 34, + SHIFT: 16, + CTRL: 17, + ALT: 18, + }; + 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: $real.hasClass(CLASSES.FAT_SET_ITEM), + }, options); + var STATES = { + PICKED: CLASSES.PICKED, + UNDEFINED: CLASSES.UNDEFINED + } + var real_id = $real.attr('id'); + 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(); + var timer; + + function open_dialog () { + kivi.popup_dialog({ + url: 'controller.pl?action=Part/part_picker_search', + data: $.extend({ + real_id: real_id, + }, ajax_data($dummy.val())), + id: 'part_selection', + dialog: { + title: kivi.t8('Part picker'), + width: 800, + height: 800, + } + }); + window.clearTimeout(timer); + return true; + } + + function ajax_data(term) { + var data = { + '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, + current: $real.val(), + }; + + 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(','); + + return data; + } + + function set_item (item) { + if (item.id) { + $real.val(item.id); + // 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(''); + } + state = STATES.PICKED; + last_real = $real.val(); + last_dummy = $dummy.val(); + $real.trigger('change'); + + if (o.fat_set_item && item.id) { + $.ajax({ + url: 'controller.pl?action=Part/show.json', + data: { id: item.id }, + success: function(rsp) { + $real.trigger('set_item:PartPicker', rsp); + }, + }); + } else { + $real.trigger('set_item:PartPicker', item); + } + annotate_state(); + } + + function make_defined_state () { + if (state == STATES.PICKED) { + annotate_state(); + return true + } else if (state == STATES.UNDEFINED && $dummy.val() === '') + set_item({}) + else { + 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 { + $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED); + } + } + + function update_results () { + $.ajax({ + url: 'controller.pl?action=Part/part_picker_result', + 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) { + if (!$('no_paginate').prop('checked')) { + if (event.keyCode == KEY.PAGE_UP) { + $('#part_picker_result a.paginate-prev').click(); + return; + } + if (event.keyCode == KEY.PAGE_DOWN) { + $('#part_picker_result a.paginate-next').click(); + return; + } + } + window.clearTimeout(timer); + timer = window.setTimeout(update_results, 100); + } + + 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) { + $.ajax($.extend(o, { + url: 'controller.pl?action=Part/ajax_autocomplete', + dataType: "json", + data: ajax_data(req.term), + success: function (data){ rsp(data) } + })); + }, + 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. + * If it is, go ahead and assume it was selected. If it wasn't don't do + * anything so that autocompletion kicks in. For don't prevent + * propagation. It would be nice to catch it, but javascript is too stupid + * to fire a tab event later on, so we'd have to reimplement the "find + * next active element in tabindex order and focus it". + */ + /* 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 + */ + $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() === '') { + set_item({}); + return true; + } else if (state == STATES.PICKED) { + return true; + } + 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 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 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 }, + 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, + reset: make_defined_state, + is_defined_state: function() { return state == STATES.PICKED }, + init_results: function () { + $('div.part_picker_part').each(function(){ + $(this).click(function(){ + 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(); + return true; + }); + }); + $('#part_selection').keydown(function(e){ + if (e.which == KEY.ESCAPE) { + close_popup(); + $dummy.focus(); + } + }); + } + } + $real.data('part_picker', pp); + return pp; + }; + $(function(){ // assortment @@ -305,5 +595,8 @@ namespace('kivi.Part', function(ns) { $('#part_warehouse_id').change(kivi.Part.reload_bin_selection); + $('input.part_autocomplete').each(function(i,real){ + kivi.Part.Picker($(real)); + }); }); -}) +}); diff --git a/js/kivi.js b/js/kivi.js index c6b707473..0372f40d3 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -243,9 +243,9 @@ namespace("kivi", function(ns) { $(elt).datepicker(); }); - if (ns.PartPicker) + if (ns.Part) ns.run_once_for('input.part_autocomplete', 'part_picker', function(elt) { - kivi.PartPicker($(elt)); + kivi.Part.Picker($(elt)); }); if (ns.ProjectPicker) diff --git a/templates/webpages/part/_part_picker_result.html b/templates/webpages/part/_part_picker_result.html index c150a0822..29a444bbc 100644 --- a/templates/webpages/part/_part_picker_result.html +++ b/templates/webpages/part/_part_picker_result.html @@ -30,5 +30,5 @@ [% L.paginate_controls(target='#part_picker_result', selector='#part_picker_result', models=SELF.models) %] diff --git a/templates/webpages/part/part_picker_search.html b/templates/webpages/part/part_picker_search.html index 5669d8eef..25ac65748 100644 --- a/templates/webpages/part/part_picker_search.html +++ b/templates/webpages/part/part_picker_search.html @@ -17,7 +17,7 @@