combo.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import _ from 'lodash'
  2. import {combo} from '../Defaults'
  3. import {baseConfig} from "./base";
  4. import {dataSourceReload} from "../DataSourceHelper";
  5. export default function () {
  6. const cc = Ext.form.field.ComboBox.prototype.constructor
  7. const {initComponent, setData} = Ext.form.field.ComboBox.prototype
  8. Ext.form.field.ComboBox.override({
  9. constructor(config) {
  10. const newConfig = _.defaultsDeep({
  11. // 强制性属性
  12. }, baseConfig(config, 'col-item'), config, combo)
  13. if (typeof newConfig.store == 'undefined') {
  14. if (_.isArray(newConfig.data)) {
  15. // ExtJS 无法直接接受数组模式
  16. newConfig.store = {
  17. fields: [newConfig.valueField, newConfig.displayField],
  18. data: newConfig.data
  19. }
  20. delete newConfig.data
  21. } else if (typeof newConfig.data !== 'object') {
  22. newConfig.store = {
  23. fields: [newConfig.valueField, newConfig.displayField],
  24. data: []
  25. }
  26. delete newConfig.data
  27. }
  28. }
  29. cc.call(this, newConfig)
  30. },
  31. setData(value) {
  32. const {config} = value
  33. if (!this.store) {
  34. this.store = new Ext.data.Store({
  35. fields: [config.valueField, config.displayField],
  36. data: value
  37. })
  38. } else {
  39. this.store.getProxy().setData(value)
  40. this.store.load()
  41. }
  42. },
  43. setLoading(value) {
  44. if (value) {
  45. if (!this.loadMask) {
  46. this.loadMask = new Ext.LoadMask(this, {msg: "loading..."});
  47. }
  48. } else {
  49. if (this.loadMask) {
  50. this.loadMask.destroy()
  51. delete this.loadMask
  52. }
  53. }
  54. },
  55. reload() {
  56. dataSourceReload(this)
  57. },
  58. initComponent() {
  59. const that = this
  60. const toggle = () => {
  61. if (that.isExpanded) {
  62. that.collapse()
  63. } else {
  64. that.expand();
  65. that.doQuery(that.allQuery, true);
  66. }
  67. }
  68. this.on({
  69. afterrender() {
  70. const {config} = this
  71. if (config.dataSource && config.autoLoad) {
  72. dataSourceReload(this)
  73. }
  74. $(this.el.dom).on('click', toggle)
  75. },
  76. destory() {
  77. $(this.el.dom).off('click', toggle)
  78. },
  79. // focus: {
  80. // // 获得焦点后自动下拉
  81. // fn(sender) {
  82. // sender.expand();
  83. // this.doQuery(this.allQuery, true);
  84. // },
  85. // },
  86. })
  87. initComponent.call(this)
  88. },
  89. });
  90. }