LinkModal.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <a-modal
  3. class="noborder"
  4. :title="title"
  5. :width="820"
  6. :visible="visible"
  7. :isLoading="isLoading"
  8. :maskClosable="false"
  9. :destroyOnClose="true"
  10. @ok="handleSubmit"
  11. @cancel="handleCancel"
  12. >
  13. <div class="links-body">
  14. <a-collapse :defaultActiveKey="keys" :bordered="false" expandIconPosition="left">
  15. <a-collapse-panel v-for="group in linkList" :key="group.key" :header="group.title">
  16. <div class="link-list">
  17. <div
  18. v-for="(item, index) in group.data"
  19. :key="index"
  20. class="link-item"
  21. :class="{ active: activeKey == item.id}"
  22. @click="handleClickItem(item)"
  23. >
  24. <span class="link-title">{{ item.title }}</span>
  25. </div>
  26. </div>
  27. </a-collapse-panel>
  28. </a-collapse>
  29. <a-drawer
  30. :title="drawer.title"
  31. placement="right"
  32. :width="350"
  33. :closable="false"
  34. :visible="drawer.visible"
  35. :getContainer="false"
  36. :wrapStyle="{ position: 'absolute' }"
  37. @close="onCloseDrawer"
  38. >
  39. <a-form v-if="drawer.visible" :form="form">
  40. <a-form-item
  41. v-for="(item, index) in curItem.form"
  42. :key="index"
  43. :label="item.lable"
  44. :labelCol="{ span: 6 }"
  45. :wrapperCol="{ span: 16 }"
  46. >
  47. <a-input
  48. v-decorator="[`values.${index}`, {
  49. initialValue: item.value,
  50. rules: [{ required: item.required, message: `${item.lable}必须填写` }]
  51. }]"
  52. />
  53. <div class="form-item-help">
  54. <small v-html="item.tips"></small>
  55. </div>
  56. </a-form-item>
  57. </a-form>
  58. </a-drawer>
  59. </div>
  60. </a-modal>
  61. </template>
  62. <script>
  63. import _ from 'lodash'
  64. import { linkList } from '@/common/model/Links'
  65. export default {
  66. name: 'LinkModal',
  67. model: {
  68. prop: 'value',
  69. event: 'change'
  70. },
  71. data () {
  72. return {
  73. // 对话框标题
  74. title: '选择链接',
  75. // modal(对话框)是否可见
  76. visible: false,
  77. // loading状态
  78. isLoading: false,
  79. // 当前表单元素
  80. form: this.$form.createForm(this),
  81. // 所有链接数据
  82. linkList,
  83. // 当前选中的键值
  84. activeKey: '',
  85. // 当前选中的链接
  86. curItem: null,
  87. // 抽屉组件属性
  88. drawer: {
  89. visible: false,
  90. title: ''
  91. }
  92. }
  93. },
  94. computed: {
  95. keys () {
  96. const { linkList } = this
  97. return linkList.map(item => item.key)
  98. }
  99. },
  100. created () {
  101. },
  102. methods: {
  103. // 显示对话框
  104. handle (record = null) {
  105. this.visible = true
  106. if (record != null) {
  107. this.handleClickItem(record)
  108. }
  109. },
  110. // 选中事件
  111. handleClickItem (item) {
  112. // 取消选中值
  113. if (this.activeKey === item.id) {
  114. this.activeKey = ''
  115. this.curItem = null
  116. return
  117. }
  118. // 记录选中值
  119. this.activeKey = item.id
  120. this.curItem = _.cloneDeep(item)
  121. // 显示表单
  122. this.onShowFrom()
  123. },
  124. // 显示表单
  125. onShowFrom () {
  126. const { curItem } = this
  127. if (curItem.form && curItem.form.length) {
  128. // 显示抽屉
  129. this.onShowDrawer()
  130. }
  131. },
  132. // 显示抽屉
  133. onShowDrawer () {
  134. const { drawer, curItem } = this
  135. drawer.visible = true
  136. drawer.title = curItem.title
  137. },
  138. // 关闭抽屉
  139. onCloseDrawer () {
  140. this.activeKey = ''
  141. this.curItem = null
  142. this.drawer.visible = false
  143. },
  144. // 关闭对话框事件
  145. handleCancel () {
  146. this.visible = false
  147. this.onCloseDrawer()
  148. },
  149. // 确认按钮
  150. handleSubmit (e) {
  151. e.preventDefault()
  152. const { curItem, form: { validateFields } } = this
  153. validateFields((errors, formData) => {
  154. if (!errors) {
  155. // 创建返回结果
  156. const result = curItem ? this.buildResult(curItem, formData) : null
  157. // 通知父端组件提交完成了
  158. this.$emit('handleSubmit', result)
  159. // 关闭对话框
  160. this.handleCancel()
  161. }
  162. })
  163. },
  164. // 构建选中的link对象
  165. buildResult (link, formData) {
  166. for (const index in link.form) {
  167. const item = link.form[index]
  168. if (!item) {
  169. continue
  170. }
  171. // 记录输入的数据
  172. if (!formData.values[index]) {
  173. formData.values[index] = ''
  174. }
  175. // 将value赋值到key指向的param对象元素
  176. item.value = formData.values[index]
  177. _.set(link.param, item.key, item.value)
  178. }
  179. // 克隆一个新对象返回
  180. return _.cloneDeep(link)
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="less" scoped>
  186. .ant-modal-root {
  187. background: #ccc;
  188. /deep/.ant-modal-body {
  189. padding: 0 24px 15px 24px;
  190. }
  191. /deep/.ant-modal-footer {
  192. padding-top: 0;
  193. }
  194. }
  195. /deep/.ant-collapse-header {
  196. padding: 14px 16px;
  197. font-size: 13px;
  198. font-weight: 700;
  199. color: #595961;
  200. }
  201. /deep/.ant-collapse-content-box {
  202. padding-top: 0 !important;
  203. }
  204. /deep/.ant-collapse > .ant-collapse-item {
  205. border-bottom: none;
  206. margin-bottom: 15px;
  207. &:last-child {
  208. margin-bottom: 0;
  209. }
  210. }
  211. .links-body {
  212. position: relative;
  213. min-height: 360px;
  214. overflow: hidden;
  215. }
  216. .link-list {
  217. .link-item {
  218. float: left;
  219. padding: 5px 15px;
  220. border: 1px solid #ccc;
  221. border-radius: 5px;
  222. margin-right: 20px;
  223. margin-bottom: 15px;
  224. font-size: 12.5px;
  225. cursor: pointer;
  226. &:hover {
  227. border: 1px solid @primary-color;
  228. color: @primary-color;
  229. }
  230. &.active {
  231. background: @primary-color;
  232. border-color: @primary-color;
  233. color: #fff;
  234. }
  235. &:last-child {
  236. margin-right: 0;
  237. }
  238. }
  239. }
  240. </style>