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");
19 fat_set_item: $real.hasClass('partpicker_fat_set_item'),
25 var real_id = $real.attr('id');
26 var $dummy = $('#' + real_id + '_name');
27 var $type = $('#' + real_id + '_type');
28 var $unit = $('#' + real_id + '_unit');
29 var $convertible_unit = $('#' + real_id + '_convertible_unit');
30 var $column = $('#' + real_id + '_column');
31 var state = STATES.PICKED;
32 var last_real = $real.val();
33 var last_dummy = $dummy.val();
34 var last_unverified_dummy = $dummy.val();
37 function open_dialog () {
39 url: 'controller.pl?action=Part/part_picker_search',
42 }, ajax_data(last_unverified_dummy)),
45 title: k.t8('Part picker'),
50 window.clearTimeout(timer);
54 function ajax_data(term) {
56 'filter.all:substr:multi::ilike': term,
58 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
59 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
60 column: $column && $column.val() ? $column.val() : '',
64 if ($type && $type.val())
65 data['filter.type'] = $type.val().split(',');
67 if ($unit && $unit.val())
68 data['filter.unit'] = $unit.val().split(',');
73 function set_item (item) {
76 // autocomplete ui has name, ajax items have description
77 $dummy.val(item.name ? item.name : item.description);
82 state = STATES.PICKED;
83 last_real = $real.val();
84 last_dummy = $dummy.val();
85 $real.trigger('change');
87 if (o.fat_set_item && item.id) {
89 url: 'controller.pl?action=Part/show.json',
90 data: { id: item.id },
91 success: function(rsp) {
92 $real.trigger('set_item:PartPicker', rsp);
96 $real.trigger('set_item:PartPicker', item);
100 function make_defined_state () {
101 if (state == STATES.PICKED)
103 else if (state == STATES.UNDEFINED && $dummy.val() == '')
106 last_unverified_dummy = $dummy.val();
107 set_item({ id: last_real, name: last_dummy })
110 function update_results () {
112 url: 'controller.pl?action=Part/part_picker_result',
114 'real_id': $real.val(),
115 }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
116 success: function(data){ $('#part_picker_result').html(data) }
120 function result_timer (event) {
121 if (!$('no_paginate').prop('checked')) {
122 if (event.keyCode == KEY.PAGE_UP) {
123 $('#part_picker_result a.paginate-prev').click();
126 if (event.keyCode == KEY.PAGE_DOWN) {
127 $('#part_picker_result a.paginate-next').click();
131 window.clearTimeout(timer);
132 timer = window.setTimeout(update_results, 100);
135 function close_popup() {
136 $('#part_selection').dialog('close');
139 $dummy.autocomplete({
140 source: function(req, rsp) {
142 url: 'controller.pl?action=Part/ajax_autocomplete',
144 data: ajax_data(req.term),
145 success: function (data){ rsp(data) }
148 select: function(event, ui) {
152 /* In case users are impatient and want to skip ahead:
153 * Capture <enter> key events and check if it's a unique hit.
154 * If it is, go ahead and assume it was selected. If it wasn't don't do
155 * anything so that autocompletion kicks in. For <tab> don't prevent
156 * propagation. It would be nice to catch it, but javascript is too stupid
157 * to fire a tab event later on, so we'd have to reimplement the "find
158 * next active element in tabindex order and focus it".
161 * event.which does not contain tab events in keypressed in firefox but will report 0
162 * chrome does not fire keypressed at all on tab or escape
163 * TODO: users expect tab to work on keydown but enter to trigger on keyup,
164 * should be handled seperately
166 $dummy.keydown(function(event){
167 if (event.which == KEY.ENTER || event.which == KEY.TAB) { // enter or tab or tab
168 // if string is empty assume they want to delete
169 if ($dummy.val() == '') {
172 } else if (state == STATES.PICKED) {
176 url: 'controller.pl?action=Part/ajax_autocomplete',
178 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
179 success: function (data){
180 if (data.length == 1) {
182 if (event.which == KEY.ENTER)
183 $('#update_button').click();
184 } else if (data.length > 1) {
185 if (event.which == KEY.ENTER)
188 make_defined_state();
190 if (event.which == KEY.TAB)
191 make_defined_state();
195 if (event.which == KEY.ENTER)
198 state = STATES.UNDEFINED;
202 $dummy.blur(function(){
203 window.clearTimeout(timer);
204 timer = window.setTimeout(make_defined_state, 100);
207 // now add a picker div after the original input
208 var pcont = $('<span>').addClass('position-absolute');
209 var picker = $('<div>');
211 pcont.append(picker);
212 picker.addClass('icon16 crm--search').click(open_dialog);
215 real: function() { return $real },
216 dummy: function() { return $dummy },
217 type: function() { return $type },
218 unit: function() { return $unit },
219 convertible_unit: function() { return $convertible_unit },
220 column: function() { return $column },
221 update_results: update_results,
222 result_timer: result_timer,
224 reset: make_defined_state,
225 init_results: function () {
226 $('div.part_picker_part').each(function(){
227 $(this).click(function(){
229 id: $(this).children('input.part_picker_id').val(),
230 name: $(this).children('input.part_picker_description').val(),
231 unit: $(this).children('input.part_picker_unit').val(),
238 $('#part_selection').keydown(function(e){
239 if (e.which == KEY.ESCAPE) {
246 $real.data('part_picker', pp);
252 $('input.part_autocomplete').each(function(i,real){
253 kivi.PartPicker($(real));