diy-operation-list.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template id="diy-operation-list">
  2. <div>
  3. <div v-for="(item,index) in value" :key="index" class="diy-operation-item">
  4. <!-- 动态插槽 -->
  5. <div :style="gutterStyle">
  6. <div v-if="item.slot">
  7. <slot :name="item.slot" :item="item" :index="index"></slot>
  8. </div>
  9. <!-- 默认类型start -->
  10. <!-- input -->
  11. <div v-else-if="item.type === 'textarea'" class="item-row ">
  12. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  13. <el-input class="flex-1" type="textarea" v-model="item.value" @input="colorInput($event,index)" :placeholder="item.placeholder" :maxlength="item.max" :show-word-limit="item.showLimit"></el-input>
  14. </div>
  15. <!-- input -->
  16. <div v-else-if="item.type === 'input'" class="item-row diy-operation-input">
  17. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  18. <el-input class="diy-input" v-model="item.value" @input="colorInput($event,index)" :placeholder="item.placeholder" :maxlength="item.max" :show-word-limit="item.showLimit"></el-input>
  19. </div>
  20. <!-- color -->
  21. <div v-else-if="item.type === 'color'" class="item-row diy-operation-color">
  22. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  23. <el-color-picker color-format="colorFormat" :show-alpha="item.showAlpha" v-model="item.value" @change="updateColor($event,index)" :predefine="item.predefineColors || []"></el-color-picker>
  24. <el-input class="diy-color-input" v-model="item.value" @input="colorInput($event,index)" maxlength="10"></el-input>
  25. </div>
  26. <!-- 尺寸大小 -->
  27. <div v-else-if="item.type === 'size'" class="item-row diy-operation-size">
  28. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  29. <div style="padding-left: 2px;">
  30. <el-slider :min="getRange(item,'minValue')" :max="getRange(item,'maxValue')" :disabled="item.disabled" v-model="item.value" @change="updateSize"></el-slider>
  31. </div>
  32. <div class="diy-text-btn">{{ getSizeText(item) }}</div>
  33. </div>
  34. <!-- 单选 -->
  35. <div v-else-if="item.type === 'radio'" class="item-row diy-operation-radio">
  36. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  37. <el-radio-group class="flex-1" v-model="item.value" @change="onChange">
  38. <el-radio :label="childItem.label" v-for="(childItem, j) in item.list" :key="j"> {{ childItem.value }}</el-radio>
  39. </el-radio-group>
  40. </div>
  41. <!-- 多选 -->
  42. <div v-else-if="item.type === 'checkbox'" class="item-row diy-operation-checkbox">
  43. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  44. <el-checkbox-group class="flex-1" v-model="item.value" @change="onChange">
  45. <el-checkbox :label="childItem.label" v-for="(childItem, j) in item.list" :key="j"> {{ childItem.value }}</el-checkbox>
  46. </el-checkbox-group>
  47. </div>
  48. <!-- 上传图片 - 只能单个 - 待优化 -->
  49. <div v-else-if="item.type === 'image'" class="diy-operation-image">
  50. <div class="diy-operation-name" :style="labelStyle(item)" v-if="item.isShowLabel !== false">{{ item.label }}</div>
  51. <div>
  52. <com-attachment :multiple="false" :max="1" @selected="selecImg($event, item)">
  53. <div class="card-upload img-upload-bor" :style="videoComputed" v-if="!item.value">
  54. <i class="el-icon-plus icon-plugs-center"></i>
  55. </div>
  56. <div class="diy-sty-img" v-else @mouseover="mouseOverImg(item)" @mouseleave="mouseLeaveImg(item)" :style="videoComputed">
  57. <el-image :style="videoComputed" :src="item.value"></el-image>
  58. <div class="diy-upd-img" v-show="item.showIcon">替换</div>
  59. <i class="el-icon-close diy-upd-icon" @click.stop="delImg(item)" v-show="item.showIcon"></i>
  60. </div>
  61. <slot name="tips"></slot>
  62. </com-attachment>
  63. </div>
  64. </div>
  65. <!-- 默认类型end -->
  66. </div>
  67. </div>
  68. </div>
  69. </template>
  70. <script>
  71. // test data
  72. /**
  73. * [{
  74. type: "color",
  75. label: "哈哈哈",
  76. value: ""
  77. }, {
  78. type: "size",
  79. label: "哈哈哈",
  80. value: 0
  81. }, {
  82. type: "radio",
  83. label: "哈哈哈",
  84. value: 0,
  85. list: [{
  86. label: 0,
  87. value: "男"
  88. }, {
  89. label: 1,
  90. value: "女"
  91. }]
  92. }, {
  93. type: "checkbox",
  94. label: "哈哈哈",
  95. value: [],
  96. list: [{
  97. label: 0,
  98. value: "男"
  99. }, {
  100. label: 1,
  101. value: "女"
  102. }]
  103. }]
  104. */
  105. Vue.component('diy-operation-list', {
  106. template: '#diy-operation-list',
  107. name: 'diy-operation-list',
  108. props: {
  109. value: {
  110. type: Array,
  111. default () {
  112. return []
  113. }
  114. },
  115. // 提示文字width
  116. labelWidth: {
  117. type: String | Number,
  118. default () {
  119. return 70
  120. }
  121. },
  122. // 提示文字
  123. labelAlign: {
  124. type: String,
  125. default: 'end'
  126. },
  127. // 总的size最小值
  128. minValue: {
  129. type: String | Number,
  130. default () {
  131. return 0
  132. }
  133. },
  134. // 总的size最大值
  135. maxValue: {
  136. type: String | Number,
  137. default () {
  138. return 100
  139. }
  140. },
  141. gutter: {
  142. type: String | Number,
  143. default () {
  144. return 0
  145. }
  146. },
  147. },
  148. data() {
  149. return {}
  150. },
  151. computed: {
  152. videoComputed() {
  153. return {
  154. width: `60px`,
  155. height: `60px`
  156. }
  157. },
  158. gutterStyle() {
  159. return {
  160. margin: `${this.gutter}px ${0}`
  161. }
  162. }
  163. },
  164. methods: {
  165. onChange() {
  166. this.$nextTick(() => {
  167. var list = deepClone(this.value)
  168. this.$emit("on-change", list);
  169. })
  170. },
  171. // 颜色
  172. updateColor() {
  173. this.onChange()
  174. },
  175. colorInput() {
  176. console.log('颜色',this.value)
  177. this.onChange()
  178. },
  179. // size
  180. getSizeText(item) {
  181. var unit = item.unit ? item.unit : "px";
  182. return `${item.value}${unit}`
  183. },
  184. getRange(item, type) {
  185. if (item[type]) {
  186. return item[type]
  187. } else {
  188. this[type]
  189. }
  190. },
  191. updateSize() {
  192. this.onChange()
  193. },
  194. // 图片
  195. selecImg(event, item) {
  196. item.value = event[0].url
  197. this.onChange()
  198. },
  199. mouseOverImg(item) {
  200. this.$set(item, "showIcon", true)
  201. },
  202. mouseLeaveImg(item) {
  203. this.$set(item, "showIcon", false)
  204. },
  205. delImg(item) {
  206. item.value = "";
  207. this.onChange()
  208. },
  209. // label样式
  210. labelStyle(item) {
  211. var labelWidth = item.labelWidth ? item.labelWidth : this.labelWidth
  212. var labelAlign = item.labelAlign ? item.labelAlign : this.labelAlign
  213. return {
  214. width: `${labelWidth}px`,
  215. 'text-align': labelAlign
  216. }
  217. },
  218. }
  219. });
  220. </script>
  221. <style scoped>
  222. .item-row {
  223. min-height: 40px;
  224. display: flex;
  225. align-items: center;
  226. }
  227. .diy-operation-image {
  228. display: flex;
  229. align-items: center;
  230. }
  231. .diy-operation-input .diy-input {
  232. flex: 1;
  233. height: 30px;
  234. }
  235. .diy-operation-input .el-input__inner {
  236. height: 100%;
  237. }
  238. .flex-1 {
  239. flex: 1;
  240. }
  241. /* .diy-operation-color,
  242. .diy-operation-size,
  243. .diy-operation-radio,
  244. .diy-operation-checkbox {} */
  245. .diy-operation-name {
  246. margin-right: 15px;
  247. text-align: end;
  248. color: #999999;
  249. }
  250. .diy-color-input {
  251. width: 74px;
  252. background: #F4F3F7;
  253. border-radius: 3px;
  254. font-size: 14px;
  255. color: #666666;
  256. text-align: center;
  257. margin-left: 15px;
  258. height: 28px;
  259. }
  260. .diy-color-input .el-input__inner {
  261. height: 100%;
  262. padding: 0 6px;
  263. }
  264. .diy-operation-color .el-color-picker {
  265. height: 30px;
  266. }
  267. .diy-operation-size .el-slider {
  268. width: 153px;
  269. margin-right: 15px;
  270. }
  271. </style>