kivitendo/js/autocomplete_part.js @ 57faab8f
854fa965 | Sven Schöling | namespace('kivi', function(k){
|
||
5cec90ad | Sven Schöling | k.PartPicker = function($real, options) {
|
||
670f1111 | Sven Schöling | // short circuit in case someone double inits us
|
||
if ($real.data("part_picker"))
|
||||
return $real.data("part_picker");
|
||||
1e1b6313 | Sven Schöling | var KEY = {
|
||
ESCAPE: 27,
|
||||
ENTER: 13,
|
||||
TAB: 9,
|
||||
46e1177c | Sven Schöling | LEFT: 37,
|
||
RIGHT: 39,
|
||||
PAGE_UP: 33,
|
||||
PAGE_DOWN: 34,
|
||||
1e1b6313 | Sven Schöling | };
|
||
681ec24c | Sven Schöling | var o = $.extend({
|
||
2504ebe1 | Sven Schöling | limit: 20,
|
||
delay: 50,
|
||||
46e1177c | Sven Schöling | fat_set_item: 0, // use this option to get a fat item json with all
|
||
// relevant data instead of the guaranteed id + name
|
||||
854fa965 | Sven Schöling | }, options);
|
||
681ec24c | Sven Schöling | var STATES = {
|
||
UNIQUE: 1,
|
||||
UNDEFINED: 0,
|
||||
}
|
||||
854fa965 | Sven Schöling | var real_id = $real.attr('id');
|
||
var $dummy = $('#' + real_id + '_name');
|
||||
var $type = $('#' + real_id + '_type');
|
||||
880a2e9f | Moritz Bunkus | var $unit = $('#' + real_id + '_unit');
|
||
df33875f | Moritz Bunkus | var $convertible_unit = $('#' + real_id + '_convertible_unit');
|
||
854fa965 | Sven Schöling | var $column = $('#' + real_id + '_column');
|
||
681ec24c | Sven Schöling | var state = STATES.PICKED;
|
||
var last_real = $real.val();
|
||||
var last_dummy = $dummy.val();
|
||||
5729856b | Sven Schöling | var timer;
|
||
function open_dialog () {
|
||||
6dbc83af | Moritz Bunkus | k.popup_dialog({
|
||
7a646681 | Sven Schöling | url: 'controller.pl?action=Part/part_picker_search',
|
||
data: $.extend({
|
||||
854fa965 | Sven Schöling | real_id: real_id,
|
||
7a646681 | Sven Schöling | }, ajax_data($dummy.val())),
|
||
a8617d4c | Sven Schöling | id: 'part_selection',
|
||
46e1177c | Sven Schöling | dialog: {
|
||
title: k.t8('Part picker'),
|
||||
width: 800,
|
||||
height: 800,
|
||||
}
|
||||
a8617d4c | Sven Schöling | });
|
||
5729856b | Sven Schöling | window.clearTimeout(timer);
|
||
a8617d4c | Sven Schöling | return true;
|
||
5729856b | Sven Schöling | }
|
||
854fa965 | Sven Schöling | |||
681ec24c | Sven Schöling | function ajax_data(term) {
|
||
e39a5a77 | Moritz Bunkus | var data = {
|
||
57faab8f | Sven Schöling | 'filter.all:substr:multi::ilike': term,
|
||
7a646681 | Sven Schöling | 'filter.obsolete': 0,
|
||
df33875f | Moritz Bunkus | 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
|
||
46e1177c | Sven Schöling | no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
|
||
df33875f | Moritz Bunkus | column: $column && $column.val() ? $column.val() : '',
|
||
9be50d3a | Sven Schöling | current: $real.val(),
|
||
e39a5a77 | Moritz Bunkus | };
|
||
880a2e9f | Moritz Bunkus | if ($type && $type.val())
|
||
data['filter.type'] = $type.val().split(',');
|
||||
if ($unit && $unit.val())
|
||||
data['filter.unit'] = $unit.val().split(',');
|
||||
e39a5a77 | Moritz Bunkus | |||
return data;
|
||||
854fa965 | Sven Schöling | }
|
||
681ec24c | Sven Schöling | function set_item (item) {
|
||
854fa965 | Sven Schöling | if (item.id) {
|
||
$real.val(item.id);
|
||||
// autocomplete ui has name, ajax items have description
|
||||
$dummy.val(item.name ? item.name : item.description);
|
||||
} else {
|
||||
$real.val('');
|
||||
$dummy.val('');
|
||||
}
|
||||
681ec24c | Sven Schöling | state = STATES.PICKED;
|
||
last_real = $real.val();
|
||||
last_dummy = $dummy.val();
|
||||
46e1177c | Sven Schöling | $real.trigger('change');
|
||
if (o.fat_set_item) {
|
||||
$.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);
|
||||
}
|
||||
681ec24c | Sven Schöling | }
|
||
function make_defined_state () {
|
||||
if (state == STATES.PICKED)
|
||||
return true
|
||||
else if (state == STATES.UNDEFINED && $dummy.val() == '')
|
||||
set_item({})
|
||||
else
|
||||
set_item({ id: last_real, name: last_dummy })
|
||||
854fa965 | Sven Schöling | }
|
||
5cec90ad | Sven Schöling | function update_results () {
|
||
$.ajax({
|
||||
url: 'controller.pl?action=Part/part_picker_result',
|
||||
7a646681 | Sven Schöling | data: $.extend({
|
||
'real_id': $real.val(),
|
||||
}, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
|
||||
5cec90ad | Sven Schöling | success: function(data){ $('#part_picker_result').html(data) }
|
||
});
|
||||
};
|
||||
5729856b | Sven Schöling | function result_timer (event) {
|
||
46e1177c | Sven Schöling | 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;
|
||||
}
|
||||
}
|
||||
5729856b | Sven Schöling | window.clearTimeout(timer);
|
||
timer = window.setTimeout(update_results, 100);
|
||||
}
|
||||
c2d1e374 | Sven Schöling | function close_popup() {
|
||
6dbc83af | Moritz Bunkus | $('#part_selection').dialog('close');
|
||
c2d1e374 | Sven Schöling | };
|
||
854fa965 | Sven Schöling | $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);
|
||||
},
|
||||
});
|
||||
2504ebe1 | Sven Schöling | /* In case users are impatient and want to skip ahead:
|
||
* Capture <enter> 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 <tab> 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".
|
||||
*/
|
||||
1e1b6313 | Sven Schöling | /* 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
|
||||
854fa965 | Sven Schöling | // if string is empty assume they want to delete
|
||
2504ebe1 | Sven Schöling | if ($dummy.val() == '') {
|
||
854fa965 | Sven Schöling | set_item({});
|
||
2504ebe1 | Sven Schöling | return true;
|
||
681ec24c | Sven Schöling | } else if (state == STATES.PICKED) {
|
||
return true;
|
||||
2504ebe1 | Sven Schöling | }
|
||
$.ajax({
|
||||
url: 'controller.pl?action=Part/ajax_autocomplete',
|
||||
dataType: "json",
|
||||
eff6af28 | Sven Schöling | data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
|
||
2504ebe1 | Sven Schöling | success: function (data){
|
||
if (data.length == 1) {
|
||||
854fa965 | Sven Schöling | set_item(data[0]);
|
||
1e1b6313 | Sven Schöling | if (event.which == KEY.ENTER)
|
||
2504ebe1 | Sven Schöling | $('#update_button').click();
|
||
3cacc231 | Sven Schöling | } else if (data.length > 1) {
|
||
1e1b6313 | Sven Schöling | if (event.which == KEY.ENTER)
|
||
854fa965 | Sven Schöling | open_dialog();
|
||
else
|
||||
681ec24c | Sven Schöling | make_defined_state();
|
||
3cacc231 | Sven Schöling | } else {
|
||
1e1b6313 | Sven Schöling | if (event.which == KEY.TAB)
|
||
3cacc231 | Sven Schöling | make_defined_state();
|
||
2504ebe1 | Sven Schöling | }
|
||
}
|
||||
});
|
||||
1e1b6313 | Sven Schöling | if (event.which == KEY.ENTER)
|
||
2504ebe1 | Sven Schöling | return false;
|
||
681ec24c | Sven Schöling | } else {
|
||
state = STATES.UNDEFINED;
|
||||
}
|
||||
2504ebe1 | Sven Schöling | });
|
||
5729856b | Sven Schöling | $dummy.blur(function(){
|
||
window.clearTimeout(timer);
|
||||
timer = window.setTimeout(make_defined_state, 100);
|
||||
});
|
||||
2504ebe1 | Sven Schöling | |||
// now add a picker div after the original input
|
||||
var pcont = $('<span>').addClass('position-absolute');
|
||||
var picker = $('<div>');
|
||||
$dummy.after(pcont);
|
||||
pcont.append(picker);
|
||||
46e1177c | Sven Schöling | picker.addClass('icon16 crm--search').click(open_dialog);
|
||
5cec90ad | Sven Schöling | |||
670f1111 | Sven Schöling | var pp = {
|
||
c2d1e374 | Sven Schöling | real: function() { return $real },
|
||
dummy: function() { return $dummy },
|
||||
type: function() { return $type },
|
||||
880a2e9f | Moritz Bunkus | unit: function() { return $unit },
|
||
df33875f | Moritz Bunkus | convertible_unit: function() { return $convertible_unit },
|
||
c2d1e374 | Sven Schöling | column: function() { return $column },
|
||
5cec90ad | Sven Schöling | update_results: update_results,
|
||
5729856b | Sven Schöling | result_timer: result_timer,
|
||
c2d1e374 | Sven Schöling | set_item: set_item,
|
||
reset: make_defined_state,
|
||||
init_results: function () {
|
||||
$('div.part_picker_part').each(function(){
|
||||
$(this).click(function(){
|
||||
set_item({
|
||||
id: $(this).children('input.part_picker_id').val(),
|
||||
46e1177c | Sven Schöling | name: $(this).children('input.part_picker_description').val(),
|
||
unit: $(this).children('input.part_picker_unit').val(),
|
||||
c2d1e374 | Sven Schöling | });
|
||
close_popup();
|
||||
46e1177c | Sven Schöling | $dummy.focus();
|
||
c2d1e374 | Sven Schöling | return true;
|
||
});
|
||||
});
|
||||
1e1b6313 | Sven Schöling | $('#part_selection').keydown(function(e){
|
||
if (e.which == KEY.ESCAPE) {
|
||||
c2d1e374 | Sven Schöling | close_popup();
|
||
$dummy.focus();
|
||||
}
|
||||
});
|
||||
}
|
||||
5cec90ad | Sven Schöling | }
|
||
670f1111 | Sven Schöling | $real.data('part_picker', pp);
|
||
return pp;
|
||||
854fa965 | Sven Schöling | }
|
||
});
|
||||
$(function(){
|
||||
$('input.part_autocomplete').each(function(i,real){
|
||||
670f1111 | Sven Schöling | kivi.PartPicker($(real));
|
||
854fa965 | Sven Schöling | })
|
||
});
|