+ ns.Picker.prototype = {
+ CLASSES: {
+ PICKED: 'partpicker-picked',
+ UNDEFINED: 'partpicker-undefined',
+ },
+ ajax_data: function(term) {
+ var data = {
+ current: this.$real.val(),
+ };
+
+ if (this.o.part_type)
+ data['filter.part_type'] = this.o.part_type.split(',');
+
+ if (this.o.status) {
+ if (this.o.status == 'active') data['filter.obsolete'] = 0;
+ if (this.o.status == 'obsolete') data['filter.obsolete'] = 1;
+ } else
+ data['filter.obsolete'] = 0;
+
+ if (this.o.classification_id)
+ data['filter.classification_id:any'] = this.o.classification_id.replaceAll(',', ' ');
+
+ if (this.o.unit)
+ data['filter.unit'] = this.o.unit.split(',');
+
+ if (this.o.convertible_unit)
+ data['filter.unit_obj.convertible_to'] = this.o.convertible_unit;
+
+ var filter_name = 'all';
+ if (this.o.with_makemodel) {
+ filter_name = 'all_with_makemodel';
+ }
+ if (this.o.with_customer_partnumber) {
+ filter_name = 'all_with_customer_partnumber';
+ }
+ data['filter.' + filter_name + ':substr:multi::ilike'] = term;
+
+ return data;
+ },
+ set_item: function(item) {
+ var self = this;
+ if (item.id) {
+ this.$real.val(item.id);
+ // autocomplete ui has name, use the value for ajax items, which contains displayable_name
+ this.$dummy.val(item.name ? item.name : item.value);
+ } else {
+ this.$real.val('');
+ this.$dummy.val('');
+ }
+ this.state = this.STATES.PICKED;
+ this.last_real = this.$real.val();
+ this.last_dummy = this.$dummy.val();
+ this.$real.trigger('change');
+
+ if (this.o.fat_set_item && item.id) {
+ $.ajax({
+ url: 'controller.pl?action=Part/show.json',
+ data: { 'part.id': item.id },
+ success: function(rsp) {
+ self.$real.trigger('set_item:PartPicker', rsp);
+ },
+ });
+ } else {
+ this.$real.trigger('set_item:PartPicker', item);
+ }
+ this.annotate_state();
+ },
+ set_multi_items: function(data) {
+ this.run_action(this.o.action.set_multi_items, [ data ]);
+ },
+ make_defined_state: function() {
+ if (this.state == this.STATES.PICKED) {
+ this.annotate_state();
+ return true
+ } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
+ this.set_item({})
+ else {
+ this.set_item({ id: this.last_real, name: this.last_dummy })
+ }
+ this.annotate_state();
+ },
+ annotate_state: function() {
+ if (this.state == this.STATES.PICKED)
+ this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
+ else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
+ this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
+ else {
+ this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
+ }
+ },
+ handle_changed_text: function(callbacks) {
+ var self = this;
+ $.ajax({
+ url: 'controller.pl?action=Part/ajax_autocomplete',
+ dataType: "json",
+ data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
+ success: function (data) {
+ if (data.length == 1) {
+ self.set_item(data[0]);
+ if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
+ } else if (data.length > 1) {
+ self.state = self.STATES.UNDEFINED;
+ if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
+ } else {
+ self.state = self.STATES.UNDEFINED;
+ if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
+ }
+ self.annotate_state();
+ }
+ });
+ },
+ /* In case users are impatient and want to skip ahead:
+ * Capture <enter> key events and check if it's a unique hit.
+ * If it is, go ahead and assume it was selected. If it wasn't don't do
+ * anything so that autocompletion kicks in. For <tab> don't prevent
+ * propagation. It would be nice to catch it, but javascript is too stupid
+ * to fire a tab event later on, so we'd have to reimplement the "find
+ * next active element in tabindex order and focus it".
+ */
+ /* note:
+ * event.which does not contain tab events in keypressed in firefox but will report 0
+ * chrome does not fire keypressed at all on tab or escape
+ */
+ handle_keydown: function(event) {
+ var self = this;
+ if (event.which == KEY.ENTER || event.which == KEY.TAB) {
+ // if string is empty assume they want to delete
+ if (self.$dummy.val() === '') {
+ self.set_item({});
+ return true;
+ } else if (self.state == self.STATES.PICKED) {
+ if (self.o.action.commit_one) {
+ self.run_action(self.o.action.commit_one);
+ }
+ return true;
+ }
+ if (event.which == KEY.TAB) {
+ event.preventDefault();
+ self.handle_changed_text();