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");
20 var real_id = $real.attr('id');
21 var $dummy = $('#' + real_id + '_name');
22 var $type = $('#' + real_id + '_type');
23 var $unit = $('#' + real_id + '_unit');
24 var $convertible_unit = $('#' + real_id + '_convertible_unit');
25 var $column = $('#' + real_id + '_column');
26 var state = STATES.PICKED;
27 var last_real = $real.val();
28 var last_dummy = $dummy.val();
31 function open_dialog () {
33 url: 'controller.pl?action=Part/part_picker_search',
36 }, ajax_data($dummy.val())),
39 window.clearTimeout(timer);
43 function ajax_data(term) {
45 'filter.all:substr::ilike': term,
47 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
48 column: $column && $column.val() ? $column.val() : '',
52 if ($type && $type.val())
53 data['filter.type'] = $type.val().split(',');
55 if ($unit && $unit.val())
56 data['filter.unit'] = $unit.val().split(',');
61 function set_item (item) {
64 // autocomplete ui has name, ajax items have description
65 $dummy.val(item.name ? item.name : item.description);
70 state = STATES.PICKED;
71 last_real = $real.val();
72 last_dummy = $dummy.val();
73 $real.trigger('change');
76 function make_defined_state () {
77 if (state == STATES.PICKED)
79 else if (state == STATES.UNDEFINED && $dummy.val() == '')
82 set_item({ id: last_real, name: last_dummy })
85 function update_results () {
87 url: 'controller.pl?action=Part/part_picker_result',
89 'real_id': $real.val(),
90 }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
91 success: function(data){ $('#part_picker_result').html(data) }
95 function result_timer (event) {
96 window.clearTimeout(timer);
97 timer = window.setTimeout(update_results, 100);
100 function close_popup() {
101 $('#part_selection').jqmClose()
104 $dummy.autocomplete({
105 source: function(req, rsp) {
107 url: 'controller.pl?action=Part/ajax_autocomplete',
109 data: ajax_data(req.term),
110 success: function (data){ rsp(data) }
113 select: function(event, ui) {
117 /* In case users are impatient and want to skip ahead:
118 * Capture <enter> key events and check if it's a unique hit.
119 * If it is, go ahead and assume it was selected. If it wasn't don't do
120 * anything so that autocompletion kicks in. For <tab> don't prevent
121 * propagation. It would be nice to catch it, but javascript is too stupid
122 * to fire a tab event later on, so we'd have to reimplement the "find
123 * next active element in tabindex order and focus it".
126 * event.which does not contain tab events in keypressed in firefox but will report 0
127 * chrome does not fire keypressed at all on tab or escape
128 * TODO: users expect tab to work on keydown but enter to trigger on keyup,
129 * should be handled seperately
131 $dummy.keydown(function(event){
132 if (event.which == KEY.ENTER || event.which == KEY.TAB) { // enter or tab or tab
133 // if string is empty assume they want to delete
134 if ($dummy.val() == '') {
137 } else if (state == STATES.PICKED) {
141 url: 'controller.pl?action=Part/ajax_autocomplete',
143 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
144 success: function (data){
145 if (data.length == 1) {
147 if (event.which == KEY.ENTER)
148 $('#update_button').click();
149 } else if (data.length > 1) {
150 if (event.which == KEY.ENTER)
153 make_defined_state();
155 if (event.which == KEY.TAB)
156 make_defined_state();
160 if (event.which == KEY.ENTER)
163 state = STATES.UNDEFINED;
167 $dummy.blur(function(){
168 window.clearTimeout(timer);
169 timer = window.setTimeout(make_defined_state, 100);
172 // now add a picker div after the original input
173 var pcont = $('<span>').addClass('position-absolute');
174 var picker = $('<div>');
176 pcont.append(picker);
177 picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog);
180 real: function() { return $real },
181 dummy: function() { return $dummy },
182 type: function() { return $type },
183 unit: function() { return $unit },
184 convertible_unit: function() { return $convertible_unit },
185 column: function() { return $column },
186 update_results: update_results,
187 result_timer: result_timer,
189 reset: make_defined_state,
190 init_results: function () {
191 $('div.part_picker_part').each(function(){
192 $(this).click(function(){
194 name: $(this).children('input.part_picker_description').val(),
195 id: $(this).children('input.part_picker_id').val(),
201 $('#part_selection').keydown(function(e){
202 if (e.which == KEY.ESCAPE) {
209 $real.data('part_picker', pp);
215 $('input.part_autocomplete').each(function(i,real){
216 kivi.PartPicker($(real));