Implementiert FB0017 Position nach Art.Nummer in Lieferschein sortieren
[kivitendo-erp.git] / js / autocomplete_part.js
index 5c6b65a..8c304bb 100644 (file)
@@ -1,38 +1,65 @@
 namespace('kivi', function(k){
-  k.PartPickerCache = { }
   k.PartPicker = function($real, options) {
+    // short circuit in case someone double inits us
+    if ($real.data("part_picker"))
+      return $real.data("part_picker");
+
+    var KEY = {
+      ESCAPE: 27,
+      ENTER:  13,
+      TAB:    9,
+      LEFT:   37,
+      RIGHT:  39,
+      PAGE_UP: 33,
+      PAGE_DOWN: 34,
+    };
+    var CLASSES = {
+      PICKED:       'partpicker-picked',
+      UNDEFINED:    'partpicker-undefined',
+      FAT_SET_ITEM: 'partpicker_fat_set_item',
+    }
     var o = $.extend({
       limit: 20,
       delay: 50,
+      fat_set_item: $real.hasClass(CLASSES.FAT_SET_ITEM),
     }, options);
     var STATES = {
-      UNIQUE: 1,
-      UNDEFINED: 0,
+      PICKED:    CLASSES.PICKED,
+      UNDEFINED: CLASSES.UNDEFINED
     }
     var real_id = $real.attr('id');
     var $dummy  = $('#' + real_id + '_name');
     var $type   = $('#' + real_id + '_type');
     var $unit   = $('#' + real_id + '_unit');
-    var $column = $('#' + real_id + '_column');
+    var $convertible_unit = $('#' + real_id + '_convertible_unit');
     var state   = STATES.PICKED;
     var last_real = $real.val();
     var last_dummy = $dummy.val();
-    var open_dialog = function(){
-      open_jqm_window({
+    var timer;
+
+    function open_dialog () {
+      k.popup_dialog({
         url: 'controller.pl?action=Part/part_picker_search',
         data: $.extend({
           real_id: real_id,
         }, ajax_data($dummy.val())),
         id: 'part_selection',
+        dialog: {
+          title: k.t8('Part picker'),
+          width: 800,
+          height: 800,
+        }
       });
+      window.clearTimeout(timer);
       return true;
-    };
+    }
 
     function ajax_data(term) {
       var data = {
-        'filter.all:substr::ilike': term,
+        'filter.all:substr:multi::ilike': term,
         'filter.obsolete': 0,
-        column:   $column.val()===undefined ? '' : $column.val(),
+        'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
+        no_paginate:  $('#no_paginate').prop('checked') ? 1 : 0,
         current:  $real.val(),
       };
 
@@ -48,8 +75,8 @@ namespace('kivi', function(k){
     function set_item (item) {
       if (item.id) {
         $real.val(item.id);
-        // autocomplete ui has name, ajax items have description
-        $dummy.val(item.name ? item.name : item.description);
+        // autocomplete ui has name, use the value for ajax items, which contains displayable_name
+        $dummy.val(item.name ? item.name : item.value);
       } else {
         $real.val('');
         $dummy.val('');
@@ -57,16 +84,45 @@ namespace('kivi', function(k){
       state = STATES.PICKED;
       last_real = $real.val();
       last_dummy = $dummy.val();
+      last_unverified_dummy = $dummy.val();
       $real.trigger('change');
+
+      if (o.fat_set_item && item.id) {
+        $.ajax({
+          url: 'controller.pl?action=Part/show.json',
+          data: { id: item.id },
+          success: function(rsp) {
+            $real.trigger('set_item:PartPicker', rsp);
+          },
+        });
+      } else {
+        $real.trigger('set_item:PartPicker', item);
+      }
+      annotate_state();
     }
 
     function make_defined_state () {
-      if (state == STATES.PICKED)
+      if (state == STATES.PICKED) {
+        annotate_state();
         return true
-      else if (state == STATES.UNDEFINED && $dummy.val() == '')
+      else if (state == STATES.UNDEFINED && $dummy.val() == '')
         set_item({})
-      else
+      else {
+        last_unverified_dummy = $dummy.val();
         set_item({ id: last_real, name: last_dummy })
+      }
+      annotate_state();
+    }
+
+    function annotate_state () {
+      if (state == STATES.PICKED)
+        $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
+      else if (state == STATES.UNDEFINED && $dummy.val() == '')
+        $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
+      else {
+        last_unverified_dummy = $dummy.val();
+        $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
+      }
     }
 
     function update_results () {
@@ -79,8 +135,23 @@ namespace('kivi', function(k){
       });
     };
 
+    function result_timer (event) {
+      if (!$('no_paginate').prop('checked')) {
+        if (event.keyCode == KEY.PAGE_UP) {
+          $('#part_picker_result a.paginate-prev').click();
+          return;
+        }
+        if (event.keyCode == KEY.PAGE_DOWN) {
+          $('#part_picker_result a.paginate-next').click();
+          return;
+        }
+      }
+      window.clearTimeout(timer);
+      timer = window.setTimeout(update_results, 100);
+    }
+
     function close_popup() {
-      $('#part_selection').jqmClose()
+      $('#part_selection').dialog('close');
     };
 
     $dummy.autocomplete({
@@ -104,8 +175,12 @@ namespace('kivi', function(k){
      *  to fire a tab event later on, so we'd have to reimplement the "find
      *  next active element in tabindex order and focus it".
      */
-    $dummy.keypress(function(event){
-      if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab
+    /* 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
+     */
+    $dummy.keydown(function(event){
+      if (event.which == KEY.ENTER || event.which == KEY.TAB) {
         // if string is empty assume they want to delete
         if ($dummy.val() == '') {
           set_item({});
@@ -113,75 +188,82 @@ namespace('kivi', function(k){
         } else if (state == STATES.PICKED) {
           return true;
         }
+        if (event.which == KEY.TAB) event.preventDefault();
         $.ajax({
           url: 'controller.pl?action=Part/ajax_autocomplete',
           dataType: "json",
           data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
-          success: function (data){
+          success: function (data) {
             if (data.length == 1) {
               set_item(data[0]);
-              if (event.keyCode == 13)
+              if (event.which == KEY.ENTER)
                 $('#update_button').click();
             } else if (data.length > 1) {
-             if (event.keyCode == 13)
+             if (event.which == KEY.ENTER)
                 open_dialog();
-              else
-                make_defined_state();
             } else {
-              if (event.keyCode == 9)
-                make_defined_state();
             }
+            annotate_state();
           }
         });
-        if (event.keyCode == 13)
+        if (event.which == KEY.ENTER)
           return false;
       } else {
         state = STATES.UNDEFINED;
       }
     });
 
-//    $dummy.blur(make_defined_state);  // blur triggers also on open_jqm_dialog
+    $dummy.blur(function(){
+      window.clearTimeout(timer);
+      timer = window.setTimeout(annotate_state, 100);
+    });
 
     // now add a picker div after the original input
     var pcont  = $('<span>').addClass('position-absolute');
     var picker = $('<div>');
     $dummy.after(pcont);
     pcont.append(picker);
-    picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog);
+    picker.addClass('icon16 search').click(open_dialog);
 
-    return {
+    var pp = {
       real:           function() { return $real },
       dummy:          function() { return $dummy },
       type:           function() { return $type },
       unit:           function() { return $unit },
-      column:         function() { return $column },
+      convertible_unit: function() { return $convertible_unit },
       update_results: update_results,
+      result_timer:   result_timer,
       set_item:       set_item,
       reset:          make_defined_state,
+      is_defined_state: function() { return state == STATES.PICKED },
       init_results:    function () {
         $('div.part_picker_part').each(function(){
           $(this).click(function(){
             set_item({
-              name: $(this).children('input.part_picker_description').val(),
               id:   $(this).children('input.part_picker_id').val(),
+              name: $(this).children('input.part_picker_description').val(),
+              unit: $(this).children('input.part_picker_unit').val(),
             });
             close_popup();
+            $dummy.focus();
             return true;
           });
         });
-        $('#part_selection').keypress(function(e){
-           if (e.keyCode == 27) { // escape
+        $('#part_selection').keydown(function(e){
+           if (e.which == KEY.ESCAPE) {
              close_popup();
              $dummy.focus();
            }
         });
       }
     }
+    $real.data('part_picker', pp);
+    return pp;
   }
 });
 
 $(function(){
   $('input.part_autocomplete').each(function(i,real){
-    kivi.PartPickerCache[real.id] = new kivi.PartPicker($(real));
+    kivi.PartPicker($(real));
   })
 });