1 namespace('kivi.Part', function(ns) {
4 ns.open_history_popup = function() {
5 var id = $("#part_id").val();
7 url: 'controller.pl?action=Part/history&part.id=' + id,
8 dialog: { title: kivi.t8('History') },
12 ns.save = function() {
13 var data = $('#ic').serializeArray();
14 data.push({ name: 'action', value: 'Part/save' });
16 $.post("controller.pl", data, kivi.eval_json_result);
19 ns.use_as_new = function() {
20 var oldid = $("#part_id").val();
21 $('#ic').attr('action', 'controller.pl?action=Part/use_as_new&old_id=' + oldid);
25 ns.delete = function() {
26 var data = $('#ic').serializeArray();
27 data.push({ name: 'action', value: 'Part/delete' });
29 $.post("controller.pl", data, kivi.eval_json_result);
32 ns.reformat_number = function(event) {
33 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
36 ns.set_tab_active_by_index = function (index) {
37 $("#ic_tabs").tabs({active: index})
40 ns.set_tab_active_by_name= function (name) {
41 var index = $('#ic_tabs a[href=#' + name + ']').parent().index();
42 ns.set_tab_active_by_index(index);
45 ns.reorder_items = function(order_by) {
46 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
47 var part_type = $("#part_part_type").val();
50 if (part_type === 'assortment') {
51 $('#assortment thead a img').remove();
52 data = $('#assortment :input').serializeArray();
53 } else if ( part_type === 'assembly') {
54 $('#assembly thead a img').remove();
55 data = $('#assembly :input').serializeArray();
64 src = "image/down.png";
67 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
69 data.push({ name: 'action', value: 'Part/reorder_items' },
70 { name: 'order_by', value: order_by },
71 { name: 'part_type', value: part_type },
72 { name: 'sort_dir', value: dir });
74 $.post("controller.pl", data, kivi.eval_json_result);
77 ns.assortment_recalc = function() {
78 var data = $('#assortment :input').serializeArray();
79 data.push({ name: 'action', value: 'Part/update_item_totals' },
80 { name: 'part_type', value: 'assortment' });
82 $.post("controller.pl", data, kivi.eval_json_result);
85 ns.assembly_recalc = function() {
86 var data = $('#assembly :input').serializeArray();
87 data.push( { name: 'action', value: 'Part/update_item_totals' },
88 { name: 'part_type', value: 'assembly' });
90 $.post("controller.pl", data, kivi.eval_json_result);
93 ns.set_assortment_sellprice = function() {
94 $("#part_sellprice_as_number").val($("#items_sellprice_sum").html());
95 // ns.set_tab_active_by_name('basic_data');
96 // $("#part_sellprice_as_number").focus();
99 ns.set_assortment_lsg_sellprice = function() {
100 $("#items_lsg_sellprice_sum_basic").closest('td').find('input').val($("#items_lsg_sellprice_sum").html());
103 ns.set_assortment_douglas_sellprice = function() {
104 $("#items_douglas_sellprice_sum_basic").closest('td').find('input').val($("#items_douglas_sellprice_sum").html());
107 ns.set_assortment_lastcost = function() {
108 $("#part_lastcost_as_number").val($("#items_lastcost_sum").html());
109 // ns.set_tab_active_by_name('basic_data');
110 // $("#part_lastcost_as_number").focus();
113 ns.set_assembly_sellprice = function() {
114 $("#part_sellprice_as_number").val($("#items_sellprice_sum").html());
115 // ns.set_tab_active_by_name('basic_data');
116 // $("#part_sellprice_as_number").focus();
119 ns.renumber_positions = function() {
120 var part_type = $("#part_part_type").val();
122 if (part_type === 'assortment') {
123 rows = $('.assortment_item_row [name="position"]');
124 } else if ( part_type === 'assembly') {
125 rows = $('.assembly_item_row [name="position"]');
127 $(rows).each(function(idx, elt) {
129 var row = $(elt).closest('tr');
130 if ( idx % 2 === 0 ) {
131 if ( row.hasClass('listrow1') ) {
132 row.removeClass('listrow1');
133 row.addClass('listrow0');
136 if ( row.hasClass('listrow0') ) {
137 row.removeClass('listrow0');
138 row.addClass('listrow1');
144 ns.delete_item_row = function(clicked) {
145 var row = $(clicked).closest('tr');
147 var part_type = $("#part_part_type").val();
148 ns.renumber_positions();
149 if (part_type === 'assortment') {
150 ns.assortment_recalc();
151 } else if ( part_type === 'assembly') {
152 ns.assembly_recalc();
156 ns.add_assortment_item = function() {
157 if ($('#add_assortment_item_id').val() === '') return;
159 $('#row_table_id thead a img').remove();
161 var data = $('#assortment :input').serializeArray();
162 data.push({ name: 'action', value: 'Part/add_assortment_item' },
163 { name: 'part.id', value: $('#part_id').val() },
164 { name: 'part.part_type', value: 'assortment' });
166 $.post("controller.pl", data, kivi.eval_json_result);
169 ns.add_assembly_item = function() {
170 if ($('#add_assembly_item_id').val() === '') return;
172 var data = $('#assembly :input').serializeArray();
173 data.push({ name: 'action', value: 'Part/add_assembly_item' },
174 { name: 'part.id', value: $("#part_id").val() },
175 { name: 'part.part_type', value: 'assortment' });
177 $.post("controller.pl", data, kivi.eval_json_result);
180 ns.redisplay_items = function(data) {
182 var part_type = $("#part_part_type").val();
183 if (part_type === 'assortment') {
184 old_rows = $('.assortment_item_row').detach();
185 } else if ( part_type === 'assembly') {
186 old_rows = $('.assembly_item_row').detach();
189 $(data).each(function(idx, elt) {
190 new_rows.push(old_rows[elt.old_pos - 1]);
192 if (part_type === 'assortment') {
193 $(new_rows).appendTo($('#assortment_items'));
194 } else if ( part_type === 'assembly') {
195 $(new_rows).appendTo($('#assembly_items'));
197 ns.renumber_positions();
200 ns.focus_last_assortment_input = function () {
201 $("#assortment_items tr:last").find('input[type=text]').filter(':visible:first').focus();
204 ns.focus_last_assembly_input = function () {
205 $("#assembly_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
208 ns.show_multi_items_dialog = function(part_type,part_id) {
210 $('#row_table_id thead a img').remove();
213 url: 'controller.pl?action=Part/show_multi_items_dialog',
214 data: { callback: 'Part/add_multi_' + part_type + '_items',
215 callback_data_id: 'ic',
216 'part.part_type': part_type,
219 id: 'jq_multi_items_dialog',
221 title: kivi.t8('Add multiple items'),
229 ns.close_multi_items_dialog = function() {
230 $('#jq_multi_items_dialog').dialog('close');
235 ns.makemodel_renumber_positions = function() {
236 $('.makemodel_row [name="position"]').each(function(idx, elt) {
241 ns.delete_makemodel_row = function(clicked) {
242 var row = $(clicked).closest('tr');
245 ns.makemodel_renumber_positions();
248 ns.add_makemodel_row = function() {
249 if ($('#add_makemodelid').val() === '') return;
251 var data = $('#makemodel_table :input').serializeArray();
252 data.push({ name: 'action', value: 'Part/add_makemodel_row' });
254 $.post("controller.pl", data, kivi.eval_json_result);
257 ns.focus_last_makemodel_input = function () {
258 $("#makemodel_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
262 ns.reload_bin_selection = function() {
263 $.post("controller.pl", { action: 'Part/warehouse_changed', warehouse_id: function(){ return $('#part_warehouse_id').val() } }, kivi.eval_json_result);
281 ns.Picker = function($real, options) {
286 fat_set_item: $real.hasClass(this.CLASSES.FAT_SET_ITEM),
288 on_enter_match_none: function(){ },
289 on_enter_match_one: function(){ $('#update_button').click(); },
290 on_enter_match_many: function(){ self.open_dialog(); }
294 this.real_id = $real.attr('id');
295 this.last_real = $real.val();
296 this.$dummy = $('#' + this.real_id + '_name');
297 this.$part_type = $('#' + this.real_id + '_part_type');
298 this.$classification_id = $('#' + this.real_id + '_classification_id');
299 this.$unit = $('#' + this.real_id + '_unit');
300 this.$convertible_unit = $('#' + this.real_id + '_convertible_unit');
301 this.autocomplete_open = false;
302 this.state = this.STATES.PICKED;
303 this.last_dummy = this.$dummy.val();
304 this.timer = undefined;
309 ns.Picker.prototype = {
311 PICKED: 'partpicker-picked',
312 UNDEFINED: 'partpicker-undefined',
313 FAT_SET_ITEM: 'partpicker_fat_set_item',
315 ajax_data: function(term) {
317 'filter.all:substr:multi::ilike': term,
318 'filter.obsolete': 0,
319 'filter.unit_obj.convertible_to': this.$convertible_unit && this.$convertible_unit.val() ? this.$convertible_unit.val() : '',
320 current: this.$real.val(),
323 if (this.$part_type && this.$part_type.val())
324 data['filter.part_type'] = this.$part_type.val().split(',');
326 if (this.$classification_id && this.$classification_id.val())
327 data['filter.classification_id'] = this.$classification_id.val().split(',');
329 if (this.$unit && this.$unit.val())
330 data['filter.unit'] = this.$unit.val().split(',');
334 set_item: function(item) {
337 this.$real.val(item.id);
338 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
339 this.$dummy.val(item.name ? item.name : item.value);
344 this.state = this.STATES.PICKED;
345 this.last_real = this.$real.val();
346 this.last_dummy = this.$dummy.val();
347 this.$real.trigger('change');
349 if (this.o.fat_set_item && item.id) {
351 url: 'controller.pl?action=Part/show.json',
352 data: { 'part.id': item.id },
353 success: function(rsp) {
354 self.$real.trigger('set_item:PartPicker', rsp);
358 this.$real.trigger('set_item:PartPicker', item);
360 this.annotate_state();
362 make_defined_state: function() {
363 if (this.state == this.STATES.PICKED) {
364 this.annotate_state();
366 } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
369 this.set_item({ id: this.last_real, name: this.last_dummy })
371 this.annotate_state();
373 annotate_state: function() {
374 if (this.state == this.STATES.PICKED)
375 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
376 else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
377 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
379 this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
382 handle_changed_text: function(callbacks) {
385 url: 'controller.pl?action=Part/ajax_autocomplete',
387 data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
388 success: function (data) {
389 if (data.length == 1) {
390 self.set_item(data[0]);
391 if (callbacks && callbacks.match_one) callbacks.match_one(data[0]);
392 } else if (data.length > 1) {
393 self.state = self.STATES.UNDEFINED;
394 if (callbacks && callbacks.match_many) callbacks.match_many(data);
396 self.state = self.STATES.UNDEFINED;
397 if (callbacks && callbacks.match_none) callbacks.match_none();
399 self.annotate_state();
403 /* In case users are impatient and want to skip ahead:
404 * Capture <enter> key events and check if it's a unique hit.
405 * If it is, go ahead and assume it was selected. If it wasn't don't do
406 * anything so that autocompletion kicks in. For <tab> don't prevent
407 * propagation. It would be nice to catch it, but javascript is too stupid
408 * to fire a tab event later on, so we'd have to reimplement the "find
409 * next active element in tabindex order and focus it".
412 * event.which does not contain tab events in keypressed in firefox but will report 0
413 * chrome does not fire keypressed at all on tab or escape
415 handle_keydown: function(event) {
417 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
418 // if string is empty assume they want to delete
419 if (self.$dummy.val() === '') {
422 } else if (self.state == self.STATES.PICKED) {
425 if (event.which == KEY.TAB) {
426 event.preventDefault();
427 self.handle_changed_text();
429 if (event.which == KEY.ENTER) {
430 self.handle_changed_text({
431 match_one: self.o.action.on_enter_match_one,
432 match_many: self.o.action.on_enter_match_many
436 } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
437 var old_options = self.$dummy.autocomplete('option');
438 self.$dummy.autocomplete('option', 'minLength', 0);
439 self.$dummy.autocomplete('search', self.$dummy.val());
440 self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
441 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
442 self.state = self.STATES.UNDEFINED;
445 open_dialog: function() {
446 new ns.PickerPopup(this);
450 this.$dummy.autocomplete({
451 source: function(req, rsp) {
452 $.ajax($.extend(self.o, {
453 url: 'controller.pl?action=Part/ajax_autocomplete',
455 data: self.ajax_data(req.term),
456 success: function (data){ rsp(data) }
459 select: function(event, ui) {
460 self.set_item(ui.item);
462 search: function(event, ui) {
463 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
464 event.preventDefault();
467 self.autocomplete_open = true;
470 self.autocomplete_open = false;
473 this.$dummy.keydown(function(event){ self.handle_keydown(event) });
474 this.$dummy.on('paste', function(){
475 setTimeout(function() {
476 self.handle_changed_text();
479 this.$dummy.blur(function(){
480 window.clearTimeout(self.timer);
481 self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
484 var popup_button = $('<span>').addClass('ppp_popup_button');
485 this.$dummy.after(popup_button);
486 popup_button.click(function() { self.open_dialog() });
489 ns.Picker.prototype.STATES = {
490 PICKED: ns.Picker.prototype.CLASSES.PICKED,
491 UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
494 ns.PickerPopup = function(pp) {
495 this.timer = undefined;
500 ns.PickerPopup.prototype = {
501 open_dialog: function() {
504 url: 'controller.pl?action=Part/part_picker_search',
506 real_id: self.pp.real_id,
507 }, self.pp.ajax_data(this.pp.$dummy.val())),
508 id: 'part_selection',
510 title: kivi.t8('Part picker'),
514 load: function() { self.init_search(); }
516 window.clearTimeout(this.timer);
519 init_search: function() {
521 $('#part_picker_filter').keypress(function(e) { self.result_timer(e) }).focus();
522 $('#no_paginate').change(function() { self.update_results() });
523 this.update_results();
525 update_results: function() {
528 url: 'controller.pl?action=Part/part_picker_result',
530 'real_id': self.pp.$real.val(),
531 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
532 }, self.pp.ajax_data(function(){
533 var val = $('#part_picker_filter').val();
534 return val === undefined ? '' : val
536 success: function(data){
537 $('#part_picker_result').html(data);
542 init_results: function() {
544 $('div.part_picker_part').each(function(){
545 $(this).click(function(){
547 id: $(this).children('input.part_picker_id').val(),
548 name: $(this).children('input.part_picker_description').val(),
549 classification_id: $(this).children('input.part_picker_classification_id').val(),
550 unit: $(this).children('input.part_picker_unit').val(),
551 partnumber: $(this).children('input.part_picker_partnumber').val(),
552 description: $(this).children('input.part_picker_description').val(),
555 self.pp.$dummy.focus();
559 $('#part_selection').keydown(function(e){
560 if (e.which == KEY.ESCAPE) {
562 self.pp.$dummy.focus();
566 result_timer: function(event) {
568 if (!$('no_paginate').prop('checked')) {
569 if (event.keyCode == KEY.PAGE_UP) {
570 $('#part_picker_result a.paginate-prev').click();
573 if (event.keyCode == KEY.PAGE_DOWN) {
574 $('#part_picker_result a.paginate-next').click();
578 window.clearTimeout(this.timer);
579 if (event.which == KEY.ENTER) {
580 self.update_results();
582 this.timer = window.setTimeout(function() { self.update_results() }, 100);
585 close_popup: function() {
586 $('#part_selection').dialog('close');
590 ns.reinit_widgets = function() {
591 kivi.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
592 if (!$(elt).data('part_picker'))
593 $(elt).data('part_picker', new kivi.Part.Picker($(elt)));
597 ns.init = function() {
604 // TODO: allow units for assortment items
605 $('#add_assortment_item_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
607 $('#ic').on('focusout', '.reformat_number', function(event) {
608 ns.reformat_number(event);
611 $('.add_assortment_item_input').keydown(function(event) {
612 if(event.keyCode == 13) {
613 event.preventDefault();
614 if ($("input[name='add_items[+].parts_id']").val() !== '' ) {
615 kivi.Part.show_multi_items_dialog("assortment");
616 // ns.add_assortment_item();
622 $('.add_assembly_item_input').keydown(function(event) {
623 if(event.keyCode == 13) {
624 event.preventDefault();
625 if ($("input[name='add_items[+].parts_id']").val() !== '' ) {
626 kivi.Part.show_multi_items_dialog("assortment");
627 // ns.add_assembly_item();
633 $('.add_makemodel_input').keydown(function(event) {
634 if(event.keyCode == 13) {
635 event.preventDefault();
636 ns.add_makemodel_row();
641 $('#part_warehouse_id').change(kivi.Part.reload_bin_selection);