Tree.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /**树组件数据结构
  3. * @typedef {Object} TreeData
  4. * @example
  5. * [
  6. * {
  7. * "expand": true, //是否默认展开
  8. * "title": "", //鼠标移上叶子节点的文字
  9. * "text": "根节点", //叶子节点的文字
  10. * "action": "", //执行的脚本
  11. * "default": true, //是否默认选中
  12. * "icon": "folder.png", //图标
  13. * "sub": [ //该节点的子节点
  14. * {
  15. * "expand": true,
  16. * "title": "",
  17. * "text": "[none]",
  18. * "action": "",
  19. * "default": false,
  20. * "icon": "folder.png",
  21. * "sub": []
  22. * },
  23. * ...
  24. * ]
  25. * }
  26. * ]
  27. */
  28. /** @class Tree 树组件。
  29. * @o2cn 树组件
  30. * @example
  31. * //可以在脚本中获取该组件
  32. * //方法1:
  33. * var datagrid = this.form.get("name"); //获取组件
  34. * //方法2
  35. * var datagrid = this.target; //在组件事件脚本中获取
  36. * @see {@link TreeData|树组件数据结构}
  37. * @extends MWF.xApplication.process.Xform.$Module
  38. * @o2category FormComponents
  39. * @o2range {Process|CMS|Portal}
  40. * @hideconstructor
  41. */
  42. MWF.xApplication.process.Xform.Tree = MWF.APPTree = new Class(
  43. /** @lends MWF.xApplication.process.Xform.Tree# */
  44. {
  45. Extends: MWF.APP$Module,
  46. options: {
  47. /**
  48. * 异步加载树前执行。this.target指向当前组件。
  49. * @event MWF.xApplication.process.Xform.Tree#beforeLoadTree
  50. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  51. */
  52. /**
  53. * 异步加载树后执行。this.target指向当前组件。
  54. * @event MWF.xApplication.process.Xform.Tree#afterLoadTree
  55. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  56. */
  57. /**
  58. * 加载树的叶子前执行。this.target指向加载的叶子。
  59. * @event MWF.xApplication.process.Xform.Tree#beforeLoadTreeNode
  60. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  61. */
  62. /**
  63. * 加载树的叶子后执行。this.target指向加载的叶子。
  64. * @event MWF.xApplication.process.Xform.Tree#afterLoadTreeNode
  65. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  66. */
  67. /**
  68. * 加载树的叶子后执行。this.target指向加载的叶子。
  69. * @event MWF.xApplication.process.Xform.Tree#expand
  70. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  71. */
  72. /**
  73. * 折叠节点的时候执行。this.target指向被折叠的节点。
  74. * @event MWF.xApplication.process.Xform.Tree#collapse
  75. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  76. */
  77. /**
  78. * 选中节点前执行。此时原来被选中的节点还未取消。this.target指向选中的节点。
  79. * @event MWF.xApplication.process.Xform.Tree#beforeSelect
  80. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  81. */
  82. /**
  83. * 选中节点后执行。this.target指向选中的节点。
  84. * @event MWF.xApplication.process.Xform.Tree#afterSelect
  85. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  86. */
  87. "moduleEvents": ["load", "queryLoad", "postLoad", "beforeLoadTree", "afterLoadTree", "beforeLoadTreeNode", "afterLoadTreeNode", "expand", "collapse", "beforeSelect", "afterSelect"]
  88. },
  89. _loadUserInterface: function(){
  90. this.node.empty();
  91. MWF.require("MWF.widget.Tree", function(){
  92. var options = {"style":"form"};
  93. if( this.json.events && typeOf(this.json.events) === "object" ){
  94. [
  95. { "beforeLoadTree" : "onQueryLoad" },
  96. { "afterLoadTree" : "onPostLoad" },
  97. { "beforeLoadTreeNode" : "onBeforeLoadTreeNode" },
  98. { "afterLoadTreeNode" : "onAfterLoadTreeNode" },
  99. { "expand" : "onPostExpand" },
  100. { "collapse" : "onPostCollapse" },
  101. { "beforeSelect" : "onBeforeSelect" },
  102. { "afterSelect" : "onAfterSelect" }
  103. ].each( function (obj) {
  104. var moduleEvent = Object.keys(obj)[0];
  105. var treeEvent = obj[moduleEvent];
  106. if( this.json.events[moduleEvent] && this.json.events[moduleEvent].code ){
  107. options[treeEvent] = function( target ){
  108. return this.form.Macro.fire(this.json.events[moduleEvent].code, target || this);
  109. }.bind(this)
  110. }
  111. }.bind(this));
  112. }
  113. /**
  114. * @summary 树组件,平台使用该组件实现树的功能,该组件为异步加载
  115. * @member {o2.widget.Tree}
  116. * @example
  117. * //可以在脚本中获取该组件
  118. * var tree = this.form.get("fieldId").tree; //获取组件对象
  119. * var children = tree.children[]; //获取第一层树叶
  120. * tree.reLoad( json ); //给整颗树重新赋数据,并重新加载
  121. */
  122. this.tree = new MWF.widget.Tree(this.node, options);
  123. this.tree.form = this.form;
  124. this._setTreeWidgetStyles();
  125. var treeData = this.json.data;
  126. if (this.json.dataType == "script") treeData = this.form.Macro.exec(((this.json.dataScript) ? this.json.dataScript.code : ""), this);
  127. this.tree.load(treeData);
  128. }.bind(this));
  129. },
  130. _setTreeWidgetStyles: function(){
  131. this.tree.css.areaNode = this.json.areaNodeStyle;
  132. this.tree.css.treeItemNode = this.json.treeItemNodeStyle;
  133. this.tree.css.textDivNode = this.json.textDivNodeStyle;
  134. this.tree.css.textDivNodeSelected = this.json.textDivNodeSelectedStyle;
  135. }
  136. });