yvtree.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import _ from 'lodash'
  2. import {tree} from '../Defaults'
  3. import {baseConfig} from "./base";
  4. import {lookupFn, lookupScope} from "../lib/lib";
  5. import {calcObject, calcObjectFlat, invokeServer} from "../lib/systemLib";
  6. import {msg} from "../message";
  7. export default function () {
  8. Ext.define('Yvan.Tree', {
  9. extend: 'Ext.tree.Panel',
  10. alias: 'widget.yvtree',
  11. xtype: 'yvtree',
  12. constructor(config) {
  13. const newConfig = _.defaultsDeep({
  14. // 强制性属性
  15. }, baseConfig(config, 'row-item'), config, tree)
  16. this.superclass.constructor.call(this, newConfig)
  17. },
  18. /**
  19. * 在根节点上增加一个节点,会触发 addNode 事件
  20. * @param props
  21. */
  22. addRoot(props) {
  23. const me = this
  24. const tableNode = me.getRootNode()
  25. const fieldNode = tableNode.createNode(new Ext.data.NodeInterface({}))
  26. if (_.isFunction(props)) {
  27. props = props.call(this, parent)
  28. }
  29. _.forOwn(props, (value, key) => {
  30. fieldNode.set(key, value)
  31. })
  32. const node = tableNode.appendChild(fieldNode)
  33. me.ensureVisible(node)
  34. me.setSelection(node)
  35. me.fireEvent('addNode', me, {
  36. parent: tableNode,
  37. node: node,
  38. });
  39. },
  40. addLast(props) {
  41. const me = this
  42. const sel = me.selModel.getSelection()
  43. if (!sel || sel.length <= 0) {
  44. msg('请选择一个要添加的同级节点路径')
  45. return
  46. }
  47. const parent = sel[0].parentNode ? sel[0].parentNode : me.getRootNode()
  48. const fieldNode = parent.createNode(new Ext.data.NodeInterface({}))
  49. if (_.isFunction(props)) {
  50. props = props.call(this, parent)
  51. }
  52. _.forOwn(props, (value, key) => {
  53. fieldNode.set(key, value)
  54. })
  55. const node = parent.appendChild(fieldNode)
  56. me.ensureVisible(node)
  57. me.setSelection(node)
  58. me.fireEvent('addNode', me, {
  59. parent: parent,
  60. node: node,
  61. });
  62. },
  63. addChildren(props) {
  64. const me = this
  65. const sel = me.selModel.getSelection()
  66. if (!sel || sel.length <= 0) {
  67. msg('请选择一个要添加子节点的路径')
  68. return
  69. }
  70. const parent = sel[0]
  71. if (_.isFunction(props)) {
  72. props = props.call(this, parent)
  73. }
  74. const fieldNode = parent.createNode(new Ext.data.NodeInterface({}))
  75. _.forOwn(props, (value, key) => {
  76. fieldNode.set(key, value)
  77. })
  78. const node = parent.appendChild(fieldNode)
  79. parent.expand()
  80. me.ensureVisible(node)
  81. me.setSelection(node)
  82. me.fireEvent('addNode', me, {
  83. parent: parent,
  84. node: node,
  85. });
  86. },
  87. initComponent() {
  88. const me = this
  89. const {config} = me
  90. const scope = lookupScope(this)
  91. if (!window["IS_DESIGN_MODE"]) {
  92. // 转换 dataSource 属性
  93. convertDataSource(me, scope, config)
  94. }
  95. this.superclass.initComponent.call(this)
  96. this.on({
  97. afterrender(sender) {
  98. const me = this
  99. const {config} = this
  100. const {dataSource} = config
  101. if (config.autoLoad) {
  102. if (config.dataSourceCallbackFn) {
  103. me.reload()
  104. } else if (_.isPlainObject(dataSource) && dataSource.method === 'invoke') {
  105. me.reload()
  106. }
  107. }
  108. }
  109. })
  110. },
  111. _setDataReal(value) {
  112. const me = this
  113. // me.setStore(new Ext.data.TreeStore({
  114. // root: {
  115. // expanded: true,
  116. // // children: value
  117. // children: [
  118. // {
  119. // id: '1', text: 'A',
  120. // children: [
  121. // {id: '1', text: 'A',},
  122. // {id: '2', text: 'B'},
  123. // ]
  124. // },
  125. // {id: '2', text: 'B'},
  126. // ]
  127. // }
  128. // }))
  129. me.store.setRoot({
  130. expanded: true,
  131. children: value
  132. })
  133. // me.store = new Ext.data.TreeStore({
  134. // root: {
  135. // expanded: true,
  136. // // children: value
  137. // children: [
  138. // {
  139. // id: '1', text: 'A',
  140. // children: [
  141. // {id: '1', text: 'A',},
  142. // {id: '2', text: 'B'},
  143. // ]
  144. // },
  145. // {id: '2', text: 'B'},
  146. // ]
  147. // }
  148. // })
  149. },
  150. /**
  151. * 重新载入数据(重新计算参数)
  152. */
  153. reload(reloadParams = {}) {
  154. const me = this
  155. const {config} = me
  156. if (config.dataSourceCallbackFn) {
  157. // 函数请求刷新
  158. const scope = lookupScope(this)
  159. // me.setLoading(true)
  160. config.dataSourceCallbackFn.call(scope, me, {
  161. successCallback(value) {
  162. me._setDataReal(value)
  163. // me.setLoading(false)
  164. me.fireEvent('dataLoadComplete', me, true, value);
  165. },
  166. failCallback(error) {
  167. // me.setLoading(false)
  168. me.fireEvent('dataLoadComplete', me, false, error);
  169. }
  170. })
  171. return
  172. }
  173. const {dataSource} = config
  174. if (_.isPlainObject(dataSource) && dataSource.method === 'invoke' && !window["IS_DESIGN_MODE"]) {
  175. const scope = lookupScope(me)
  176. const params = calcObjectFlat(scope.viewModel.data, _.defaultsDeep({}, reloadParams, dataSource.params),)
  177. me.setLoading(true)
  178. invokeServer(dataSource.url, params)
  179. .then(res => {
  180. me._setDataReal(res.data)
  181. me.fireEvent('dataLoadComplete', me, res)
  182. })
  183. .finally(() => {
  184. me.setLoading(false)
  185. })
  186. }
  187. },
  188. filterByText(text) {
  189. this.filterBy(text, 'text');
  190. },
  191. filterBy(text, by) {
  192. this.clearFilter();
  193. const view = this.getView(),
  194. me = this,
  195. nodesAndParents = [];
  196. this.getRootNode().cascadeBy(function (tree, view) {
  197. let currNode = this;
  198. if (currNode && currNode.data[by] && currNode.data[by].toString().toLowerCase().indexOf(text.toLowerCase()) > -1) {
  199. me.expandPath(currNode.getPath());
  200. while (currNode.parentNode) {
  201. nodesAndParents.push(currNode.id);
  202. currNode = currNode.parentNode;
  203. }
  204. }
  205. }, null, [me, view]);
  206. this.getRootNode().cascadeBy(function (tree, view) {
  207. var uiNode = view.getNodeByRecord(this);
  208. if (uiNode && !Ext.Array.contains(nodesAndParents, this.id)) {
  209. Ext.get(uiNode).setDisplayed('none');
  210. }
  211. }, null, [me, view]);
  212. },
  213. clearFilter() {
  214. const view = this.getView();
  215. this.getRootNode().cascadeBy(function (tree, view) {
  216. var uiNode = view.getNodeByRecord(this);
  217. if (uiNode) {
  218. Ext.get(uiNode).setDisplayed('table-row');
  219. }
  220. }, null, [this, view]);
  221. },
  222. root: [],
  223. rootVisible: false,
  224. tbar: [
  225. {
  226. xtype: 'trigger',
  227. triggerCls: 'x-form-clear-trigger',
  228. onTriggerClick(sender) {
  229. sender.setValue('');
  230. const scope = sender.up("yvtree")
  231. scope.clearFilter()
  232. },
  233. flex: 1,
  234. emptyText: '搜索过滤',
  235. enableKeyEvents: true,
  236. listeners: {
  237. keyup: {
  238. fn(sender, e) {
  239. if (e.ESC == e.getKey()) {
  240. sender.onTriggerClick(sender);
  241. } else {
  242. const scope = sender.up("yvtree")
  243. scope.filterByText(this.getRawValue());
  244. }
  245. },
  246. buffer: 500
  247. }
  248. }
  249. },
  250. {
  251. xtype: 'button', iconCls: 'x-fa fa-refresh', tooltip: '刷新',
  252. listeners: {
  253. click(sender) {
  254. const scope = sender.up("yvtree")
  255. scope.reload()
  256. }
  257. }
  258. },
  259. {
  260. xtype: 'button', iconCls: 'x-fa fa-plus-square-o', tooltip: '全部展开',
  261. listeners: {
  262. click(sender) {
  263. const scope = sender.up("yvtree")
  264. scope.expandAll()
  265. }
  266. }
  267. },
  268. {
  269. xtype: 'button', iconCls: 'x-fa fa-minus-square-o', tooltip: '全部收起',
  270. listeners: {
  271. click(sender) {
  272. const scope = sender.up("yvtree")
  273. scope.collapseAll()
  274. }
  275. }
  276. }
  277. ],
  278. })
  279. }
  280. function convertDataSource(sender, scope, newConfig) {
  281. if (typeof newConfig.store !== 'undefined') {
  282. // 有 store 属性的情况下,不做任何事
  283. return
  284. }
  285. if (typeof newConfig.dataSource === 'undefined') {
  286. // 没有定义 dataSource 的情况下,不做任何事
  287. return
  288. }
  289. let {dataSource} = newConfig
  290. if (typeof dataSource === 'string') {
  291. // dataSource 是字符串的情况下,找到成员函数
  292. dataSource = lookupFn(scope, dataSource)
  293. }
  294. if (typeof dataSource === 'function') {
  295. // dataSource 是函数的情况下,在 afterrender 之后进行回调
  296. newConfig.store = new Ext.data.TreeStore({
  297. root: {
  298. expanded: true,
  299. children: []
  300. }
  301. })
  302. newConfig.dataSourceCallbackFn = dataSource
  303. return
  304. }
  305. // throw new TypeError('无法识别的调用方法')
  306. }