1 namespace('kivi', function(k){
2 k.PartPickerCache = { }
3 k.PartPicker = function($real, options) {
12 var real_id = $real.attr('id');
13 var $dummy = $('#' + real_id + '_name');
14 var $type = $('#' + real_id + '_type');
15 var $column = $('#' + real_id + '_column');
16 var state = STATES.PICKED;
17 var last_real = $real.val();
18 var last_dummy = $dummy.val();
19 var open_dialog = function(){
23 action: 'Part/part_picker_search',
25 'filter.all:substr::ilike': function(){ return $dummy.val() },
26 'filter.type': function(){ return $type.val() },
27 'column': function(){ return $column.val() },
34 function ajax_data(term) {
37 type: function() { return $type.val() },
38 column: function() { return $column.val()===undefined ? '' : $column.val() },
39 current: function() { return $real.val() },
44 function set_item (item) {
47 // autocomplete ui has name, ajax items have description
48 $dummy.val(item.name ? item.name : item.description);
53 state = STATES.PICKED;
54 last_real = $real.val();
55 last_dummy = $dummy.val();
56 $real.trigger('change');
59 function make_defined_state () {
60 if (state == STATES.PICKED)
62 else if (state == STATES.UNDEFINED && $dummy.val() == '')
65 set_item({ id: last_real, name: last_dummy })
68 function update_results () {
70 url: 'controller.pl?action=Part/part_picker_result',
72 'filter.all:substr::ilike': function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val },
73 'filter.type': $type.val(),
74 'column': $column.val(),
77 success: function(data){ $('#part_picker_result').html(data) }
81 function close_popup() {
82 $('#part_selection').jqmClose()
86 source: function(req, rsp) {
88 url: 'controller.pl?action=Part/ajax_autocomplete',
90 data: ajax_data(req.term),
91 success: function (data){ rsp(data) }
94 select: function(event, ui) {
98 /* In case users are impatient and want to skip ahead:
99 * Capture <enter> key events and check if it's a unique hit.
100 * If it is, go ahead and assume it was selected. If it wasn't don't do
101 * anything so that autocompletion kicks in. For <tab> don't prevent
102 * propagation. It would be nice to catch it, but javascript is too stupid
103 * to fire a tab event later on, so we'd have to reimplement the "find
104 * next active element in tabindex order and focus it".
106 $dummy.keypress(function(event){
107 if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab
108 // if string is empty assume they want to delete
109 if ($dummy.val() == '') {
112 } else if (state == STATES.PICKED) {
116 url: 'controller.pl?action=Part/ajax_autocomplete',
118 data: ajax_data($dummy.val()),
119 success: function (data){
120 if (data.length == 1) {
122 if (event.keyCode == 13)
123 $('#update_button').click();
124 } else if (data.length > 1) {
125 if (event.keyCode == 13)
128 make_defined_state();
130 if (event.keyCode == 9)
131 make_defined_state();
135 if (event.keyCode == 13)
138 state = STATES.UNDEFINED;
142 // $dummy.blur(make_defined_state); // blur triggers also on open_jqm_dialog
144 // now add a picker div after the original input
145 var pcont = $('<span>').addClass('position-absolute');
146 var picker = $('<div>');
148 pcont.append(picker);
149 picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog);
152 real: function() { return $real },
153 dummy: function() { return $dummy },
154 type: function() { return $type },
155 column: function() { return $column },
156 update_results: update_results,
158 reset: make_defined_state,
159 init_results: function () {
160 $('div.part_picker_part').each(function(){
161 $(this).click(function(){
163 name: $(this).children('input.part_picker_description').val(),
164 id: $(this).children('input.part_picker_id').val(),
170 $('#part_selection').keypress(function(e){
171 if (e.keyCode == 27) { // escape
182 $('input.part_autocomplete').each(function(i,real){
183 kivi.PartPickerCache[real.id] = new kivi.PartPicker($(real));