Revision 681ec24c
Von Sven Schöling vor mehr als 11 Jahren hinzugefügt
js/autocomplete_part.js | ||
---|---|---|
1 | 1 |
namespace('kivi', function(k){ |
2 | 2 |
k.part_picker = function($real, options) { |
3 |
o = $.extend({ |
|
3 |
var o = $.extend({
|
|
4 | 4 |
limit: 20, |
5 | 5 |
delay: 50, |
6 | 6 |
}, options); |
7 |
|
|
7 |
var STATES = { |
|
8 |
UNIQUE: 1, |
|
9 |
UNDEFINED: 0, |
|
10 |
} |
|
8 | 11 |
var real_id = $real.attr('id'); |
9 | 12 |
var $dummy = $('#' + real_id + '_name'); |
10 | 13 |
var $type = $('#' + real_id + '_type'); |
11 | 14 |
var $column = $('#' + real_id + '_column'); |
15 |
var state = STATES.PICKED; |
|
16 |
var last_real = $real.val(); |
|
17 |
var last_dummy = $dummy.val(); |
|
12 | 18 |
var open_dialog = function(){ |
13 | 19 |
open_jqm_window({ |
14 | 20 |
url: 'controller.pl', |
... | ... | |
24 | 30 |
return true; |
25 | 31 |
}; |
26 | 32 |
|
27 |
var ajax_data = function(term) {
|
|
33 |
function ajax_data(term) {
|
|
28 | 34 |
return { |
29 | 35 |
term: term, |
30 | 36 |
type: function() { return $type.val() }, |
... | ... | |
34 | 40 |
} |
35 | 41 |
} |
36 | 42 |
|
37 |
var set_item = function (item) {
|
|
43 |
function set_item (item) {
|
|
38 | 44 |
if (item.id) { |
39 | 45 |
$real.val(item.id); |
40 | 46 |
// autocomplete ui has name, ajax items have description |
... | ... | |
43 | 49 |
$real.val(''); |
44 | 50 |
$dummy.val(''); |
45 | 51 |
} |
52 |
state = STATES.PICKED; |
|
53 |
last_real = $real.val(); |
|
54 |
last_dummy = $dummy.val(); |
|
55 |
console.log('last:' + last_real + ' dummy:' + last_dummy); |
|
56 |
} |
|
57 |
|
|
58 |
function make_defined_state () { |
|
59 |
if (state == STATES.PICKED) |
|
60 |
return true |
|
61 |
else if (state == STATES.UNDEFINED && $dummy.val() == '') |
|
62 |
set_item({}) |
|
63 |
else |
|
64 |
set_item({ id: last_real, name: last_dummy }) |
|
46 | 65 |
} |
47 | 66 |
|
48 | 67 |
$dummy.autocomplete({ |
... | ... | |
72 | 91 |
if ($dummy.val() == '') { |
73 | 92 |
set_item({}); |
74 | 93 |
return true; |
94 |
} else if (state == STATES.PICKED) { |
|
95 |
return true; |
|
75 | 96 |
} |
76 | 97 |
$.ajax({ |
77 | 98 |
url: 'controller.pl?action=Part/ajax_autocomplete', |
... | ... | |
83 | 104 |
if (event.keyCode == 13) |
84 | 105 |
$('#update_button').click(); |
85 | 106 |
} else { |
86 |
if (event.keyCode == 13)
|
|
107 |
if (event.keyCode == 13) |
|
87 | 108 |
open_dialog(); |
88 | 109 |
else |
89 |
set_item({});
|
|
110 |
make_defined_state();
|
|
90 | 111 |
} |
91 | 112 |
} |
92 | 113 |
}); |
93 | 114 |
if (event.keyCode == 13) |
94 | 115 |
return false; |
95 |
}; |
|
116 |
} else { |
|
117 |
state = STATES.UNDEFINED; |
|
118 |
} |
|
96 | 119 |
}); |
97 | 120 |
|
98 |
$dummy.blur(function(){ |
|
99 |
if ($dummy.val() == '') |
|
100 |
$real.val(''); |
|
101 |
}); |
|
121 |
// $dummy.blur(make_defined_state); // blur triggers also on open_jqm_dialog |
|
102 | 122 |
|
103 | 123 |
// now add a picker div after the original input |
104 | 124 |
var pcont = $('<span>').addClass('position-absolute'); |
Auch abrufbar als: Unified diff
internal state, besseres zurücksetzen auf den letzten bekannten zustand