You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

132 lines
2.9 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // for license information please see license.txt
  4. // constructor: parent, label, method, args
  5. wn.ui.Tree = Class.extend({
  6. init: function(args) {
  7. $.extend(this, args);
  8. this.nodes = {};
  9. this.$w = $('<div class="tree">').appendTo(this.parent);
  10. this.rootnode = new wn.ui.TreeNode({
  11. tree: this,
  12. parent: this.$w,
  13. label: this.label,
  14. expandable: true
  15. });
  16. this.set_style();
  17. },
  18. set_style: function() {
  19. wn.dom.set_style("\
  20. .tree li { list-style: none; }\
  21. .tree ul { margin-top: 2px; }\
  22. .tree-link { cursor: pointer; }\
  23. ")
  24. }
  25. })
  26. wn.ui.TreeNode = Class.extend({
  27. init: function(args) {
  28. var me = this;
  29. $.extend(this, args);
  30. this.loaded = false;
  31. this.expanded = false;
  32. this.tree.nodes[this.label] = this;
  33. this.$a = $('<span class="tree-link">')
  34. .click(function() {
  35. if(me.expandable && me.tree.method && !me.loaded) {
  36. me.load()
  37. } else {
  38. me.selectnode();
  39. }
  40. if(me.tree.click) me.tree.click(this);
  41. })
  42. .bind('reload', function() { me.reload(); })
  43. .data('label', this.label)
  44. .appendTo(this.parent);
  45. // label with icon
  46. var icon_html = '<i class="icon-fixed-width icon-file"></i>';
  47. if(this.expandable) {
  48. icon_html = '<i class="icon-fixed-width icon-folder-close"></i>';
  49. }
  50. $(icon_html + ' <a class="tree-label">' + this.label + "</a>").
  51. appendTo(this.$a);
  52. if(this.tree.onrender) {
  53. this.tree.onrender(this);
  54. }
  55. },
  56. selectnode: function() {
  57. // expand children
  58. if(this.$ul) {
  59. this.$ul.toggle();
  60. // open close icon
  61. this.$a.find('i').removeClass();
  62. if(this.$ul.css('display').toLowerCase()=='block') {
  63. this.$a.find('i').addClass('icon-fixed-width icon-folder-open');
  64. } else {
  65. this.$a.find('i').addClass('icon-fixed-width icon-folder-close');
  66. }
  67. }
  68. // select this link
  69. this.tree.$w.find('.selected')
  70. .removeClass('selected');
  71. this.$a.toggleClass('selected');
  72. this.expanded = !this.expanded;
  73. },
  74. reload: function() {
  75. if(this.expanded) {
  76. this.$a.click(); // collapse
  77. }
  78. if(this.$ul) {
  79. this.$ul.empty();
  80. }
  81. this.load();
  82. },
  83. addnode: function(data) {
  84. if(!this.$ul) {
  85. this.$ul = $('<ul>').toggle(false).appendTo(this.parent);
  86. }
  87. return new wn.ui.TreeNode({
  88. tree:this.tree,
  89. parent: $('<li>').appendTo(this.$ul),
  90. label: data.value,
  91. expandable: data.expandable,
  92. data: data
  93. });
  94. },
  95. load: function() {
  96. var me = this;
  97. args = $.extend(this.tree.args || {}, {
  98. parent: this.data ? this.data.value : null
  99. });
  100. $(me.$a).set_working();
  101. return wn.call({
  102. method: this.tree.method,
  103. args: args,
  104. callback: function(r) {
  105. $(me.$a).done_working();
  106. if (r.message) {
  107. $.each(r.message, function(i, v) {
  108. node = me.addnode(v);
  109. node.$a
  110. .data('node-data', v)
  111. .data('node', node);
  112. });
  113. }
  114. me.loaded = true;
  115. // expand
  116. me.selectnode();
  117. }
  118. })
  119. }
  120. })