]> wagnertech.de Git - mfinanz.git/blobdiff - js/kivi.Part.js
kivi.Part.js: Datenübergabe Picker<->Presenter auf data umgestellt
[mfinanz.git] / js / kivi.Part.js
index 86c49a2e855db6ef7f292b54c9fb9a89afb346ea..d7bbd2ffdcd1411e781255726f73b9878e0c7797 100644 (file)
@@ -283,21 +283,16 @@ namespace('kivi.Part', function(ns) {
     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();
@@ -310,24 +305,25 @@ namespace('kivi.Part', function(ns) {
     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;
     },
@@ -400,9 +396,50 @@ namespace('kivi.Part', function(ns) {
         }
       });
     },
+    /*  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;
@@ -429,54 +466,12 @@ namespace('kivi.Part', function(ns) {
           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);