changed git call from https to git readonly
[atutor.git] / mods / ldap / jscripts / jqgrid / js / grid.inlinedit.js
1 ;(function($){\r
2 /**\r
3  * jqGrid extension for manipulating Grid Data\r
4  * Tony Tomov tony@trirand.com\r
5  * http://trirand.com/blog/ \r
6  * Dual licensed under the MIT and GPL licenses:\r
7  * http://www.opensource.org/licenses/mit-license.php\r
8  * http://www.gnu.org/licenses/gpl.html\r
9 **/ \r
10 $.fn.extend({\r
11 //Editing\r
12         editRow : function(rowid,keys,oneditfunc,succesfunc, url, extraparam, aftersavefunc,errorfunc) {\r
13                 return this.each(function(){\r
14                         var $t = this, nm, tmp, editable, cnt=0, focus=null, svr=[];\r
15                         if (!$t.grid ) { return; }\r
16                         var sz, ml,hc;\r
17                         if( !$t.p.multiselect ) {\r
18                                 editable = $("#"+rowid,$t.grid.bDiv).attr("editable") || "0";\r
19                                 if (editable == "0") {\r
20                                         $('#'+rowid+' td',$t.grid.bDiv).each( function(i) {                                             \r
21                                                 nm = $t.p.colModel[i].name;\r
22                                                 hc = $t.p.colModel[i].hidden===true ? true : false;\r
23                                                 tmp = $(this).html().replace(/\&nbsp\;/ig,'');\r
24                                                 svr[nm]=tmp;\r
25                                                 if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true && !hc) {\r
26                                                         if(focus===null) { focus = i; }\r
27                                                         $(this).html("");\r
28                                                         var opt = $.extend($t.p.colModel[i].editoptions || {} ,{id:rowid+"_"+nm,name:nm});\r
29                                                         if(!$t.p.colModel[i].edittype) { $t.p.colModel[i].edittype = "text"; }\r
30                                                         var elc = createEl($t.p.colModel[i].edittype,opt,tmp,$(this));\r
31                                                         $(elc).addClass("editable");\r
32                                                         $(this).append(elc);\r
33                                                         //Agin IE\r
34                                                         if($t.p.colModel[i].edittype == "select" && $t.p.colModel[i].editoptions.multiple===true && $.browser.msie) {\r
35                                                                 $(elc).width($(elc).width());\r
36                                                         }\r
37                                                         cnt++;\r
38                                                 }\r
39                                         });\r
40                                         if(cnt > 0) {\r
41                                                 svr['id'] = rowid; $t.p.savedRow.push(svr);\r
42                                                 $('#'+rowid,$t.grid.bDiv).attr("editable","1");\r
43                                                 $('#'+rowid+" td:eq("+focus+") input",$t.grid.bDiv).focus();\r
44                                                 if(keys===true) {\r
45                                                         $('#'+rowid,$t.grid.bDiv).bind("keydown",function(e) {\r
46                                                                 if (e.keyCode === 27) { $($t).restoreRow(rowid); }\r
47                                                                 if (e.keyCode === 13) {\r
48                                                                         $($t).saveRow(rowid,succesfunc, url, extraparam, aftersavefunc,errorfunc);\r
49                                                                 }\r
50                                                                 e.stopPropagation();\r
51                                                         });\r
52                                                 }\r
53                                                 if( typeof oneditfunc === "function") { oneditfunc(rowid); }\r
54                                         }\r
55                                 }\r
56                         }\r
57                 });\r
58         },\r
59         saveRow : function(rowid, succesfunc, url, extraparam, aftersavefunc,errorfunc) {\r
60                 return this.each(function(){\r
61                 var $t = this, nm, tmp={}, tmp2, editable, fr;\r
62                 if (!$t.grid ) { return; }\r
63                 editable = $('#'+rowid,$t.grid.bDiv).attr("editable");\r
64                 url = url ? url : $t.p.editurl;\r
65                 if (editable==="1" && url) {\r
66                         $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {\r
67                                 nm = $t.p.colModel[i].name;\r
68                                 if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true) {\r
69                                         if( $t.p.colModel[i].hidden===true) { tmp[nm] = $(this).html(); }\r
70                                         else {\r
71                                                 switch ($t.p.colModel[i].edittype) {\r
72                                                         case "checkbox":\r
73                                                                 tmp[nm]=  $("input",this).attr("checked") ? 1 : 0; \r
74                                                                 break;\r
75                                                         case 'text':\r
76                                                         case 'password':\r
77                                                                 tmp[nm]= $("input",this).val();\r
78                                                                 break;\r
79                                                         case 'textarea':\r
80                                                                 tmp[nm]= $("textarea",this).val();\r
81                                                                 break;\r
82                                                         case 'select':\r
83                                                                 if(!$t.p.colModel[i].editoptions.multiple) {\r
84                                                                         tmp[nm] = $("select>option:selected",this).val();\r
85                                                                 } else {\r
86                                                                         var sel = $("select",this);\r
87                                                                         tmp[nm] = $(sel).val();\r
88                                                                 }\r
89                                                                 break;\r
90                                                 }\r
91                                         }\r
92                                 }\r
93                         });\r
94                         if(tmp) { tmp["id"] = rowid; if(extraparam) { $.extend(tmp,extraparam);} }\r
95                         if(!$t.grid.hDiv.loading) {\r
96                                 $t.grid.hDiv.loading = true;\r
97                                 $("div.loading",$t.grid.hDiv).fadeIn("fast");\r
98                                 $.ajax({url:url,\r
99                                         data: tmp,\r
100                                         type: "POST",\r
101                                         complete: function(res,stat){\r
102                                                 if (stat === "success"){\r
103                                                         var ret;\r
104                                                         if( typeof succesfunc === "function") { ret = succesfunc(res); }\r
105                                                         else ret = true;\r
106                                                         if (ret===true) {\r
107                                                                 $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {\r
108                                                                         nm = $t.p.colModel[i].name;\r
109                                                                         if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true) {\r
110                                                                                 switch ($t.p.colModel[i].edittype) {\r
111                                                                                         case "select":\r
112                                                                                                 if(!$t.p.colModel[i].editoptions.multiple) {\r
113                                                                                                         tmp2 = $("select>option:selected", this).text();\r
114                                                                                                 } else if( $t.p.colModel[i].editoptions.multiple ===true) {\r
115                                                                                                         var selectedText = [];\r
116                                                                                                         $("select > option:selected",this).each(\r
117                                                                                                                 function(i,selected){\r
118                                                                                                                         selectedText[i] = $(selected).text();\r
119                                                                                                                 }\r
120                                                                                                         );\r
121                                                                                                         tmp2= selectedText.join(",");\r
122                                                                                                 }\r
123                                                                                                 break;\r
124                                                                                         case "checkbox":\r
125                                                                                                 var cbv = $t.p.colModel[i].editoptions.value.split(":") || ["Yes","No"];\r
126                                                                                                 tmp2 = $("input",this).attr("checked") ? cbv[0] : cbv[1];\r
127                                                                                                 break;\r
128                                                                                         case "password":\r
129                                                                                         case "text":\r
130                                                                                         case "textarea":\r
131                                                                                                 tmp2 = $("input, textarea", this).val();\r
132                                                                                                 break;\r
133                                                                                 }\r
134                                                                                 $(this).empty();\r
135                                                                                 $(this).html(tmp2 || " ");\r
136                                                                         }\r
137                                                                 });\r
138                                                                 $('#'+rowid,$t.grid.bDiv).attr("editable","0");\r
139                                                                 for( var k=0;k<$t.p.savedRow.length;k++) {\r
140                                                                         if( $t.p.savedRow[k].id===rowid) {fr = k; break;}\r
141                                                                 };\r
142                                                                 if(fr >= 0) { $t.p.savedRow.splice(fr,1); }\r
143                                                                 if( typeof aftersavefunc === "function") { aftersavefunc(rowid,res.responseText); }\r
144                                                         } else { $($t).restoreRow(rowid); }\r
145                                                 }\r
146                                         },\r
147                                         error:function(res,stat){\r
148                                                 if(typeof errorfunc == "function") {\r
149                                                         errorfunc(res,stat)\r
150                                                 } else {\r
151                                                         alert("Error Row: "+rowid+" Result: " +res.status+":"+res.statusText+" Status: "+stat);\r
152                                                 }\r
153                                         }\r
154                                 });\r
155                                 $t.grid.hDiv.loading = false;\r
156                                 $("div.loading",$t.grid.hDiv).fadeOut("fast");\r
157                                 $("#"+rowid,$t.grid.bDiv).unbind("keydown");\r
158                         }\r
159                 }\r
160                 });\r
161         },\r
162         restoreRow : function(rowid) {\r
163                 return this.each(function(){\r
164                         var $t= this, nm, fr;\r
165                         if (!$t.grid ) { return; }\r
166                         for( var k=0;k<$t.p.savedRow.length;k++) {\r
167                                 if( $t.p.savedRow[k].id===rowid) {fr = k; break;}\r
168                         };\r
169                         if(fr >= 0) {\r
170                                 $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {\r
171                                         nm = $t.p.colModel[i].name;\r
172                                         if ( nm !== 'cb' && nm !== 'subgrid') {\r
173                                                 $(this).empty();\r
174                                                 $(this).html($t.p.savedRow[fr][nm] || "&nbsp;");\r
175                                         }\r
176                                 });\r
177                                 $('#'+rowid,$t.grid.bDiv).attr("editable","0");\r
178                                 $t.p.savedRow.splice(fr,1);\r
179                         }\r
180                 });\r
181         }\r
182 //end inline edit\r
183 });\r
184 })(jQuery);\r