1 namespace('kivi', function(k){
4 k.ProjectPicker = function($real, options) {
5 // short circuit in case someone double inits us
6 if ($real.data("project_picker"))
7 return $real.data("project_picker");
22 PICKED: 'projectpicker-picked',
23 UNDEFINED: 'projectpicker-undefined',
30 PICKED: CLASSES.PICKED,
31 UNDEFINED: CLASSES.UNDEFINED
33 var real_id = $real.attr('id');
34 var $dummy = $('#' + real_id + '_name');
35 var $customer_id = $('#' + real_id + '_customer_id');
36 var state = STATES.PICKED;
37 var last_real = $real.val();
38 var last_dummy = $dummy.val();
41 function ajax_data(term) {
43 'filter.all:substr:multi::ilike': term,
44 'filter.valid': 'valid',
45 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
49 if ($customer_id && $customer_id.val())
50 data['filter.customer_id'] = $customer_id.val().split(',');
55 function set_item (item) {
58 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
59 $dummy.val(item.name ? item.name : item.value);
64 state = STATES.PICKED;
65 last_real = $real.val();
66 last_dummy = $dummy.val();
68 $real.trigger('change');
69 $real.trigger('set_item:ProjectPicker', item);
74 function make_defined_state () {
75 if (state == STATES.PICKED) {
78 } else if (state == STATES.UNDEFINED && $dummy.val() === '')
81 set_item({ id: last_real, name: last_dummy })
86 function annotate_state () {
87 if (state == STATES.PICKED)
88 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
89 else if (state == STATES.UNDEFINED && $dummy.val() === '')
90 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
92 $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
96 function update_results () {
98 url: 'controller.pl?action=Project/project_picker_result',
100 'real_id': $real.val(),
101 }, ajax_data(function(){ var val = $('#project_picker_filter').val(); return val === undefined ? '' : val })),
102 success: function(data){ $('#project_picker_result').html(data) }
106 function result_timer (event) {
107 if (!$('no_paginate').prop('checked')) {
108 if (event.keyCode == KEY.PAGE_UP) {
109 $('#project_picker_result a.paginate-prev').click();
112 if (event.keyCode == KEY.PAGE_DOWN) {
113 $('#project_picker_result a.paginate-next').click();
117 window.clearTimeout(timer);
118 timer = window.setTimeout(update_results, 100);
121 function handle_changed_text(callbacks) {
123 url: 'controller.pl?action=Project/ajax_autocomplete',
125 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
126 success: function (data) {
127 if (data.length == 1) {
129 if (callbacks && callbacks.match_one) callbacks.match_one(data[0]);
130 } else if (data.length > 1) {
131 state = STATES.UNDEFINED;
132 if (callbacks && callbacks.match_many) callbacks.match_many(data);
134 state = STATES.UNDEFINED;
135 if (callbacks &&callbacks.match_none) callbacks.match_none();
142 $dummy.autocomplete({
143 source: function(req, rsp) {
145 url: 'controller.pl?action=Project/ajax_autocomplete',
147 data: ajax_data(req.term),
148 success: function (data){ rsp(data) }
151 select: function(event, ui) {
154 search: function(event, ui) {
155 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
156 event.preventDefault();
159 /* In case users are impatient and want to skip ahead:
160 * Capture <enter> key events and check if it's a unique hit.
161 * If it is, go ahead and assume it was selected. If it wasn't don't do
162 * anything so that autocompletion kicks in. For <tab> don't prevent
163 * propagation. It would be nice to catch it, but javascript is too stupid
164 * to fire a tab event later on, so we'd have to reimplement the "find
165 * next active element in tabindex order and focus it".
168 * event.which does not contain tab events in keypressed in firefox but will report 0
169 * chrome does not fire keypressed at all on tab or escape
171 $dummy.keydown(function(event){
172 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
173 // if string is empty assume they want to delete
174 if ($dummy.val() === '') {
177 } else if (state == STATES.PICKED) {
180 if (event.which == KEY.TAB) {
181 event.preventDefault();
182 handle_changed_text();
184 if (event.which == KEY.ENTER) {
185 handle_changed_text({
186 match_one: function(){$('#update_button').click();},
190 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
191 state = STATES.UNDEFINED;
195 $dummy.on('paste', function(){
196 setTimeout(function() {
197 handle_changed_text();
201 $dummy.blur(function(){
202 window.clearTimeout(timer);
203 timer = window.setTimeout(annotate_state, 100);
206 // now add a picker div after the original input
207 var pcont = $('<span>').addClass('position-absolute');
208 var picker = $('<div>');
210 pcont.append(picker);
213 real: function() { return $real },
214 dummy: function() { return $dummy },
215 type: function() { return $type },
216 customer_id: function() { return $customer_id },
217 update_results: update_results,
218 result_timer: result_timer,
220 reset: make_defined_state,
221 is_defined_state: function() { return state == STATES.PICKED },
222 init_results: function () {
223 $('div.project_picker_project').each(function(){
224 $(this).click(function(){
226 id: $(this).children('input.project_picker_id').val(),
227 name: $(this).children('input.project_picker_description').val(),
233 $('#project_selection').keydown(function(e){
234 if (e.which == KEY.ESCAPE) {
240 $real.data('project_picker', pp);
246 $('input.project_autocomplete').each(function(i,real){
247 kivi.ProjectPicker($(real));