this.o = $.extend({
limit: 20,
delay: 50,
- fat_set_item: $real.hasClass(this.CLASSES.FAT_SET_ITEM),
action: {
on_enter_match_none: function(){ },
on_enter_match_one: function(){ $('#update_button').click(); },
on_enter_match_many: function(){ self.open_dialog(); }
}
- }, options);
+ }, $real.data('part-picker-data'), options);
this.$real = $real;
this.real_id = $real.attr('id');
this.last_real = $real.val();
- this.$dummy = $('#' + this.real_id + '_name');
- this.$part_type = $('#' + this.real_id + '_part_type');
- this.$classification_id = $('#' + this.real_id + '_classification_id');
- this.$unit = $('#' + this.real_id + '_unit');
- this.$convertible_unit = $('#' + this.real_id + '_convertible_unit');
+ this.$dummy = $($real.siblings()[0]);
this.autocomplete_open = false;
this.state = this.STATES.PICKED;
this.last_dummy = this.$dummy.val();
CLASSES: {
PICKED: 'partpicker-picked',
UNDEFINED: 'partpicker-undefined',
- FAT_SET_ITEM: 'partpicker_fat_set_item',
},
ajax_data: function(term) {
var data = {
'filter.all:substr:multi::ilike': term,
'filter.obsolete': 0,
- 'filter.unit_obj.convertible_to': this.$convertible_unit && this.$convertible_unit.val() ? this.$convertible_unit.val() : '',
current: this.$real.val(),
};
- if (this.$part_type && this.$part_type.val())
- data['filter.part_type'] = this.$part_type.val().split(',');
+ if (this.o.part_type)
+ data['filter.part_type'] = this.o.part_type.split(',');
- if (this.$classification_id && this.$classification_id.val())
- data['filter.classification_id'] = this.$classification_id.val().split(',');
+ if (this.o.classification_id)
+ data['filter.classification_id'] = this.o.classification_id.split(',');
- if (this.$unit && this.$unit.val())
- data['filter.unit'] = this.$unit.val().split(',');
+ 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;
return data;
},
}
});
},
+ /* 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) {
+ return true;
+ }
+ if (event.which == KEY.TAB) {
+ event.preventDefault();
+ self.handle_changed_text();
+ }
+ if (event.which == KEY.ENTER) {
+ self.handle_changed_text({
+ match_one: self.o.action.on_enter_match_one,
+ match_many: self.o.action.on_enter_match_many
+ });
+ return false;
+ }
+ } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
+ var old_options = self.$dummy.autocomplete('option');
+ self.$dummy.autocomplete('option', 'minLength', 0);
+ self.$dummy.autocomplete('search', self.$dummy.val());
+ self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
+ } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
+ self.state = self.STATES.UNDEFINED;
+ }
+ },
open_dialog: function() {
- // TODO: take the actual object here
- var dialog = new ns.PickerPopup(this);
+ new ns.PickerPopup(this);
},
init: function() {
var self = this;
self.autocomplete_open = false;
}
});
- /* 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
- */
- this.$dummy.keydown(function(event){
- 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) {
- return true;
- }
- if (event.which == KEY.TAB) {
- event.preventDefault();
- self.handle_changed_text();
- }
- if (event.which == KEY.ENTER) {
- self.handle_changed_text({
- match_one: self.o.action.on_enter_match_one,
- match_many: self.o.action.on_enter_match_many
- });
- return false;
- }
- } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
- var old_options = self.$dummy.autocomplete('option');
- self.$dummy.autocomplete('option', 'minLength', 0);
- self.$dummy.autocomplete('search', self.$dummy.val());
- self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
- } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
- self.state = self.STATES.UNDEFINED;
- }
- });
-
+ this.$dummy.keydown(function(event){ self.handle_keydown(event) });
this.$dummy.on('paste', function(){
setTimeout(function() {
self.handle_changed_text();
}, 1);
});
-
this.$dummy.blur(function(){
window.clearTimeout(self.timer);
self.timer = window.setTimeout(function() { self.annotate_state() }, 100);