1 namespace('kivi', function(k){
4 k.CustomerVendorPicker = function($real, options) {
5 // short circuit in case someone double inits us
6 if ($real.data("customer_vendor_picker"))
7 return $real.data("customer_vendor_picker");
22 PICKED: 'customer-vendor-picker-picked',
23 UNDEFINED: 'customer-vendor-picker-undefined',
24 FAT_SET_ITEM: 'customer-vendor-picker-fat-set-item',
29 fat_set_item: $real.hasClass(CLASSES.FAT_SET_ITEM),
32 PICKED: CLASSES.PICKED,
33 UNDEFINED: CLASSES.UNDEFINED
35 var real_id = $real.attr('id');
36 var $dummy = $('#' + real_id + '_name');
37 var $type = $('#' + real_id + '_type');
38 var $unit = $('#' + real_id + '_unit');
39 var state = STATES.PICKED;
40 var last_real = $real.val();
41 var last_dummy = $dummy.val();
44 function ajax_data(term) {
46 'filter.all:substr:multi::ilike': term,
55 function set_item (item) {
58 // autocomplete ui has name, ajax items have description
59 $dummy.val(item.name ? item.name : item.description);
64 state = STATES.PICKED;
65 last_real = $real.val();
66 last_dummy = $dummy.val();
67 $real.trigger('change');
69 if (o.fat_set_item && item.id) {
71 url: 'controller.pl?action=CustomerVendor/show.json',
72 data: { id: item.id, db: item.type },
73 success: function(rsp) {
74 $real.trigger('set_item:CustomerVendorPicker', rsp);
78 $real.trigger('set_item:CustomerVendorPicker', item);
83 function make_defined_state () {
84 if (state == STATES.PICKED) {
87 } else if (state == STATES.UNDEFINED && $dummy.val() === '')
90 set_item({ id: last_real, name: last_dummy })
95 function annotate_state () {
96 if (state == STATES.PICKED)
97 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
98 else if (state == STATES.UNDEFINED && $dummy.val() === '')
99 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
101 $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
105 function handle_changed_text(callbacks) {
107 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
109 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
110 success: function (data) {
111 if (data.length == 1) {
113 if (callbacks && callbacks.match_one) callbacks.match_one(data[0]);
114 } else if (data.length > 1) {
115 state = STATES.UNDEFINED;
116 if (callbacks && callbacks.match_many) callbacks.match_many(data);
118 state = STATES.UNDEFINED;
119 if (callbacks &&callbacks.match_none) callbacks.match_none();
126 $dummy.autocomplete({
127 source: function(req, rsp) {
129 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
131 data: ajax_data(req.term),
132 success: function (data){ rsp(data) }
135 select: function(event, ui) {
138 search: function(event, ui) {
139 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
140 event.preventDefault();
143 /* In case users are impatient and want to skip ahead:
144 * Capture <enter> key events and check if it's a unique hit.
145 * If it is, go ahead and assume it was selected. If it wasn't don't do
146 * anything so that autocompletion kicks in. For <tab> don't prevent
147 * propagation. It would be nice to catch it, but javascript is too stupid
148 * to fire a tab event later on, so we'd have to reimplement the "find
149 * next active element in tabindex order and focus it".
152 * event.which does not contain tab events in keypressed in firefox but will report 0
153 * chrome does not fire keypressed at all on tab or escape
155 $dummy.keydown(function(event){
156 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
157 // if string is empty assume they want to delete
158 if ($dummy.val() === '') {
161 } else if (state == STATES.PICKED) {
164 if (event.which == KEY.TAB) {
165 event.preventDefault();
166 handle_changed_text();
168 if (event.which == KEY.ENTER) {
169 handle_changed_text({
170 match_one: function(){$('#update_button').click();},
174 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
175 state = STATES.UNDEFINED;
179 $dummy.on('paste', function(){
180 setTimeout(function() {
181 handle_changed_text();
185 $dummy.blur(function(){
186 window.clearTimeout(timer);
187 timer = window.setTimeout(annotate_state, 100);
190 // now add a picker div after the original input
192 real: function() { return $real },
193 dummy: function() { return $dummy },
194 type: function() { return $type },
196 reset: make_defined_state,
197 is_defined_state: function() { return state == STATES.PICKED },
199 $real.data('customer_vendor_picker', pp);
205 $('input.customer_vendor_autocomplete').each(function(i,real){
206 kivi.CustomerVendorPicker($(real));