/*jshint eqeqeq:false, eqnull:true, devel:true */ /*global jQuery, define */ (function( factory ) { "use strict"; if ( typeof define === "function" && define.amd ) { // AMD. Register as an anonymous module. define([ "jquery", "./grid.base", "./grid.common" ], factory ); } else { // Browser globals factory( jQuery ); } }(function( $ ) { "use strict"; //module begin $.jgrid.inlineEdit = $.jgrid.inlineEdit || {}; $.jgrid.extend({ //Editing editRow : function(rowid,keys,oneditfunc,successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc) { // Compatible mode old versions var o={}, args = $.makeArray(arguments).slice(1), $t = this[0]; if( $.type(args[0]) === "object" ) { o = args[0]; } else { if (keys !== undefined) { o.keys = keys; } if ($.isFunction(oneditfunc)) { o.oneditfunc = oneditfunc; } if ($.isFunction(successfunc)) { o.successfunc = successfunc; } if (url !== undefined) { o.url = url; } if (extraparam !== undefined) { o.extraparam = extraparam; } if ($.isFunction(aftersavefunc)) { o.aftersavefunc = aftersavefunc; } if ($.isFunction(errorfunc)) { o.errorfunc = errorfunc; } if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; } // last two not as param, but as object (sorry) //if (restoreAfterError !== undefined) { o.restoreAfterError = restoreAfterError; } //if (mtype !== undefined) { o.mtype = mtype || "POST"; } } o = $.extend(true, { keys : false, keyevent : "keydown", oneditfunc: null, successfunc: null, url: null, extraparam: {}, aftersavefunc: null, errorfunc: null, afterrestorefunc: null, restoreAfterError: true, mtype: "POST", focusField : true, saveui : "enable", savetext : $.jgrid.getRegional($t,'defaults.savetext') }, $.jgrid.inlineEdit, o ); // End compatible return this.each(function(){ var nm, tmp, editable, cnt=0, focus=null, svr={}, ind,cm, bfer, inpclass = $(this).jqGrid('getStyleUI',$t.p.styleUI+".inlinedit",'inputClass', true); if (!$t.grid ) { return; } ind = $($t).jqGrid("getInd",rowid,true); if( ind === false ) {return;} $t.p.beforeAction = true; bfer = $.isFunction( o.beforeEditRow ) ? o.beforeEditRow.call($t,o, rowid) : undefined; if( bfer === undefined ) { bfer = true; } if(!bfer) { $t.p.beforeAction = false; return; } editable = $(ind).attr("editable") || "0"; if (editable === "0" && !$(ind).hasClass("not-editable-row")) { cm = $t.p.colModel; $('td[role="gridcell"]',ind).each( function(i) { nm = cm[i].name; var treeg = $t.p.treeGrid===true && nm === $t.p.ExpandColumn; if(treeg) { tmp = $("span:first",this).html();} else { try { tmp = $.unformat.call($t,this,{rowId:rowid, colModel:cm[i]},i); } catch (_) { tmp = ( cm[i].edittype && cm[i].edittype === 'textarea' ) ? $(this).text() : $(this).html(); } } if ( nm !== 'cb' && nm !== 'subgrid' && nm !== 'rn') { if($t.p.autoencode) { tmp = $.jgrid.htmlDecode(tmp); } svr[nm]=tmp; if(cm[i].editable===true) { if(focus===null) { focus = i; } if (treeg) { $("span:first",this).html(""); } else { $(this).html(""); } var opt = $.extend({},cm[i].editoptions || {},{id:rowid+"_"+nm,name:nm,rowId:rowid, oper:'edit'}); if(!cm[i].edittype) { cm[i].edittype = "text"; } if(tmp === " " || tmp === " " || (tmp.length===1 && tmp.charCodeAt(0)===160) ) {tmp='';} var elc = $.jgrid.createEl.call($t,cm[i].edittype,opt,tmp,true,$.extend({},$.jgrid.ajaxOptions,$t.p.ajaxSelectOptions || {})); $(elc).addClass("editable inline-edit-cell"); if( $.inArray(cm[i].edittype, ['text','textarea','password','select']) > -1) { $(elc).addClass( inpclass ); } if(treeg) { $("span:first",this).append(elc); } else { $(this).append(elc); } $.jgrid.bindEv.call($t, elc, opt); //Again IE if(cm[i].edittype === "select" && cm[i].editoptions!==undefined && cm[i].editoptions.multiple===true && cm[i].editoptions.dataUrl===undefined && $.jgrid.msie()) { $(elc).width($(elc).width()); } cnt++; } } }); if(cnt > 0) { svr.id = rowid; $t.p.savedRow.push(svr); $(ind).attr("editable","1"); if(o.focusField ) { if(typeof o.focusField === 'number' && parseInt(o.focusField,10) <= cm.length) { focus = o.focusField; } setTimeout(function(){ var fe = $("td:eq("+focus+") :input:visible",ind).not(":disabled"); if(fe.length > 0) { fe.focus(); } },0); } if(o.keys===true) { $(ind).on( o.keyevent ,function(e) { if (e.keyCode === 27) { $($t).jqGrid("restoreRow",rowid, o); if($t.p.inlineNav) { try { $($t).jqGrid('showAddEditButtons'); } catch (eer1) {} } return false; } if (e.keyCode === 13) { var ta = e.target; if(ta.tagName === 'TEXTAREA') { return true; } if( $($t).jqGrid("saveRow", rowid, o ) ) { if($t.p.inlineNav) { try { $($t).jqGrid('showAddEditButtons'); } catch (eer2) {} } } return false; } }); } $($t).triggerHandler("jqGridInlineEditRow", [rowid, o]); if( $.isFunction(o.oneditfunc)) { o.oneditfunc.call($t, rowid); } } } }); }, saveRow : function(rowid, successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc) { // Compatible mode old versions var args = $.makeArray(arguments).slice(1), o = {}, $t = this[0]; if( $.type(args[0]) === "object" ) { o = args[0]; } else { if ($.isFunction(successfunc)) { o.successfunc = successfunc; } if (url !== undefined) { o.url = url; } if (extraparam !== undefined) { o.extraparam = extraparam; } if ($.isFunction(aftersavefunc)) { o.aftersavefunc = aftersavefunc; } if ($.isFunction(errorfunc)) { o.errorfunc = errorfunc; } if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; } } o = $.extend(true, { successfunc: null, url: null, extraparam: {}, aftersavefunc: null, errorfunc: null, afterrestorefunc: null, restoreAfterError: true, mtype: "POST", saveui : "enable", savetext : $.jgrid.getRegional($t,'defaults.savetext') }, $.jgrid.inlineEdit, o ); // End compatible var success = false, nm, tmp={}, tmp2={}, tmp3= {}, editable, fr, cv, ind, nullIfEmpty=false, error = $.trim( $($t).jqGrid('getStyleUI', $t.p.styleUI+'.common', 'error', true) ); if (!$t.grid ) { return success; } ind = $($t).jqGrid("getInd",rowid,true); if(ind === false) {return success;} var errors = $.jgrid.getRegional($t, 'errors'), edit =$.jgrid.getRegional($t, 'edit'), bfsr = $.isFunction( o.beforeSaveRow ) ? o.beforeSaveRow.call($t,o, rowid) : undefined; if( bfsr === undefined ) { bfsr = true; } if(!bfsr) { return; } editable = $(ind).attr("editable"); o.url = o.url || $t.p.editurl; if (editable==="1") { var cm, index, elem; $('td[role="gridcell"]',ind).each(function(i) { cm = $t.p.colModel[i]; nm = cm.name; elem = ""; if ( nm !== 'cb' && nm !== 'subgrid' && cm.editable===true && nm !== 'rn' && !$(this).hasClass('not-editable-cell')) { switch (cm.edittype) { case "checkbox": var cbv = ["Yes","No"]; if(cm.editoptions && cm.editoptions.value) { cbv = cm.editoptions.value.split(":"); } tmp[nm]= $("input",this).is(":checked") ? cbv[0] : cbv[1]; elem = $("input",this); break; case 'text': case 'password': case 'textarea': case "button" : tmp[nm]=$("input, textarea",this).val(); elem = $("input, textarea",this); break; case 'select': if(!cm.editoptions.multiple) { tmp[nm] = $("select option:selected",this).val(); tmp2[nm] = $("select option:selected", this).text(); } else { var sel = $("select",this), selectedText = []; tmp[nm] = $(sel).val(); if(tmp[nm]) { tmp[nm]= tmp[nm].join(","); } else { tmp[nm] =""; } $("select option:selected",this).each( function(i,selected){ selectedText[i] = $(selected).text(); } ); tmp2[nm] = selectedText.join(","); } if(cm.formatter && cm.formatter === 'select') { tmp2={}; } elem = $("select",this); break; case 'custom' : try { if(cm.editoptions && $.isFunction(cm.editoptions.custom_value)) { tmp[nm] = cm.editoptions.custom_value.call($t, $(".customelement",this),'get'); if (tmp[nm] === undefined) { throw "e2"; } } else { throw "e1"; } } catch (e) { if (e==="e1") { $.jgrid.info_dialog(errors.errcap,"function 'custom_value' "+edit.msg.nodefined,edit.bClose, {styleUI : $t.p.styleUI }); } else { $.jgrid.info_dialog(errors.errcap,e.message,edit.bClose, {styleUI : $t.p.styleUI }); } } break; } cv = $.jgrid.checkValues.call($t,tmp[nm],i); if(cv[0] === false) { index = i; return false; } if($t.p.autoencode) { tmp[nm] = $.jgrid.htmlEncode(tmp[nm]); } if(o.url !== 'clientArray' && cm.editoptions && cm.editoptions.NullIfEmpty === true) { if(tmp[nm] === "") { tmp3[nm] = 'null'; nullIfEmpty = true; } } } }); if (cv[0] === false){ try { if( $.isFunction($t.p.validationCell) ) { $t.p.validationCell.call($t, elem, cv[1], ind.rowIndex, index); } else { var tr = $($t).jqGrid('getGridRowById', rowid), positions = $.jgrid.findPos(tr); $.jgrid.info_dialog(errors.errcap,cv[1],edit.bClose,{ left:positions[0], top:positions[1]+$(tr).outerHeight(), styleUI : $t.p.styleUI, onClose: function(){ if(index >= 0 ) { $("#"+rowid+"_" +$t.p.colModel[index].name).focus(); } } }); } } catch (e) { alert(cv[1]); } return success; } var idname, opers = $t.p.prmNames, oldRowId = rowid; if ($t.p.keyName === false) { idname = opers.id; } else { idname = $t.p.keyName; } if(tmp) { tmp[opers.oper] = opers.editoper; if (tmp[idname] === undefined || tmp[idname]==="") { tmp[idname] = rowid; } else if (ind.id !== $t.p.idPrefix + tmp[idname]) { // rename rowid var oldid = $.jgrid.stripPref($t.p.idPrefix, rowid); if ($t.p._index[oldid] !== undefined) { $t.p._index[tmp[idname]] = $t.p._index[oldid]; delete $t.p._index[oldid]; } rowid = $t.p.idPrefix + tmp[idname]; $(ind).attr("id", rowid); if ($t.p.selrow === oldRowId) { $t.p.selrow = rowid; } if ($.isArray($t.p.selarrrow)) { var i = $.inArray(oldRowId, $t.p.selarrrow); if (i>=0) { $t.p.selarrrow[i] = rowid; } } if ($t.p.multiselect) { var newCboxId = "jqg_" + $t.p.id + "_" + rowid; $("input.cbox",ind) .attr("id", newCboxId) .attr("name", newCboxId); } // TODO: to test the case of frozen columns } if($t.p.inlineData === undefined) { $t.p.inlineData ={}; } tmp = $.extend({},tmp,$t.p.inlineData,o.extraparam); } if (o.url === 'clientArray') { tmp = $.extend({},tmp, tmp2); if($t.p.autoencode) { $.each(tmp,function(n,v){ tmp[n] = $.jgrid.htmlDecode(v); }); } var k, resp = $($t).jqGrid("setRowData",rowid,tmp); $(ind).attr("editable","0"); for(k=0;k<$t.p.savedRow.length;k++) { if( String($t.p.savedRow[k].id) === String(oldRowId)) {fr = k; break;} } if(fr >= 0) { $t.p.savedRow.splice(fr,1); } $($t).triggerHandler("jqGridInlineAfterSaveRow", [rowid, resp, tmp, o]); if( $.isFunction(o.aftersavefunc) ) { o.aftersavefunc.call($t, rowid, resp, tmp, o); } success = true; $(ind).removeClass("jqgrid-new-row").off("keydown"); } else { $($t).jqGrid("progressBar", {method:"show", loadtype : o.saveui, htmlcontent: o.savetext }); tmp3 = $.extend({},tmp,tmp3); tmp3[idname] = $.jgrid.stripPref($t.p.idPrefix, tmp3[idname]); $.ajax($.extend({ url:o.url, data: $.isFunction($t.p.serializeRowData) ? $t.p.serializeRowData.call($t, tmp3) : tmp3, type: o.mtype, async : false, //?!? complete: function(res,stat){ $($t).jqGrid("progressBar", {method:"hide", loadtype : o.saveui, htmlcontent: o.savetext}); if (stat === "success"){ var ret = true, sucret, k; sucret = $($t).triggerHandler("jqGridInlineSuccessSaveRow", [res, rowid, o]); if (!$.isArray(sucret)) {sucret = [true, tmp3];} if (sucret[0] && $.isFunction(o.successfunc)) {sucret = o.successfunc.call($t, res);} if($.isArray(sucret)) { // expect array - status, data, rowid ret = sucret[0]; tmp = sucret[1] || tmp; } else { ret = sucret; } if (ret===true) { if($t.p.autoencode) { $.each(tmp,function(n,v){ tmp[n] = $.jgrid.htmlDecode(v); }); } if(nullIfEmpty) { $.each(tmp,function( n ){ if(tmp[n] === 'null' ) { tmp[n] = ''; } }); } tmp = $.extend({},tmp, tmp2); $($t).jqGrid("setRowData",rowid,tmp); $(ind).attr("editable","0"); for(k=0;k<$t.p.savedRow.length;k++) { if( String($t.p.savedRow[k].id) === String(rowid)) {fr = k; break;} } if(fr >= 0) { $t.p.savedRow.splice(fr,1); } $($t).triggerHandler("jqGridInlineAfterSaveRow", [rowid, res, tmp, o]); if( $.isFunction(o.aftersavefunc) ) { o.aftersavefunc.call($t, rowid, res, tmp, o); } success = true; $(ind).removeClass("jqgrid-new-row").off("keydown"); } else { $($t).triggerHandler("jqGridInlineErrorSaveRow", [rowid, res, stat, null, o]); if($.isFunction(o.errorfunc) ) { o.errorfunc.call($t, rowid, res, stat, null); } if(o.restoreAfterError === true) { $($t).jqGrid("restoreRow",rowid, o); } } } }, error:function(res,stat,err){ $("#lui_"+$.jgrid.jqID($t.p.id)).hide(); $($t).triggerHandler("jqGridInlineErrorSaveRow", [rowid, res, stat, err, o]); if($.isFunction(o.errorfunc) ) { o.errorfunc.call($t, rowid, res, stat, err); } else { var rT = res.responseText || res.statusText; try { $.jgrid.info_dialog(errors.errcap,'