forked from hkalbertl/jquery.appendGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.appendGrid-1.2.0.min.js
13 lines (13 loc) · 17.4 KB
/
jquery.appendGrid-1.2.0.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
* jQuery appendGrid v1.2.0
* https://appendgrid.apphb.com/
*
* Copyright 2013 Albert L.
* Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html)
* and MIT (http://www.opensource.org/licenses/mit-license.php) licenses.
*
* Depends:
* jQuery v1.9.1+
* jquery UI v1.10.2+
*/
(function(e){var n={caption:null,initRows:3,initData:null,firstCellWidth:32,lastCellWidth:89,columns:null,i18n:null,idPrefix:null,rowDragging:false,hideMoveUpDown:false,dataLoaded:null,afterRowAppended:null,afterRowInserted:null,afterRowSwapped:null,beforeRowRemove:null,afterRowRemoved:null,_uniqueIndex:0,_rowOrder:null,_isDataLoaded:false,_visibleCount:0};var p={type:"text",name:null,value:null,display:null,displayCss:null,cellCss:null,ctrlAttr:null,ctrlProp:null,ctrlCss:null,ctrlClass:null,ctrlOptions:null,uiOption:null,uiTooltip:null,customBuilder:null,customGetter:null,customSetter:null,onClick:null,onChange:null};var j={append:"Append Row",removeLast:"Remove Last Row",insert:"Insert Row Above",remove:"Remove Current Row",moveUp:"Move Up",moveDown:"Move Down",rowDrag:"Sort Row"};var i={init:function(E){var B=this;var A,t,D,v,x,u;if(B.length>0){if(!e.isArray(E.columns)||E.columns.length==0){alert("Cannot initial grid without column information!");return B}A=B[0];if(k(A.tagName)||A.tagName!="TABLE"){alert("Cannot initial grid on element other than TABLE!");return B}var w=e.extend({},n,E);if(e.isPlainObject(E.i18n)){w.textResources=e.extend({},j,E.i18n)}else{w.textResources=e.extend({},j)}w._rowOrder=[];if(k(w.idPrefix)){if(k(A.id)||A.id==""){w.idPrefix="ag"+new Date().getTime()}else{w.idPrefix=A.id}}t=document.createElement("thead");t.className="ui-widget-header";D=document.createElement("tbody");D.className="ui-widget-content";v=document.createElement("tfoot");v.className="ui-widget-header";e(A).empty().addClass("appendGrid").addClass("ui-widget").append(t,D,v);w._visibleCount=0;x=t.insertRow(-1);u=x.insertCell(-1);u.className="ui-widget-header";e(u).width(w.firstCellWidth);for(var C=0;C<w.columns.length;C++){var y=e.extend({},p,w.columns[C]);w.columns[C]=y;if(w.columns[C].type!="hidden"){w._visibleCount++;u=x.insertCell(-1);u.className="ui-widget-header";e(u).text(w.columns[C].display);if(w.columns[C].displayCss){e(u).css(w.columns[C].displayCss)}}}u=x.insertCell(-1);u.className="ui-widget-header";e(u).width(w.lastCellWidth);if(w.caption){x=t.insertRow(0);u=x.insertCell(-1);u.className="ui-state-active caption";u.colSpan=2+w._visibleCount;e(u).text(w.caption)}x=v.insertRow(-1);u=x.insertCell(-1);u.colSpan=2+w._visibleCount;e(u).append(e("<input/>").attr({type:"hidden",id:w.idPrefix+"_rowOrder",name:w.idPrefix+"_rowOrder"}).addClass("row-order"));e(u).append(e("<button/>").attr({type:"button",title:w.textResources.append}).button({icons:{primary:"ui-icon-plusthick"},text:false}).click(function(){d(A,1,null,null)}));e(u).append(e("<button/>").attr({type:"button",title:w.textResources.removeLast}).button({icons:{primary:"ui-icon-closethick"},text:false}).click(function(){g(A,null,this.value,false)}));if(w.rowDragging){e(D).sortable({axis:"y",containment:A,handle:".rowDrag",helper:function(G,F){var H=F.children();var z=F.clone();z.children().each(function(I){e(this).width(H.eq(I).width())});return z},update:function(G,H){var z=H.item[0].id.substring(H.item[0].id.lastIndexOf("_")+1);var F=H.item[0].rowIndex-e("tr",t).length;m(A,H.originalPosition.top>H.position.top,z,F)}})}e(A).data("appendGrid",w);if(e.isArray(E.initData)){a(A,E.initData,true)}else{e(A).appendGrid("appendRow",w.initRows)}}return B},isReady:function(){var v=this,t=false;if(v.length>0){var u=v.first().data("appendGrid");if(u){t=true}}return t},isDataLoaded:function(){var v=this,t=null;if(this.length==1){var u=v.data("appendGrid");if(u){return u._isDataLoaded}else{return false}}else{return false}},load:function(t){var u=this;if(u.length>0){a(u[0],t,false)}return u},appendRow:function(t){return this.appendGrid("insertRow",t)},insertRow:function(w,A,u){var z=this;if(z.length>0){var t=z[0],y=null;var x=e(t).data("appendGrid");if(!x){alert("`appendGrid` does not initialized")}else{y=d(t,w||1,A,u);if(e.isNumeric(A)||e.isNumeric(u)){o(t,y.rowIndex);var v=x._rowOrder[y.addedRows[0]];e("#"+x.idPrefix+"_Insert_"+v,t).focus()}}}return z},removeRow:function(z,y){var x=this,A=-1;if(x.length>0){var w=x[0],t,B,v;var u=e(w).data("appendGrid");if(!u){alert("`appendGrid` does not initialized")}else{if(u._rowOrder.length>0){g(w,z,y,true)}}}return x},moveUpRow:function(z,y){var x=this,A=-1;if(x.length>0){var w=x[0],D,C,t,v;var u=e(w).data("appendGrid");if(!u){alert("`appendGrid` does not initialized")}else{var B=null;D=w.getElementsByTagName("tbody")[0];if(e.isNumeric(z)&&z>0&&z<u._rowOrder.length){B=z;y=u._rowOrder[z];C=document.getElementById(u.idPrefix+"_Row_"+y,w)}else{if(e.isNumeric(y)){B=f(y,u);C=document.getElementById(u.idPrefix+"_Row_"+y,w)}}if(B!=null&&B>0){t=document.getElementById(u.idPrefix+"_Row_"+u._rowOrder[B-1],w);D.removeChild(C);D.insertBefore(C,t);u._rowOrder[B]=u._rowOrder[B-1];u._rowOrder[B-1]=y;v=e("td.first",t).html();e("td.first",t).html(e("td.first",C).html());e("td.first",C).html(v);b(w,u);e("td.last button.moveUp",C).removeClass("ui-state-hover").blur();e("td.last button.moveUp",t).focus();if(u.afterRowSwapped){u.afterRowSwapped(w,B,B-1)}}}}return x},moveDownRow:function(z,y){var x=this,A=-1;if(x.length>0){var w=x[0],D,C,t,v;var u=e(w).data("appendGrid");if(!u){alert("`appendGrid` does not initialized")}else{var B=null;D=w.getElementsByTagName("tbody")[0];if(e.isNumeric(z)&&z>=0&&z<u._rowOrder.length-1){B=z;y=u._rowOrder[z];C=document.getElementById(u.idPrefix+"_Row_"+y,w)}else{if(e.isNumeric(y)){B=f(y,u);C=document.getElementById(u.idPrefix+"_Row_"+y,w)}}if(B!=null&&B!=u._rowOrder.length-1){t=document.getElementById(u.idPrefix+"_Row_"+u._rowOrder[B+1],w);D.removeChild(t);D.insertBefore(t,C);u._rowOrder[B]=u._rowOrder[B+1];u._rowOrder[B+1]=y;v=e("td.first",t).html();e("td.first",t).html(e("td.first",C).html());e("td.first",C).html(v);b(w,u);e("td.last button.moveDown",C).removeClass("ui-state-hover").blur();e("td.last button.moveDown",t).focus();if(u.afterRowSwapped){u.afterRowSwapped(w,B,B+1)}}}}return x},getRowCount:function(){var u=this;if(u.length>0){var t=u.data("appendGrid");if(t){return t._rowOrder.length}else{alert("`appendGrid` does not initialized")}}else{alert("Cannot get values on multiple grid")}return null},getUniqueIndex:function(v){var u=this;if(u.length>0&&v>=0){var t=u.data("appendGrid");if(t){if(v<t._rowOrder.length){return t._rowOrder[v]}}else{alert("`appendGrid` does not initialized")}}return null},getRowIndex:function(t){var v=this;if(v.length>0){var u=v.data("appendGrid");if(u){for(var w=0;w<u._rowOrder.length;w++){if(u._rowOrder[w]==t){return w}}return null}else{alert("`appendGrid` does not initialized")}}return null},getRowValue:function(y,u,x){var w=this,t=null;if(w.length>0){var v=w.data("appendGrid");if(v){if(e.isNumeric(y)&&y>=0&&y<v._rowOrder.length){u=v._rowOrder[y]}if(!k(u)){t=h(v,u,x)}}else{alert("`appendGrid` does not initialized")}}return t},getAllValue:function(w){var x=this,t=null,u;if(x.length>0){var v=e(x).data("appendGrid");if(v){t=w?{}:[];for(var y=0;y<v._rowOrder.length;y++){if(w){u=h(v,v._rowOrder[y],y);e.extend(t,u)}else{u=h(v,v._rowOrder[y]);t.push(u)}}if(w){t._RowCount=v._rowOrder.length}}}return t},getCtrlValue:function(t,w){var u=this;if(u.length>0){settings=u.data("appendGrid");if(settings&&w>=0&&w<settings._rowOrder.length){for(var v=0;v<settings.columns.length;v++){if(settings.columns[v].name===t){return c(settings,v,settings._rowOrder[w])}}}}return null},setCtrlValue:function(u,A,w){var x=this;if(x.length>0){var t=this,v=e(this).data("appendGrid");if(v&&A>=0&&A<v._rowOrder.length){for(var y=0;y<v.columns.length;y++){if(v.columns[y].name==u){l(v,y,v._rowOrder[A],w);break}}}}return x},getCellCtrl:function(v,u){var w=this,t=null;if(w.length==1){settings=w.data("appendGrid");if(!settings){alert("`appendGrid` does not initialized")}else{for(var x=0;x<settings.columns.length;x++){if(settings.columns[x].name===v){return q(settings.columns[x].type,settings.idPrefix,v,u);break}}}}return null},getCellCtrlByRowIndex:function(v,y){var w=this,t=null;if(w.length==1){settings=w.data("appendGrid");if(!settings){alert("`appendGrid` does not initialized")}else{if(y>=0&&y<settings._rowOrder.length){var u=settings._rowOrder[y];for(var x=0;x<settings.columns.length;x++){if(settings.columns[x].name===v){return q(settings.columns[x].type,settings.idPrefix,v,u)}}}}}return null},setCellCtrlByRowIndex:function(v,A,w){var x=this,t=null;if(x.length==1){settings=x.data("appendGrid");if(!settings){alert("`appendGrid` does not initialized")}else{if(A>=0&&A<settings._rowOrder.length){var u=settings._rowOrder[A];for(var y=0;y<settings.columns.length;y++){if(settings.columns[y].name===v){return setCellCtrl(settings.columns[y].type,settings.idPrefix,v,u,w)}}}}}return null},getRowOrder:function(){var v=this,t=null;if(this.length==1){var u=v.data("appendGrid");if(u){t=u._rowOrder.slice()}else{alert("`appendGrid` does not initialized")}}else{alert("Cannot get values on multiple grid")}return t}};function d(I,A,H,E){var M=e(I).data("appendGrid");var L=[],t=null,u,F,J=[];var v=I.getElementsByTagName("thead")[0];var C=I.getElementsByTagName("tbody")[0];if(e.isNumeric(E)){for(var B=0;B<M._rowOrder.length;B++){if(M._rowOrder[B]==E){H=B;if(B!=0){t=B-1}break}}}else{if(e.isNumeric(H)){if(H>=M._rowOrder.length){H=null}else{t=H-1}}else{if(M._rowOrder.length!=0){H=null;t=M._rowOrder.length-1}}}for(var B=0;B<A;B++){M._uniqueIndex++;u=M._uniqueIndex;J.length=0;if(e.isNumeric(H)){M._rowOrder.splice(H,0,u);tbRow=C.insertRow(H);L.push(H)}else{M._rowOrder.push(u);tbRow=C.insertRow(-1);L.push(M._rowOrder.length-1)}tbRow.id=M.idPrefix+"_Row_"+u;tbCell=tbRow.insertCell(-1);e(tbCell).addClass("ui-widget-content first").text(M._rowOrder.length).data("appendGrid",u);for(var D=0;D<M.columns.length;D++){if(M.columns[D].type=="hidden"){J.push(D);continue}tbCell=tbRow.insertCell(-1);tbCell.className="ui-widget-content";if(M.columns[D].cellCss!=null){e(tbCell).css(M.columns[D].cellCss)}F=null;if(M.columns[D].type=="custom"){if(typeof(M.columns[D].customBuilder)=="function"){F=M.columns[D].customBuilder(tbCell,M.idPrefix,M.columns[D].name,u)}}else{if(M.columns[D].type=="select"){F=document.createElement("select");F.id=M.idPrefix+"_"+M.columns[D].name+"_"+u;F.name=F.id;if(e.isArray(M.columns[D].ctrlOptions)){if(M.columns[D].ctrlOptions.length>0){if(e.isPlainObject(M.columns[D].ctrlOptions[0])){for(var G=0;G<M.columns[D].ctrlOptions.length;G++){F.options[F.options.length]=new Option(M.columns[D].ctrlOptions[G].label,M.columns[D].ctrlOptions[G].value)}}else{for(var G=0;G<M.columns[D].ctrlOptions.length;G++){F.options[F.options.length]=new Option(M.columns[D].ctrlOptions[G],M.columns[D].ctrlOptions[G])}}}}else{if(e.isPlainObject(M.columns[D].ctrlOptions)){for(var G in M.columns[D].ctrlOptions){F.options[F.options.length]=new Option(M.columns[D].ctrlOptions[G],G)}}else{if(typeof(M.columns[D].ctrlOptions)=="string"){var K=M.columns[D].ctrlOptions.split(";");for(var G=0;G<K.length;G++){var N=K[G].indexOf(":");if(-1==N){F.options[F.options.length]=new Option(K[G],K[G])}else{F.options[F.options.length]=new Option(K[G].substring(N+1,K[G].length),K[G].substring(0,N))}}}}}tbCell.appendChild(F)}else{if(M.columns[D].type=="checkbox"){F=document.createElement("input");F.type="checkbox";F.id=M.idPrefix+"_"+M.columns[D].name+"_"+u;F.name=F.id;F.value=1;tbCell.appendChild(F);tbCell.style.textAlign="center"}else{if(-1!=M.columns[D].type.search(/^(color|date|datetime|datetime\-local|email|month|number|range|search|tel|time|url|week)$/)){F=document.createElement("input");try{F.type=M.columns[D].type}catch(w){}F.id=M.idPrefix+"_"+M.columns[D].name+"_"+u;F.name=F.id;tbCell.appendChild(F)}else{F=document.createElement("input");F.type="text";F.id=M.idPrefix+"_"+M.columns[D].name+"_"+u;F.name=F.id;tbCell.appendChild(F);if(M.columns[D].type=="ui-datepicker"){e(F).datepicker(M.columns[D].uiOption)}else{if(M.columns[D].type=="ui-spinner"){e(F).spinner(M.columns[D].uiOption)}else{if(M.columns[D].type=="ui-autocomplete"){e(F).autocomplete(M.columns[D].uiOption)}}}}}}}if(M.columns[D].type!="custom"){if(M.columns[D].ctrlAttr!=null){e(F).attr(M.columns[D].ctrlAttr)}if(M.columns[D].ctrlProp!=null){e(F).prop(M.columns[D].ctrlProp)}if(M.columns[D].ctrlCss!=null){e(F).css(M.columns[D].ctrlCss)}if(M.columns[D].ctrlClass!=null){e(F).addClass(M.columns[D].ctrlClass)}if(M.columns[D].uiTooltip){e(F).tooltip(M.columns[D].uiTooltip)}if(typeof(M.columns[D].onClick)=="function"){e(F).click({caller:I,callback:M.columns[D].onClick,uniqueIndex:u},function(x){x.data.callback(x,e(x.data.caller).appendGrid("getRowIndex",x.data.uniqueIndex))})}if(typeof(M.columns[D].onChange)=="function"){e(F).change({caller:I,callback:M.columns[D].onChange,uniqueIndex:u},function(x){x.data.callback(x,e(x.data.caller).appendGrid("getRowIndex",x.data.uniqueIndex))})}}if(!k(M.columns[D].value)){l(M,D,u,M.columns[D].value)}}tbCell=tbRow.insertCell(-1);tbCell.className="ui-widget-content last";e(tbCell).append(e("<button/>").addClass("insert").val(u).attr({id:M.idPrefix+"_Insert_"+u,type:"button",title:M.textResources.insert,tabindex:-1}).button({icons:{primary:"ui-icon-arrowreturnthick-1-w"},text:false}).click(function(){e(I).appendGrid("insertRow",1,null,this.value)}));e(tbCell).append(e("<button/>").addClass("delete").val(u).attr({id:M.idPrefix+"_Delete_"+u,type:"button",title:M.textResources.remove,tabindex:-1}).button({icons:{primary:"ui-icon-trash"},text:false}).click(function(){g(I,null,this.value,false)}));if(!M.hideMoveUpDown){e(tbCell).append(e("<button/>").addClass("moveUp").val(u).attr({id:M.idPrefix+"_MoveUp_"+u,type:"button",title:M.textResources.moveUp,tabindex:-1}).button({icons:{primary:"ui-icon-arrowthick-1-n"},text:false}).click(function(){e(I).appendGrid("moveUpRow",null,this.value)}));e(tbCell).append(e("<button/>").addClass("moveDown").val(u).attr({id:M.idPrefix+"_MoveDown_"+u,type:"button",title:M.textResources.moveDown,tabindex:-1}).button({icons:{primary:"ui-icon-arrowthick-1-s"},text:false}).click(function(){e(I).appendGrid("moveDownRow",null,this.value)}))}if(M.rowDragging){e(tbCell).append(e("<div/>").attr("title",M.textResources.rowDrag).addClass("rowDrag ui-state-default ui-corner-all").append(e("<div/>").addClass("ui-icon ui-icon-carat-2-n-s")))}for(var D=0;D<J.length;D++){F=document.createElement("input");F.id=M.idPrefix+"_"+M.columns[J[D]].name+"_"+u;F.type="hidden";tbCell.appendChild(F)}}b(I,M);if(e.isNumeric(H)){if(typeof(M.afterRowInserted)=="function"){M.afterRowInserted(I,t,L)}}else{if(typeof(M.afterRowAppended)=="function"){M.afterRowAppended(I,t,L)}}return{addedRows:L,parentIndex:t,rowIndex:H}}function g(u,A,t,w){var v=e(u).data("appendGrid");var y=u.getElementsByTagName("tbody")[0];if(e.isNumeric(t)){for(var x=0;x<v._rowOrder.length;x++){if(v._rowOrder[x]==t){A=x;break}}}if(e.isNumeric(A)){if(w||typeof(v.beforeRowRemove)!="function"||v.beforeRowRemove(u,A)){v._rowOrder.splice(A,1);y.deleteRow(A);b(u,v);o(u,A);if(typeof(v.afterRowRemoved)=="function"){v.afterRowRemoved(u,A)}}}else{if(w||typeof(v.beforeRowRemove)!="function"||v.beforeRowRemove(u,v._rowOrder.length-1)){t=v._rowOrder.pop();y.deleteRow(-1);b(u,v);if(typeof(v.afterRowRemoved)=="function"){v.afterRowRemoved(u,null)}}}}function o(t,w){var u=e(t).data("appendGrid");for(var v=w;v<u._rowOrder.length;v++){e("#"+u.idPrefix+"_Row_"+u._rowOrder[v]+" td.first",t).text(v+1)}}function a(z,v,y){var D,x,u,A,C;var w=e(z).data("appendGrid");if(w){D=z.getElementsByTagName("tbody")[0];e(D).empty();w._rowOrder.length=0;w._uniqueIndex=0;if(v!=null&&v.length){C=d(z,v.length,null,null);for(var t=0;t<C.addedRows.length;t++){for(var B=0;B<w.columns.length;B++){l(w,B,w._rowOrder[t],v[t][w.columns[B].name])}}}w._isDataLoaded=true;if(y){w.initData=null}e(z).data("appendGrid",w);if(typeof(w.dataLoaded)=="function"){w.dataLoaded(z)}}}function f(t,u){for(var v=0;v<u._rowOrder.length;v++){if(u._rowOrder[v]==t){return v}}return null}function k(t){return typeof(t)=="undefined"||t==null}function r(u,t){if(!k(u)&&e.isPlainObject(u)&&!k(u[t])){return u[t]}return null}function b(t,u){e(t).data("appendGrid",u);e("#"+u.idPrefix+"_rowOrder",t).val(u._rowOrder.join())}function s(u,t){var w=null;for(var v=0;v<u._rowOrder.length;v++){if(u._rowOrder[v]==t){return v}}return w}function h(w,u,y){var t={},v=null;for(var x=0;x<w.columns.length;x++){v=w.columns[x].name+(k(y)?"":"_"+y);t[v]=c(w,x,u)}return t}function c(x,v,t){var y=null,w=x.columns[v].type,u=x.columns[v].name;if(w=="checkbox"){y=q(w,x.idPrefix,u,t);if(y==null){return null}else{return y.checked?1:0}}else{if(w=="custom"){if(typeof(x.columns[v].customGetter)=="function"){return x.columns[v].customGetter(x.idPrefix,u,t)}else{return null}}else{y=q(w,x.idPrefix,u,t);if(y==null){return null}else{return y.value}}}}function q(v,w,u,t){return document.getElementById(w+"_"+u+"_"+t)}function l(x,v,t,y){var w=x.columns[v].type;var u=x.columns[v].name;if(w=="checkbox"){q(w,x.idPrefix,u,t).checked=(y!=null&&y!=0)}else{if(w=="custom"){if(typeof(x.columns[v].customSetter)=="function"){x.columns[v].customSetter(x.idPrefix,u,t,y)}}else{q(w,x.idPrefix,u,t).value=(y==null?"":y)}}}function m(u,A,t,v){var w=e(u).data("appendGrid");var y=-1;for(var x=0;x<w._rowOrder.length;x++){if(w._rowOrder[x]==t){if(A){y=v;w._rowOrder.splice(x,1);w._rowOrder.splice(v,0,t)}else{y=x;w._rowOrder.splice(v+1,0,t);w._rowOrder.splice(x,1)}break}}o(u,y);b(u,w)}e.fn.appendGrid=function(t){if(i[t]){return i[t].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof(t)==="object"||!t){return i.init.apply(this,arguments)}else{e.error("`"+t+"` method does not supported by appendGrid...")}}}})(jQuery);