Eltree.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. o2.xDesktop.requireApp("process.Xform", "$Elinput", null, false);
  2. /** @class Eltree 基于Element UI的树形控件。
  3. * @o2cn 树形控件
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var input = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var input = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS|Portal}
  13. * @hideconstructor
  14. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|Element UI Tree 树形控件}
  15. */
  16. MWF.xApplication.process.Xform.Eltree = MWF.APPEltree = new Class(
  17. /** @lends o2.xApplication.process.Xform.Eltree# */
  18. {
  19. Implements: [Events],
  20. Extends: MWF.APP$Elinput,
  21. options: {
  22. "moduleEvents": ["load", "queryLoad", "postLoad"],
  23. /**
  24. * 节点被点击时的回调。this.event[0]指向传递给 data 属性的数组中该节点所对应的对象;
  25. * this.event[1]为节点对应的 Node;this.event[2]为节点组件本身
  26. * @event MWF.xApplication.process.Xform.Eltree#node-click
  27. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  28. */
  29. /**
  30. * 当某一节点被鼠标右键点击时会触发该事件。this.event[0]指向Event;
  31. * this.event[1]为传递给 data 属性的数组中该节点所对应的对象;this.event[2]为节点对应的 Node;
  32. * this.event[3]为节点组件本身
  33. * @event MWF.xApplication.process.Xform.Eltree#node-contextmenu
  34. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  35. */
  36. /**
  37. * 节点选中状态发生变化时的回调。this.event[0]为传递给 data 属性的数组中该节点所对应的对象;
  38. * this.event[1]为节点本身是否被选中;this.event[2]为节点的子树中是否有被选中的节点
  39. * @event MWF.xApplication.process.Xform.Eltree#check-change
  40. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  41. */
  42. /**
  43. * 当复选框被点击的时候触发。this.event[0]为传递给 data 属性的数组中该节点所对应的对象;
  44. * this.event[1]为树目前的选中状态对象,包含 checkedNodes、checkedKeys、halfCheckedNodes、halfCheckedKeys 四个属性
  45. * @event MWF.xApplication.process.Xform.Eltree#check
  46. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  47. */
  48. /**
  49. * 当前选中节点变化时触发的事件。this.event[0]为当前节点的数据;
  50. * this.event[1]为当前节点的 Node 对象
  51. * * @event MWF.xApplication.process.Xform.Eltree#current-change
  52. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  53. */
  54. /**
  55. * 节点被展开时触发的事件。this.event[0]为传递给 data 属性的数组中该节点所对应的对象;
  56. * this.event[1]为节点对应的 Node;this.event[2]为节点组件本身
  57. * * @event MWF.xApplication.process.Xform.Eltree#node-expand
  58. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  59. */
  60. /**
  61. * 节点被关闭时触发的事件。this.event[0]为传递给 data 属性的数组中该节点所对应的对象;
  62. * this.event[1]为节点对应的 Node;this.event[2]为节点组件本身
  63. * * @event MWF.xApplication.process.Xform.Eltree#node-collapse
  64. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  65. */
  66. /**
  67. * 节点开始拖拽时触发的事件。this.event[0]为被拖拽节点对应的 Node;
  68. * this.event[1]为Event
  69. * * @event MWF.xApplication.process.Xform.Eltree#node-drag-start
  70. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  71. */
  72. /**
  73. * 拖拽进入其他节点时触发的事件。this.event[0]为被拖拽节点对应的 Node;
  74. * this.event[1]为所进入节点对应的 Node;this.event[2]为event
  75. * * @event MWF.xApplication.process.Xform.Eltree#node-drag-enter
  76. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  77. */
  78. /**
  79. * 拖拽离开某个节点时触发的事件。this.event[0]为被拖拽节点对应的 Node;
  80. * this.event[1]为所进入节点对应的 Node;this.event[2]为event
  81. * * @event MWF.xApplication.process.Xform.Eltree#node-drag-leave
  82. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  83. */
  84. /**
  85. * 在拖拽节点时触发的事件(类似浏览器的 mouseover 事件) 。this.event[0]为被拖拽节点对应的 Node;
  86. * this.event[1]为所进入节点对应的 Node;this.event[2]为event
  87. * * @event MWF.xApplication.process.Xform.Eltree#node-drag-over
  88. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  89. */
  90. /**
  91. * 拖拽结束时(可能未成功)触发的事件。this.event[0]为被拖拽节点对应的 Node;
  92. * this.event[1]为结束拖拽时最后进入的节点(可能为空);
  93. * this.event[2]为被拖拽节点的放置位置(before、after、inner)
  94. * this.event[3]为event
  95. * @event MWF.xApplication.process.Xform.Eltree#node-drag-end
  96. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  97. */
  98. /**
  99. * 拖拽成功完成时触发的事件。this.event[0]为被拖拽节点对应的 Node;
  100. * this.event[1]为结束拖拽时最后进入的节点;
  101. * this.event[2]为被拖拽节点的放置位置(before、after、inner)
  102. * this.event[3]为event
  103. * @event MWF.xApplication.process.Xform.Eltree#node-drop
  104. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|树组件的Events章节}
  105. */
  106. "elEvents": ["node-click", "node-contextmenu", "check-change", "check", "current-change","node-expand",
  107. "node-collapse","node-drag-start","node-drag-enter","node-drag-leave","node-drag-over","node-drag-end","node-drop"]
  108. },
  109. __setReadonly: function(data){
  110. if (this.isReadonly()) {
  111. this.node.set("text", data);
  112. if( this.json.elProperties ){
  113. this.node.set(this.json.elProperties );
  114. }
  115. if (this.json.elStyles){
  116. this.node.setStyles( this._parseStyles(this.json.elStyles) );
  117. }
  118. }
  119. },
  120. _loadNode: function(){
  121. // if (this.isReadonly()) this.json.disabled = true;
  122. this._loadNodeEdit();
  123. },
  124. _appendVueData: function(){
  125. if (!this.json.emptyText) this.json.emptyText = "";
  126. // if (!this.json.renderAfterExpand) this.json.renderAfterExpand = true;
  127. if (!this.json.highlightCurrent) this.json.highlightCurrent = false;
  128. if (!this.json.defaultExpandAll) this.json.defaultExpandAll = false;
  129. // if (!this.json.expandOnClickNode) this.json.expandOnClickNode = true;
  130. if (!this.json.accordion) this.json.accordion = false;
  131. if (!this.json.indent) this.json.indent = 16;
  132. if (this.json.currentNodeKey && this.json.currentNodeKey.code){
  133. this.json.currentNodeKey = this.form.Macro.fire(this.json.currentNodeKey.code, this);
  134. }
  135. if( !this.json.defaultExpandAll && this.json.defaultExpandedKeys && this.json.defaultExpandedKeys.code ){
  136. this.json.defaultExpandedKeys = this.form.Macro.fire(this.json.defaultExpandedKeys.code, this);
  137. }
  138. if( this.json.showCheckbox && this.json.defaultCheckedKeys && this.json.defaultCheckedKeys.code ){
  139. this.json.defaultCheckedKeys = this.form.Macro.fire(this.json.defaultCheckedKeys.code, this);
  140. }
  141. // if( this.json.lazy && this.json.loadFun && this.json.loadFun.code ){
  142. // this.json.lazyLoadFun = function(node, resolve){
  143. // return this.form.Macro.fire(this.json.loadFun.code, this, {
  144. // node: node,
  145. // resolve: resolve
  146. // });
  147. // }.bind(this)
  148. // }
  149. if( this.json.draggable ){
  150. if( this.json.allowDrag && this.json.allowDrag.code ){
  151. this.json.allowDragFun = function(node){
  152. return this.form.Macro.fire(this.json.allowDrag.code, this, node);
  153. }.bind(this)
  154. }
  155. if( this.json.allowDrop && this.json.allowDrop.code ){
  156. this.json.allowDropFun = function(node){
  157. return this.form.Macro.fire(this.json.allowDrop.code, this, node);
  158. }.bind(this)
  159. }
  160. }
  161. if (this.json.dataType === "script"){
  162. this.json.data = this.form.Macro.exec(((this.json.dataScript) ? this.json.dataScript.code : ""), this);
  163. }else{
  164. this.json.data = this.json.dataJson;
  165. }
  166. this.parseData();
  167. },
  168. _createElementHtml: function() {
  169. var html = "<el-tree";
  170. html += " v-model=\""+this.json.$id+"\"";
  171. html += " :data=\"data\"";
  172. html += " :empty-text=\"emptyText\"";
  173. html += " :props=\"defaultProps\"";
  174. html += " :render-after-expand=\"renderAfterExpand\"";
  175. html += " :highlight-current=\"highlightCurrent\"";
  176. html += " :default-expand-all=\"defaultExpandAll\"";
  177. html += " :expand-on-click-node=\"expandOnClickNode\"";
  178. html += " :accordion=\"accordion\"";
  179. html += " :indent=\"indent\"";
  180. if( this.json.nodeKey ){
  181. html += " :node-key=\"nodeKey\"";
  182. }
  183. if( this.json.currentNodeKey && this.json.currentNodeKey.code ){
  184. html += " :current-node-key=\"currentNodeKey\"";
  185. }
  186. if( !this.json.defaultExpandAll && this.json.defaultExpandedKeys && this.json.defaultExpandedKeys.code ){
  187. html += " :default-expanded-keys=\"defaultExpandedKeys\"";
  188. }
  189. if( this.json.showCheckbox ){
  190. html += " :show-checkbox=\"showCheckbox\"";
  191. html += " :check-on-click-node=\"checkOnClickNode\"";
  192. html += " :check-strictly=\"checkStrictly\"";
  193. if( this.json.defaultCheckedKeys && this.json.defaultCheckedKeys.code ){
  194. html += " :default-checked-keys=\"defaultCheckedKeys\"";
  195. }
  196. }
  197. // if( this.json.lazy ){
  198. // html += " :lazy=\"lazy\"";
  199. // if( this.json.loadFun && this.json.loadFun.code ){
  200. // html += " :load=\"lazyLoadFun\"";
  201. // }
  202. // }
  203. if( this.json.draggable ){
  204. html += " :draggable=\"draggable\"";
  205. if( this.json.allowDrag && this.json.allowDrag.code ){
  206. html += " :allow-drag=\"allowDragFun\"";
  207. }
  208. if( this.json.allowDrop && this.json.allowDrop.code ){
  209. html += " :allow-drop=\"allowDropFun\"";
  210. }
  211. }
  212. this.options.elEvents.forEach(function(k){
  213. html += " @"+k+"=\"$loadElEvent_"+k.camelCase()+"\"";
  214. });
  215. if (this.json.elProperties){
  216. Object.keys(this.json.elProperties).forEach(function(k){
  217. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  218. }, this);
  219. }
  220. if (this.json.elStyles) html += " :style=\"elStyles\"";
  221. html += ">";
  222. if (this.json.vueSlot) html += this.json.vueSlot;
  223. html += "</el-tree>";
  224. return html;
  225. },
  226. parseData: function ( data ) {
  227. if( !data )data = this.json.data;
  228. var config = {};
  229. //label和children转成defaultOptions的数组
  230. if( this.json.defaultProps ){
  231. var p = this.json.defaultProps;
  232. if( p.children !== "children" && o2.typeOf(p.children)==="string") config.children = p.children;
  233. if( p.label !== "label" && o2.typeOf(p.label)==="string" )config.label = p.label;
  234. if( p.disabled !== "disabled" && o2.typeOf(p.disabled)==="string")config.disabled = p.disabled;
  235. if( p.label !== "isLeaf" && o2.typeOf(p.isLeaf)==="string")config.isLeaf = p.isLeaf;
  236. }
  237. //把id转成成node-key,
  238. if( this.json.nodeKey && this.json.nodeKey !== "id" )config.id = this.json.nodeKey;
  239. if( Object.keys(config).length > 0 ){
  240. this._parseData(data, config);
  241. }
  242. },
  243. _parseData: function ( data, config ) {
  244. if( o2.typeOf(data) === "array" ){
  245. data.each(function(d){ this._parse(d, config) }.bind(this))
  246. }else{
  247. this._parse(data, config);
  248. }
  249. },
  250. _parse: function (data, config) {
  251. Object.each(config, function (value, key) {
  252. if( data[key] )data[value] = data[key];
  253. });
  254. var children = data[ config.children || "children" ];
  255. if(children && o2.typeOf(children)==="array" )this._parseData( children, config );
  256. }
  257. });