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");
15 var real_id = $real.attr('id');
16 var $dummy = $('#' + real_id + '_name');
17 var $type = $('#' + real_id + '_type');
18 var $unit = $('#' + real_id + '_unit');
19 var $convertible_unit = $('#' + real_id + '_convertible_unit');
20 var $column = $('#' + real_id + '_column');
21 var state = STATES.PICKED;
22 var last_real = $real.val();
23 var last_dummy = $dummy.val();
24 var open_dialog = function(){
26 url: 'controller.pl?action=Part/part_picker_search',
29 }, ajax_data($dummy.val())),
35 function ajax_data(term) {
37 'filter.all:substr::ilike': term,
39 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
40 column: $column && $column.val() ? $column.val() : '',
44 if ($type && $type.val())
45 data['filter.type'] = $type.val().split(',');
47 if ($unit && $unit.val())
48 data['filter.unit'] = $unit.val().split(',');
53 function set_item (item) {
56 // autocomplete ui has name, ajax items have description
57 $dummy.val(item.name ? item.name : item.description);
62 state = STATES.PICKED;
63 last_real = $real.val();
64 last_dummy = $dummy.val();
65 $real.trigger('change');
68 function make_defined_state () {
69 if (state == STATES.PICKED)
71 else if (state == STATES.UNDEFINED && $dummy.val() == '')
74 set_item({ id: last_real, name: last_dummy })
77 function update_results () {
79 url: 'controller.pl?action=Part/part_picker_result',
81 'real_id': $real.val(),
82 }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
83 success: function(data){ $('#part_picker_result').html(data) }
87 function close_popup() {
88 $('#part_selection').jqmClose()
92 source: function(req, rsp) {
94 url: 'controller.pl?action=Part/ajax_autocomplete',
96 data: ajax_data(req.term),
97 success: function (data){ rsp(data) }
100 select: function(event, ui) {
104 /* In case users are impatient and want to skip ahead:
105 * Capture <enter> key events and check if it's a unique hit.
106 * If it is, go ahead and assume it was selected. If it wasn't don't do
107 * anything so that autocompletion kicks in. For <tab> don't prevent
108 * propagation. It would be nice to catch it, but javascript is too stupid
109 * to fire a tab event later on, so we'd have to reimplement the "find
110 * next active element in tabindex order and focus it".
112 $dummy.keypress(function(event){
113 if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab
114 // if string is empty assume they want to delete
115 if ($dummy.val() == '') {
118 } else if (state == STATES.PICKED) {
122 url: 'controller.pl?action=Part/ajax_autocomplete',
124 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
125 success: function (data){
126 if (data.length == 1) {
128 if (event.keyCode == 13)
129 $('#update_button').click();
130 } else if (data.length > 1) {
131 if (event.keyCode == 13)
134 make_defined_state();
136 if (event.keyCode == 9)
137 make_defined_state();
141 if (event.keyCode == 13)
144 state = STATES.UNDEFINED;
148 // $dummy.blur(make_defined_state); // blur triggers also on open_jqm_dialog
150 // now add a picker div after the original input
151 var pcont = $('<span>').addClass('position-absolute');
152 var picker = $('<div>');
154 pcont.append(picker);
155 picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog);
158 real: function() { return $real },
159 dummy: function() { return $dummy },
160 type: function() { return $type },
161 unit: function() { return $unit },
162 convertible_unit: function() { return $convertible_unit },
163 column: function() { return $column },
164 update_results: update_results,
166 reset: make_defined_state,
167 init_results: function () {
168 $('div.part_picker_part').each(function(){
169 $(this).click(function(){
171 name: $(this).children('input.part_picker_description').val(),
172 id: $(this).children('input.part_picker_id').val(),
178 $('#part_selection').keypress(function(e){
179 if (e.keyCode == 27) { // escape
186 $real.data('part_picker', pp);
192 $('input.part_autocomplete').each(function(i,real){
193 kivi.PartPicker($(real));