Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
12 роки тому
13 роки тому
12 роки тому
12 роки тому
12 роки тому
13 роки тому
12 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-file"></i>';
  47. if(this.expandable) {
  48. icon_html = '<i class="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-folder-open');
  64. } else {
  65. this.$a.find('i').addClass('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.label
  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.data('node-data', v);
  110. });
  111. }
  112. me.loaded = true;
  113. // expand
  114. me.selectnode();
  115. }
  116. })
  117. }
  118. })