PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/2dc_jqgrid/public/javascripts/jqgrid/grid.treegrid.js

https://github.com/minikermit/finapp
JavaScript | 435 lines | 414 code | 3 blank | 18 comment | 120 complexity | 090eec8bb169a43382c9e65e7d98efe3 MD5 | raw file
Possible License(s): MIT
  1. ;(function($) {
  2. /*
  3. **
  4. * jqGrid extension - Tree Grid
  5. * Tony Tomov tony@trirand.com
  6. * http://trirand.com/blog/
  7. * Dual licensed under the MIT and GPL licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl.html
  10. **/
  11. $.fn.extend({
  12. setTreeNode : function(rd, row){
  13. return this.each(function(){
  14. var $t = this;
  15. if( !$t.grid || !$t.p.treeGrid ) { return; }
  16. var expCol=0,i=0;
  17. if(!$t.p.expColInd) {
  18. for (var key in $t.p.colModel){
  19. if($t.p.colModel[key].name == $t.p.ExpandColumn) {
  20. expCol = i;
  21. $t.p.expColInd = expCol;
  22. break;
  23. }
  24. i++;
  25. }
  26. if(!$t.p.expColInd ) {$t.p.expColInd = expCol;}
  27. } else {
  28. expCol = $t.p.expColInd;
  29. }
  30. var expanded = $t.p.treeReader.expanded_field;
  31. var isLeaf = $t.p.treeReader.leaf_field;
  32. var level = $t.p.treeReader.level_field;
  33. row.level = rd[level];
  34. if($t.p.treeGridModel == 'nested') {
  35. row.lft = rd[$t.p.treeReader.left_field];
  36. row.rgt = rd[$t.p.treeReader.right_field];
  37. if(!rd[isLeaf]) {
  38. // NS Model
  39. rd[isLeaf] = (parseInt(row.rgt,10) === parseInt(row.lft,10)+1) ? 'true' : 'false';
  40. }
  41. } else {
  42. row.parent_id = rd[$t.p.treeReader.parent_id_field];
  43. }
  44. var curExpand = (rd[expanded] && rd[expanded] == "true") ? true : false;
  45. var curLevel = parseInt(row.level,10);
  46. var ident,lftpos;
  47. if($t.p.tree_root_level === 0) {
  48. ident = curLevel+1;
  49. lftpos = curLevel;
  50. } else {
  51. ident = curLevel;
  52. lftpos = curLevel -1;
  53. }
  54. var twrap = document.createElement("div");
  55. $(twrap).addClass("tree-wrap").width(ident*18);
  56. var treeimg = document.createElement("div");
  57. $(treeimg).css("left",lftpos*18).addClass("ui-icon");
  58. twrap.appendChild(treeimg);
  59. if(rd[isLeaf] == "true") {
  60. $(treeimg).addClass($t.p.treeIcons.leaf+" tree-leaf");
  61. row.isLeaf = true;
  62. } else {
  63. if(rd[expanded] == "true") {
  64. $(treeimg).addClass($t.p.treeIcons.minus+" tree-minus treeclick");
  65. row.expanded = true;
  66. } else {
  67. $(treeimg).addClass($t.p.treeIcons.plus+" tree-plus treeclick");
  68. row.expanded = false;
  69. }
  70. row.isLeaf = false;
  71. }
  72. if(parseInt(rd[level],10) !== parseInt($t.p.tree_root_level,10)) {
  73. if(!$($t).isVisibleNode(row)){
  74. $(row).css("display","none");
  75. }
  76. }
  77. var mhtm = $("td:eq("+expCol+")",row).html();
  78. var thecell = $("td:eq("+expCol+")",row).html("<span>"+mhtm+"</span>").prepend(twrap);
  79. $(".treeclick",thecell).click(function(e){
  80. var target = e.target || e.srcElement;
  81. var ind =$(target,$t.rows).parents("tr:first")[0].rowIndex;
  82. if(!$t.rows[ind].isLeaf){
  83. if($t.rows[ind].expanded){
  84. $($t).collapseRow($t.rows[ind]);
  85. $($t).collapseNode($t.rows[ind]);
  86. } else {
  87. $($t).expandRow($t.rows[ind]);
  88. $($t).expandNode($t.rows[ind]);
  89. }
  90. }
  91. //e.stopPropagation();
  92. return false;
  93. });
  94. if($t.p.ExpandColClick === true) {
  95. $("span", thecell).css("cursor","pointer").click(function(e){
  96. var target = e.target || e.srcElement;
  97. var ind =$(target,$t.rows).parents("tr:first")[0].rowIndex;
  98. if(!$t.rows[ind].isLeaf){
  99. if($t.rows[ind].expanded){
  100. $($t).collapseRow($t.rows[ind]);
  101. $($t).collapseNode($t.rows[ind]);
  102. } else {
  103. $($t).expandRow($t.rows[ind]);
  104. $($t).expandNode($t.rows[ind]);
  105. }
  106. }
  107. $($t).setSelection($t.rows[ind].id);
  108. return false;
  109. });
  110. }
  111. });
  112. },
  113. setTreeGrid : function() {
  114. return this.each(function (){
  115. var $t = this;
  116. if(!$t.p.treeGrid) { return; }
  117. $.extend($t.p,{treedatatype: null});
  118. $t.p.treeIcons = $.extend({plus:'ui-icon-triangle-1-e',minus:'ui-icon-triangle-1-s',leaf:'ui-icon-radio-off'},$t.p.treeIcons || {});
  119. if($t.p.treeGridModel == 'nested') {
  120. $t.p.treeReader = $.extend({
  121. level_field: "level",
  122. left_field:"lft",
  123. right_field: "rgt",
  124. leaf_field: "isLeaf",
  125. expanded_field: "expanded"
  126. },$t.p.treeReader);
  127. } else
  128. if($t.p.treeGridModel == 'adjacency') {
  129. $t.p.treeReader = $.extend({
  130. level_field: "level",
  131. parent_id_field: "parent",
  132. leaf_field: "isLeaf",
  133. expanded_field: "expanded"
  134. },$t.p.treeReader );
  135. }
  136. });
  137. },
  138. expandRow: function (record){
  139. this.each(function(){
  140. var $t = this;
  141. if(!$t.grid || !$t.p.treeGrid) { return; }
  142. var childern = $($t).getNodeChildren(record);
  143. //if ($($t).isVisibleNode(record)) {
  144. $(childern).each(function(i){
  145. $(this).css("display","");
  146. if(this.expanded) {
  147. $($t).expandRow(this);
  148. }
  149. });
  150. //}
  151. });
  152. },
  153. collapseRow : function (record) {
  154. this.each(function(){
  155. var $t = this;
  156. if(!$t.grid || !$t.p.treeGrid) { return; }
  157. var childern = $($t).getNodeChildren(record);
  158. $(childern).each(function(i){
  159. $(this).css("display","none");
  160. $($t).collapseRow(this);
  161. });
  162. });
  163. },
  164. // NS ,adjacency models
  165. getRootNodes : function() {
  166. var result = [];
  167. this.each(function(){
  168. var $t = this;
  169. if(!$t.grid || !$t.p.treeGrid) { return; }
  170. switch ($t.p.treeGridModel) {
  171. case 'nested' :
  172. var level = $t.p.treeReader.level_field;
  173. $($t.rows).each(function(i){
  174. if(parseInt(this[level],10) === parseInt($t.p.tree_root_level,10)) {
  175. result.push(this);
  176. }
  177. });
  178. break;
  179. case 'adjacency' :
  180. $($t.rows).each(function(i){
  181. if(this.parent_id.toLowerCase() == "null") {
  182. result.push(this);
  183. }
  184. });
  185. break;
  186. }
  187. });
  188. return result;
  189. },
  190. getNodeDepth : function(rc) {
  191. var ret = null;
  192. this.each(function(){
  193. var $t = this;
  194. if(!this.grid || !this.p.treeGrid) { return; }
  195. switch ($t.p.treeGridModel) {
  196. case 'nested' :
  197. ret = parseInt(rc.level,10) - parseInt(this.p.tree_root_level,10);
  198. break;
  199. case 'adjacency' :
  200. ret = $($t).getNodeAncestors(rc);
  201. break;
  202. }
  203. });
  204. return ret;
  205. },
  206. getNodeParent : function(rc) {
  207. var result = null;
  208. this.each(function(){
  209. var $t = this;
  210. if(!$t.grid || !$t.p.treeGrid) { return; }
  211. switch ($t.p.treeGridModel) {
  212. case 'nested' :
  213. var lft = parseInt(rc.lft,10), rgt = parseInt(rc.rgt,10), level = parseInt(rc.level,10);
  214. $(this.rows).each(function(){
  215. if(parseInt(this.level,10) === level-1 && parseInt(this.lft) < lft && parseInt(this.rgt) > rgt) {
  216. result = this;
  217. return false;
  218. }
  219. });
  220. break;
  221. case 'adjacency' :
  222. $(this.rows).each(function(){
  223. if(this.id === rc.parent_id ) {
  224. result = this;
  225. return false;
  226. }
  227. });
  228. break;
  229. }
  230. });
  231. return result;
  232. },
  233. getNodeChildren : function(rc) {
  234. var result = [];
  235. this.each(function(){
  236. var $t = this;
  237. if(!$t.grid || !$t.p.treeGrid) { return; }
  238. switch ($t.p.treeGridModel) {
  239. case 'nested' :
  240. var lft = parseInt(rc.lft,10), rgt = parseInt(rc.rgt,10), level = parseInt(rc.level,10);
  241. var ind = rc.rowIndex;
  242. $(this.rows).slice(1).each(function(i){
  243. if(parseInt(this.level,10) === level+1 && parseInt(this.lft,10) > lft && parseInt(this.rgt,10) < rgt) {
  244. result.push(this);
  245. }
  246. });
  247. break;
  248. case 'adjacency' :
  249. $(this.rows).slice(1).each(function(i){
  250. if(this.parent_id == rc.id) {
  251. result.push(this);
  252. }
  253. });
  254. break;
  255. }
  256. });
  257. return result;
  258. },
  259. getFullTreeNode : function(rc) {
  260. var result = [];
  261. this.each(function(){
  262. var $t = this;
  263. if(!$t.grid || !$t.p.treeGrid) { return; }
  264. switch ($t.p.treeGridModel) {
  265. case 'nested' :
  266. var lft = parseInt(rc.lft,10), rgt = parseInt(rc.rgt,10), level = parseInt(rc.level,10);
  267. var ind = rc.rowIndex;
  268. $(this.rows).slice(1).each(function(i){
  269. if(parseInt(this.level,10) >= level && parseInt(this.lft,10) >= lft && parseInt(this.lft,10) <= rgt) {
  270. result.push(this);
  271. }
  272. });
  273. break;
  274. case 'adjacency' :
  275. break;
  276. }
  277. });
  278. return result;
  279. },
  280. // End NS, adjacency Model
  281. getNodeAncestors : function(rc) {
  282. var ancestors = [];
  283. this.each(function(){
  284. if(!this.grid || !this.p.treeGrid) { return; }
  285. var parent = $(this).getNodeParent(rc);
  286. while (parent) {
  287. ancestors.push(parent);
  288. parent = $(this).getNodeParent(parent);
  289. }
  290. });
  291. return ancestors;
  292. },
  293. isVisibleNode : function(rc) {
  294. var result = true;
  295. this.each(function(){
  296. var $t = this;
  297. if(!$t.grid || !$t.p.treeGrid) { return; }
  298. var ancestors = $($t).getNodeAncestors(rc);
  299. $(ancestors).each(function(){
  300. result = result && this.expanded;
  301. if(!result) {return false;}
  302. });
  303. });
  304. return result;
  305. },
  306. isNodeLoaded : function(rc) {
  307. var result;
  308. this.each(function(){
  309. var $t = this;
  310. if(!$t.grid || !$t.p.treeGrid) { return; }
  311. if(rc.loaded !== undefined) {
  312. result = rc.loaded;
  313. } else if( rc.isLeaf || $($t).getNodeChildren(rc).length > 0){
  314. result = true;
  315. } else {
  316. result = false;
  317. }
  318. });
  319. return result;
  320. },
  321. expandNode : function(rc) {
  322. return this.each(function(){
  323. if(!this.grid || !this.p.treeGrid) { return; }
  324. if(!rc.expanded) {
  325. if( $(this).isNodeLoaded(rc) ) {
  326. rc.expanded = true;
  327. $("div.treeclick",rc).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  328. } else {
  329. rc.expanded = true;
  330. $("div.treeclick",rc).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  331. this.p.treeANode = rc.rowIndex;
  332. this.p.datatype = this.p.treedatatype;
  333. if(this.p.treeGridModel == 'nested') {
  334. $(this).setGridParam({postData:{nodeid:rc.id,n_left:rc.lft,n_right:rc.rgt,n_level:rc.level}});
  335. } else {
  336. $(this).setGridParam({postData:{nodeid:rc.id,parentid:rc.parent_id,n_level:rc.level}});
  337. }
  338. $(this).trigger("reloadGrid");
  339. if(this.p.treeGridModel == 'nested') {
  340. $(this).setGridParam({postData:{nodeid:'',n_left:'',n_right:'',n_level:''}});
  341. } else {
  342. $(this).setGridParam({postData:{nodeid:'',parentid:'',n_level:''}});
  343. }
  344. }
  345. }
  346. });
  347. },
  348. collapseNode : function(rc) {
  349. return this.each(function(){
  350. if(!this.grid || !this.p.treeGrid) { return; }
  351. if(rc.expanded) {
  352. rc.expanded = false;
  353. $("div.treeclick",rc).removeClass(this.p.treeIcons.minus+" tree-minus").addClass(this.p.treeIcons.plus+" tree-plus");
  354. }
  355. });
  356. },
  357. SortTree : function( newDir) {
  358. return this.each(function(){
  359. if(!this.grid || !this.p.treeGrid) { return; }
  360. var i, len,
  361. rec, records = [], $t = this,
  362. roots = $(this).getRootNodes();
  363. // Sorting roots
  364. roots.sort(function(a, b) {
  365. if (a.sortKey < b.sortKey) {return -newDir;}
  366. if (a.sortKey > b.sortKey) {return newDir;}
  367. return 0;
  368. });
  369. if(roots[0]){
  370. $("td",roots[0]).each( function( k ) {
  371. $(this).css("width",$t.grid.headers[k].width+"px");
  372. $t.grid.cols[k] = this;
  373. });
  374. }
  375. // Sorting children
  376. for (i = 0, len = roots.length; i < len; i++) {
  377. rec = roots[i];
  378. records.push(rec);
  379. $(this).collectChildrenSortTree(records, rec, newDir);
  380. }
  381. $.each(records, function(index, row) {
  382. $('tbody',$t.grid.bDiv).append(row);
  383. row.sortKey = null;
  384. });
  385. });
  386. },
  387. collectChildrenSortTree : function(records, rec, newDir) {
  388. return this.each(function(){
  389. if(!this.grid || !this.p.treeGrid) { return; }
  390. var i, len,
  391. child,
  392. children = $(this).getNodeChildren(rec);
  393. children.sort(function(a, b) {
  394. if (a.sortKey < b.sortKey) {return -newDir;}
  395. if (a.sortKey > b.sortKey) {return newDir;}
  396. return 0;
  397. });
  398. for (i = 0, len = children.length; i < len; i++) {
  399. child = children[i];
  400. records.push(child);
  401. $(this).collectChildrenSortTree(records, child,newDir);
  402. }
  403. });
  404. },
  405. // experimental
  406. setTreeRow : function(rowid, data) {
  407. var nm, success=false;
  408. this.each(function(){
  409. var t = this;
  410. if(!t.grid || !t.p.treeGrid) { return; }
  411. success = $(t).setRowData(rowid,data);
  412. });
  413. return success;
  414. },
  415. delTreeNode : function (rowid) {
  416. return this.each(function () {
  417. var $t = this;
  418. if(!$t.grid || !$t.p.treeGrid) { return; }
  419. var rc = $($t).getInd($t.rows,rowid,true);
  420. if (rc) {
  421. var dr = $($t).getNodeChildren(rc);
  422. if(dr.length>0){
  423. for (var i=0;i<dr.length;i++){
  424. $($t).delRowData(dr[i].id);
  425. }
  426. }
  427. $($t).delRowData(rc.id);
  428. }
  429. });
  430. }
  431. });
  432. })(jQuery);