1
0

demo.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. window.addEvent('domready',function(){
  2. Mif.Tree.implement({
  3. getTarget: function(event){
  4. var target = event.target;
  5. var cls = target.className;
  6. while(!(/mif-tree/).test(target.className)){
  7. target = target.parentNode;
  8. }
  9. var test = target.className.match(/mif-tree-(gadjet)-[^n]|mif-tree-(name)/);
  10. if(!test){
  11. return {
  12. node: false,
  13. target: 'node'
  14. };
  15. }
  16. for(var i = 3; i > 0; i--){
  17. if(test[i]){
  18. var type = test[i];
  19. break;
  20. }
  21. }
  22. if(type == 'name' && cls != 'tag'){
  23. return {
  24. node: false,
  25. target: 'node'
  26. };
  27. }
  28. return {
  29. node: Mif.Tree.Nodes[target.getAttribute('uid')],
  30. target: type
  31. };
  32. }
  33. });
  34. Mif.Tree.Node.prototype.getNextVisible_ = Mif.Tree.Node.prototype.getNextVisible;
  35. Mif.Tree.Node.prototype.getPreviousVisible_ = Mif.Tree.Node.prototype.getPreviousVisible;
  36. Mif.Tree.Node.implement({
  37. hide: function(){
  38. this.state.hidden = true;
  39. this.getDOM('node').style.display = 'none';
  40. },
  41. show: function(){
  42. this.state.hidden = false;
  43. this.getDOM('node').style.display = 'block';
  44. },
  45. isHide: function(){
  46. return this.state.hidden;
  47. },
  48. getNextVisible: function(){
  49. var node = this;
  50. do{
  51. node = node.getNextVisible_();
  52. if(!node) return false;
  53. }while(node.isHide())
  54. return node;
  55. },
  56. getPreviousVisible: function(){
  57. var node = this;
  58. do{
  59. node = node.getPreviousVisible_();
  60. if(!node) return false;
  61. }while(node.isHide())
  62. return node;
  63. }
  64. });
  65. Mif.Tree.KeyNav.implement({
  66. goForward: function(current){
  67. var forward = current;
  68. do{
  69. forward = forward.getNextVisible();
  70. if(!forward) return;
  71. }while(forward.type == 'close');
  72. if( forward ) this.tree.select(forward);
  73. },
  74. goBack: function(current){
  75. var back = current;
  76. do{
  77. back = back.getPreviousVisible();
  78. if(!back) return;
  79. }while(back.type == 'close');
  80. if (back) this.tree.select(back);
  81. }
  82. });
  83. DOMTree = new Mif.Tree({
  84. container: $('tree_container'),
  85. forest: true,
  86. types: {
  87. open:{
  88. cls: 'tag-open',
  89. loadable: true
  90. },
  91. close:{
  92. loadable: false,
  93. cls: 'tag-close'
  94. }
  95. },
  96. dfltType: 'open',
  97. height: 18,
  98. initialize: function(){
  99. new Mif.Tree.KeyNav(this);
  100. },
  101. onToggle: function(node, state){
  102. var next = node.getNext();
  103. if(!next) return;
  104. next[state ? 'show' : 'hide']();
  105. },
  106. onDrawChildren: function(parent){
  107. parent.children.each(function(child){
  108. if(child.type != 'close') return;
  109. child.hide();
  110. });
  111. },
  112. onSelect: function(node){
  113. if(node.type == 'close'){
  114. node.tree.select(node.getPrevious());
  115. }
  116. this.wrapper.focus();
  117. }
  118. })
  119. .load({
  120. json: [{
  121. property: {
  122. name: '&lt;<span class="tag">html</span>&gt;'
  123. },
  124. data:{
  125. el: $(document.documentElement)
  126. }
  127. },
  128. {
  129. property: {
  130. name: '&lt;/<span class="tag">html</span>&gt;'
  131. },
  132. type: 'close'
  133. }]
  134. });
  135. DOMTree.loadOptions = function(node){
  136. var json = [];
  137. var el = node.data.el;
  138. el.getChildren().each(function(child){
  139. var attrs = '';
  140. Array.from(child.attributes).each(function(attr){
  141. attrs += " " + attr.nodeName + '="<span class="attr-value">' + attr.nodeValue + '</span>"';
  142. });
  143. var hasChildren = child.getChildren().length > 0;
  144. var name;
  145. if(!hasChildren){
  146. var text = child.firstChild ? child.firstChild.nodeValue : '';
  147. name = '&lt;<span class="tag">' + child.get('tag') + '</span>' + attrs + '&gt;<span class="value">' + text + '</span>&lt;/<span class="tag">' + child.get('tag') + '</span>&gt;';
  148. var loadable = false;
  149. }else{
  150. name = '&lt;<span class="tag">' + child.get('tag') + '</span>' + attrs + '&gt;';
  151. loadable = true;
  152. }
  153. json.push({
  154. property: {
  155. name: name,
  156. loadable: loadable
  157. },
  158. data: {
  159. el: child
  160. }
  161. });
  162. json.push({
  163. property: {
  164. name: '&lt;/<span class="tag">' + child.get('tag') + '</span>&gt;'
  165. },
  166. type: 'close'
  167. });
  168. });
  169. return {json: json};
  170. };
  171. if(Browser.ie){
  172. (new Element('div', {styles:{color: 'red', fontSize: '20px', paddingLeft: '20px'}}).set('html', 'LOL IE')).inject($('tree_container'),'before');
  173. };
  174. });