PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/RazorPad.Website/Scripts/grid/grid.custom.js

#
JavaScript | 440 lines | 423 code | 2 blank | 15 comment | 137 complexity | e67e54aafca8f01d958ad29f1f81180f MD5 | raw file
  1. (function($){
  2. /**
  3. * jqGrid extension for custom methods
  4. * Tony Tomov tony@trirand.com
  5. * http://trirand.com/blog/
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl-2.0.html
  9. **/
  10. /*global jQuery, $ */
  11. $.jgrid.extend({
  12. getColProp : function(colname){
  13. var ret ={}, $t = this[0];
  14. if ( !$t.grid ) { return false; }
  15. var cM = $t.p.colModel;
  16. for ( var i =0;i<cM.length;i++ ) {
  17. if ( cM[i].name == colname ) {
  18. ret = cM[i];
  19. break;
  20. }
  21. }
  22. return ret;
  23. },
  24. setColProp : function(colname, obj){
  25. //do not set width will not work
  26. return this.each(function(){
  27. if ( this.grid ) {
  28. if ( obj ) {
  29. var cM = this.p.colModel;
  30. for ( var i =0;i<cM.length;i++ ) {
  31. if ( cM[i].name == colname ) {
  32. $.extend(this.p.colModel[i],obj);
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. });
  39. },
  40. sortGrid : function(colname,reload, sor){
  41. return this.each(function(){
  42. var $t=this,idx=-1;
  43. if ( !$t.grid ) { return;}
  44. if ( !colname ) { colname = $t.p.sortname; }
  45. for ( var i=0;i<$t.p.colModel.length;i++ ) {
  46. if ( $t.p.colModel[i].index == colname || $t.p.colModel[i].name==colname ) {
  47. idx = i;
  48. break;
  49. }
  50. }
  51. if ( idx!=-1 ){
  52. var sort = $t.p.colModel[idx].sortable;
  53. if ( typeof sort !== 'boolean' ) { sort = true; }
  54. if ( typeof reload !=='boolean' ) { reload = false; }
  55. if ( sort ) { $t.sortData("jqgh_"+$t.p.id+"_" + colname, idx, reload, sor); }
  56. }
  57. });
  58. },
  59. GridDestroy : function () {
  60. return this.each(function(){
  61. if ( this.grid ) {
  62. if ( this.p.pager ) { // if not part of grid
  63. $(this.p.pager).remove();
  64. }
  65. var gid = this.id;
  66. try {
  67. $("#gbox_"+gid).remove();
  68. } catch (_) {}
  69. }
  70. });
  71. },
  72. GridUnload : function(){
  73. return this.each(function(){
  74. if ( !this.grid ) {return;}
  75. var defgrid = {id: $(this).attr('id'),cl: $(this).attr('class')};
  76. if (this.p.pager) {
  77. $(this.p.pager).empty().removeClass("ui-state-default ui-jqgrid-pager corner-bottom");
  78. }
  79. var newtable = document.createElement('table');
  80. $(newtable).attr({id:defgrid.id});
  81. newtable.className = defgrid.cl;
  82. var gid = this.id;
  83. $(newtable).removeClass("ui-jqgrid-btable");
  84. if( $(this.p.pager).parents("#gbox_"+gid).length === 1 ) {
  85. $(newtable).insertBefore("#gbox_"+gid).show();
  86. $(this.p.pager).insertBefore("#gbox_"+gid);
  87. } else {
  88. $(newtable).insertBefore("#gbox_"+gid).show();
  89. }
  90. $("#gbox_"+gid).remove();
  91. });
  92. },
  93. setGridState : function(state) {
  94. return this.each(function(){
  95. if ( !this.grid ) {return;}
  96. var $t = this;
  97. if(state == 'hidden'){
  98. $(".ui-jqgrid-bdiv, .ui-jqgrid-hdiv","#gview_"+$t.p.id).slideUp("fast");
  99. if($t.p.pager) {$($t.p.pager).slideUp("fast");}
  100. if($t.p.toppager) {$($t.p.toppager).slideUp("fast");}
  101. if($t.p.toolbar[0]===true) {
  102. if( $t.p.toolbar[1]=='both') {
  103. $($t.grid.ubDiv).slideUp("fast");
  104. }
  105. $($t.grid.uDiv).slideUp("fast");
  106. }
  107. if($t.p.footerrow) { $(".ui-jqgrid-sdiv","#gbox_"+$t.p.id).slideUp("fast"); }
  108. $(".ui-jqgrid-titlebar-close span",$t.grid.cDiv).removeClass("ui-icon-circle-triangle-n").addClass("ui-icon-circle-triangle-s");
  109. $t.p.gridstate = 'hidden';
  110. } else if(state=='visible') {
  111. $(".ui-jqgrid-hdiv, .ui-jqgrid-bdiv","#gview_"+$t.p.id).slideDown("fast");
  112. if($t.p.pager) {$($t.p.pager).slideDown("fast");}
  113. if($t.p.toppager) {$($t.p.toppager).slideDown("fast");}
  114. if($t.p.toolbar[0]===true) {
  115. if( $t.p.toolbar[1]=='both') {
  116. $($t.grid.ubDiv).slideDown("fast");
  117. }
  118. $($t.grid.uDiv).slideDown("fast");
  119. }
  120. if($t.p.footerrow) { $(".ui-jqgrid-sdiv","#gbox_"+$t.p.id).slideDown("fast"); }
  121. $(".ui-jqgrid-titlebar-close span",$t.grid.cDiv).removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-n");
  122. $t.p.gridstate = 'visible';
  123. }
  124. });
  125. },
  126. filterToolbar : function(p){
  127. p = $.extend({
  128. autosearch: true,
  129. searchOnEnter : true,
  130. beforeSearch: null,
  131. afterSearch: null,
  132. beforeClear: null,
  133. afterClear: null,
  134. searchurl : '',
  135. stringResult: false,
  136. groupOp: 'AND',
  137. defaultSearch : "bw"
  138. },p || {});
  139. return this.each(function(){
  140. var $t = this;
  141. if(this.ftoolbar) { return; }
  142. var triggerToolbar = function() {
  143. var sdata={}, j=0, v, nm, sopt={},so;
  144. $.each($t.p.colModel,function(i,n){
  145. nm = this.index || this.name;
  146. switch (this.stype) {
  147. case 'select' :
  148. so = (this.searchoptions && this.searchoptions.sopt) ? this.searchoptions.sopt[0] : 'eq';
  149. v = $("#gs_"+$.jgrid.jqID(this.name),$t.grid.hDiv).val();
  150. if(v) {
  151. sdata[nm] = v;
  152. sopt[nm] = so;
  153. j++;
  154. } else {
  155. try {
  156. delete $t.p.postData[nm];
  157. } catch (e) {}
  158. }
  159. break;
  160. case 'text':
  161. so = (this.searchoptions && this.searchoptions.sopt) ? this.searchoptions.sopt[0] : p.defaultSearch;
  162. v = $("#gs_"+$.jgrid.jqID(this.name), $t.grid.hDiv).val();
  163. if(v) {
  164. sdata[nm] = v;
  165. sopt[nm] = so;
  166. j++;
  167. } else {
  168. try {
  169. delete $t.p.postData[nm];
  170. } catch (z) {}
  171. }
  172. break;
  173. }
  174. });
  175. var sd = j>0 ? true : false;
  176. if(p.stringResult === true || $t.p.datatype == "local") {
  177. var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
  178. var gi=0;
  179. $.each(sdata,function(i,n){
  180. if (gi > 0) {ruleGroup += ",";}
  181. ruleGroup += "{\"field\":\"" + i + "\",";
  182. ruleGroup += "\"op\":\"" + sopt[i] + "\",";
  183. n+="";
  184. ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
  185. gi++;
  186. });
  187. ruleGroup += "]}";
  188. $.extend($t.p.postData,{filters:ruleGroup});
  189. $.each(['searchField', 'searchString', 'searchOper'], function(i, n){
  190. if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
  191. });
  192. } else {
  193. $.extend($t.p.postData,sdata);
  194. }
  195. var saveurl;
  196. if($t.p.searchurl) {
  197. saveurl = $t.p.url;
  198. $($t).jqGrid("setGridParam",{url:$t.p.searchurl});
  199. }
  200. var bsr = false;
  201. if($.isFunction(p.beforeSearch)){bsr = p.beforeSearch.call($t);}
  202. if(!bsr) { $($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]); }
  203. if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
  204. if($.isFunction(p.afterSearch)){p.afterSearch();}
  205. };
  206. var clearToolbar = function(trigger){
  207. var sdata={}, v, j=0, nm;
  208. trigger = (typeof trigger != 'boolean') ? true : trigger;
  209. $.each($t.p.colModel,function(i,n){
  210. v = (this.searchoptions && this.searchoptions.defaultValue) ? this.searchoptions.defaultValue : "";
  211. nm = this.index || this.name;
  212. switch (this.stype) {
  213. case 'select' :
  214. var v1;
  215. $("#gs_"+$.jgrid.jqID(nm)+" option",$t.grid.hDiv).each(function (i){
  216. if(i===0) { this.selected = true; }
  217. if ($(this).text() == v) {
  218. this.selected = true;
  219. v1 = $(this).val();
  220. return false;
  221. }
  222. });
  223. if (v1) {
  224. // post the key and not the text
  225. sdata[nm] = v1;
  226. j++;
  227. } else {
  228. try {
  229. delete $t.p.postData[nm];
  230. } catch(e) {}
  231. }
  232. break;
  233. case 'text':
  234. $("#gs_"+$.jgrid.jqID(nm),$t.grid.hDiv).val(v);
  235. if(v) {
  236. sdata[nm] = v;
  237. j++;
  238. } else {
  239. try {
  240. delete $t.p.postData[nm];
  241. } catch (y){}
  242. }
  243. break;
  244. }
  245. });
  246. var sd = j>0 ? true : false;
  247. if(p.stringResult === true || $t.p.datatype == "local") {
  248. var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
  249. var gi=0;
  250. $.each(sdata,function(i,n){
  251. if (gi > 0) {ruleGroup += ",";}
  252. ruleGroup += "{\"field\":\"" + i + "\",";
  253. ruleGroup += "\"op\":\"" + "eq" + "\",";
  254. n+="";
  255. ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
  256. gi++;
  257. });
  258. ruleGroup += "]}";
  259. $.extend($t.p.postData,{filters:ruleGroup});
  260. $.each(['searchField', 'searchString', 'searchOper'], function(i, n){
  261. if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
  262. });
  263. } else {
  264. $.extend($t.p.postData,sdata);
  265. }
  266. var saveurl;
  267. if($t.p.searchurl) {
  268. saveurl = $t.p.url;
  269. $($t).jqGrid("setGridParam",{url:$t.p.searchurl});
  270. }
  271. var bcv = false;
  272. if($.isFunction(p.beforeClear)){bcv = p.beforeClear.call($t);}
  273. if(!bcv) {
  274. if(trigger) {
  275. $($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]);
  276. }
  277. }
  278. if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
  279. if($.isFunction(p.afterClear)){p.afterClear();}
  280. };
  281. var toggleToolbar = function(){
  282. var trow = $("tr.ui-search-toolbar",$t.grid.hDiv);
  283. if(trow.css("display")=='none') { trow.show(); }
  284. else { trow.hide(); }
  285. };
  286. // create the row
  287. function bindEvents(selector, events) {
  288. var jElem = $(selector);
  289. if (jElem[0]) {
  290. jQuery.each(events, function() {
  291. if (this.data !== undefined) {
  292. jElem.bind(this.type, this.data, this.fn);
  293. } else {
  294. jElem.bind(this.type, this.fn);
  295. }
  296. });
  297. }
  298. }
  299. var tr = $("<tr class='ui-search-toolbar' role='rowheader'></tr>");
  300. var timeoutHnd;
  301. $.each($t.p.colModel,function(i,n){
  302. var cm=this, thd , th, soptions,surl,self;
  303. th = $("<th role='columnheader' class='ui-state-default ui-th-column ui-th-"+$t.p.direction+"'></th>");
  304. thd = $("<div style='width:100%;position:relative;height:100%;padding-right:0.3em;'></div>");
  305. if(this.hidden===true) { $(th).css("display","none");}
  306. this.search = this.search === false ? false : true;
  307. if(typeof this.stype == 'undefined' ) {this.stype='text';}
  308. soptions = $.extend({},this.searchoptions || {});
  309. if(this.search){
  310. switch (this.stype)
  311. {
  312. case "select":
  313. surl = this.surl || soptions.dataUrl;
  314. if(surl) {
  315. // data returned should have already constructed html select
  316. // primitive jQuery load
  317. self = thd;
  318. $.ajax($.extend({
  319. url: surl,
  320. dataType: "html",
  321. complete: function(res,status) {
  322. if(soptions.buildSelect !== undefined) {
  323. var d = soptions.buildSelect(res);
  324. if (d) { $(self).append(d); }
  325. } else {
  326. $(self).append(res.responseText);
  327. }
  328. if(soptions.defaultValue) { $("select",self).val(soptions.defaultValue); }
  329. $("select",self).attr({name:cm.index || cm.name, id: "gs_"+cm.name});
  330. if(soptions.attr) {$("select",self).attr(soptions.attr);}
  331. $("select",self).css({width: "100%"});
  332. // preserve autoserch
  333. if(soptions.dataInit !== undefined) { soptions.dataInit($("select",self)[0]); }
  334. if(soptions.dataEvents !== undefined) { bindEvents($("select",self)[0],soptions.dataEvents); }
  335. if(p.autosearch===true){
  336. $("select",self).change(function(e){
  337. triggerToolbar();
  338. return false;
  339. });
  340. }
  341. res=null;
  342. }
  343. }, $.jgrid.ajaxOptions, $t.p.ajaxSelectOptions || {} ));
  344. } else {
  345. var oSv;
  346. if(cm.searchoptions && cm.searchoptions.value) {
  347. oSv = cm.searchoptions.value;
  348. } else if(cm.editoptions && cm.editoptions.value) {
  349. oSv = cm.editoptions.value;
  350. }
  351. if (oSv) {
  352. var elem = document.createElement("select");
  353. elem.style.width = "100%";
  354. $(elem).attr({name:cm.index || cm.name, id: "gs_"+cm.name});
  355. var so, sv, ov;
  356. if(typeof oSv === "string") {
  357. so = oSv.split(";");
  358. for(var k=0; k<so.length;k++){
  359. sv = so[k].split(":");
  360. ov = document.createElement("option");
  361. ov.value = sv[0]; ov.innerHTML = sv[1];
  362. elem.appendChild(ov);
  363. }
  364. } else if(typeof oSv === "object" ) {
  365. for ( var key in oSv) {
  366. if(oSv.hasOwnProperty(key)) {
  367. ov = document.createElement("option");
  368. ov.value = key; ov.innerHTML = oSv[key];
  369. elem.appendChild(ov);
  370. }
  371. }
  372. }
  373. if(soptions.defaultValue) { $(elem).val(soptions.defaultValue); }
  374. if(soptions.attr) {$(elem).attr(soptions.attr);}
  375. if(soptions.dataInit !== undefined) { soptions.dataInit(elem); }
  376. if(soptions.dataEvents !== undefined) { bindEvents(elem, soptions.dataEvents); }
  377. $(thd).append(elem);
  378. if(p.autosearch===true){
  379. $(elem).change(function(e){
  380. triggerToolbar();
  381. return false;
  382. });
  383. }
  384. }
  385. }
  386. break;
  387. case 'text':
  388. var df = soptions.defaultValue ? soptions.defaultValue: "";
  389. $(thd).append("<input type='text' style='width:95%;padding:0px;' name='"+(cm.index || cm.name)+"' id='gs_"+cm.name+"' value='"+df+"'/>");
  390. if(soptions.attr) {$("input",thd).attr(soptions.attr);}
  391. if(soptions.dataInit !== undefined) { soptions.dataInit($("input",thd)[0]); }
  392. if(soptions.dataEvents !== undefined) { bindEvents($("input",thd)[0], soptions.dataEvents); }
  393. if(p.autosearch===true){
  394. if(p.searchOnEnter) {
  395. $("input",thd).keypress(function(e){
  396. var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  397. if(key == 13){
  398. triggerToolbar();
  399. return false;
  400. }
  401. return this;
  402. });
  403. } else {
  404. $("input",thd).keydown(function(e){
  405. var key = e.which;
  406. switch (key) {
  407. case 13:
  408. return false;
  409. case 9 :
  410. case 16:
  411. case 37:
  412. case 38:
  413. case 39:
  414. case 40:
  415. case 27:
  416. break;
  417. default :
  418. if(timeoutHnd) { clearTimeout(timeoutHnd); }
  419. timeoutHnd = setTimeout(function(){triggerToolbar();},500);
  420. }
  421. });
  422. }
  423. }
  424. break;
  425. }
  426. }
  427. $(th).append(thd);
  428. $(tr).append(th);
  429. });
  430. $("table thead",$t.grid.hDiv).append(tr);
  431. this.ftoolbar = true;
  432. this.triggerToolbar = triggerToolbar;
  433. this.clearToolbar = clearToolbar;
  434. this.toggleToolbar = toggleToolbar;
  435. });
  436. }
  437. });
  438. })(jQuery);