Part Picker
[kivitendo-erp.git] / 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       },
21       limit: 20,
22       delay: 50,
23       select: function(event, ui) {
24         $(real).val(ui.item.id);
25         $dummy.val(ui.item.name);
26       },
27     });
28     /*  In case users are impatient and want to skip ahead:
29      *  Capture <enter> key events and check if it's a unique hit.
30      *  If it is, go ahead and assume it was selected. If it wasn't don't do
31      *  anything so that autocompletion kicks in.  For <tab> don't prevent
32      *  propagation. It would be nice to catch it, but javascript is too stupid
33      *  to fire a tab event later on, so we'd have to reimplement the "find
34      *  next active element in tabindex order and focus it".
35      */
36     $dummy.keypress(function(event){
37       if (event.keyCode == 13 || event.keyCode == 9) { // enter or tab or tab
38         // if string is empty asume they want to delete
39         if ($dummy.val() == '') {
40           $(real).val('');
41           return true;
42         }
43         $.ajax({
44           url: 'controller.pl?action=Part/ajax_autocomplete',
45           dataType: "json",
46           data: {
47             term: $dummy.val(),
48             type: function() { return $type.val() },
49             column: function() { return $column.val()===undefined ? '' : $column.val() },
50             current: function() { return real.value },
51             obsolete: 0,
52           },
53           success: function (data){
54             // only one
55             if (data.length == 1) {
56               $(real).val(data[0].id);
57               $dummy.val(data[0].description);
58               if (event.keyCode == 13)
59                 $('#update_button').click();
60             }
61           }
62         });
63         if (event.keyCode == 13)
64           return false;
65       };
66     });
67
68     $dummy.blur(function(){
69       if ($dummy.val() == '')
70         $(real).val('');
71     })
72
73     // now add a picker div after the original input
74     var pcont  = $('<span>').addClass('position-absolute');
75     var picker = $('<div>');
76     $dummy.after(pcont);
77     pcont.append(picker);
78     picker.addClass('icon16 CRM--Schnellsuche').click(function(){
79       open_jqm_window({
80         url: 'controller.pl',
81         data: {
82           action: 'Part/part_picker_search',
83           real_id: function() { return $(real).attr('id') },
84           'filter.all:substr::ilike': function(){ return $dummy.val() },
85           'filter.type': function(){ return $type.val() },
86           'column': function(){ return $column.val() },
87           'real_id': function() { return real.id },
88         },
89         id: 'part_selection',
90       });
91       return true;
92     });
93   });
94 })