Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 854fa965

Von Sven Schöling vor mehr als 11 Jahren hinzugefügt

  • ID 854fa965ce1e3f3c203d8a68e6b898cc8384757f
  • Vorgänger a8617d4c
  • Nachfolger 681ec24c

Diverse kleine Verbesserungen und namespace Kapselung

Unterschiede anzeigen:

js/autocomplete_part.js
1
$(function(){
2
  $('input.part_autocomplete').each(function(i,real){
3
    var $dummy  = $('#' + real.id + '_name');
4
    var $type   = $('#' + real.id + '_type');
5
    var $column = $('#' + real.id + '_column');
6
    $dummy.autocomplete({
7
      source: function(req, rsp) {
8
        $.ajax({
9
          url: 'controller.pl?action=Part/ajax_autocomplete',
10
          dataType: "json",
11
          data: {
12
            term: req.term,
13
            type: function() { return $type.val() },
14
            column: function() { return $column.val()===undefined ? '' : $column.val() },
15
            current: function() { return real.value },
16
            obsolete: 0,
17
          },
18
          success: function (data){ rsp(data) }
19
        });
20
      },
1
namespace('kivi', function(k){
2
  k.part_picker = function($real, options) {
3
    o = $.extend({
21 4
      limit: 20,
22 5
      delay: 50,
23
      select: function(event, ui) {
24
        $(real).val(ui.item.id);
25
        $dummy.val(ui.item.name);
26
      },
27
    });
6
    }, options);
28 7

  
8
    var real_id = $real.attr('id');
9
    var $dummy  = $('#' + real_id + '_name');
10
    var $type   = $('#' + real_id + '_type');
11
    var $column = $('#' + real_id + '_column');
29 12
    var open_dialog = function(){
30 13
      open_jqm_window({
31 14
        url: 'controller.pl',
32 15
        data: {
33 16
          action: 'Part/part_picker_search',
34
          real_id: function() { return $(real).attr('id') },
17
          real_id: real_id,
35 18
          'filter.all:substr::ilike': function(){ return $dummy.val() },
36
          'filter.type': function(){ return $type.val() },
37
          'column': function(){ return $column.val() },
38
          'real_id': function() { return real.id },
19
          'filter.type':              function(){ return $type.val() },
20
          'column':                   function(){ return $column.val() },
39 21
        },
40 22
        id: 'part_selection',
41 23
      });
42 24
      return true;
43 25
    };
26

  
27
    var ajax_data = function(term) {
28
      return {
29
        term:     term,
30
        type:     function() { return $type.val() },
31
        column:   function() { return $column.val()===undefined ? '' : $column.val() },
32
        current:  function() { return $real.val() },
33
        obsolete: 0,
34
      }
35
    }
36

  
37
    var set_item = function (item) {
38
      if (item.id) {
39
        $real.val(item.id);
40
        // autocomplete ui has name, ajax items have description
41
        $dummy.val(item.name ? item.name : item.description);
42
      } else {
43
        $real.val('');
44
        $dummy.val('');
45
      }
46
    }
47

  
48
    $dummy.autocomplete({
49
      source: function(req, rsp) {
50
        $.ajax($.extend(o, {
51
          url:      'controller.pl?action=Part/ajax_autocomplete',
52
          dataType: "json",
53
          data:     ajax_data(req.term),
54
          success:  function (data){ rsp(data) }
55
        }));
56
      },
57
      select: function(event, ui) {
58
        set_item(ui.item);
59
      },
60
    });
44 61
    /*  In case users are impatient and want to skip ahead:
45 62
     *  Capture <enter> key events and check if it's a unique hit.
46 63
     *  If it is, go ahead and assume it was selected. If it wasn't don't do
......
51 68
     */
52 69
    $dummy.keypress(function(event){
53 70
      if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab
54
        // if string is empty asume they want to delete
71
        // if string is empty assume they want to delete
55 72
        if ($dummy.val() == '') {
56
          $(real).val('');
73
          set_item({});
57 74
          return true;
58 75
        }
59 76
        $.ajax({
60 77
          url: 'controller.pl?action=Part/ajax_autocomplete',
61 78
          dataType: "json",
62
          data: {
63
            term: $dummy.val(),
64
            type: function() { return $type.val() },
65
            column: function() { return $column.val()===undefined ? '' : $column.val() },
66
            current: function() { return real.value },
67
            obsolete: 0,
68
          },
79
          data: ajax_data($dummy.val()),
69 80
          success: function (data){
70
            // only one
71 81
            if (data.length == 1) {
72
              $(real).val(data[0].id);
73
              $dummy.val(data[0].description);
82
              set_item(data[0]);
74 83
              if (event.keyCode == 13)
75 84
                $('#update_button').click();
76 85
            } else {
77
              open_dialog();
86
              if (event.keyCode == 13)
87
                open_dialog();
88
              else
89
                set_item({});
78 90
            }
79 91
          }
80 92
        });
......
85 97

  
86 98
    $dummy.blur(function(){
87 99
      if ($dummy.val() == '')
88
        $(real).val('');
100
        $real.val('');
89 101
    });
90 102

  
91 103
    // now add a picker div after the original input
......
94 106
    $dummy.after(pcont);
95 107
    pcont.append(picker);
96 108
    picker.addClass('icon16 CRM--Schnellsuche').click(open_dialog);
97
  });
98
})
109
  }
110
});
111

  
112
$(function(){
113
  $('input.part_autocomplete').each(function(i,real){
114
    kivi.part_picker($(real));
115
  })
116
});

Auch abrufbar als: Unified diff