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',
28 }, $real.data('project-picker-data'), options);
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 open_dialog () {
43 url: 'controller.pl?action=Project/project_picker_search',
44 // data that can be accessed in template project_picker_search via FORM.boss
45 data: $.extend({ // add id of part to the rest of the data in ajax_data, e.g. no_paginate, booked, ...
48 }, ajax_data($dummy.val())),
49 id: 'project_selection',
51 title: k.t8('Project picker'),
55 load: function() { init_search(); }
57 window.clearTimeout(timer);
61 function init_search() {
62 $('#project_picker_filter').keypress(function(e) { result_timer(e) }).focus();
63 $('#no_paginate').change(function() { update_results() });
64 $('#project_picker_clear_filter').click(function() {
65 $('#project_picker_filter').val('').focus();
70 function ajax_data(term) {
72 'filter.all:substr:multi::ilike': term,
73 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
78 data['filter.customer_id'] = o.customer_id.split(',');
81 if (o.active === 'active') data['filter.active'] = 'active';
82 if (o.active === 'inactive') data['filter.active'] = 'inactive';
85 data['filter.active'] = 'active'; // default
89 if (o.valid === 'valid') data['filter.valid'] = 'valid';
90 if (o.valid === 'invalid') data['filter.valid'] = 'invalid';
93 data['filter.valid'] = 'valid'; // default
96 if (o.description_style)
97 data['description_style'] = o.description_style;
102 function set_item (item) {
105 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
106 $dummy.val(item.name ? item.name : item.value);
111 state = STATES.PICKED;
112 last_real = $real.val();
113 last_dummy = $dummy.val();
115 $real.trigger('change');
116 $real.trigger('set_item:ProjectPicker', item);
121 function make_defined_state () {
122 if (state == STATES.PICKED) {
125 } else if (state == STATES.UNDEFINED && $dummy.val() === '')
128 set_item({ id: last_real, name: last_dummy })
133 function annotate_state () {
134 if (state == STATES.PICKED)
135 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
136 else if (state == STATES.UNDEFINED && $dummy.val() === '')
137 $dummy.removeClass(STATES.UNDEFINED).addClass(STATES.PICKED);
139 $dummy.addClass(STATES.UNDEFINED).removeClass(STATES.PICKED);
143 function update_results () {
145 url: 'controller.pl?action=Project/project_picker_result',
147 'real_id': $real.val(),
148 }, ajax_data(function(){ var val = $('#project_picker_filter').val(); return val === undefined ? '' : val })),
149 success: function(data){
150 $('#project_picker_result').html(data);
155 function result_timer (event) {
156 if (!$('no_paginate').prop('checked')) {
157 if (event.keyCode == KEY.PAGE_UP) {
158 $('#project_picker_result a.paginate-prev').click();
161 if (event.keyCode == KEY.PAGE_DOWN) {
162 $('#project_picker_result a.paginate-next').click();
166 window.clearTimeout(timer);
167 timer = window.setTimeout(update_results, 100);
170 function close_popup() {
171 $('#project_selection').dialog('close');
174 function handle_changed_text(callbacks) {
176 url: 'controller.pl?action=Project/ajax_autocomplete',
178 data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
179 success: function (data) {
180 if (data.length == 1) {
182 if (callbacks && callbacks.match_one) callbacks.match_one(data[0]);
183 } else if (data.length > 1) {
184 state = STATES.UNDEFINED;
185 if (callbacks && callbacks.match_many) callbacks.match_many(data);
187 state = STATES.UNDEFINED;
188 if (callbacks &&callbacks.match_none) callbacks.match_none();
195 $dummy.autocomplete({
196 source: function(req, rsp) {
198 url: 'controller.pl?action=Project/ajax_autocomplete',
200 data: ajax_data(req.term),
201 success: function (data){ rsp(data) }
204 select: function(event, ui) {
207 search: function(event, ui) {
208 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
209 event.preventDefault();
212 /* In case users are impatient and want to skip ahead:
213 * Capture <enter> key events and check if it's a unique hit.
214 * If it is, go ahead and assume it was selected. If it wasn't don't do
215 * anything so that autocompletion kicks in. For <tab> don't prevent
216 * propagation. It would be nice to catch it, but javascript is too stupid
217 * to fire a tab event later on, so we'd have to reimplement the "find
218 * next active element in tabindex order and focus it".
221 * event.which does not contain tab events in keypressed in firefox but will report 0
222 * chrome does not fire keypressed at all on tab or escape
224 $dummy.keydown(function(event){
225 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
226 // if string is empty assume they want to delete
227 if ($dummy.val() === '') {
230 } else if (state == STATES.PICKED) {
233 if (event.which == KEY.TAB) {
234 event.preventDefault();
235 handle_changed_text();
237 if (event.which == KEY.ENTER) {
238 handle_changed_text({
239 match_one: function(){$('#update_button').click();},
243 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
244 state = STATES.UNDEFINED;
248 $dummy.on('paste', function(){
249 setTimeout(function() {
250 handle_changed_text();
254 $dummy.blur(function(){
255 window.clearTimeout(timer);
256 timer = window.setTimeout(annotate_state, 100);
259 // now add a picker div after the original input
260 var popup_button = $('<span>').addClass('ppp_popup_button');
261 $dummy.after(popup_button);
262 popup_button.click(open_dialog);
264 real: function() { return $real },
265 dummy: function() { return $dummy },
266 update_results: update_results,
267 result_timer: result_timer,
269 reset: make_defined_state,
270 is_defined_state: function() { return state == STATES.PICKED },
271 init_results: function() {
272 $('div.project_picker_project').each(function(){
273 $(this).click(function(){
275 id: $(this).children('input.project_picker_id').val(),
276 name: $(this).children('input.project_picker_description').val(),
282 $('#project_selection').keydown(function(e){
283 if (e.which == KEY.ESCAPE) {
290 $real.data('project_picker', pp);
296 $('input.project_autocomplete').each(function(i,real){
297 kivi.ProjectPicker($(real));