/******************************************************************************* * ATTRIBUTES * ****************************************************************************/ var rouvenfo_tables = []; var rouvenfo_valuelists = []; function RouvenfoTableObj(){ this.table_id = null; this.column_template_id = null; this.api_url = null; this.columns = []; this.selected_row_index = -1; this.filtertext = null; this.row_doubleclicked_function = null; this.row_edit_function = null; this.row_delete_function = null; this.table_data = null; this.id_columnname = "id"; this.after_load_callback = null; this.filter_column = null; this.filter_direction_asc = true; this.show_edit_row = false; this.show_delete_row = false; this.is_new_record = false; this.table_row_clicked = null; this.visible = true; this.table_row_class = null; } function RouvenfoColumnObj(dataname, headertext, type, format, valuelist_id, template, classnames, width, click, visible, enablefilter, inlineedit){ this.dataname = dataname; this.headertext = headertext; this.type = type; this.format = format; this.valuelist_id = valuelist_id; this.template = template; this.classnames = classnames; this.width = width; this.click = click; this.visible = visible; this.enablefilter = enablefilter; this.inlineedit = inlineedit; this.filteractive = false; this.filtertext = null; this.filtervalue = null; } const ROUVENFO_COLUMN_TYPES = { INT: 1, DECIMAL: 2, TEXT: 3, DATETIME: 4, CURRENCY: 5, BOOLEAN: 6, DATE: 7}; const ROUVENFO_TABLE_HEADER = "rouvenfo-table-header"; const ROUVENFO_TABLE_HEADER_CONTAINER = "rouvenfo-table-header-container"; const ROUVENFO_TABLE_DATA_CLASS = "rouvenfo-table-data"; const ROUVENFO_TABLE_DATA_SCROLL_CLASS = "rouvenfo-table-data-scrollbar"; const ROUVENFO_TABLE_DATA_CONTAINER_CLASS = "rouvenfo-table-data-container"; /******************************************************************************* * rouvenfo_add_table * * Adds a new Table * * @param rouvenfoTableObj -> Parameter Object with all configurations * * ****************************************************************************/ function rouvenfo_table_add_table(tableObj, loadData){ rouvenfo_table_create_table_structure(tableObj); rouvenfo_table_add_columns(tableObj); $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_DATA_SCROLL_CLASS).scroll(function(){ var position = $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_DATA_SCROLL_CLASS).scrollLeft(); $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER).css("transform", "translateX(-" + position + "px)"); }); $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " > span").click(rouvenfo_table_header_clicked); rouvenfo_tables.push(tableObj); if(loadData){ rouvenfo_table_load_data(tableObj); } } /******************************************************************************* * rouvenfo_table_add_columns * * Creats the table columns * * @param tableObj * * ****************************************************************************/ function rouvenfo_table_add_columns(tableObj){ var cols = $("#" + tableObj.column_template_id + " > span"); for(var i = 0; i < cols.length; i++){ rouvenfo_table_add_column_to_tableobj(tableObj, cols[i]); } if(tableObj.show_edit_row){ rouvenfo_table_add_column_edit(tableObj); } if(tableObj.show_delete_row){ rouvenfo_table_add_column_delete(tableObj); } } /******************************************************************************* * rouvenfo_table_add_column_to_tableobj * * Adds the column to the tableObj * * @param tableObj * @param element * * ****************************************************************************/ function rouvenfo_table_add_column_to_tableobj(tableObj, element){ var name = element.getAttribute("rouvenfo-data"); var type = element.getAttribute("rouvenfo-type"); var format = element.getAttribute("rouvenfo-format"); var valuelist = element.getAttribute("rouvenfo-valuelist"); var template = element.innerHTML; var headertext = element.getAttribute("rouvenfo-headertext"); var width = element.getAttribute("rouvenfo-width"); var click = element.getAttribute("rouvenfo-click"); var classList = element.classList; var classnames = ""; var enablefilter = element.getAttribute("rouvenfo-filter"); var visible = element.getAttribute("rouvenfo-visible"); var inlineedit = element.getAttribute("rouvenfo-inline-edit"); classList.forEach(function(el){ classnames += el + " "; }); if(!type){ type = ROUVENFO_COLUMN_TYPES.TEXT; } if(!enablefilter){ enablefilter = false; } if(!inlineedit){ inlineedit = false; } if(visible == "false"){ visible = false; } else{ visible = true; } var column = new RouvenfoColumnObj(name, headertext, type, format, valuelist, template, classnames, width, click, visible, enablefilter, inlineedit); tableObj.columns.push(column); rouvenfo_table_add_column_to_header(tableObj, column); } function rouvenfo_table_add_column_edit(tableObj){ var item = $(document.createElement("span")); item.append(""); var column = new RouvenfoColumnObj("", null, null, null, null, item[0].innerHTML, "col-edit col-fa-img", 30, null, true, false); tableObj.columns.push(column); rouvenfo_table_add_column_to_header(tableObj, column); } function rouvenfo_table_add_column_delete(tableObj){ var item = $(document.createElement("span")); item.append(""); var column = new RouvenfoColumnObj("", null, null, null, null, item[0].innerHTML, "col-delete col-fa-img", 30, null, true, false); tableObj.columns.push(column); rouvenfo_table_add_column_to_header(tableObj, column); } /******************************************************************************* * rouvenfo_table_add_column_to_header * * Adds the column to the DOM table-header * * @param tableObj * @param element * * ****************************************************************************/ function rouvenfo_table_add_column_to_header(tableObj, columnObj){ if(!columnObj.visible){ return; } var item = $(document.createElement("span")); item.addClass(columnObj.classnames); item.addClass("rouvenfo-table-header-" + columnObj.dataname); item.attr("rouvenfo-data", columnObj.dataname); if(columnObj.width){ item.css("width", columnObj.width + "px"); } if(columnObj.headertext){ item.text(columnObj.headertext); } else{ item.append(" "); } if(columnObj.enablefilter){ item.append(""); } $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER).append(item); } function rouvenfo_table_get_header_text(tableObj, columnId){ for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].dataname == columnId){ return tableObj.columns[i].headertext; } } return ""; } /******************************************************************************* * rouvenfo_table_create_table_structure * * Creats the table structure in the DOM * * @param tableObj * * ****************************************************************************/ function rouvenfo_table_create_table_structure(tableObj){ rouvenfo_append_element_div_class_if_not_exists("#" + tableObj.table_id, ROUVENFO_TABLE_HEADER); rouvenfo_append_element_div_class_if_not_exists("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER, ROUVENFO_TABLE_HEADER_CONTAINER); rouvenfo_append_element_div_class_if_not_exists("#" + tableObj.table_id, ROUVENFO_TABLE_DATA_CLASS); rouvenfo_append_element_div_class_if_not_exists("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_DATA_CLASS, ROUVENFO_TABLE_DATA_SCROLL_CLASS); rouvenfo_append_element_div_class_if_not_exists("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_DATA_SCROLL_CLASS, ROUVENFO_TABLE_DATA_CONTAINER_CLASS); } /******************************************************************************* * rouvenfo_table_get_tableobj_by_id * * Gets the tableObj from the id * * @param table_id * * ****************************************************************************/ function rouvenfo_table_get_tableobj_by_id(table_id){ for(var i = 0; i < rouvenfo_tables.length; i++){ if(rouvenfo_tables[i].table_id == table_id){ return rouvenfo_tables[i]; } } return null; } /******************************************************************************* * rouvenfo_get_valuelist_byid * * Gets the valuelist from the id * * @param valuelist_id * * ****************************************************************************/ function rouvenfo_get_valuelist_byid(valuelist_id){ for(var i = 0; i < rouvenfo_valuelists.length; i++){ if(rouvenfo_valuelists[i].id == valuelist_id){ return rouvenfo_valuelists[i]; } } return null; } /******************************************************************************* * rouvenfo_table_load_data * * Loads the data from the api * * @param tableObj * * ****************************************************************************/ function rouvenfo_table_load_data(tableObj){ var url = tableObj.api_url; var isFilter = false; if(tableObj.filtertext){ url += "?filter=" + tableObj.filtertext; isFilter = true; } for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].filteractive && tableObj.columns[i].filtertext){ if(!isFilter){ url += "?filter_manuell=true"; } url += "&"; url += tableObj.columns[i].dataname + "=" + tableObj.columns[i].filtertext; isFilter = true; } else if(tableObj.columns[i].filteractive && tableObj.columns[i].filtervalue){ if(!isFilter){ url += "?filter_manuell=true"; } url += "&"; if(tableObj.columns[i].type == ROUVENFO_COLUMN_TYPES.DATE){ var dateParts = tableObj.columns[i].filtervalue.split('-'); if(dateParts[0] != "" && dateParts[1] != "" && dateParts[2] != ""){ url += tableObj.columns[i].dataname + "=" + tableObj.columns[i].filtervalue; } else{ var parts = []; if(dateParts[0] != ""){ parts.push(tableObj.columns[i].dataname + "_y=" + dateParts[0]); } if(dateParts[1] != ""){ parts.push(tableObj.columns[i].dataname + "_m=" + dateParts[1]); } if(dateParts[2] != ""){ parts.push(tableObj.columns[i].dataname + "_d=" + dateParts[2]); } url += parts.join("&"); } } else{ url += tableObj.columns[i].dataname + "=" + tableObj.columns[i].filtervalue; } isFilter = true; } } $.get({ url: url, contentType: "application/json", success: function(data){ rouvenfo_table_show_data(data, tableObj); } }); } /******************************************************************************* * rouvenfo_table_show_data * * Shows the data * * @param data * @param tableObj * * ****************************************************************************/ function rouvenfo_table_show_data(data, tableObj){ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_DATA_CONTAINER_CLASS).empty(); if(tableObj.filter_column){ data = rouvenfo_table_data_sort(data, tableObj.filter_column, tableObj.filter_direction_asc, tableObj); rouvenfo_table_set_sort_icon(tableObj); } rouvenfo_table_set_filter_icon(tableObj); tableObj.table_data = data; for(i = 0; i < data.length; i++){ rouvenfo_table_add_row_element(tableObj, data[i]); } rouvenfo_table_select_current_row(tableObj); if(tableObj.after_load_callback){ tableObj.after_load_callback(); } } function rouvenfo_table_set_sort_icon(tableObj){ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " .rouvenfo-table-sort-icon").remove(); if(tableObj.filter_column){ if(tableObj.filter_direction_asc){ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " span.rouvenfo-table-header-" + tableObj.filter_column).append(""); } else{ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " span.rouvenfo-table-header-" + tableObj.filter_column).append(""); } } } function rouvenfo_table_set_filter_icon(tableObj){ for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].filteractive){ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " span.rouvenfo-table-header-" + tableObj.columns[i].dataname + " .rouvenfo-header-filter-icon").css("display", "inline-block"); } else{ $("#" + tableObj.table_id + " ." + ROUVENFO_TABLE_HEADER_CONTAINER + " span.rouvenfo-table-header-" + tableObj.columns[i].dataname + " .rouvenfo-header-filter-icon").css("display", ""); } } } function rouvenfo_table_add_row_element(tableObj, rowdata){ var row = $(document.createElement("div")); row.addClass("rouvenfo-table-row"); if(tableObj.table_row_clicked){ row.click(tableObj.table_row_clicked); } else{ row.click(rouvenfo_table_row_clicked); } if(tableObj.row_doubleclicked_function){ row.on('dblclick', tableObj.row_doubleclicked_function); } for(var c = 0; c < tableObj.columns.length; c++){ var columnObj = tableObj.columns[c]; if(!columnObj.visible){ continue; } var apiValue = rowdata[columnObj.dataname]; if(columnObj.type == ROUVENFO_COLUMN_TYPES.DATE){ if(apiValue && !(apiValue instanceof Date)){ rowdata[columnObj.dataname] = new Date(apiValue); apiValue = rowdata[columnObj.dataname]; } } var valueToShow = rouvenfo_table_get_column_value(columnObj, apiValue); var col = $(document.createElement("span")); col.addClass(columnObj.classnames); col.addClass("rouvenfo-data-" + columnObj.dataname); if(columnObj.valuelist_id){ col.addClass("rouvenfo-data-" + columnObj.dataname + "-" + apiValue); } if(columnObj.width){ col.css("width", columnObj.width + "px"); } if(columnObj.click){ col.click([columnObj.click]); } if(columnObj.template){ col.append(columnObj.template); } else if(columnObj.inlineedit && columnObj.type == 6){ var checkbox = $(document.createElement("input")); checkbox.attr("type", "checkbox"); checkbox.css("cursor", "pointer"); checkbox.prop('checked', apiValue); checkbox.change({table_id: tableObj.table_id, columnname: columnObj.dataname, id: rowdata[tableObj.id_columnname]}, function(event) { var tableObj = rouvenfo_table_get_tableobj_by_id(event.data.table_id); var value = $(event.target).is(":checked"); var rowData = null; for(var i = 0; i < tableObj.table_data.length; i++){ if(tableObj.table_data[i][tableObj.id_columnname] == event.data.id){ rowData = tableObj.table_data[i]; i = tableObj.table_data.length; } } rowData[event.data.columnname] = value; console.log(rowData); $.ajax({ type: "PUT", url: tableObj.api_url + "/" + event.data.id, data: JSON.stringify(rowData) }); }); col.append(checkbox); } else{ col.html(valueToShow); } row.append(col); } if(tableObj.table_row_class){ row.addClass(tableObj.table_row_class(rowdata)); } $("#" + tableObj.table_id + " .rouvenfo-table-data-container").append(row); } function rouvenfo_table_get_column_value(columnObj, value){ if(columnObj.valuelist_id){ return rouvenfo_get_valuelist_value(columnObj.valuelist_id, value); } if(columnObj.type == ROUVENFO_COLUMN_TYPES.CURRENCY){ return Intl.NumberFormat('de-CH', { style: 'currency', currency: 'CHF' }).format(value); } else if(columnObj.type == ROUVENFO_COLUMN_TYPES.BOOLEAN){ if(value){ return ""; } else{ return ""; } } else if(columnObj.type == ROUVENFO_COLUMN_TYPES.DATE){ if(value){ return ("0" + value.getDate()).slice(-2) + "." + ("0"+(value.getMonth()+1)).slice(-2) + "." + value.getFullYear(); } return ""; } return value; } /******************************************************************************* * rouvenfo_table_data_sort * * Sorts the data with the given column name * * @param data * @param column_name * @param sort_asc * * ****************************************************************************/ function rouvenfo_table_data_sort(data, column_name, sort_asc, tableObj){ var columnObj = null; for(var c = 0; c < tableObj.columns.length; c++){ if(tableObj.columns[c].dataname == column_name){ columnObj = tableObj.columns[c]; } } for(var i = 0; i < data.length; i++){ for(var k = i + 1; k < data.length; k++){ if(data[i][column_name] > data[k][column_name] && sort_asc || data[i][column_name] < data[k][column_name] && !sort_asc){ var temp = data[i]; data[i] = data[k]; data[k] = temp; } } } return data; } /******************************************************************************* * rouvenfo_get_valuelist_value * * Gets the value of the valuelist * * @param valuelist_id * @param id * * ****************************************************************************/ function rouvenfo_get_valuelist_value(valuelist_id, id){ var valuelist = rouvenfo_get_valuelist_byid(valuelist_id); for(var k = 0; k < valuelist.data.length; k++){ if(valuelist.data[k].id == id){ return valuelist.data[k].value; } } return ""; } function rouvenfo_table_select_current_row(tableObj){ var rows = $("#" + tableObj.table_id + " .rouvenfo-table-row"); if(tableObj.selected_row_index >= rows.length){ tableObj.selected_row_index = -1; } if(tableObj.selected_row_index == -1 && rows.length > 0){ tableObj.selected_row_index = 0; } if(tableObj.selected_row_index >= 0){ rows[tableObj.selected_row_index].classList.add("selected-row"); if(tableObj.table_row_clicked){ tableObj.table_row_clicked(); } } } function rouvenfo_table_row_clicked(el){ var id = ""; var parent = el.target.parentNode; while(parent !== null && (parent.classList === null || !parent.classList.contains("rouvenfo-table"))){ parent = parent.parentNode; } if(!parent){ return; } var tableObj = rouvenfo_table_get_tableobj_by_id(parent.id); rouvenfo_table_clear_selected_rows(tableObj); parent = el.target; while(parent !== null && (parent.classList === null || !parent.classList.contains("rouvenfo-table-row"))){ parent = parent.parentNode; } tableObj.selected_row_index = rouvenfo_get_child_index(parent); parent.classList.add("selected-row"); } function rouvenfo_table_save_record(tableObj, contentId){ var obj = rouvenfo_table_get_record_object(tableObj, contentId); if(tableObj.is_new_record){ $.ajax({ type: "POST", url: tableObj.api_url, data: JSON.stringify(obj), success: function(){ rouvenfo_close_current_window(); tableObj.is_new_record = false; rouvenfo_table_load_data(tableObj); } }); } else{ var dataRow = tableObj.table_data[tableObj.selected_row_index]; for(var c = 0; c < tableObj.columns.length; c++){ var columnName = tableObj.columns[c].dataname; if(obj[columnName] !== undefined){ dataRow[columnName] = obj[columnName]; } } $.ajax({ type: "PUT", url: tableObj.api_url + "/" + dataRow[tableObj.id_columnname], data: JSON.stringify(dataRow), success: function(){ rouvenfo_close_current_window(); rouvenfo_table_load_data(tableObj); } }); } } function rouvenfo_table_clear_selected_rows(tableObj){ var rows = $("#" + tableObj.table_id + " .rouvenfo-table-row"); rows.removeClass("selected-row"); } function rouvenfo_table_bind_window_data(tableObj, contentId){ var el = $("#" + contentId); var dataRow = tableObj.table_data[tableObj.selected_row_index]; for(var c = 0; c < tableObj.columns.length; c++){ rouvenfo_table_window_bind_set_column_value(el, tableObj.columns[c], dataRow[tableObj.columns[c].dataname]); } tableObj.is_new_record = false; } function rouvenfo_delete_selected_record(tableObj){ show_dialog_yes_no("Löschen", "Bist du sicher, dass du diesen Datensatz löschen möchtest?", "rouvenfo_delete_selected_record_yes('" + tableObj.table_id + "')", "rouvenfo_delete_selected_record_no('" + tableObj.table_id + "')"); } function rouvenfo_delete_selected_record_yes(tableId){ rouvenfo_close_current_window(); var tableObj = rouvenfo_table_get_tableobj_by_id(tableId); $.ajax({ type: "DELETE", url: tableObj.api_url + "/" + tableObj.table_data[tableObj.selected_row_index][tableObj.id_columnname], success: function(){ rouvenfo_table_load_data(tableObj); } }); console.log(tableObj.selected_row_index); } function rouvenfo_delete_selected_record_no(tableId){ rouvenfo_close_current_window(); } function rouvenfo_clear_record(tableObj, contentId){ var el = $("#" + contentId); for(var c = 0; c < tableObj.columns.length; c++){ rouvenfo_table_window_bind_set_column_value(el, tableObj.columns[c], null); } } function rouvenfo_table_get_record_object(tableObj, contentId){ var el = $("#" + contentId); var obj = {}; for(var c = 0; c < tableObj.columns.length; c++){ var val = rouvenfo_table_window_bind_get_column_value(el, tableObj.columns[c]); if(val != null){ obj[tableObj.columns[c].dataname] = val; } } return obj; } function rouvenfo_table_window_bind_get_element(windowElement, columnObj){ if(!columnObj.dataname){ return null; } var element = windowElement.find("input[rouvenfo-data=" + columnObj.dataname + "]"); if(element.length > 0){ return element; } element = windowElement.find("span[rouvenfo-data=" + columnObj.dataname + "]"); if(element.length > 0){ return element; } element = windowElement.find("div[rouvenfo-data=" + columnObj.dataname + "].rouvenfo-combobox"); if(element.length > 0){ return element; } element = windowElement.find("div[rouvenfo-data=" + columnObj.dataname + "]"); if(element.length > 0){ return element; } return null; } function rouvenfo_table_window_bind_get_column_value(windowElement, columnObj){ var element = rouvenfo_table_window_bind_get_element(windowElement, columnObj); if(!element){ return null; } if(element.hasClass("rouvenfo-combobox")){ return rouvenfo_get_combobox_current_id(element.attr("id")); } if(element.is(':checkbox')){ return element.is(":checked"); } if(columnObj.type == ROUVENFO_COLUMN_TYPES.DATE){ var date = element.val(); if(!date){ return null; } var dateSplit = date.split("."); if(dateSplit.length != 3){ return null; } return dateSplit[2] + "-" + dateSplit[1] + "-" + dateSplit[0]; } return element.val(); } function rouvenfo_table_window_bind_set_column_value(windowElement, columnObj, value){ var element = rouvenfo_table_window_bind_get_element(windowElement, columnObj); if(!element){ return; } if(element.hasClass("rouvenfo-combobox")){ rouvenfo_set_combobox_current_id(element.attr("id"), value); return; } if(element.is(':checkbox')){ element.prop('checked', value); return; } if(columnObj.type == ROUVENFO_COLUMN_TYPES.DATE){ if(!value){ element.val(value); return; } element.val(("0" + value.getDate()).slice(-2) + "." + ("0"+(value.getMonth()+1)).slice(-2) + "." + value.getFullYear()); return; } element.val(value); } function rouvenfo_table_header_clicked(event){ var col = event.target.getAttribute("rouvenfo-data"); if(!col){ return; } var tableObj = rouvenfo_table_get_tableobj_from_element(event.target); if(tableObj.filter_column == col){ tableObj.filter_direction_asc = !tableObj.filter_direction_asc; } else{ tableObj.filter_column = col; tableObj.filter_direction_asc = true; } rouvenfo_table_show_data(tableObj.table_data, tableObj); } function rouvenfo_table_get_tableobj_from_element(element){ var id = ""; while(element !== null && (element.classList === null || !element.classList.contains("rouvenfo-table"))){ element = element.parentNode; } if(!element){ return null; } return rouvenfo_table_get_tableobj_by_id(element.id); }/******************************************************************************* * ATTRIBUTES * ****************************************************************************/ var rouvenfo_comboboxes = []; /******************************************************************************* * Window Ready * ****************************************************************************/ $(function() { $("body").click(function(event){ for(var i=0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].is_open){ $("#" + rouvenfo_comboboxes[i].id + "-list").hide(); $("#" + rouvenfo_comboboxes[i].id).removeClass("rouvenfo-combobox-focused"); rouvenfo_comboboxes[i].is_open = false; } } }); }); /******************************************************************************* * rouvenfo_add_combobox * * Adds a new Combobox * * @param id -> The html id of the combobox * @param values -> Array of id,values pair {id: string, value: string} * @param init_id -> The value which is selected and shown at beginning * @param onDataChanged -> Callback when data is changed * * ****************************************************************************/ function rouvenfo_add_combobox(id, values, init_id, onDataChanged){ var cmb = document.createElement("div"); cmb.classList.add("rouvenfo-combobox-list"); cmb.id = id + "-list"; cmb.style.display = "none"; $("#" + id).append(""); $("#" + id).append(""); var init_value = null; for(var i = 0; i < values.length; i++){ var item = document.createElement("div"); item.classList.add("rouvenfo-combobox-list-item"); item.innerHTML = values[i].value; item.setAttribute("id", values[i].id); cmb.appendChild(item); if(init_id == values[i].id){ init_value = values[i].value; } } rouvenfo_comboboxes.push({ current_id: init_id, current_value: init_value, id: id, is_open: false, values: values, onDataChanged: onDataChanged }); console.log(onDataChanged); $("#" + id + " .rouvenfo-combobox-content-text").text(init_value); $("#" + id).click(rouvenfo_combobox_clicked); $("#" + id).after(cmb); $("#" + id + "-list .rouvenfo-combobox-list-item").click({id: id}, rouvenfo_combobox_list_item_clicked); } function rouvenfo_combobox_remove(id){ $("#" + id + "-list").remove(); for(var i = 0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].id == id){ rouvenfo_comboboxes.splice(i, 1); return; } } } /******************************************************************************* * rouvenfo_combobox_clicked * * When on the comobox self is clicked * * @param event * * ****************************************************************************/ function rouvenfo_combobox_clicked(event){ var currentNode = event.target; while(currentNode && !currentNode.id){ currentNode = currentNode.parentNode; } $("#" + currentNode.id + "-list").css("left", currentNode.offsetLeft); $("#" + currentNode.id).addClass("rouvenfo-combobox-focused"); for(var i = 0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].id == currentNode.id){ rouvenfo_comboboxes[i].is_open = true; } } $("#" + currentNode.id + "-list").show(); event.stopPropagation(); } /******************************************************************************* * rouvenfo_combobox_list_item_clicked * * When on a item of the comobox is clicked * * @param event * * ****************************************************************************/ function rouvenfo_combobox_list_item_clicked(event){ $("#" + event.data.id + " .rouvenfo-combobox-content-text").text(event.target.innerHTML); for(var i = 0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].id == event.data.id){ rouvenfo_comboboxes[i].current_id = event.target.id; rouvenfo_comboboxes[i].current_value = event.target.innerHTML; rouvenfo_comboboxes[i].is_open = false; $("#" + rouvenfo_comboboxes[i].id).removeClass("rouvenfo-combobox-focused"); console.log(rouvenfo_comboboxes[i].onDataChanged); if(rouvenfo_comboboxes[i].onDataChanged){ rouvenfo_comboboxes[i].onDataChanged(newId, newValue); } } } $("#" + event.data.id + "-list").hide(); } /******************************************************************************* * rouvenfo_get_combobox_current_id * * Gets the current id of the param id combobox * * @param id -> Html Id of the combobox * * ****************************************************************************/ function rouvenfo_get_combobox_current_id(id){ for(var i = 0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].id == id){ return rouvenfo_comboboxes[i].current_id; } } return null; } function rouvenfo_get_combobox_by_id(id){ for(var i = 0; i < rouvenfo_comboboxes.length; i++){ if(rouvenfo_comboboxes[i].id == id){ return rouvenfo_comboboxes[i]; } } return null; } function rouvenfo_set_combobox_current_id(comboboxId, id){ var currentCombobox = rouvenfo_get_combobox_by_id(comboboxId); if(!currentCombobox){ return; } currentCombobox.current_id = null; currentCombobox.current_value = ""; for(var i = 0; i < currentCombobox.values.length; i++){ if(id == currentCombobox.values[i].id){ currentCombobox.current_id = currentCombobox.values[i].id; currentCombobox.current_value = currentCombobox.values[i].value; } } $("#" + comboboxId + " .rouvenfo-combobox-content-text").text(currentCombobox.current_value); }/******************************************************************************* * ATTRIBUTES * ****************************************************************************/ var rouvenfo_current_window_id = null; /******************************************************************************* * open_new_window * * @param window_id -> Id of the window * @param title -> title of the window * @param height -> optional: height of the window * @param width -> optional: width of the window * * Opens the new window. * * ****************************************************************************/ function rouvenfo_open_new_window(window_id, title, height, width){ if(rouvenfo_current_window_id) return; rouvenfo_current_window_id = window_id; if($("#rouvenfo-window-full-background").length === 0){ $('body').append($('
', { id: 'rouvenfo-window-full-background' })); } $("#rouvenfo-window-full-background").css("display", "block"); $("#" + rouvenfo_current_window_id).css("display", "block"); if($("#" + rouvenfo_current_window_id + " .rouvenfo-window-title").length === 0){ $("#" + rouvenfo_current_window_id).prepend($('', { class: 'rouvenfo-window-title' })); } if($("#" + rouvenfo_current_window_id + " .rouvenfo-window-title .rouvenfo-window-title-text").length === 0){ $("#" + rouvenfo_current_window_id + " .rouvenfo-window-title").append($('', { class: 'rouvenfo-window-title-text' })); } if($("#" + rouvenfo_current_window_id + " .rouvenfo-window-title .rouvenfo-window-title-close").length === 0){ $("#" + rouvenfo_current_window_id + " .rouvenfo-window-title").append($('', { class: 'rouvenfo-window-title-close' , onclick: 'rouvenfo_close_current_window()' })); $("#" + rouvenfo_current_window_id + " .rouvenfo-window-title .rouvenfo-window-title-close").append($('', { class: 'far fa-x' })); } $("#" + rouvenfo_current_window_id + " .rouvenfo-window-title .rouvenfo-window-title-text").text(title); $("#" + rouvenfo_current_window_id).addClass("rouvenfo-window"); if(height){ $("#" + rouvenfo_current_window_id).css("height", height); } if(width){ $("#" + rouvenfo_current_window_id).css("width", width); } } /******************************************************************************* * close_window_clicked * * Closes the current window. * * ****************************************************************************/ function rouvenfo_close_current_window(){ if(!rouvenfo_current_window_id) return; $("#rouvenfo-window-full-background").css("display", "none"); $("#" + rouvenfo_current_window_id).css("display", "none"); rouvenfo_current_window_id = null; } function show_dialog_yes_no(title, text, rouvenfo_window_yes_callback, rouvenfo_window_no_callback){ if($("#rouvenfo_yes_no_dialog").length === 0){ $('body').append($('', { id: 'rouvenfo_yes_no_dialog' })); $("#rouvenfo_yes_no_dialog").append($('', { class: 'rouvenfo-window-content' })); $("#rouvenfo_yes_no_dialog").append($('', { class: 'rouvenfo-window-footer' })); $("#rouvenfo_yes_no_dialog .rouvenfo-window-footer").append($('', { class: 'rouvenfo-window-control-no button', onclick: rouvenfo_window_no_callback })); $("#rouvenfo_yes_no_dialog .rouvenfo-window-footer .rouvenfo-window-control-no").text("Nein"); $("#rouvenfo_yes_no_dialog .rouvenfo-window-footer").append($('', { class: 'rouvenfo-window-control-yes button button-primary', onclick: rouvenfo_window_yes_callback })); $("#rouvenfo_yes_no_dialog .rouvenfo-window-footer .rouvenfo-window-control-yes").text("Ja"); } $("#rouvenfo_yes_no_dialog .rouvenfo-window-content").text(text); rouvenfo_open_new_window("rouvenfo_yes_no_dialog", title); }function rouvenfo_filter_column(table_id, column_name){ var tableObj = rouvenfo_table_get_tableobj_by_id(table_id); for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].dataname == column_name){ var text = ""; var value = ""; if(tableObj.columns[i].filtertext){ text = tableObj.columns[i].filtertext; } if(tableObj.columns[i].filtervalue){ value = tableObj.columns[i].filtervalue; } if($("#rouvenfo-window-table-filter").length === 0){ $("body").append($('', { id: 'rouvenfo-window-table-filter' })); } $("#rouvenfo-window-table-filter").empty(); $("#rouvenfo-window-table-filter").append(""); if(tableObj.columns[i].valuelist_id != null){ var valueSplit = value.split(";"); if(value == ""){ valueSplit = []; } for(var s = 0; s < valueSplit.length; s++){ $("#rouvenfo-window-table-filter-container").append(""); rouvenfo_add_combobox("rouvenfo-window-table-filter-valuelist" + s, rouvenfo_get_valuelist_byid(tableObj.columns[i].valuelist_id).data, valueSplit[s]); $("#rouvenfo-window-table-filter-valuelist" + s + "-list").on("click", {valuelist_id: tableObj.columns[i].valuelist_id}, function(event){ onComboboxItemClicked(event); }); } $("#rouvenfo-window-table-filter-container").append(""); rouvenfo_add_combobox("rouvenfo-window-table-filter-valuelist" + s, rouvenfo_get_valuelist_byid(tableObj.columns[i].valuelist_id).data, null); $("#rouvenfo-window-table-filter-valuelist" + s + "-list").on("click", {valuelist_id: tableObj.columns[i].valuelist_id}, function(event){ onComboboxItemClicked(event); }); } else if(tableObj.columns[i].type == ROUVENFO_COLUMN_TYPES.DATE){ var valueSplit = value.split("-"); if(value == ""){ valueSplit = ["","",""]; } var year = valueSplit[0]; var month = valueSplit[1]; var day = valueSplit[2]; $("#rouvenfo-window-table-filter-container").append("Tag"); $("#rouvenfo-window-table-filter-container").append(""); $("#rouvenfo-window-table-filter-container").append("Monat"); $("#rouvenfo-window-table-filter-container").append(""); $("#rouvenfo-window-table-filter-container").append("Jahr"); $("#rouvenfo-window-table-filter-container").append(""); } else{ $("#rouvenfo-window-table-filter-container").append(""); } var buttonCont = document.createElement("div"); $(buttonCont).addClass("rouvenfo-table-filter-button-container"); $(buttonCont).append(""); $(buttonCont).append(""); $("#rouvenfo-window-table-filter").append(buttonCont); rouvenfo_open_new_window("rouvenfo-window-table-filter", "Filter", null, 240); } } } function rouvenfo_filter_submit(table_id, column_name){ var tableObj = rouvenfo_table_get_tableobj_by_id(table_id); for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].dataname == column_name){ var value = null; var text = null; if(tableObj.columns[i].valuelist_id != null){ var valueList = []; for(var k = 0; k < 100; k++){ if($("#rouvenfo-window-table-filter-valuelist" + k).length != 0){ if(rouvenfo_get_combobox_by_id("rouvenfo-window-table-filter-valuelist" + k).current_id != null){ valueList.push(rouvenfo_get_combobox_by_id("rouvenfo-window-table-filter-valuelist" + k).current_id); } rouvenfo_combobox_remove("rouvenfo-window-table-filter-valuelist" + k); } } value = valueList.join(";"); } else if(tableObj.columns[i].type == ROUVENFO_COLUMN_TYPES.DATE){ var day = $("#rouvenfo-window-table-filter-input-day").val(); var month = $("#rouvenfo-window-table-filter-input-month").val(); var year = $("#rouvenfo-window-table-filter-input-year").val(); value = year + "-" + month + "-" + day; } else{ text = $("#rouvenfo-window-table-filter-input").val(); } if(value != ""){ tableObj.columns[i].filteractive = true; tableObj.columns[i].filtertext = text; tableObj.columns[i].filtervalue = value; } else{ tableObj.columns[i].filteractive = false; tableObj.columns[i].filtertext = null; tableObj.columns[i].filtervalue = null; } } } rouvenfo_close_current_window(); rouvenfo_table_load_data(tableObj); } function rouvenfo_filter_delete(table_id, column_name){ var tableObj = rouvenfo_table_get_tableobj_by_id(table_id); for(var i = 0; i < tableObj.columns.length; i++){ if(tableObj.columns[i].dataname == column_name){ tableObj.columns[i].filteractive = false; tableObj.columns[i].filtertext = null; tableObj.columns[i].filtervalue = null; for(var k = 0; k < 100; k++){ if($("#rouvenfo-window-table-filter-valuelist" + k).length != 0){ rouvenfo_combobox_remove("rouvenfo-window-table-filter-valuelist" + k); } } } } rouvenfo_close_current_window(); rouvenfo_table_load_data(tableObj); } function onComboboxItemClicked(event){ for(var k = 0; k < 100; k++){ if($("#rouvenfo-window-table-filter-valuelist" + k).length == 0){ if(rouvenfo_get_combobox_by_id("rouvenfo-window-table-filter-valuelist" + (k-1)).current_id != null){ $("#rouvenfo-window-table-filter-container").append(""); rouvenfo_add_combobox("rouvenfo-window-table-filter-valuelist" + k, rouvenfo_get_valuelist_byid(event.data.valuelist_id).data, null); $("#rouvenfo-window-table-filter-valuelist" + k + "-list").on("click", {valuelist_id: event.data.valuelist_id}, function(event){ onComboboxItemClicked(event); }); } k = 100; } } }var mainTableObj = null; function rouvenfo_table_crud_add_form(form_id, template_id, edit_template_id, api_url){ var form = $("#" + form_id); form.append(""); mainTableObj = new RouvenfoTableObj(); mainTableObj.edit_template_id = edit_template_id; mainTableObj.table_id = "tbl-" + form_id; mainTableObj.column_template_id = template_id; mainTableObj.api_url = api_url; mainTableObj.row_doubleclicked_function = rouvenfo_table_crud_open_window_edit; //tableObj.after_load_callback = table_member_after_load; //tableObj.filter_column = "lastname"; //tableObj.show_edit_row = true; //tableObj.row_edit_function = "table_member_open_edit(event)"; //tableObj.row_delete_function = "rouvenfo_delete_selected_record(memberTableObj)"; //tableObj.show_delete_row = true; //tableObj.table_row_clicked = table_member_row_clicked; //tableObj.table_row_class = table_member_set_row_inactive_class; rouvenfo_table_add_table(mainTableObj, true); } function rouvenfo_table_crud_open_window_edit(el){ rouvenfo_table_row_clicked(el); rouvenfo_open_new_window(mainTableObj.edit_template_id + "-window", "Bearbeiten", null, 1100); rouvenfo_table_bind_window_data(mainTableObj, mainTableObj.edit_template_id + "-content"); }function rouvenfo_get_child_index(node) { return Array.prototype.indexOf.call(node.parentNode.childNodes, node); } function rouvenfo_append_element_div_class_if_not_exists(selector, classname){ var element = $(selector + " ." + classname); if(element.length == 0){ $(selector).append(""); } } function setCookie(name,value,days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }function rouvenfo_add_split(name, id, panel1, panel2){ $("#" + id).on("mousedown", {name: name, id: id, panel1: panel1, panel2: panel2}, function(event){ $(document).mousemove({name: event.data.name, id: event.data.id, panel1: event.data.panel1, panel2: event.data.panel2}, function(event) { setPosition(event.data.id, event.data.panel1, event.data.panel2, event.pageY); setCookie(event.data.name + "-split", (event.pageY)); }); }); $("#" + id).on("mouseup", function(event){ $(document).off('mousemove'); }); var cookie = getCookie(name + "-split"); if(cookie){ setPosition(id, panel1, panel2, cookie); } } function setPosition(id, panel1, panel2 , y){ $("#" + panel1).css("height", (y - 150) + "px"); $("#" + id).css("top", (y - 50) + "px"); $("#" + panel2).css("top", (y - 45) + "px"); $("#" + panel2).css("height", "calc(100% - " + (Number(y) + 55) + "px)"); } $('head').append(''); $('head').append(''); $('head').append(''); $('head').append(''); $('head').append(''); $('head').append('');