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 $dummy.autocomplete({
158 source: function(req, rsp) {
160 url: 'controller.pl?action=Part/ajax_autocomplete',
162 data: ajax_data(req.term),
163 success: function (data){ rsp(data) }
166 select: function(event, ui) {
170 /* In case users are impatient and want to skip ahead:
171 * Capture <enter> key events and check if it's a unique hit.
172 * If it is, go ahead and assume it was selected. If it wasn't don't do
173 * anything so that autocompletion kicks in. For <tab> don't prevent
174 * propagation. It would be nice to catch it, but javascript is too stupid
175 * to fire a tab event later on, so we'd have to reimplement the "find
176 * next active element in tabindex order and focus it".
179 * event.which does not contain tab events in keypressed in firefox but will report 0
180 * chrome does not fire keypressed at all on tab or escape
182 $dummy.keydown(function(event){
183 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
184 // if string is empty assume they want to delete
185 if ($dummy.val() == '') {
188 } else if (state == STATES.PICKED) {
191 if (event.which == KEY.TAB) event.preventDefault();
193 url: 'controller.pl?action=Part/ajax_autocomplete',
195 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
196 success: function (data) {
197 if (data.length == 1) {
199 if (event.which == KEY.ENTER)
200 $('#update_button').click();
201 } else if (data.length > 1) {
202 if (event.which == KEY.ENTER)
209 if (event.which == KEY.ENTER)
212 state = STATES.UNDEFINED;
216 $dummy.blur(function(){
217 window.clearTimeout(timer);
218 timer = window.setTimeout(annotate_state, 100);
221 // now add a picker div after the original input
222 var pcont = $('<span>').addClass('position-absolute');
223 var picker = $('<div>');
225 pcont.append(picker);
226 picker.addClass('icon16 crm--search').click(open_dialog);
229 real: function() { return $real },
230 dummy: function() { return $dummy },
231 type: function() { return $type },
232 unit: function() { return $unit },
233 convertible_unit: function() { return $convertible_unit },
234 update_results: update_results,
235 result_timer: result_timer,
237 reset: make_defined_state,
238 is_defined_state: function() { return state == STATES.PICKED },
239 init_results: function () {
240 $('div.part_picker_part').each(function(){
241 $(this).click(function(){
243 id: $(this).children('input.part_picker_id').val(),
244 name: $(this).children('input.part_picker_description').val(),
245 unit: $(this).children('input.part_picker_unit').val(),
252 $('#part_selection').keydown(function(e){
253 if (e.which == KEY.ESCAPE) {
260 $real.data('part_picker', pp);
266 $('input.part_autocomplete').each(function(i,real){
267 kivi.PartPicker($(real));