grid.treegrid.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*jshint eqeqeq:false */
  2. /*global jQuery, define */
  3. (function( factory ) {
  4. "use strict";
  5. if ( typeof define === "function" && define.amd ) {
  6. // AMD. Register as an anonymous module.
  7. define([
  8. "jquery",
  9. "js/grid.base"
  10. ], factory );
  11. } else {
  12. // Browser globals
  13. factory( jQuery );
  14. }
  15. }(function( $ ) {
  16. "use strict";
  17. //module begin
  18. $.jgrid.extend({
  19. setTreeNode : function(i, len){
  20. return this.each(function(){
  21. var $t = this;
  22. if( !$t.grid || !$t.p.treeGrid ) {return;}
  23. var expCol = $t.p.expColInd,
  24. expanded = $t.p.treeReader.expanded_field,
  25. isLeaf = $t.p.treeReader.leaf_field,
  26. level = $t.p.treeReader.level_field,
  27. icon = $t.p.treeReader.icon_field,
  28. loaded = $t.p.treeReader.loaded, lft, rgt, curLevel, ident,lftpos, twrap,
  29. ldat, lf,
  30. common = $.jgrid.styleUI[($t.p.styleUI || 'jQueryUI')].common,
  31. index = i;
  32. $($t).triggerHandler("jqGridBeforeSetTreeNode", [index, len]);
  33. if($.isFunction($t.p.beforeSetTreeNode)) {
  34. $t.p.beforeSetTreeNode.call($t, index, len);
  35. }
  36. while(i<len) {
  37. var ind = $.jgrid.stripPref($t.p.idPrefix, $t.rows[i].id), dind = $t.p._index[ind], expan;
  38. ldat = $t.p.data[dind];
  39. //$t.rows[i].level = ldat[level];
  40. if($t.p.treeGridModel === 'nested') {
  41. if(!ldat[isLeaf]) {
  42. lft = parseInt(ldat[$t.p.treeReader.left_field],10);
  43. rgt = parseInt(ldat[$t.p.treeReader.right_field],10);
  44. // NS Model
  45. ldat[isLeaf] = (rgt === lft+1) ? 'true' : 'false';
  46. $t.rows[i].cells[$t.p._treeleafpos].innerHTML = ldat[isLeaf];
  47. }
  48. }
  49. //else {
  50. //row.parent_id = rd[$t.p.treeReader.parent_id_field];
  51. //}
  52. curLevel = parseInt(ldat[level],10);
  53. if($t.p.tree_root_level === 0) {
  54. ident = curLevel+1;
  55. lftpos = curLevel;
  56. } else {
  57. ident = curLevel;
  58. lftpos = curLevel -1;
  59. }
  60. twrap = "<div class='tree-wrap tree-wrap-"+$t.p.direction+"' style='width:"+(ident*18)+"px;'>";
  61. twrap += "<div style='"+($t.p.direction==="rtl" ? "right:" : "left:")+(lftpos*18)+"px;' class='"+common.icon_base+" ";
  62. if(ldat[loaded] !== undefined) {
  63. if(ldat[loaded]==="true" || ldat[loaded]===true) {
  64. ldat[loaded] = true;
  65. } else {
  66. ldat[loaded] = false;
  67. }
  68. }
  69. if(ldat[isLeaf] === "true" || ldat[isLeaf] === true) {
  70. twrap += ((ldat[icon] !== undefined && ldat[icon] !== "") ? ldat[icon] : $t.p.treeIcons.leaf)+" tree-leaf treeclick";
  71. ldat[isLeaf] = true;
  72. lf="leaf";
  73. } else {
  74. ldat[isLeaf] = false;
  75. lf="";
  76. }
  77. ldat[expanded] = ((ldat[expanded] === "true" || ldat[expanded] === true) ? true : false) && (ldat[loaded] || ldat[loaded] === undefined);
  78. if(ldat[expanded] === false) {
  79. twrap += ((ldat[isLeaf] === true) ? "'" : $t.p.treeIcons.plus+" tree-plus treeclick'");
  80. } else {
  81. twrap += ((ldat[isLeaf] === true) ? "'" : $t.p.treeIcons.minus+" tree-minus treeclick'");
  82. }
  83. twrap += "></div></div>";
  84. $($t.rows[i].cells[expCol]).wrapInner("<span class='cell-wrapper"+lf+"'></span>").prepend(twrap);
  85. if(curLevel !== parseInt($t.p.tree_root_level,10)) {
  86. //var pn = $($t).jqGrid('getNodeParent',ldat);
  87. //expan = pn && pn.hasOwnProperty(expanded) ? pn[expanded] : true;
  88. expan = $($t).jqGrid('isVisibleNode',ldat); // overhead
  89. if( !expan ){
  90. $($t.rows[i]).css("display","none");
  91. }
  92. }
  93. $($t.rows[i].cells[expCol])
  94. .find("div.treeclick")
  95. .on("click",function(e){
  96. var target = e.target || e.srcElement,
  97. ind2 =$.jgrid.stripPref($t.p.idPrefix,$(target,$t.rows).closest("tr.jqgrow")[0].id),
  98. pos = $t.p._index[ind2];
  99. if(!$t.p.data[pos][isLeaf]){
  100. if($t.p.data[pos][expanded]){
  101. $($t).jqGrid("collapseRow",$t.p.data[pos]);
  102. $($t).jqGrid("collapseNode",$t.p.data[pos]);
  103. } else {
  104. $($t).jqGrid("expandRow",$t.p.data[pos]);
  105. $($t).jqGrid("expandNode",$t.p.data[pos]);
  106. }
  107. }
  108. return false;
  109. });
  110. if($t.p.ExpandColClick === true) {
  111. $($t.rows[i].cells[expCol])
  112. .find("span.cell-wrapper")
  113. .css("cursor","pointer")
  114. .on("click",function(e) {
  115. var target = e.target || e.srcElement,
  116. ind2 =$.jgrid.stripPref($t.p.idPrefix,$(target,$t.rows).closest("tr.jqgrow")[0].id),
  117. pos = $t.p._index[ind2];
  118. if(!$t.p.data[pos][isLeaf]){
  119. if($t.p.data[pos][expanded]){
  120. $($t).jqGrid("collapseRow",$t.p.data[pos]);
  121. $($t).jqGrid("collapseNode",$t.p.data[pos]);
  122. } else {
  123. $($t).jqGrid("expandRow",$t.p.data[pos]);
  124. $($t).jqGrid("expandNode",$t.p.data[pos]);
  125. }
  126. }
  127. $($t).jqGrid("setSelection",ind2);
  128. return false;
  129. });
  130. }
  131. i++;
  132. }
  133. $($t).triggerHandler("jqGridAfterSetTreeNode", [index, len]);
  134. if($.isFunction($t.p.afterSetTreeNode)) {
  135. $t.p.afterSetTreeNode.call($t, index, len);
  136. }
  137. });
  138. },
  139. setTreeGrid : function() {
  140. return this.each(function (){
  141. var $t = this, i=0, pico, ecol = false, nm, key, tkey, dupcols=[],
  142. classes = $.jgrid.styleUI[($t.p.styleUI || 'jQueryUI')].treegrid;
  143. if(!$t.p.treeGrid) {return;}
  144. if(!$t.p.treedatatype ) {$.extend($t.p,{treedatatype: $t.p.datatype});}
  145. if($t.p.loadonce) { $t.p.treedatatype = 'local'; }
  146. $t.p.subGrid = false;$t.p.altRows =false;
  147. //bvn
  148. if (!$t.p.treeGrid_bigData) {
  149. $t.p.pgbuttons = false;
  150. $t.p.pginput = false;
  151. $t.p.rowList = [];
  152. }
  153. $t.p.gridview = true;
  154. //bvn
  155. if($t.p.rowTotal === null && !$t.p.treeGrid_bigData ) { $t.p.rowNum = 10000; }
  156. $t.p.multiselect = false;
  157. // $t.p.rowList = [];
  158. $t.p.expColInd = 0;
  159. pico = classes.icon_plus;
  160. if($t.p.styleUI === 'jQueryUI') {
  161. pico += ($t.p.direction==="rtl" ? 'w' : 'e');
  162. }
  163. $t.p.treeIcons = $.extend({plus:pico, minus: classes.icon_minus, leaf: classes.icon_leaf},$t.p.treeIcons || {});
  164. if($t.p.treeGridModel === 'nested') {
  165. $t.p.treeReader = $.extend({
  166. level_field: "level",
  167. left_field:"lft",
  168. right_field: "rgt",
  169. leaf_field: "isLeaf",
  170. expanded_field: "expanded",
  171. loaded: "loaded",
  172. icon_field: "icon"
  173. },$t.p.treeReader);
  174. } else if($t.p.treeGridModel === 'adjacency') {
  175. $t.p.treeReader = $.extend({
  176. level_field: "level",
  177. parent_id_field: "parent",
  178. leaf_field: "isLeaf",
  179. expanded_field: "expanded",
  180. loaded: "loaded",
  181. icon_field: "icon"
  182. },$t.p.treeReader );
  183. }
  184. for ( key in $t.p.colModel){
  185. if($t.p.colModel.hasOwnProperty(key)) {
  186. nm = $t.p.colModel[key].name;
  187. if( nm === $t.p.ExpandColumn && !ecol ) {
  188. ecol = true;
  189. $t.p.expColInd = i;
  190. }
  191. i++;
  192. //
  193. for(tkey in $t.p.treeReader) {
  194. if($t.p.treeReader.hasOwnProperty(tkey) && $t.p.treeReader[tkey] === nm) {
  195. dupcols.push(nm);
  196. }
  197. }
  198. }
  199. }
  200. $.each($t.p.treeReader,function(j,n){
  201. if(n && $.inArray(n, dupcols) === -1){
  202. if(j==='leaf_field') { $t.p._treeleafpos= i; }
  203. i++;
  204. $t.p.colNames.push(n);
  205. $t.p.colModel.push({name:n,width:1,hidden:true,sortable:false,resizable:false,hidedlg:true,editable:true,search:false});
  206. }
  207. });
  208. });
  209. },
  210. expandRow: function (record){
  211. this.each(function(){
  212. var $t = this;
  213. //bvn
  214. if (!$t.p.treeGrid_bigData) {
  215. var $rootpages = $t.p.lastpage;
  216. }
  217. if(!$t.grid || !$t.p.treeGrid) {return;}
  218. var childern = $($t).jqGrid("getNodeChildren",record),
  219. //if ($($t).jqGrid("isVisibleNode",record)) {
  220. expanded = $t.p.treeReader.expanded_field,
  221. rowid = record[$t.p.localReader.id],
  222. ret = $($t).triggerHandler("jqGridBeforeExpandTreeGridRow", [rowid, record, childern]);
  223. if(ret === undefined ) {
  224. ret = true;
  225. }
  226. if(ret && $.isFunction($t.p.beforeExpandTreeGridRow)) {
  227. ret = $t.p.beforeExpandTreeGridRow.call($t, rowid, record, childern);
  228. }
  229. if( ret === false ) { return; }
  230. $(childern).each(function(){
  231. var id = $t.p.idPrefix + $.jgrid.getAccessor(this,$t.p.localReader.id);
  232. $($($t).jqGrid('getGridRowById', id)).css("display","");
  233. if(this[expanded]) {
  234. $($t).jqGrid("expandRow",this);
  235. }
  236. });
  237. $($t).triggerHandler("jqGridAfterExpandTreeGridRow", [rowid, record, childern]);
  238. if($.isFunction($t.p.afterExpandTreeGridRow)) {
  239. $t.p.afterExpandTreeGridRow.call($t, rowid, record, childern);
  240. }
  241. //bvn
  242. if (!$t.p.treeGrid_bigData) {
  243. $t.p.lastpage = $rootpages;
  244. }
  245. //}
  246. });
  247. },
  248. collapseRow : function (record) {
  249. this.each(function(){
  250. var $t = this;
  251. if(!$t.grid || !$t.p.treeGrid) {return;}
  252. var childern = $($t).jqGrid("getNodeChildren",record),
  253. expanded = $t.p.treeReader.expanded_field,
  254. rowid = record[$t.p.localReader.id],
  255. ret = $($t).triggerHandler("jqGridBeforeCollapseTreeGridRow", [rowid, record, childern]);
  256. if(ret === undefined ) {
  257. ret = true;
  258. }
  259. if(ret && $.isFunction($t.p.beforeCollapseTreeGridRow)) {
  260. ret = $t.p.beforeCollapseTreeGridRow.call($t, rowid, record, childern);
  261. }
  262. if( ret === false ) { return; }
  263. $(childern).each(function(){
  264. var id = $t.p.idPrefix + $.jgrid.getAccessor(this,$t.p.localReader.id);
  265. $($($t).jqGrid('getGridRowById', id)).css("display","none");
  266. if(this[expanded]){
  267. $($t).jqGrid("collapseRow",this);
  268. }
  269. });
  270. $($t).triggerHandler("jqGridAfterCollapseTreeGridRow", [rowid, record, childern]);
  271. if($.isFunction($t.p.afterCollapseTreeGridRow)) {
  272. $t.p.afterCollapseTreeGridRow.call($t, rowid, record, childern);
  273. }
  274. });
  275. },
  276. // NS ,adjacency models
  277. getRootNodes : function(currentview) {
  278. var result = [];
  279. this.each(function(){
  280. var $t = this, level, parent_id, view;
  281. if(!$t.grid || !$t.p.treeGrid) {return;}
  282. if( typeof currentview !== 'boolean') {
  283. currentview = false;
  284. }
  285. if(currentview) {
  286. view = $($t).jqGrid('getRowData', null, true);
  287. } else {
  288. view = $t.p.data;
  289. }
  290. switch ($t.p.treeGridModel) {
  291. case 'nested' :
  292. level = $t.p.treeReader.level_field;
  293. $(view).each(function() {
  294. if(parseInt(this[level],10) === parseInt($t.p.tree_root_level,10)) {
  295. if(currentview){
  296. result.push($t.p.data[$t.p._index[this[$t.p.keyName]]]);
  297. } else {
  298. result.push(this);
  299. }
  300. }
  301. });
  302. break;
  303. case 'adjacency' :
  304. parent_id = $t.p.treeReader.parent_id_field;
  305. $(view).each(function(){
  306. if(this[parent_id] === null || String(this[parent_id]).toLowerCase() === "null") {
  307. if(currentview){
  308. result.push($t.p.data[$t.p._index[this[$t.p.keyName]]]);
  309. } else {
  310. result.push(this);
  311. }
  312. }
  313. });
  314. break;
  315. }
  316. });
  317. return result;
  318. },
  319. getNodeDepth : function(rc) {
  320. var ret = null;
  321. this.each(function(){
  322. if(!this.grid || !this.p.treeGrid) {return;}
  323. var $t = this;
  324. switch ($t.p.treeGridModel) {
  325. case 'nested' :
  326. var level = $t.p.treeReader.level_field;
  327. ret = parseInt(rc[level],10) - parseInt($t.p.tree_root_level,10);
  328. break;
  329. case 'adjacency' :
  330. ret = $($t).jqGrid("getNodeAncestors",rc).length;
  331. break;
  332. }
  333. });
  334. return ret;
  335. },
  336. getNodeParent : function(rc) {
  337. var result = null;
  338. this.each(function(){
  339. var $t = this;
  340. if(!$t.grid || !$t.p.treeGrid) {return;}
  341. switch ($t.p.treeGridModel) {
  342. case 'nested' :
  343. var lftc = $t.p.treeReader.left_field,
  344. rgtc = $t.p.treeReader.right_field,
  345. levelc = $t.p.treeReader.level_field,
  346. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  347. $(this.p.data).each(function(){
  348. if(parseInt(this[levelc],10) === level-1 && parseInt(this[lftc],10) < lft && parseInt(this[rgtc],10) > rgt) {
  349. result = this;
  350. return false;
  351. }
  352. });
  353. break;
  354. case 'adjacency' :
  355. var parent_id = $t.p.treeReader.parent_id_field,
  356. dtid = $t.p.localReader.id,
  357. ind = rc[dtid], pos = $t.p._index[ind];
  358. while(pos--) {
  359. if( String( $t.p.data[pos][dtid]) === String( $.jgrid.stripPref($t.p.idPrefix, rc[parent_id]) ) ) {
  360. result = $t.p.data[pos];
  361. break;
  362. }
  363. }
  364. break;
  365. }
  366. });
  367. return result;
  368. },
  369. getNodeChildren : function(rc, currentview) {
  370. var result = [];
  371. this.each(function(){
  372. var $t = this;
  373. if(!$t.grid || !$t.p.treeGrid) {return;}
  374. var i, len = currentview ? this.rows.length : this.p.data.length, row;
  375. switch ($t.p.treeGridModel) {
  376. case 'nested' :
  377. var lftc = $t.p.treeReader.left_field,
  378. rgtc = $t.p.treeReader.right_field,
  379. levelc = $t.p.treeReader.level_field,
  380. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  381. for(i=0; i < len; i++) {
  382. row = currentview ? $t.p.data[$t.p._index[this.rows[i].id]] : $t.p.data[i];
  383. if(row && parseInt(row[levelc],10) === level+1 && parseInt(row[lftc],10) > lft && parseInt(row[rgtc],10) < rgt) {
  384. result.push(row);
  385. }
  386. }
  387. break;
  388. case 'adjacency' :
  389. var parent_id = $t.p.treeReader.parent_id_field,
  390. dtid = $t.p.localReader.id;
  391. for(i=0; i < len; i++) {
  392. row = currentview ? $t.p.data[$t.p._index[this.rows[i].id]] : $t.p.data[i];
  393. if(row && String(row[parent_id]) === String( $.jgrid.stripPref($t.p.idPrefix, rc[dtid]) ) ) {
  394. result.push(row);
  395. }
  396. }
  397. break;
  398. }
  399. });
  400. return result;
  401. },
  402. getFullTreeNode : function(rc, expand) {
  403. var result = [];
  404. this.each(function(){
  405. var $t = this, len,expanded = $t.p.treeReader.expanded_field;
  406. if(!$t.grid || !$t.p.treeGrid) {return;}
  407. if(expand == null || typeof expand !== 'boolean') {
  408. expand = false;
  409. }
  410. switch ($t.p.treeGridModel) {
  411. case 'nested' :
  412. var lftc = $t.p.treeReader.left_field,
  413. rgtc = $t.p.treeReader.right_field,
  414. levelc = $t.p.treeReader.level_field,
  415. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  416. $(this.p.data).each(function(){
  417. if(parseInt(this[levelc],10) >= level && parseInt(this[lftc],10) >= lft && parseInt(this[lftc],10) <= rgt) {
  418. if(expand) { this[expanded] = true; }
  419. result.push(this);
  420. }
  421. });
  422. break;
  423. case 'adjacency' :
  424. if(rc) {
  425. result.push(rc);
  426. var parent_id = $t.p.treeReader.parent_id_field,
  427. dtid = $t.p.localReader.id;
  428. $(this.p.data).each(function(i){
  429. len = result.length;
  430. for (i = 0; i < len; i++) {
  431. if ( String( $.jgrid.stripPref($t.p.idPrefix, result[i][dtid]) ) === String( this[parent_id] ) ) {
  432. if(expand) { this[expanded] = true; }
  433. result.push(this);
  434. break;
  435. }
  436. }
  437. });
  438. }
  439. break;
  440. }
  441. });
  442. return result;
  443. },
  444. // End NS, adjacency Model
  445. getNodeAncestors : function(rc, reverse, expanded) {
  446. var ancestors = [];
  447. if(reverse === undefined ) {
  448. reverse = false;
  449. }
  450. this.each(function(){
  451. if(!this.grid || !this.p.treeGrid) {return;}
  452. if(expanded === undefined ) {
  453. expanded = false;
  454. } else {
  455. expanded = this.p.treeReader.expanded_field;
  456. }
  457. var parent = $(this).jqGrid("getNodeParent",rc);
  458. while (parent) {
  459. if(expanded) {
  460. try{
  461. parent[expanded] = true;
  462. } catch (etn) {}
  463. }
  464. if(reverse) {
  465. ancestors.unshift(parent);
  466. } else {
  467. ancestors.push(parent);
  468. }
  469. parent = $(this).jqGrid("getNodeParent",parent);
  470. }
  471. });
  472. return ancestors;
  473. },
  474. isVisibleNode : function(rc) {
  475. var result = true;
  476. this.each(function(){
  477. var $t = this;
  478. if(!$t.grid || !$t.p.treeGrid) {return;}
  479. var ancestors = $($t).jqGrid("getNodeAncestors",rc),
  480. expanded = $t.p.treeReader.expanded_field;
  481. $(ancestors).each(function(){
  482. result = result && this[expanded];
  483. if(!result) {return false;}
  484. });
  485. });
  486. return result;
  487. },
  488. isNodeLoaded : function(rc) {
  489. var result;
  490. this.each(function(){
  491. var $t = this;
  492. if(!$t.grid || !$t.p.treeGrid) {return;}
  493. var isLeaf = $t.p.treeReader.leaf_field,
  494. loaded = $t.p.treeReader.loaded;
  495. if(rc !== undefined ) {
  496. if(rc[loaded] !== undefined) {
  497. result = rc[loaded];
  498. } else if( rc[isLeaf] || $($t).jqGrid("getNodeChildren",rc).length > 0){
  499. result = true;
  500. } else {
  501. result = false;
  502. }
  503. } else {
  504. result = false;
  505. }
  506. });
  507. return result;
  508. },
  509. setLeaf : function (rc, state, collapsed) {
  510. return this.each(function(){
  511. var id = $.jgrid.getAccessor(rc,this.p.localReader.id),
  512. rc1 = $("#"+id,this.grid.bDiv)[0],
  513. isLeaf = this.p.treeReader.leaf_field;
  514. try {
  515. var dr = this.p._index[id];
  516. if(dr != null) {
  517. this.p.data[dr][isLeaf] = state;
  518. }
  519. } catch(E){}
  520. if(state === true) {
  521. // set it in data
  522. $("div.treeclick",rc1).removeClass(this.p.treeIcons.minus+" tree-minus "+this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.leaf +" tree-leaf");
  523. } else if(state === false) {
  524. var ico = this.p.treeIcons.minus+" tree-minus";
  525. if(collapsed) {
  526. ico = this.p.treeIcons.plus+" tree-plus";
  527. }
  528. $("div.treeclick",rc1).removeClass(this.p.treeIcons.leaf +" tree-leaf").addClass( ico );
  529. }
  530. });
  531. },
  532. reloadNode: function(rc, reloadcurrent) {
  533. return this.each(function(){
  534. if(!this.grid || !this.p.treeGrid) {return;}
  535. var rid = this.p.localReader.id,
  536. currselection = this.p.selrow;
  537. $(this).jqGrid("delChildren", rc[rid]);
  538. if(reloadcurrent=== undefined) {
  539. reloadcurrent = false;
  540. }
  541. if(!reloadcurrent) {
  542. if(!jQuery._data( this, "events" ).jqGridAfterSetTreeNode) {
  543. $(this).on("jqGridAfterSetTreeNode.reloadNode", function(){
  544. var isLeaf = this.p.treeReader.leaf_field;
  545. if(this.p.reloadnode ) {
  546. var rc = this.p.reloadnode,
  547. chld = $(this).jqGrid('getNodeChildren', rc);
  548. if(rc[isLeaf] && chld.length) {
  549. $(this).jqGrid('setLeaf', rc, false);
  550. } else if(!rc[isLeaf] && chld.length === 0) {
  551. $(this).jqGrid('setLeaf', rc, true);
  552. }
  553. }
  554. this.p.reloadnode = false;
  555. });
  556. }
  557. }
  558. var expanded = this.p.treeReader.expanded_field,
  559. parent = this.p.treeReader.parent_id_field,
  560. loaded = this.p.treeReader.loaded,
  561. level = this.p.treeReader.level_field,
  562. isLeaf = this.p.treeReader.leaf_field,
  563. lft = this.p.treeReader.left_field,
  564. rgt = this.p.treeReader.right_field;
  565. var id = $.jgrid.getAccessor(rc,this.p.localReader.id),
  566. rc1 = $("#"+id,this.grid.bDiv)[0];
  567. rc[expanded] = true;
  568. if(!rc[isLeaf]) {
  569. $("div.treeclick",rc1).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  570. }
  571. this.p.treeANode = rc1.rowIndex;
  572. this.p.datatype = this.p.treedatatype;
  573. this.p.reloadnode = rc;
  574. if(reloadcurrent) {
  575. this.p.treeANode = rc1.rowIndex > 0 ? rc1.rowIndex - 1 : 1;
  576. $(this).jqGrid('delRowData', id);
  577. }
  578. if(this.p.treeGridModel === 'nested') {
  579. $(this).jqGrid("setGridParam",{postData:{nodeid:id,n_left:rc[lft],n_right:rc[rgt],n_level:rc[level]}});
  580. } else {
  581. $(this).jqGrid("setGridParam",{postData:{nodeid:id,parentid:rc[parent],n_level:rc[level]}} );
  582. }
  583. $(this).trigger("reloadGrid");
  584. rc[loaded] = true;
  585. if(this.p.treeGridModel === 'nested') {
  586. $(this).jqGrid("setGridParam",{selrow: currselection, postData:{nodeid:'',n_left:'',n_right:'',n_level:''}});
  587. } else {
  588. $(this).jqGrid("setGridParam",{selrow: currselection, postData:{nodeid:'',parentid:'',n_level:''}});
  589. }
  590. });
  591. },
  592. expandNode : function(rc) {
  593. return this.each(function(){
  594. if(!this.grid || !this.p.treeGrid) {return;}
  595. var $t = this,
  596. expanded = this.p.treeReader.expanded_field,
  597. parent = this.p.treeReader.parent_id_field,
  598. loaded = this.p.treeReader.loaded,
  599. level = this.p.treeReader.level_field,
  600. lft = this.p.treeReader.left_field,
  601. rgt = this.p.treeReader.right_field;
  602. if(!rc[expanded]) {
  603. var id = $.jgrid.getAccessor(rc,this.p.localReader.id),
  604. rc1 = $("#" + this.p.idPrefix + $.jgrid.jqID(id),this.grid.bDiv)[0],
  605. position = this.p._index[id],
  606. ret = $($t).triggerHandler("jqGridBeforeExpandTreeGridNode", [id, rc]);
  607. if(ret === undefined ) {
  608. ret = true;
  609. }
  610. if( ret && $.isFunction(this.p.beforeExpandTreeGridNode) ) {
  611. ret = this.p.beforeExpandTreeGridNode.call(this, id, rc );
  612. }
  613. if( ret === false ) { return; }
  614. if( $(this).jqGrid("isNodeLoaded",this.p.data[position]) ) {
  615. rc[expanded] = true;
  616. $("div.treeclick",rc1).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  617. } else if (!this.grid.hDiv.loading) {
  618. rc[expanded] = true;
  619. $("div.treeclick",rc1).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  620. this.p.treeANode = rc1.rowIndex;
  621. this.p.datatype = this.p.treedatatype;
  622. if(this.p.treeGridModel === 'nested') {
  623. $(this).jqGrid("setGridParam",{postData:{nodeid:id,n_left:rc[lft],n_right:rc[rgt],n_level:rc[level]}});
  624. } else {
  625. $(this).jqGrid("setGridParam",{postData:{nodeid:id,parentid:rc[parent],n_level:rc[level]}} );
  626. }
  627. $(this).trigger("reloadGrid");
  628. rc[loaded] = true;
  629. if(this.p.treeGridModel === 'nested') {
  630. $(this).jqGrid("setGridParam",{postData:{nodeid:'',n_left:'',n_right:'',n_level:''}});
  631. } else {
  632. $(this).jqGrid("setGridParam",{postData:{nodeid:'',parentid:'',n_level:''}});
  633. }
  634. }
  635. $($t).triggerHandler("jqGridAfterExpandTreeGridNode", [id, rc]);
  636. if($.isFunction(this.p.afterExpandTreeGridNode)) {
  637. this.p.afterExpandTreeGridNode.call(this, id, rc );
  638. }
  639. }
  640. });
  641. },
  642. collapseNode : function(rc) {
  643. return this.each(function(){
  644. if(!this.grid || !this.p.treeGrid) {return;}
  645. var expanded = this.p.treeReader.expanded_field,
  646. $t = this;
  647. if(rc[expanded]) {
  648. var id = $.jgrid.getAccessor(rc,this.p.localReader.id),
  649. rc1 = $("#" + this.p.idPrefix + $.jgrid.jqID(id),this.grid.bDiv)[0],
  650. ret = $($t).triggerHandler("jqGridBeforeCollapseTreeGridNode", [id, rc]);
  651. if(ret === undefined ) {
  652. ret = true;
  653. }
  654. if( ret && $.isFunction(this.p.beforeCollapseTreeGridNode) ) {
  655. ret = this.p.beforeCollapseTreeGridNode.call(this, id, rc );
  656. }
  657. rc[expanded] = false;
  658. if( ret === false ) { return; }
  659. $("div.treeclick",rc1).removeClass(this.p.treeIcons.minus+" tree-minus").addClass(this.p.treeIcons.plus+" tree-plus");
  660. $($t).triggerHandler("jqGridAfterCollapseTreeGridNode", [id, rc]);
  661. if($.isFunction(this.p.afterCollapseTreeGridNode)) {
  662. this.p.afterCollapseTreeGridNode.call(this, id, rc );
  663. }
  664. }
  665. });
  666. },
  667. SortTree : function( sortname, newDir, st, datefmt) {
  668. return this.each(function(){
  669. if(!this.grid || !this.p.treeGrid) {return;}
  670. var i, len,
  671. rec, records = [], $t = this, query, roots,
  672. rt = $(this).jqGrid("getRootNodes", $t.p.search);
  673. // Sorting roots
  674. query = $.jgrid.from.call(this, rt);
  675. query.orderBy(sortname, newDir, st, datefmt);
  676. roots = query.select();
  677. // Sorting children
  678. for (i = 0, len = roots.length; i < len; i++) {
  679. rec = roots[i];
  680. records.push(rec);
  681. $(this).jqGrid("collectChildrenSortTree",records, rec, sortname, newDir, st, datefmt);
  682. }
  683. $.each(records, function(index) {
  684. var id = $.jgrid.getAccessor(this, $t.p.localReader.id);
  685. $('#'+$.jgrid.jqID($t.p.id)+ ' tbody tr:eq('+index+')').after($('tr#'+$.jgrid.jqID(id), $t.grid.bDiv));
  686. });
  687. query = null;roots=null;records=null;
  688. });
  689. },
  690. searchTree : function ( recs ) {
  691. var i= recs.length || 0, ancestors=[], lid, roots=[], result=[],tid, alen, rlen, j, k;
  692. this.each(function(){
  693. if(!this.grid || !this.p.treeGrid) {
  694. return;
  695. }
  696. if(i) {
  697. lid = this.p.localReader.id;
  698. while( i-- ) { // reverse
  699. ancestors = $(this).jqGrid('getNodeAncestors', recs[i], true, true);
  700. //add the searched item
  701. ancestors.push(recs[i]);
  702. tid = ancestors[0][lid];
  703. if($.inArray(tid, roots ) !== -1) { // ignore repeated, but add missing
  704. for( j = 0, alen = ancestors.length; j < alen; j++) {
  705. //$.inArray ?!?
  706. var found = false;
  707. for( k=0, rlen = result.length; k < rlen; k++) {
  708. if(ancestors[j][lid] === result[k][lid]) {
  709. found = true;
  710. break;
  711. }
  712. }
  713. if(!found) {
  714. result.push(ancestors[j]);
  715. }
  716. }
  717. continue;
  718. } else {
  719. roots.push( tid );
  720. }
  721. result = result.concat( ancestors );
  722. }
  723. }
  724. });
  725. return result;
  726. },
  727. collectChildrenSortTree : function(records, rec, sortname, newDir,st, datefmt) {
  728. return this.each(function(){
  729. if(!this.grid || !this.p.treeGrid) {return;}
  730. var i, len,
  731. child, ch, query, children;
  732. ch = $(this).jqGrid("getNodeChildren",rec, this.p.search);
  733. query = $.jgrid.from.call(this, ch);
  734. query.orderBy(sortname, newDir, st, datefmt);
  735. children = query.select();
  736. for (i = 0, len = children.length; i < len; i++) {
  737. child = children[i];
  738. records.push(child);
  739. $(this).jqGrid("collectChildrenSortTree",records, child, sortname, newDir, st, datefmt);
  740. }
  741. });
  742. },
  743. // experimental
  744. setTreeRow : function(rowid, data) {
  745. var success=false;
  746. this.each(function(){
  747. var t = this;
  748. if(!t.grid || !t.p.treeGrid) {return;}
  749. success = $(t).jqGrid("setRowData", rowid, data);
  750. });
  751. return success;
  752. },
  753. delTreeNode : function (rowid) {
  754. return this.each(function () {
  755. var $t = this, rid = $t.p.localReader.id, i,
  756. left = $t.p.treeReader.left_field,
  757. right = $t.p.treeReader.right_field, myright, width, res, key;
  758. if(!$t.grid || !$t.p.treeGrid) {return;}
  759. var rc = $t.p._index[rowid];
  760. if (rc !== undefined) {
  761. // nested
  762. myright = parseInt($t.p.data[rc][right],10);
  763. width = myright - parseInt($t.p.data[rc][left],10) + 1;
  764. var dr = $($t).jqGrid("getFullTreeNode",$t.p.data[rc]);
  765. if(dr.length>0){
  766. for (i=0;i<dr.length;i++){
  767. $($t).jqGrid("delRowData",dr[i][rid]);
  768. }
  769. }
  770. if( $t.p.treeGridModel === "nested") {
  771. // ToDo - update grid data
  772. res = $.jgrid.from.call($t, $t.p.data)
  773. .greater(left,myright,{stype:'integer'})
  774. .select();
  775. if(res.length) {
  776. for( key in res) {
  777. if(res.hasOwnProperty(key)) {
  778. res[key][left] = parseInt(res[key][left],10) - width ;
  779. }
  780. }
  781. }
  782. res = $.jgrid.from.call($t, $t.p.data)
  783. .greater(right,myright,{stype:'integer'})
  784. .select();
  785. if(res.length) {
  786. for( key in res) {
  787. if(res.hasOwnProperty(key)) {
  788. res[key][right] = parseInt(res[key][right],10) - width ;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. });
  795. },
  796. delChildren : function (rowid) {
  797. return this.each(function () {
  798. var $t = this, rid = $t.p.localReader.id,
  799. left = $t.p.treeReader.left_field,
  800. right = $t.p.treeReader.right_field, myright, width, res, key;
  801. if(!$t.grid || !$t.p.treeGrid) {return;}
  802. var rc = $t.p._index[rowid];
  803. if (rc !== undefined) {
  804. // nested
  805. myright = parseInt($t.p.data[rc][right],10);
  806. width = myright - parseInt($t.p.data[rc][left],10) + 1;
  807. var dr = $($t).jqGrid("getFullTreeNode",$t.p.data[rc]);
  808. if(dr.length>0){
  809. for (var i=0;i<dr.length;i++){
  810. if(dr[i][rid] !== rowid)
  811. $($t).jqGrid("delRowData",dr[i][rid]);
  812. }
  813. }
  814. if( $t.p.treeGridModel === "nested") {
  815. // ToDo - update grid data
  816. res = $.jgrid.from($t.p.data)
  817. .greater(left,myright,{stype:'integer'})
  818. .select();
  819. if(res.length) {
  820. for( key in res) {
  821. if(res.hasOwnProperty(key)) {
  822. res[key][left] = parseInt(res[key][left],10) - width ;
  823. }
  824. }
  825. }
  826. res = $.jgrid.from($t.p.data)
  827. .greater(right,myright,{stype:'integer'})
  828. .select();
  829. if(res.length) {
  830. for( key in res) {
  831. if(res.hasOwnProperty(key)) {
  832. res[key][right] = parseInt(res[key][right],10) - width ;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. });
  839. },
  840. addChildNode : function( nodeid, parentid, data, expandData ) {
  841. //return this.each(function(){
  842. var $t = this[0];
  843. if(data) {
  844. // we suppose tha the id is autoincremet and
  845. var expanded = $t.p.treeReader.expanded_field,
  846. isLeaf = $t.p.treeReader.leaf_field,
  847. level = $t.p.treeReader.level_field,
  848. //icon = $t.p.treeReader.icon_field,
  849. parent = $t.p.treeReader.parent_id_field,
  850. left = $t.p.treeReader.left_field,
  851. right = $t.p.treeReader.right_field,
  852. loaded = $t.p.treeReader.loaded,
  853. method, parentindex, parentdata, parentlevel, i, len, max=0, rowind = parentid, leaf, maxright;
  854. if(expandData===undefined) {expandData = false;}
  855. if ( nodeid == null ) {
  856. i = $t.p.data.length-1;
  857. if( i>= 0 ) {
  858. while(i>=0){max = Math.max(max, parseInt($t.p.data[i][$t.p.localReader.id],10)); i--;}
  859. }
  860. nodeid = max+1;
  861. }
  862. var prow = $($t).jqGrid('getInd', parentid);
  863. leaf = false;
  864. // if not a parent we assume root
  865. if ( parentid === undefined || parentid === null || parentid==="") {
  866. parentid = null;
  867. rowind = null;
  868. method = 'last';
  869. parentlevel = $t.p.tree_root_level;
  870. i = $t.p.data.length+1;
  871. } else {
  872. method = 'after';
  873. parentindex = $t.p._index[parentid];
  874. parentdata = $t.p.data[parentindex];
  875. parentid = parentdata[$t.p.localReader.id];
  876. parentlevel = parseInt(parentdata[level],10)+1;
  877. var childs = $($t).jqGrid('getFullTreeNode', parentdata);
  878. // if there are child nodes get the last index of it
  879. if(childs.length) {
  880. i = childs[childs.length-1][$t.p.localReader.id];
  881. rowind = i;
  882. i = $($t).jqGrid('getInd',rowind)+1;
  883. } else {
  884. i = $($t).jqGrid('getInd', parentid)+1;
  885. }
  886. // if the node is leaf
  887. if(parentdata[isLeaf]) {
  888. leaf = true;
  889. parentdata[expanded] = true;
  890. //var prow = $($t).jqGrid('getInd', parentid);
  891. $($t.rows[prow])
  892. .find("span.cell-wrapperleaf").removeClass("cell-wrapperleaf").addClass("cell-wrapper")
  893. .end()
  894. .find("div.tree-leaf").removeClass($t.p.treeIcons.leaf+" tree-leaf").addClass($t.p.treeIcons.minus+" tree-minus");
  895. $t.p.data[parentindex][isLeaf] = false;
  896. parentdata[loaded] = true;
  897. }
  898. }
  899. len = i+1;
  900. if( data[expanded]===undefined) {data[expanded]= false;}
  901. if( data[loaded]===undefined ) { data[loaded] = false;}
  902. data[level] = parentlevel;
  903. if( data[isLeaf]===undefined) {data[isLeaf]= true;}
  904. if( $t.p.treeGridModel === "adjacency") {
  905. data[parent] = parentid;
  906. }
  907. if( $t.p.treeGridModel === "nested") {
  908. // this method requiere more attention
  909. var query, res, key;
  910. //maxright = parseInt(maxright,10);
  911. // ToDo - update grid data
  912. if(parentid !== null) {
  913. maxright = parseInt(parentdata[right],10);
  914. query = $.jgrid.from.call($t, $t.p.data);
  915. query = query.greaterOrEquals(right,maxright,{stype:'integer'});
  916. res = query.select();
  917. if(res.length) {
  918. for( key in res) {
  919. if(res.hasOwnProperty(key)) {
  920. res[key][left] = res[key][left] > maxright ? parseInt(res[key][left],10) +2 : res[key][left];
  921. res[key][right] = res[key][right] >= maxright ? parseInt(res[key][right],10) +2 : res[key][right];
  922. }
  923. }
  924. }
  925. data[left] = maxright;
  926. data[right]= maxright+1;
  927. } else {
  928. maxright = parseInt( $($t).jqGrid('getCol', right, false, 'max'), 10);
  929. res = $.jgrid.from.call($t, $t.p.data)
  930. .greater(left,maxright,{stype:'integer'})
  931. .select();
  932. if(res.length) {
  933. for( key in res) {
  934. if(res.hasOwnProperty(key)) {
  935. res[key][left] = parseInt(res[key][left],10) +2 ;
  936. }
  937. }
  938. }
  939. res = $.jgrid.from.call($t, $t.p.data)
  940. .greater(right,maxright,{stype:'integer'})
  941. .select();
  942. if(res.length) {
  943. for( key in res) {
  944. if(res.hasOwnProperty(key)) {
  945. res[key][right] = parseInt(res[key][right],10) +2 ;
  946. }
  947. }
  948. }
  949. data[left] = maxright+1;
  950. data[right] = maxright + 2;
  951. }
  952. }
  953. if( parentid === null || $($t).jqGrid("isNodeLoaded",parentdata) || leaf ) {
  954. $($t).jqGrid('addRowData', nodeid, data, method, rowind);
  955. $($t).jqGrid('setTreeNode', i, len);
  956. }
  957. if(parentdata && !parentdata[expanded] && expandData) {
  958. $($t.rows[prow])
  959. .find("div.treeclick")
  960. .click();
  961. }
  962. }
  963. //});
  964. }
  965. });
  966. //module end
  967. }));