1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-17 22:06:41 +02:00
TableFilter/libs/ezEditTable/ezEditTable.js

9 lines
68 KiB
JavaScript
Raw Normal View History

2015-04-06 10:45:46 +02:00
/*------------------------------------------------------------------------
- ezEditTable v2.3 by Max Guglielmi
- build date: Sun Mar 15 2015 15:46:31
- http://edittable.free.fr
- Copyright (c) 2012, License required for use
------------------------------------------------------------------------*/
function setEditTable(){if(0!==arguments.length){var a=new EditTable(arguments[0],arguments[1],arguments[2]);return a.Init(),a}}var EditTable=function(a){if(0!==arguments.length&&(this.id=a,this.version="2.3",this.table=this.Get(a),this.tBody=this.table.tBodies[0],this.startRow=0,this.config=null,this.nbCells=null,window.et_activeGrid||(window.et_activeGrid=""),null!==this.table&&"table"===this.table.nodeName.LCase())){var b=this.Tag(this.tBody,"tr");if(b.length>0&&(this.startRow=b[0].rowIndex),arguments.length>1)for(var c=0;c<arguments.length;c++){var d=typeof arguments[c];"number"===d.LCase()&&(this.startRow=arguments[c]),"object"===d.LCase()&&(this.config=arguments[c])}this.nbCells=this.GetCellsNb(this.startRow);var e=this.config||{};this.selection=e.selection===!1?!1:!0,this.keyNav=void 0!==e.key_navigation?e.key_navigation:!0,this.editable=void 0!==e.editable?e.editable:!1,this.tableCss=void 0!==e.table_css?e.table_css:"ezEditableTable",this.unselectableCss=void 0!==e.unselectable_css?e.unselectable_css:"ezUnselectable",this.basePath=void 0!==e.base_path?e.base_path:"ezEditTable/",this.activityIndicatorCss=void 0!==e.activity_indicator_css?e.activity_indicator_css:"ezOpacity",this.onServerActivityStart=this.IsFn(e.on_server_activity_start)?e.on_server_activity_start:null,this.onServerActivityStop=this.IsFn(e.on_server_activity_stop)?e.on_server_activity_stop:null,this.selectionModel=void 0!==e.selection_model?e.selection_model.LCase():"single",this.defaultSelection=void 0!==e.default_selection?e.default_selection.LCase():"row",this.keySelection=this.editable?!0:void 0!==e.key_selection?e.key_selection:!0,this.selectRowAtStart=void 0!==e.select_row_at_start?e.select_row_at_start:!1,this.rowIndexAtStart=void 0!==e.row_index_at_start?parseInt(e.row_index_at_start,10):this.startRow,this.scrollIntoView=void 0!==e.scroll_into_view?e.scroll_into_view:!1,this.activeRowCss=void 0!==e.active_row_css?e.active_row_css:"ezActiveRow",this.selectedRowCss=void 0!==e.selected_row_css?e.selected_row_css:"ezSelectedRow",this.activeCellCss=void 0!==e.active_cell_css?e.active_cell_css:"ezActiveCell",this.nbRowsPerPage=void 0!==e.nb_rows_per_page?e.nb_rows_per_page:10,this.onSelectionInit=this.IsFn(e.on_selection_initialized)?e.on_selection_initialized:null,this.onBeforeSelectedRow=this.IsFn(e.on_before_selected_row)?e.on_before_selected_row:null,this.onAfterSelectedRow=this.IsFn(e.on_after_selected_row)?e.on_after_selected_row:null,this.onBeforeSelectedCell=this.IsFn(e.on_before_selected_cell)?e.on_before_selected_cell:null,this.onAfterSelectedCell=this.IsFn(e.on_after_selected_cell)?e.on_after_selected_cell:null,this.onBeforeDeselectedRow=this.IsFn(e.on_before_deselected_row)?e.on_before_deselected_row:null,this.onAfterDeselectedRow=this.IsFn(e.on_after_deselected_row)?e.on_after_deselected_row:null,this.onBeforeDeselectedCell=this.IsFn(e.on_before_deselected_cell)?e.on_before_deselected_cell:null,this.onAfterDeselectedCell=this.IsFn(e.on_after_deselected_cell)?e.on_after_deselected_cell:null,this.onValidateRow=this.IsFn(e.on_validate_row)?e.on_validate_row:null,this.onValidateCell=this.IsFn(e.on_validate_cell)?e.on_validate_cell:null,this.editorModel=void 0!==e.editor_model?e.editor_model.LCase():"cell",this.openEditorAction=void 0!==e.open_editor_action?e.open_editor_action.LCase():"dblclick",this.ajax=window.jQuery&&e.ajax!==!1?!0:!1,this.autoSave=void 0!==e.auto_save?e.auto_save:this.editable,this.autoSaveModel=void 0!==e.auto_save_model?e.auto_save_model:"row",this.autoSaveType=void 0!==e.auto_save_type?e.auto_save_type:"both",this.editableOnKeystroke=void 0!==e.editable_on_keystroke?e.editable_on_keystroke:!1,this.newRowPrefix=void 0!==e.new_row_prefix?e.new_row_prefix:"tr",this.formSubmitInterval=void 0!==e.form_submit_interval?e.form_submit_interval:50,this.newRowPos=void 0!==e.new_row_pos?e.new_row_pos:"top",this.edtTypes={none:"none",input:"input",textarea:"textarea",select:"select",multiple:"multiple",bool:"boolean",uploader:"uploader",command:"command",custom:"custom"},this.editors=[],this.cellEditors=this.IsArray(e.cell_e
}}if(this.o.editorSortSlcOptions[a]){var k=this.o.editorSortSlcOptions[a].LCase();if("numdesc"==k)try{e.sort(this.o.Sort.NumDesc)}catch(l){}else if("numasc"==k)try{e.sort(this.o.Sort.NumAsc)}catch(l){}else try{e.sort(this.o.Sort.IgnoreCase)}catch(l){}}for(var m=0;m<e.length;m++){var n=this.o.CreateElm("option",["value",f[m]||e[m]]);n.appendChild(this.o.CreateText(e[m])),b.appendChild(n)}var o=this;return this.o.Event.Add(b,"change",function(a){var c=o.o.GetCell(a);c&&(c.setAttribute(o.o.attrText,b.options[b.selectedIndex].text),c.setAttribute(o.o.attrValue,b.options[b.selectedIndex].value),b.setAttribute(o.o.attrText,b.options[b.selectedIndex].text),b.setAttribute(o.o.attrValue,b.options[b.selectedIndex].value))}),this.o.Event.Add(b,"blur",function(a){o.Event.OnBlur.call(o,a)}),this.o.Event.Add(b,"keypress",function(a){o.Event.OnKeyPress.call(o,a)}),b},SetCommandEditor:function(a){if(void 0!==a&&this.o.IsEditorType(a,this.o.edtTypes.command)){this.edtBtns=[],this.addBtns=[],this.delBtns=[],this.submitBtns=[],this.cancelBtns=[];for(var b=this.o.Editable,c=this.o,d=this.o.startRow;d<this.o.GetRowsNb();d++){var e=this.o.table.rows[d],f=e.cells[a];if(e&&f){var g=this.o.CreateElm("div",["class",this.o.commandEditorCss]);if(-1!=this.o.cmdEnabledBtns.indexOf("update")){var h=this.o.CreateElm("button",["id","editBtn_"+d+"_"+this.o.id],["title",this.o.cmdUpdateBtnTitle],["css",this.o.cmdUpdateBtnCss],[this.o.attrColIndex,d]);this.o.cmdUpdateBtnStyle&&(h.style.cssText=this.o.cmdUpdateBtnStyle),h.innerHTML=this.o.cmdUpdateBtnIcon+this.o.cmdUpdateBtnText,g.appendChild(h),this.o.Event.Add(h,"click",function(a){b.Edit.call(b,a)}),-1==this.edtBtns.indexOf(h)&&(this.edtBtns[d]=h)}if(-1!=this.o.cmdEnabledBtns.indexOf("insert")){var i=this.o.CreateElm("button",["id","createBtn_"+d+"_"+this.o.id],["title",this.o.cmdInsertBtnTitle],["css",this.o.cmdInsertBtnCss],[this.o.attrColIndex,d]);this.o.cmdInsertBtnStyle&&(i.style.cssText=this.o.cmdInsertBtnStyle),i.innerHTML=this.o.cmdInsertBtnIcon+this.o.cmdInsertBtnText,g.appendChild(i),this.o.Event.Add(i,"click",function(){b.AddNewRow(),b.SetCommandEditor(b.o.editorCmdColIndex)}),-1==this.addBtns.indexOf(i)&&(this.addBtns[d]=i)}if(-1!=this.o.cmdEnabledBtns.indexOf("delete")){var j=this.o.CreateElm("button",["id","delBtn_"+d+"_"+this.o.id],["title",this.o.cmdDeleteBtnTitle],["css",this.o.cmdDeleteBtnCss],[this.o.attrColIndex,d]);this.o.cmdDeleteBtnStyle&&(j.style.cssText=this.o.cmdDeleteBtnStyle),j.innerHTML=this.o.cmdDeleteBtnIcon+this.o.cmdDeleteBtnText,g.appendChild(j),this.o.Event.Add(j,"click",function(){b.SubmitDeletedRows()}),-1==this.delBtns.indexOf(j)&&(this.delBtns[d]=j)}if(-1!=this.o.cmdEnabledBtns.indexOf("submit")){var k=this.o.CreateElm("button",["id","postBtn_"+d+"_"+this.o.id],["title",this.o.cmdSubmitBtnTitle],["style","display:none;"],["css",this.o.cmdSubmitBtnCss],[this.o.attrColIndex,d]);k.style.display="none",this.o.cmdSubmitBtnStyle&&(k.style.cssText+=this.o.cmdSubmitBtnStyle),k.innerHTML=this.o.cmdSubmitBtnIcon+this.o.cmdSubmitBtnText,g.appendChild(k),this.o.Event.Add(k,"click",function(a){c.Event.Stop(a),b.CloseRowEditor(),b.SubmitAll()}),-1==this.submitBtns.indexOf(k)&&(this.submitBtns[d]=k)}if(-1!=this.o.cmdEnabledBtns.indexOf("cancel")){var l=this.o.CreateElm("button",["id","cancelBtn_"+d+"_"+this.o.id],["title",this.o.cmdCancelBtnTitle],["style","display:none;"],["css",this.o.cmdCancelBtnCss],[this.o.attrColIndex,d]);l.style.display="none",this.o.cmdCancelBtnStyle&&(l.style.cssText+=this.o.cmdCancelBtnStyle),l.innerHTML=this.o.cmdCancelBtnIcon+this.o.cmdCancelBtnText,g.appendChild(l),this.o.Event.Add(l,"click",function(a){c.Event.Stop(a),b.CloseRowEditor()}),-1==this.cancelBtns.indexOf(l)&&(this.cancelBtns[d]=l)}f.innerHTML="",f.appendChild(g)}}}},CreateUploaderEditor:function(a){var b=new Uploader(this.o,a);return b.Init(),b},OpenCellEditor:function(a){if(a){var b=a.cellIndex,c=this.o.editors[b];if(this.o.onBeforeOpenEditor&&this.o.onBeforeOpenEditor.call(null,this.o,a,c),this.activeCellEditor=a,this.openCellEditor=b,this.o.IsEditorType(b,this.o.edtTypes.uploader))c.O
}j.o.editable&&"click"==j.o.openEditorAction&&l.Edit.call(l,a)}}}},EditTable.prototype={Init:function(){this.Css.Add(this.table,this.tableCss+" "+this.unselectableCss),this.Selection.Init(),this.Editable.Init()},GetCellsNb:function(a){var b=void 0===a?this.table.rows[this.startRow]:this.table.rows[a];return b.cells.length},GetRowsNb:function(){return this.table.rows.length},GetRow:function(a){return this.GetElement(a,"tr")},GetRowByIndex:function(a){return this.table.rows[a]},GetCell:function(a){return this.GetElement(a,"td")||this.GetElement(a,"th")},GetTableFromElement:function(a){if(!a)return null;for(;a.parentNode;){if("TABLE"===a.nodeName.UCase())return a;a=a.parentNode}return null},GetElement:function(a,b){for(var c,d=this.Event.GetElement(a);d.parentNode;){if(d.nodeName.UCase()===b.UCase()&&this.IsParentValid(d)){c=d;break}d=d.parentNode}return c},IsParentValid:function(a){for(;a.parentNode;){if("TABLE"===a.nodeName.UCase())return a.id==this.id?!0:!1;a=a.parentNode}return!1},IsSelectable:function(){return this.selection},IsEditable:function(){return this.editable},ClearSelections:function(){this.Selection.ClearSelections()},IsEditorType:function(a,b){return this.editorTypes[a]===b},IsObj:function(a){return a&&a.constructor==Object},IsFn:function(a){return a&&a.constructor==Function},IsArray:function(a){return a&&a.constructor==Array},Get:function(a){return document.getElementById(a)},Tag:function(a,b){return a?a.getElementsByTagName(b):null},GetText:function(a){if(!a)return"";var b=a.textContent||a.innerText||a.innerHTML.replace(/\<[^<>]+>/g,"");return b.replace(/^\s+/,"").replace(/\s+$/,"").Trim()},CreateElm:function(a){if(void 0!==a&&null!==a&&""!==a){var b=document.createElement(a);if(arguments.length>1)for(var c=0;c<arguments.length;c++){var d=typeof arguments[c];"object"==d.LCase()&&2==arguments[c].length&&b.setAttribute(arguments[c][0],arguments[c][1])}return b}},CreateText:function(a){return document.createTextNode(a)},CreateId:function(a){return(a||this.newRowPrefix)+(new Date).getTime()},StandardBody:function(){return"CSS1Compat"==document.compatMode?document.documentElement:document.body},Css:{Has:function(a,b){return a?a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)")):!1},Add:function(a,b){a&&(this.Has(a,b)||(a.className+=" "+b))},Remove:function(a,b){if(a&&this.Has(a,b)){var c=new RegExp("(\\s|^)"+b+"(\\s|$)");a.className=a.className.replace(c,"")}}},Event:{evt:{},Bind:function(a,b,c){a in this.evt||(this.evt[a]={}),b in this.evt[a]||(this.evt[a][b]=[]),this.evt[a][b].push([c,!0]),this.Add(a,b,c)},Unbind:function(a,b,c){if(a in this.evt){var d=this.evt[a];if(b in d)for(var e=d[b],f=e.length;f--;){var g=e[f];g[0].toString()==c.toString()&&this.Remove(a,b,g[0])}}},Add:function(a,b,c,d){a.attachEvent?a.attachEvent("on"+b,c):a.addEventListener?a.addEventListener(b,c,void 0===d?!1:d):a["on"+b]=c},Remove:function(a,b,c,d){a.detachEvent?a.detachEvent("on"+b,c):a.removeEventListener?a.removeEventListener(b,c,void 0===d?!1:d):a["on"+b]=null},Get:function(a){return a||window.event},GetElement:function(a){return a&&a.target||event&&event.srcElement},GetKey:function(a){var b=this.Get(a),c=b.charCode?b.charCode:b.keyCode?b.keyCode:b.which?b.which:0;return c},Stop:function(a){var b=this.Get(a);b.stopPropagation?b.stopPropagation():b.cancelBubble=!0},Cancel:function(a){var b=this.Get(a);b.preventDefault?b.preventDefault():b.returnValue=!1}},IncludeFile:function(a,b,c,d){var e,f=void 0===d?"script":d,g=this,h=!1,i=this.Tag(document,"head")[0];e="link"==f.LCase()?this.CreateElm("link",["id",a],["type","text/css"],["rel","stylesheet"],["href",b]):this.CreateElm("script",["id",a],["type","text/javascript"],["src",b]),e.onload=e.onreadystatechange=function(){h||this.readyState&&"loaded"!=this.readyState&&"complete"!=this.readyState||(h=!0,"function"==typeof c&&(i.removeChild(e),c.call(null,g,this)))},i.appendChild(e)},Sort:{NumAsc:function(a,b){return a-b},NumDesc:function(a,b){return b-a},IgnoreCase:function(a,b){var c=a.LCase(),d=b.LCase();return d>c?-1:c>d?1:0}}},"undefined"==typeof String.prototype.LCase&&(String.