/* Class to create JS table with products or sth. mz 2015-02-02 */ var EcmJsTable = function(metadata, container, type) { // check params this.metadata = metadata || false; this.container = container || false; this.type = type || 'EditView'; if (this.metadata === false) return 'No metadata'; if (this.container === false) return 'No containner'; searchedItems = null; // create fieldDataType array fieldsDataType = new Array(); $.each(this.metadata, function(i, col) { $.each(col.content, function(j, f) { if (f.dataType) fieldsDataType[col.name + f.name] = f.dataType; }); }); this.fieldsDataType = fieldsDataType; this.container.closest('td').attr('colspan', '4'); } EcmJsTable.prototype.getHeaders = function() { var params = new Array; params[0] = this.metadata; params[1] = this.type; var c = this.container; // AJAX call jQuery.ajax({ type : 'POST', url : 'index.php?entryPoint=HandleEcmAjax', data : { ecmclass : 'EcmJsTable', job : 'getHeaders', params : utf8_to_b64(JSON.stringifyNoSecurity(params)), }, dataType : 'json', async : false, success : function(response) { c.html(response[0]); }, }); } EcmJsTable.prototype.setRowDate = function(element,index) { var items = this.items; var element_name = $(element).attr("name"); if (element_name == 'quantity_recipe') { var inputs = $(element).closest("tr").find("input"); var total_quantity = UnformatNumber($(element).val()); items[index ]['quantity_recipe'] = total_quantity; $.each(inputs, function(row_index, row) { if ($(row).attr("name") == 'divider') { var divider = UnformatNumber($(row).val()); total_quantity = total_quantity /divider; } if($(row).attr("name")=='quantity'){ if(total_quantity=='Infinity'){ total_quantity=0; } if(total_quantity=='NaN'){ total_quantity=0; } $(row).val(QuantityFormat(total_quantity,8)); console.log(items); items[index ]['quantity'] = total_quantity.toFixed(8); } }); } if (element_name == 'divider') { var inputs = $(element).closest("tr").find("input"); var total_quantity = UnformatNumber($(element).val()); items[index ]['divider'] = total_quantity; $.each(inputs, function(row_index, row) { if ($(row).attr("name") == 'quantity_recipe') { var divider = UnformatNumber($(row).val()); total_quantity = divider/total_quantity; } if($(row).attr("name")=='quantity'){ if(total_quantity=='Infinity'){ total_quantity=0; } if(total_quantity=='NaN'){ total_quantity=0; } $(row).val(QuantityFormat(total_quantity,8)); items[index ]['quantity'] = total_quantity.toFixed(8); } }); } console.log(items); this.items=items; } EcmJsTable.prototype.fillTable = function() { var metadata = this.metadata; var c = this.container; var fdt = this.fieldsDataType; var that = this; // clear table c.find('#EcmJsItemsTable > tbody').empty(); $.each(this.items, function(row_index, row) { var item = row; var c_row = $(""); // current row $.each(metadata, function(col_index, col) { if ((col.showIn) && (col.showIn != that.type)) return; var c_cell = $(""); // Lp. if (col.name == 'number') { c_cell.html(parseInt(row_index) + 1); c_cell.css('text-align', 'center'); c_cell.attr('name', 'number'); c_cell.attr('id', 'number'); c_row.append(c_cell); return; } // Options if (col.name == 'options') { // no options in DetailView if (that.type == 'DetailView') return; c_cell.css('text-align', 'center'); c_cell.attr('name', 'opt'); c_cell.attr('id', 'opt'); // move up var img = $(''); img.click(function() { that.moveRowUp(row_index); }); img.attr('src', 'modules/EcmSales/images/moverowup.gif'); img.css('cursor', 'pointer'); c_cell.append(img); // move down var img = $(''); img.click(function() { that.moveRowDown(row_index); }); img.attr('src', 'modules/EcmSales/images/moverowdown.gif'); img.css('cursor', 'pointer'); c_cell.append(img); // line break c_cell.append('
'); // delete row var img = $(''); img.click(function() { that.deleteRow(row_index); }); img.attr('src', 'modules/EcmSales/images/deleterow.gif'); img.css('cursor', 'pointer'); c_cell.append(img); c_row.append(c_cell); return; } // normal fields $.each(col.content, function(field_index, field) { var f_name = col.name + field.name; var val = item[f_name]; if (field.customCodeEdit && that.type == 'EditView') { // replace {{row_index}} var tmp = field.customCodeEdit; while (tmp.indexOf('{{row_index}}') != -1) tmp = tmp.replace('{{row_index}}', row_index + ''); c_cell.append(tmp); return; } // set attr.type as type if is empty if (!field.attr) field.attr = new Array(); if (!field.attr.type) field.attr.type = 'text'; if (fdt[f_name] == 'number') { var precision = 2; if (field.precision) precision = field.precision; if(fdt['quantity']){ val = QuantityFormat(val, precision); } else { val = FormatNumber(val, precision); } } if ((that.type == 'DetailView' || field.readonly) && field.attr.type != 'hidden' && field.dataType!='number') { var f = $('

'); f.html(val); } else var f = $(''); f.val(val); f.attr('name', f_name); f.attr('id', f_name); if (precision) f.attr('precision', precision); f.addClass('inputs'); // set attributes if(field.readonly){ f.attr('readonly', 'readonly'); } if (field.onChange) f.attr('onChange', field.onChange); if (field.attr) $.each(field.attr, function(n, v) { f.attr(n, v); }); // set css if (field.css) $.each(field.css, function(n, v) { f.css(n, v); }); c_cell.append(f); }); c_row.append(c_cell); }); c.find('#EcmJsItemsTable > tbody').append(c_row); }); // some actions in EditView if (this.type == 'EditView') { // set onClick to all fields var that = this; this.container.find("#EcmJsItemsTable tbody tr input").on("blur", function() { that.updateItems(); }); // select all in inputs on click this.container.find("#EcmJsItemsTable tbody tr input[type='text']").on( "click", function() { $(this).select(); }); // add search this.addSearchRow(); } } EcmJsTable.prototype.addSearchRow = function() { var row = $('') var that = this; // Fuck yeah :D var col_count = this.metadata.length; cl(col_count); $.each(this.metadata, function(i, col) { var td = $(''); if (i == 0 && col_count > 1) { var f = $(''); f.attr('name', 'autocompleteSocket'); f.attr('id', 'autocompleteSocket'); f.addClass('inputs'); f.attr('readonly', 'readonly'); td.append(f); } if (col.searchTrigger) { var f = $(''); f.val('SF'); f.addClass('inputs'); f.css('text-align', 'right'); f.click(function() { $(this).val('') }); f.blur(function() { $(this).val('SF') }); f.keyup(function() { that.quickSearchTrigger($(this).val()) }); td.append(f) } row.append(td); }); this.container.find('#EcmJsItemsTable > tbody').append(row); } EcmJsTable.prototype.quickSearchTrigger = function(searchString) { var that = this; if (this.timer) { clearTimeout(this.timer); } this.timer = setTimeout(function() { that.quickSearch(searchString); }, 600); } EcmJsTable.prototype.quickSearch = function(searchString) { if (!$.isFunction(this.customQuickSearch)) console.log('Error: No search function!'); else this.showSearchBox(this.customQuickSearch(searchString)); } EcmJsTable.prototype.showSearchBox = function(items) { var sd = this.container.find("#EcmSearchBox"); sd.empty(); var that = this; if (items.length == 0) sd.html('Brak wyników'); else { var ul = $(''); ul.css('display', 'table'); $.each(items, function(index, item) { var li = $('
  • '); li.css('width', '100%'); li.html(item.value); li.click(function() { that.addItem(item.fields); }); ul.append(li); }); sd.append(ul); } sd.show('slow'); } EcmJsTable.prototype.addItem = function(item) { this.items.push(JSON.parse(item)); this.fillTable(); this.container.find("#EcmSearchBox").html(''); this.container.find("#EcmSearchBox").hide('slow'); } EcmJsTable.prototype.updateItems = function() { var items = this.items; var count = this.container.find('#EcmJsItemsTable tr').length - 1; // -1 - // thead row var rows = this.container.find('#EcmJsItemsTable tr'); var fdt = this.fieldsDataType; for (var index = 1; index != count; index++) { $.each($(rows[index]).find('td'), function(cell_i, cell) { $.each($(cell).children(), function(field_i, field) { var name = $(field).attr('name'); var val = $(field).val(); if (fdt[name] == 'number') { var val = UnformatNumber(val); $(field).val( FormatNumber(val, $(field) .attr('precision'))); } items[index - 1][name] = val; }); }); } this.items = items; } EcmJsTable.prototype.saveItems = function() { if (!$.isFunction(this.customSave)) console.log('Error: No search function!'); else{ this.customSave(utf8_to_b64(JSON.stringifyNoSecurity(this.items)), $( '[name=record]').val()); } } EcmJsTable.prototype.setItems = function(items) { this.items = items; } EcmJsTable.prototype.getItems = function() { return this.items; } EcmJsTable.prototype.getItemsCount = function() { return this.items.length; } EcmJsTable.prototype.deleteRow = function(index) { this.items.splice(index, 1); this.fillTable(); } EcmJsTable.prototype.moveRowUp = function(index) { if (index == 0) return; // Can't go upper.. :( var new_index = index - 1; var old_index = index; if (new_index >= this.items.length) { var k = new_index - this.items.length; while ((k--) + 1) { this.items.push(undefined); } } this.items.splice(new_index, 0, this.items.splice(old_index, 1)[0]); this.fillTable(); } EcmJsTable.prototype.moveRowDown = function(index) { if (index == this.items.length - 1) return; // Can't go lower.. :( var new_index = index + 1; var old_index = index; if (new_index >= this.items.length) { var k = new_index - this.items.length; while ((k--) + 1) { this.items.push(undefined); } } this.items.splice(new_index, 0, this.items.splice(old_index, 1)[0]); this.fillTable(); } // helper function utf8_to_b64(str) { return window.btoa(unescape(encodeURIComponent(str))); } function b64_to_utf8(str) { return decodeURIComponent(escape(window.atob(str))); } (function($) { $.fn.findNext = function(sel) { var $result = $(sel).first(); if ($result.length <= 0) { return $result; } $result = []; var thisIndex = $('*').index($(this)); var selIndex = Number.MAX_VALUE; // Number.MAX_SAFE_INTEGER is not // yet fully supported $(sel).each(function(i, val) { var valIndex = $('*').index($(val)); if (thisIndex < valIndex && valIndex < selIndex) { selIndex = valIndex; $result = $(val); } }); return $result; }; })(jQuery);