dynamicForm.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <!--
  2. * @Descripttion: 大屏设计器-右区动态表单
  3. * @Author: Devli
  4. * @Date: 2021-3-13 11:04:24
  5. * @Last Modified by: qianlishi
  6. * @Last Modified time: 2022-3-14 14:04:24
  7. !-->
  8. <template>
  9. <div class="collapse-input-style">
  10. <el-form label-width="100px" label-position="left">
  11. <template v-for="(item, index) in options">
  12. <div v-if="isShowForm(item, '[object Object]')" :key="index">
  13. <el-form-item
  14. v-if="
  15. inputShow[item.name] &&
  16. item.type != 'dycustComponents' &&
  17. item.type != 'dynamic-add-table'
  18. "
  19. :label="item.label"
  20. :prop="item.name"
  21. :required="item.required"
  22. >
  23. <el-input-number
  24. v-if="item.type == 'el-input-number'"
  25. size="mini"
  26. style="width: 100%"
  27. v-model.trim="formData[item.name]"
  28. :disabled="item.disabled"
  29. controls-position="right"
  30. @change="changed($event, item.name)"
  31. />
  32. <el-input
  33. v-if="item.type == 'el-input-text'"
  34. v-model.trim="formData[item.name]"
  35. :disabled="item.disabled"
  36. type="text"
  37. size="mini"
  38. placeholder="请输入内容"
  39. clearable
  40. @change="changed($event, item.name)"
  41. />
  42. <el-input
  43. v-if="item.type == 'el-input-textarea'"
  44. v-model.trim="formData[item.name]"
  45. type="textarea"
  46. size="mini"
  47. rows="2"
  48. :disabled="item.disabled"
  49. placeholder="请输入内容"
  50. @change="changed($event, item.name)"
  51. />
  52. <el-switch
  53. v-if="item.type == 'el-switch'"
  54. v-model="formData[item.name]"
  55. size="mini"
  56. placeholder="请输入内容"
  57. :disabled="item.disabled"
  58. @change="changed($event, item.name)"
  59. />
  60. <ColorPicker
  61. v-if="item.type == 'vue-color'"
  62. v-model="formData[item.name]"
  63. @change="(val) => changed(val, item.name)"
  64. />
  65. <customUpload
  66. v-if="item.type == 'custom-upload'"
  67. v-model="formData[item.name]"
  68. @change="changed($event, item.name)"
  69. />
  70. <imageSelect
  71. v-if="item.type == 'image-select'"
  72. v-model="formData[item.name]"
  73. @change="changed($event, item.name)"
  74. />
  75. <el-radio-group
  76. v-if="item.type == 'el-radio-group'"
  77. v-model="formData[item.name]"
  78. @change="(val) => changed(val, item.name)"
  79. >
  80. <el-radio
  81. v-for="itemChild in item.selectOptions"
  82. :key="itemChild.code"
  83. :label="itemChild.code"
  84. >{{ itemChild.name }}</el-radio
  85. >
  86. </el-radio-group>
  87. <el-select
  88. v-if="item.type == 'el-select'"
  89. size="mini"
  90. :disabled="item.disabled"
  91. v-model="formData[item.name]"
  92. :multiple="item.multiple"
  93. clearable
  94. placeholder="请选择"
  95. @change="(val) => changed(val, item.name)"
  96. >
  97. <el-option
  98. v-for="itemChild in item.selectOptions"
  99. :key="itemChild.code"
  100. :label="itemChild.name"
  101. :value="itemChild.code"
  102. />
  103. </el-select>
  104. <el-slider
  105. v-if="item.type == 'el-slider'"
  106. v-model="formData[item.name]"
  107. @change="(val) => changed(val, item.name)"
  108. :step="item.options==null?1:item.options.step"
  109. :min="item.options==null?1:item.options.min"
  110. :max="item.options==null?100:item.options.max"
  111. :show-stops="item.options==null?false:item.options.showStops"
  112. />
  113. <el-button
  114. v-if="item.type == 'el-button'"
  115. type="primary"
  116. size="mini"
  117. plain
  118. @click="addStaticData"
  119. >编辑</el-button
  120. >
  121. <el-button
  122. v-if="item.type == 'methods'"
  123. type="primary"
  124. size="mini"
  125. @click="methodsVisible = true"
  126. >添加事件</el-button
  127. >
  128. <!-- 弹窗 -->
  129. <el-dialog
  130. title="代码编辑"
  131. :visible.sync="dialogVisibleStaticData"
  132. width="50%"
  133. :before-close="handleClose"
  134. >
  135. <vue-json-editor
  136. v-model="formData[item.name]"
  137. :show-btns="false"
  138. :mode="'code'"
  139. lang="zh"
  140. class="my-editor"
  141. />
  142. <span slot="footer" class="dialog-footer">
  143. <el-button @click="dialogVisibleStaticData = false"
  144. >取 消</el-button
  145. >
  146. <el-button type="primary" @click="saveData">确 定</el-button>
  147. </span>
  148. </el-dialog>
  149. <el-dialog
  150. title="代码编辑"
  151. :visible.sync="methodsVisible"
  152. width="50%"
  153. :before-close="handleClose"
  154. >
  155. <monaco-editor
  156. v-model.trim="formData[item.name]"
  157. language="javascript"
  158. style="height: 500px"
  159. />
  160. <span slot="footer" class="dialog-footer">
  161. <el-button @click="methodsVisible = false">取 消</el-button>
  162. <el-button type="primary" @click="saveData">确 定</el-button>
  163. </span>
  164. </el-dialog>
  165. </el-form-item>
  166. <dynamicComponents
  167. v-if="item.type == 'dycustComponents' && inputShow[item.name]"
  168. v-model="formData[item.name]"
  169. :chart-type="item.chartType"
  170. :dict-key="item.dictKey"
  171. @change="changed($event, item.name)"
  172. />
  173. <dynamic-add-table
  174. v-if="item.type == 'dynamic-add-table' && inputShow[item.name]"
  175. v-model="formData[item.name]"
  176. :chart-type="item.chartType"
  177. @change="changed($event, item.name)"
  178. />
  179. <dynamic-add-radar
  180. v-if="item.type == 'dynamic-add-radar' && inputShow[item.name]"
  181. v-model="formData[item.name]"
  182. :chart-type="item.chartType"
  183. @change="changed($event, item.name)"
  184. />
  185. </div>
  186. <div v-else-if="isShowForm(item, '[object Array]')" :key="'a-' + index">
  187. <el-collapse accordion>
  188. <el-collapse-item
  189. v-for="(itemChild, indexChild) in item"
  190. :key="indexChild"
  191. :title="itemChild.name"
  192. :name="indexChild"
  193. >
  194. <template v-for="(itemChildList, idx) in itemChild.list">
  195. <el-form-item
  196. :key="idx"
  197. :label="itemChildList.label"
  198. :prop="itemChildList.name"
  199. :required="itemChildList.required"
  200. >
  201. <el-input-number
  202. v-if="itemChildList.type == 'el-input-number'"
  203. size="mini"
  204. style="width: 100%"
  205. v-model="formData[itemChildList.name]"
  206. :disabled="item.disabled"
  207. controls-position="right"
  208. :placeholder="itemChildList.placeholder"
  209. @change="changed($event, itemChildList.name)"
  210. />
  211. <el-input
  212. v-if="itemChildList.type == 'el-input-text'"
  213. v-model.trim="formData[itemChildList.name]"
  214. :disabled="item.disabled"
  215. type="text"
  216. size="mini"
  217. placeholder="请输入内容"
  218. clearable
  219. @change="changed($event, itemChildList.name)"
  220. />
  221. <el-input
  222. v-if="itemChildList.type == 'el-input-textarea'"
  223. v-model.trim="formData[itemChildList.name]"
  224. :disabled="item.disabled"
  225. size="mini"
  226. type="textarea"
  227. rows="2"
  228. placeholder="请输入内容"
  229. @change="changed($event, itemChildList.name)"
  230. />
  231. <el-switch
  232. v-if="itemChildList.type == 'el-switch'"
  233. v-model="formData[itemChildList.name]"
  234. :disabled="item.disabled"
  235. placeholder="请输入内容"
  236. size="mini"
  237. @change="changed($event, itemChildList.name)"
  238. />
  239. <ColorPicker
  240. v-if="itemChildList.type == 'vue-color'"
  241. v-model="formData[itemChildList.name]"
  242. @change="(val) => changed(val, itemChildList.name)"
  243. />
  244. <el-upload
  245. v-if="itemChildList.type == 'el-upload-picture'"
  246. size="mini"
  247. action="https://jsonplaceholder.typicode.com/posts/"
  248. list-type="picture-card"
  249. />
  250. <el-radio-group
  251. v-if="itemChildList.type == 'el-radio-group'"
  252. v-model="formData[itemChildList.name]"
  253. @change="(val) => changed(val, itemChildList.name)"
  254. >
  255. <el-radio
  256. v-for="it in itemChildList.selectOptions"
  257. :key="it.code"
  258. :label="it.code"
  259. >{{ it.name }}</el-radio
  260. >
  261. </el-radio-group>
  262. <el-select
  263. v-if="itemChildList.type == 'el-select'"
  264. size="mini"
  265. v-model="formData[itemChildList.name]"
  266. clearable
  267. placeholder="请选择"
  268. @change="(val) => changed(val, itemChildList.name)"
  269. >
  270. <el-option
  271. v-for="it in itemChildList.selectOptions"
  272. :key="it.code"
  273. :label="it.name"
  274. :value="it.code"
  275. />
  276. </el-select>
  277. <el-slider
  278. v-if="itemChildList.type == 'el-slider'"
  279. v-model="formData[itemChildList.name]"
  280. @change="(val) => changed(val, itemChildList.name)"
  281. />
  282. </el-form-item>
  283. <customColorComponents
  284. v-if="itemChildList.type == 'customColor'"
  285. :key="'b-' + idx"
  286. v-model="formData[itemChildList.name]"
  287. @change="changed($event, itemChildList.name)"
  288. />
  289. <customNameComponents
  290. v-if="itemChildList.type == 'customName'"
  291. :key="'b-' + idx"
  292. v-model="formData[itemChildList.name]"
  293. @change="changed($event, itemChildList.name)"
  294. />
  295. <componentLinkage
  296. v-if="itemChildList.type == 'componentLinkage'"
  297. :key="'cl-' + idx"
  298. v-model="formData[itemChildList.name]"
  299. :layer-widget="layerWidget"
  300. :widget-params-config="widgetParamsConfig"
  301. :widget-index="widgetIndex"
  302. @change="changed($event, itemChildList.name)"
  303. />
  304. </template>
  305. </el-collapse-item>
  306. </el-collapse>
  307. </div>
  308. </template>
  309. </el-form>
  310. </div>
  311. </template>
  312. <script>
  313. import ColorPicker from "./colorPicker.vue";
  314. import vueJsonEditor from "vue-json-editor";
  315. import "codemirror/lib/codemirror.css"; // 核心样式
  316. import "codemirror/theme/cobalt.css"; // 引入主题后还需要在 options 中指定主题才会生效
  317. // language
  318. import "codemirror/mode/vue/vue.js";
  319. import "codemirror/mode/javascript/javascript.js";
  320. import "codemirror/mode/sql/sql.js";
  321. import "codemirror/mode/shell/shell.js";
  322. import dynamicComponents from "./dynamicComponents.vue";
  323. import customColorComponents from "./customColorComponents";
  324. import customNameComponents from "./customNameComponents";
  325. import dynamicAddTable from "./dynamicAddTable.vue";
  326. import customUpload from "./customUpload.vue";
  327. import dynamicAddRadar from "./dynamicAddRadar";
  328. import MonacoEditor from "@/components/MonacoEditor/index";
  329. import componentLinkage from './componentLinkage';
  330. import imageSelect from './imageSelect';
  331. export default {
  332. name: "DynamicForm",
  333. components: {
  334. imageSelect,
  335. ColorPicker,
  336. vueJsonEditor,
  337. dynamicComponents,
  338. customColorComponents,
  339. customNameComponents,
  340. dynamicAddTable,
  341. customUpload,
  342. dynamicAddRadar,
  343. MonacoEditor,
  344. componentLinkage
  345. },
  346. model: {
  347. prop: "value",
  348. event: "input",
  349. },
  350. props: {
  351. options: Array,
  352. value: {
  353. type: [Object],
  354. default: () => {},
  355. },
  356. layerWidget: {
  357. type: Array,
  358. default: () => []
  359. },
  360. widgetParamsConfig: {
  361. type: Array,
  362. default: () => []
  363. },
  364. widgetIndex: {
  365. type: Number,
  366. default: -1
  367. }
  368. },
  369. data() {
  370. return {
  371. formData: {},
  372. inputShow: {}, // 控制表单是否显示
  373. dialogVisibleStaticData: false,
  374. methodsVisible: false,
  375. validationRules: "",
  376. optionsJavascript: {
  377. mode: "text/javascript",
  378. tabSize: 2, // 缩进格式
  379. lineNumbers: true, // 显示行号
  380. line: true,
  381. styleActiveLine: true, // 高亮选中行
  382. hintOptions: {
  383. completeSingle: true, // 当匹配只有一项的时候是否自动补全
  384. },
  385. },
  386. };
  387. },
  388. watch: {
  389. value(newValue, oldValue) {
  390. this.formData = newValue || {};
  391. },
  392. options(val) {
  393. this.setDefaultValue();
  394. this.isShowData();
  395. },
  396. },
  397. created() {
  398. this.isShowData();
  399. this.setDefaultValue();
  400. },
  401. mounted() {},
  402. methods: {
  403. onJsonChange(val) {
  404. console.log(val);
  405. },
  406. onJsonSave(val) {
  407. console.log(val);
  408. },
  409. // 无论哪个输入框改变 都需要触发事件 将值回传
  410. changed(val, key) {
  411. console.log(val);
  412. console.log(key);
  413. if (val.extend) {
  414. this.$set(this.formData, key, val.value);
  415. } else {
  416. this.$set(this.formData, key, val);
  417. }
  418. this.$emit("onChanged", this.formData);
  419. // key为当前用户操作的表单组件
  420. for (let i = 0; i < this.options.length; i++) {
  421. let item = this.options[i];
  422. if (item.relactiveDom == key) {
  423. this.inputShow[item.name] = val == item.relactiveDomValue;
  424. this.inputShow = Object.assign({}, this.inputShow);
  425. }
  426. }
  427. },
  428. saveData() {
  429. this.$emit("onChanged", this.formData);
  430. this.dialogVisibleStaticData = false;
  431. this.methodsVisible = false;
  432. },
  433. // 静态数据
  434. addStaticData() {
  435. this.dialogVisibleStaticData = true;
  436. },
  437. handleClose() {
  438. this.dialogVisibleStaticData = false;
  439. this.methodsVisible = false;
  440. },
  441. // 组件属性 数据是否展示动态还是静态数据
  442. isShowData() {
  443. let currentData = {};
  444. const data = [];
  445. for (let i = 0; i < this.options.length; i++) {
  446. // 添加默认的inputShow值
  447. this.inputShow[this.options[i].name] = true;
  448. if (this.options[i].selectValue) {
  449. currentData = this.options[i];
  450. } else {
  451. data.push(this.options[i]);
  452. }
  453. }
  454. data.forEach((el) => {
  455. if (el.relactiveDomValue != currentData.value) {
  456. this.inputShow[el.name] = false;
  457. }
  458. });
  459. },
  460. // 组件拖入时 赋值
  461. setDefaultValue() {
  462. if (this.options && this.options.length > 0) {
  463. for (let i = 0; i < this.options.length; i++) {
  464. const obj = this.options[i];
  465. if (Object.prototype.toString.call(obj) == "[object Object]") {
  466. this.formData[this.options[i].name] = this.deepClone(
  467. this.options[i].value
  468. );
  469. } else if (Object.prototype.toString.call(obj) == "[object Array]") {
  470. for (let j = 0; j < obj.length; j++) {
  471. const list = obj[j].list;
  472. list.forEach((el) => {
  473. this.formData[el.name] = el.value;
  474. });
  475. }
  476. }
  477. }
  478. this.formData = Object.assign({}, this.formData);
  479. }
  480. },
  481. // 是否显示 那种格式
  482. isShowForm(val, type) {
  483. return Object.prototype.toString.call(val) == type;
  484. },
  485. },
  486. };
  487. </script>
  488. <style lang="scss">
  489. .el-form-item {
  490. margin-bottom: 5px;
  491. }
  492. .el-form-item__label {
  493. font-size: 12px;
  494. color: #fff;
  495. }
  496. .code-mirror {
  497. width: 100%;
  498. height: 100% !important;
  499. }
  500. .el-collapse {
  501. border-top: none;
  502. border-bottom: none;
  503. }
  504. .el-collapse-item__header {
  505. height: 40px;
  506. line-height: 40px;
  507. background: transparent;
  508. color: #bcc9d4;
  509. font-weight: 300;
  510. font-size: 12px;
  511. border-color: #282e3a;
  512. }
  513. .el-collapse-item__wrap {
  514. background: transparent;
  515. border: none;
  516. }
  517. .el-collapse-item__content {
  518. background: transparent;
  519. padding-bottom: 0;
  520. }
  521. </style>