X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/5d711a25d9257690164f396b25f57095776790d6..426a5bf3c8b6a5cd2509699ebace0d24e614f2b7:/js/kivi.Part.js diff --git a/js/kivi.Part.js b/js/kivi.Part.js index cd97a3c98..d7bbd2ffd 100644 --- a/js/kivi.Part.js +++ b/js/kivi.Part.js @@ -1,4 +1,5 @@ namespace('kivi.Part', function(ns) { + 'use strict'; ns.open_history_popup = function() { var id = $("#part_id").val(); @@ -52,7 +53,7 @@ namespace('kivi.Part', function(ns) { } else if ( part_type === 'assembly') { $('#assembly thead a img').remove(); data = $('#assembly :input').serializeArray(); - }; + } var src; if (dir == "1") { @@ -122,7 +123,7 @@ namespace('kivi.Part', function(ns) { rows = $('.assortment_item_row [name="position"]'); } else if ( part_type === 'assembly') { rows = $('.assembly_item_row [name="position"]'); - }; + } $(rows).each(function(idx, elt) { $(elt).html(idx+1); var row = $(elt).closest('tr'); @@ -130,13 +131,13 @@ namespace('kivi.Part', function(ns) { if ( row.hasClass('listrow1') ) { row.removeClass('listrow1'); row.addClass('listrow0'); - }; + } } else { if ( row.hasClass('listrow0') ) { row.removeClass('listrow0'); row.addClass('listrow1'); - }; - }; + } + } }); }; @@ -149,7 +150,7 @@ namespace('kivi.Part', function(ns) { ns.assortment_recalc(); } else if ( part_type === 'assembly') { ns.assembly_recalc(); - }; + } }; ns.add_assortment_item = function() { @@ -183,7 +184,7 @@ namespace('kivi.Part', function(ns) { old_rows = $('.assortment_item_row').detach(); } else if ( part_type === 'assembly') { old_rows = $('.assembly_item_row').detach(); - }; + } var new_rows = []; $(data).each(function(idx, elt) { new_rows.push(old_rows[elt.old_pos - 1]); @@ -192,7 +193,7 @@ namespace('kivi.Part', function(ns) { $(new_rows).appendTo($('#assortment_items')); } else if ( part_type === 'assembly') { $(new_rows).appendTo($('#assembly_items')); - }; + } ns.renumber_positions(); }; @@ -262,6 +263,337 @@ namespace('kivi.Part', function(ns) { $.post("controller.pl", { action: 'Part/warehouse_changed', warehouse_id: function(){ return $('#part_warehouse_id').val() } }, kivi.eval_json_result); } + var KEY = { + TAB: 9, + ENTER: 13, + SHIFT: 16, + CTRL: 17, + ALT: 18, + ESCAPE: 27, + PAGE_UP: 33, + PAGE_DOWN: 34, + LEFT: 37, + UP: 38, + RIGHT: 39, + DOWN: 40, + }; + + ns.Picker = function($real, options) { + var self = this; + this.o = $.extend({ + limit: 20, + delay: 50, + action: { + on_enter_match_none: function(){ }, + on_enter_match_one: function(){ $('#update_button').click(); }, + on_enter_match_many: function(){ self.open_dialog(); } + } + }, $real.data('part-picker-data'), options); + this.$real = $real; + this.real_id = $real.attr('id'); + this.last_real = $real.val(); + this.$dummy = $($real.siblings()[0]); + this.autocomplete_open = false; + this.state = this.STATES.PICKED; + this.last_dummy = this.$dummy.val(); + this.timer = undefined; + + this.init(); + }; + + ns.Picker.prototype = { + CLASSES: { + PICKED: 'partpicker-picked', + UNDEFINED: 'partpicker-undefined', + }, + ajax_data: function(term) { + var data = { + 'filter.all:substr:multi::ilike': term, + 'filter.obsolete': 0, + current: this.$real.val(), + }; + + if (this.o.part_type) + data['filter.part_type'] = this.o.part_type.split(','); + + if (this.o.classification_id) + data['filter.classification_id'] = this.o.classification_id.split(','); + + if (this.o.unit) + data['filter.unit'] = this.o.unit.split(','); + + if (this.o.convertible_unit) + data['filter.unit_obj.convertible_to'] = this.o.convertible_unit; + + return data; + }, + set_item: function(item) { + var self = this; + if (item.id) { + this.$real.val(item.id); + // autocomplete ui has name, use the value for ajax items, which contains displayable_name + this.$dummy.val(item.name ? item.name : item.value); + } else { + this.$real.val(''); + this.$dummy.val(''); + } + this.state = this.STATES.PICKED; + this.last_real = this.$real.val(); + this.last_dummy = this.$dummy.val(); + this.$real.trigger('change'); + + if (this.o.fat_set_item && item.id) { + $.ajax({ + url: 'controller.pl?action=Part/show.json', + data: { 'part.id': item.id }, + success: function(rsp) { + self.$real.trigger('set_item:PartPicker', rsp); + }, + }); + } else { + this.$real.trigger('set_item:PartPicker', item); + } + this.annotate_state(); + }, + make_defined_state: function() { + if (this.state == this.STATES.PICKED) { + this.annotate_state(); + return true + } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '') + this.set_item({}) + else { + this.set_item({ id: this.last_real, name: this.last_dummy }) + } + this.annotate_state(); + }, + annotate_state: function() { + if (this.state == this.STATES.PICKED) + this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED); + else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '') + this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED); + else { + this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED); + } + }, + handle_changed_text: function(callbacks) { + var self = this; + $.ajax({ + url: 'controller.pl?action=Part/ajax_autocomplete', + dataType: "json", + data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ), + success: function (data) { + if (data.length == 1) { + self.set_item(data[0]); + if (callbacks && callbacks.match_one) callbacks.match_one(data[0]); + } else if (data.length > 1) { + self.state = self.STATES.UNDEFINED; + if (callbacks && callbacks.match_many) callbacks.match_many(data); + } else { + self.state = self.STATES.UNDEFINED; + if (callbacks && callbacks.match_none) callbacks.match_none(); + } + self.annotate_state(); + } + }); + }, + /* In case users are impatient and want to skip ahead: + * Capture key events and check if it's a unique hit. + * If it is, go ahead and assume it was selected. If it wasn't don't do + * anything so that autocompletion kicks in. For don't prevent + * propagation. It would be nice to catch it, but javascript is too stupid + * to fire a tab event later on, so we'd have to reimplement the "find + * next active element in tabindex order and focus it". + */ + /* note: + * event.which does not contain tab events in keypressed in firefox but will report 0 + * chrome does not fire keypressed at all on tab or escape + */ + handle_keydown: function(event) { + var self = this; + if (event.which == KEY.ENTER || event.which == KEY.TAB) { + // if string is empty assume they want to delete + if (self.$dummy.val() === '') { + self.set_item({}); + return true; + } else if (self.state == self.STATES.PICKED) { + return true; + } + if (event.which == KEY.TAB) { + event.preventDefault(); + self.handle_changed_text(); + } + if (event.which == KEY.ENTER) { + self.handle_changed_text({ + match_one: self.o.action.on_enter_match_one, + match_many: self.o.action.on_enter_match_many + }); + return false; + } + } else if (event.which == KEY.DOWN && !self.autocomplete_open) { + var old_options = self.$dummy.autocomplete('option'); + self.$dummy.autocomplete('option', 'minLength', 0); + self.$dummy.autocomplete('search', self.$dummy.val()); + self.$dummy.autocomplete('option', 'minLength', old_options.minLength); + } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) { + self.state = self.STATES.UNDEFINED; + } + }, + open_dialog: function() { + new ns.PickerPopup(this); + }, + init: function() { + var self = this; + this.$dummy.autocomplete({ + source: function(req, rsp) { + $.ajax($.extend(self.o, { + url: 'controller.pl?action=Part/ajax_autocomplete', + dataType: "json", + data: self.ajax_data(req.term), + success: function (data){ rsp(data) } + })); + }, + select: function(event, ui) { + self.set_item(ui.item); + }, + search: function(event, ui) { + if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT)) + event.preventDefault(); + }, + open: function() { + self.autocomplete_open = true; + }, + close: function() { + self.autocomplete_open = false; + } + }); + this.$dummy.keydown(function(event){ self.handle_keydown(event) }); + this.$dummy.on('paste', function(){ + setTimeout(function() { + self.handle_changed_text(); + }, 1); + }); + this.$dummy.blur(function(){ + window.clearTimeout(self.timer); + self.timer = window.setTimeout(function() { self.annotate_state() }, 100); + }); + + var popup_button = $('').addClass('ppp_popup_button'); + this.$dummy.after(popup_button); + popup_button.click(function() { self.open_dialog() }); + } + }; + ns.Picker.prototype.STATES = { + PICKED: ns.Picker.prototype.CLASSES.PICKED, + UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED + }; + + ns.PickerPopup = function(pp) { + this.timer = undefined; + this.pp = pp; + this.open_dialog(); + }; + + ns.PickerPopup.prototype = { + open_dialog: function() { + var self = this; + kivi.popup_dialog({ + url: 'controller.pl?action=Part/part_picker_search', + data: $.extend({ + real_id: self.pp.real_id, + }, self.pp.ajax_data(this.pp.$dummy.val())), + id: 'part_selection', + dialog: { + title: kivi.t8('Part picker'), + width: 800, + height: 800, + }, + load: function() { self.init_search(); } + }); + window.clearTimeout(this.timer); + return true; + }, + init_search: function() { + var self = this; + $('#part_picker_filter').keypress(function(e) { self.result_timer(e) }).focus(); + $('#no_paginate').change(function() { self.update_results() }); + this.update_results(); + }, + update_results: function() { + var self = this; + $.ajax({ + url: 'controller.pl?action=Part/part_picker_result', + data: $.extend({ + 'real_id': self.pp.$real.val(), + no_paginate: $('#no_paginate').prop('checked') ? 1 : 0, + }, self.pp.ajax_data(function(){ + var val = $('#part_picker_filter').val(); + return val === undefined ? '' : val + })), + success: function(data){ + $('#part_picker_result').html(data); + self.init_results(); + } + }); + }, + init_results: function() { + var self = this; + $('div.part_picker_part').each(function(){ + $(this).click(function(){ + self.pp.set_item({ + id: $(this).children('input.part_picker_id').val(), + name: $(this).children('input.part_picker_description').val(), + classification_id: $(this).children('input.part_picker_classification_id').val(), + unit: $(this).children('input.part_picker_unit').val(), + partnumber: $(this).children('input.part_picker_partnumber').val(), + description: $(this).children('input.part_picker_description').val(), + }); + self.close_popup(); + self.pp.$dummy.focus(); + return true; + }); + }); + $('#part_selection').keydown(function(e){ + if (e.which == KEY.ESCAPE) { + self.close_popup(); + self.pp.$dummy.focus(); + } + }); + }, + result_timer: function(event) { + var self = this; + if (!$('no_paginate').prop('checked')) { + if (event.keyCode == KEY.PAGE_UP) { + $('#part_picker_result a.paginate-prev').click(); + return; + } + if (event.keyCode == KEY.PAGE_DOWN) { + $('#part_picker_result a.paginate-next').click(); + return; + } + } + window.clearTimeout(this.timer); + if (event.which == KEY.ENTER) { + self.update_results(); + } else { + this.timer = window.setTimeout(function() { self.update_results() }, 100); + } + }, + close_popup: function() { + $('#part_selection').dialog('close'); + } + }; + + ns.reinit_widgets = function() { + kivi.run_once_for('input.part_autocomplete', 'part_picker', function(elt) { + if (!$(elt).data('part_picker')) + $(elt).data('part_picker', new kivi.Part.Picker($(elt))); + }); + } + + ns.init = function() { + ns.reinit_widgets(); + } + $(function(){ // assortment @@ -275,10 +607,10 @@ namespace('kivi.Part', function(ns) { $('.add_assortment_item_input').keydown(function(event) { if(event.keyCode == 13) { event.preventDefault(); - if ($("input[name='add_items[+].parts_id']").val() != '' ) { + if ($("input[name='add_items[+].parts_id']").val() !== '' ) { kivi.Part.show_multi_items_dialog("assortment"); // ns.add_assortment_item(); - }; + } return false; } }); @@ -286,7 +618,7 @@ namespace('kivi.Part', function(ns) { $('.add_assembly_item_input').keydown(function(event) { if(event.keyCode == 13) { event.preventDefault(); - if ($("input[name='add_items[+].parts_id']").val() != '' ) { + if ($("input[name='add_items[+].parts_id']").val() !== '' ) { kivi.Part.show_multi_items_dialog("assortment"); // ns.add_assembly_item(); } @@ -304,5 +636,6 @@ namespace('kivi.Part', function(ns) { $('#part_warehouse_id').change(kivi.Part.reload_bin_selection); + ns.init(); }); -}) +});