1 namespace('kivi', function(k){
2 k.CustomerVendorPicker = function($real, options) {
3 // short circuit in case someone double inits us
4 if ($real.data("customer_vendor_picker"))
5 return $real.data("customer_vendor_picker");
17 PICKED: 'customer-vendor-picker-picked',
18 UNDEFINED: 'customer-vendor-picker-undefined',
19 FAT_SET_ITEM: 'customer-vendor-picker-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 state = STATES.PICKED;
35 var last_real = $real.val();
36 var last_dummy = $dummy.val();
39 function ajax_data(term) {
41 'filter.all:substr:multi::ilike': term,
50 function set_item (item) {
53 // autocomplete ui has name, ajax items have description
54 $dummy.val(item.name ? item.name : item.description);
59 state = STATES.PICKED;
60 last_real = $real.val();
61 last_dummy = $dummy.val();
62 last_unverified_dummy = $dummy.val();
63 $real.trigger('change');
65 if (o.fat_set_item && item.id) {
67 url: 'controller.pl?action=CustomerVendor/show.json',
68 data: { id: item.id, db: item.type },
69 success: function(rsp) {
70 $real.trigger('set_item:CustomerVendorPicker', rsp);
74 $real.trigger('set_item:CustomerVendorPicker', item);
79 function make_defined_state () {
80 if (state == STATES.PICKED) {
83 } else if (state == STATES.UNDEFINED && $dummy.val() == '')
86 last_unverified_dummy = $dummy.val();
87 set_item({ id: last_real, name: last_dummy })
92 function annotate_state () {
93 if (state == STATES.PICKED)
94 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
95 else if (state == STATES.UNDEFINED && $dummy.val() == '')
96 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
98 last_unverified_dummy = $dummy.val();
99 $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
103 $dummy.autocomplete({
104 source: function(req, rsp) {
106 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
108 data: ajax_data(req.term),
109 success: function (data){ rsp(data) }
112 select: function(event, ui) {
116 /* In case users are impatient and want to skip ahead:
117 * Capture <enter> key events and check if it's a unique hit.
118 * If it is, go ahead and assume it was selected. If it wasn't don't do
119 * anything so that autocompletion kicks in. For <tab> don't prevent
120 * propagation. It would be nice to catch it, but javascript is too stupid
121 * to fire a tab event later on, so we'd have to reimplement the "find
122 * next active element in tabindex order and focus it".
125 * event.which does not contain tab events in keypressed in firefox but will report 0
126 * chrome does not fire keypressed at all on tab or escape
128 $dummy.keydown(function(event){
129 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
130 // if string is empty assume they want to delete
131 if ($dummy.val() == '') {
134 } else if (state == STATES.PICKED) {
137 if (event.which == KEY.TAB) event.preventDefault();
139 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
141 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
142 success: function (data) {
143 if (data.length == 1) {
145 if (event.which == KEY.ENTER)
146 $('#update_button').click();
152 if (event.which == KEY.ENTER)
155 state = STATES.UNDEFINED;
159 $dummy.blur(function(){
160 window.clearTimeout(timer);
161 timer = window.setTimeout(annotate_state, 100);
164 // now add a picker div after the original input
166 real: function() { return $real },
167 dummy: function() { return $dummy },
168 type: function() { return $type },
170 reset: make_defined_state,
171 is_defined_state: function() { return state == STATES.PICKED },
173 $real.data('customer_vendor_picker', pp);
179 $('input.customer_vendor_autocomplete').each(function(i,real){
180 kivi.CustomerVendorPicker($(real));