1 namespace('kivi', function(k){
2 k.PartPicker = function($real, options) {
3 // short circuit in case someone double inits us
4 if ($real.data("part_picker"))
5 return $real.data("part_picker");
17 PICKED: 'partpicker-picked',
18 UNDEFINED: 'partpicker-undefined',
19 FAT_SET_ITEM: 'partpicker_fat_set_item',
24 fat_set_item: $real.hasClass(CLASSES.FAT_SET_ITEM),
27 PICKED: CLASSES.PICKED,
28 UNDEFINED: CLASSES.UNDEFINED
30 var real_id = $real.attr('id');
31 var $dummy = $('#' + real_id + '_name');
32 var $type = $('#' + real_id + '_type');
33 var $unit = $('#' + real_id + '_unit');
34 var $convertible_unit = $('#' + real_id + '_convertible_unit');
35 var state = STATES.PICKED;
36 var last_real = $real.val();
37 var last_dummy = $dummy.val();
40 function open_dialog () {
42 url: 'controller.pl?action=Part/part_picker_search',
45 }, ajax_data($dummy.val())),
48 title: k.t8('Part picker'),
53 window.clearTimeout(timer);
57 function ajax_data(term) {
59 'filter.all:substr:multi::ilike': term,
61 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
62 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
66 if ($type && $type.val())
67 data['filter.type'] = $type.val().split(',');
69 if ($unit && $unit.val())
70 data['filter.unit'] = $unit.val().split(',');
75 function set_item (item) {
78 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
79 $dummy.val(item.name ? item.name : item.value);
84 state = STATES.PICKED;
85 last_real = $real.val();
86 last_dummy = $dummy.val();
87 last_unverified_dummy = $dummy.val();
88 $real.trigger('change');
90 if (o.fat_set_item && item.id) {
92 url: 'controller.pl?action=Part/show.json',
93 data: { id: item.id },
94 success: function(rsp) {
95 $real.trigger('set_item:PartPicker', rsp);
99 $real.trigger('set_item:PartPicker', item);
104 function make_defined_state () {
105 if (state == STATES.PICKED) {
108 } else if (state == STATES.UNDEFINED && $dummy.val() == '')
111 last_unverified_dummy = $dummy.val();
112 set_item({ id: last_real, name: last_dummy })
117 function annotate_state () {
118 if (state == STATES.PICKED)
119 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
120 else if (state == STATES.UNDEFINED && $dummy.val() == '')
121 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
123 last_unverified_dummy = $dummy.val();
124 $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
128 function update_results () {
130 url: 'controller.pl?action=Part/part_picker_result',
132 'real_id': $real.val(),
133 }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
134 success: function(data){ $('#part_picker_result').html(data) }
138 function result_timer (event) {
139 if (!$('no_paginate').prop('checked')) {
140 if (event.keyCode == KEY.PAGE_UP) {
141 $('#part_picker_result a.paginate-prev').click();
144 if (event.keyCode == KEY.PAGE_DOWN) {
145 $('#part_picker_result a.paginate-next').click();
149 window.clearTimeout(timer);
150 timer = window.setTimeout(update_results, 100);
153 function close_popup() {
154 $('#part_selection').dialog('close');
157 function handle_changed_text(callbacks) {
159 url: 'controller.pl?action=Part/ajax_autocomplete',
161 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
162 success: function (data) {
163 if (data.length == 1) {
165 if (callbacks && callbacks.match_one) callbacks.match_one(data[0]);
166 } else if (data.length > 1) {
167 state = STATES.UNDEFINED;
168 if (callbacks && callbacks.match_many) callbacks.match_many(data);
170 state = STATES.UNDEFINED;
171 if (callbacks &&callbacks.match_none) callbacks.match_none();
178 $dummy.autocomplete({
179 source: function(req, rsp) {
181 url: 'controller.pl?action=Part/ajax_autocomplete',
183 data: ajax_data(req.term),
184 success: function (data){ rsp(data) }
187 select: function(event, ui) {
191 /* In case users are impatient and want to skip ahead:
192 * Capture <enter> key events and check if it's a unique hit.
193 * If it is, go ahead and assume it was selected. If it wasn't don't do
194 * anything so that autocompletion kicks in. For <tab> don't prevent
195 * propagation. It would be nice to catch it, but javascript is too stupid
196 * to fire a tab event later on, so we'd have to reimplement the "find
197 * next active element in tabindex order and focus it".
200 * event.which does not contain tab events in keypressed in firefox but will report 0
201 * chrome does not fire keypressed at all on tab or escape
203 $dummy.keydown(function(event){
204 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
205 // if string is empty assume they want to delete
206 if ($dummy.val() == '') {
209 } else if (state == STATES.PICKED) {
212 if (event.which == KEY.TAB) {
213 event.preventDefault();
214 handle_changed_text();
216 if (event.which == KEY.ENTER) {
217 handle_changed_text({
218 match_one: function(){$('#update_button').click();},
219 match_many: function(){open_dialog();}
224 state = STATES.UNDEFINED;
228 $dummy.on('paste', function(){
229 setTimeout(function() {
230 handle_changed_text();
234 $dummy.blur(function(){
235 window.clearTimeout(timer);
236 timer = window.setTimeout(annotate_state, 100);
239 // now add a picker div after the original input
240 var popup_button = $('<span>').addClass('ppp_popup_button');
241 $dummy.after(popup_button);
242 popup_button.click(open_dialog);
245 real: function() { return $real },
246 dummy: function() { return $dummy },
247 type: function() { return $type },
248 unit: function() { return $unit },
249 convertible_unit: function() { return $convertible_unit },
250 update_results: update_results,
251 result_timer: result_timer,
253 reset: make_defined_state,
254 is_defined_state: function() { return state == STATES.PICKED },
255 init_results: function () {
256 $('div.part_picker_part').each(function(){
257 $(this).click(function(){
259 id: $(this).children('input.part_picker_id').val(),
260 name: $(this).children('input.part_picker_description').val(),
261 unit: $(this).children('input.part_picker_unit').val(),
262 partnumber: $(this).children('input.part_picker_partnumber').val(),
263 description: $(this).children('input.part_picker_description').val(),
270 $('#part_selection').keydown(function(e){
271 if (e.which == KEY.ESCAPE) {
278 $real.data('part_picker', pp);
284 $('input.part_autocomplete').each(function(i,real){
285 kivi.PartPicker($(real));