diy-operation-list.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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)"></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. </com-attachment>
  62. </div>
  63. </div>
  64. <!-- 默认类型end -->
  65. </div>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. // test data
  71. /**
  72. * [{
  73. type: "color",
  74. label: "哈哈哈",
  75. value: ""
  76. }, {
  77. type: "size",
  78. label: "哈哈哈",
  79. value: 0
  80. }, {
  81. type: "radio",
  82. label: "哈哈哈",
  83. value: 0,
  84. list: [{
  85. label: 0,
  86. value: "男"
  87. }, {
  88. label: 1,
  89. value: "女"
  90. }]
  91. }, {
  92. type: "checkbox",
  93. label: "哈哈哈",
  94. value: [],
  95. list: [{
  96. label: 0,
  97. value: "男"
  98. }, {
  99. label: 1,
  100. value: "女"
  101. }]
  102. }]
  103. */
  104. Vue.component('diy-operation-list', {
  105. template: '#diy-operation-list',
  106. name: 'diy-operation-list',
  107. props: {
  108. value: {
  109. type: Array,
  110. default () {
  111. return []
  112. }
  113. },
  114. // 提示文字width
  115. labelWidth: {
  116. type: String | Number,
  117. default () {
  118. return 70
  119. }
  120. },
  121. // 提示文字
  122. labelAlign: {
  123. type: String,
  124. default: 'end'
  125. },
  126. // 总的size最小值
  127. minValue: {
  128. type: String | Number,
  129. default () {
  130. return 0
  131. }
  132. },
  133. // 总的size最大值
  134. maxValue: {
  135. type: String | Number,
  136. default () {
  137. return 100
  138. }
  139. },
  140. gutter: {
  141. type: String | Number,
  142. default () {
  143. return 0
  144. }
  145. },
  146. },
  147. data() {
  148. return {}
  149. },
  150. computed: {
  151. videoComputed() {
  152. return {
  153. width: `60px`,
  154. height: `60px`
  155. }
  156. },
  157. gutterStyle() {
  158. return {
  159. margin: `${this.gutter}px ${0}`
  160. }
  161. }
  162. },
  163. methods: {
  164. onChange() {
  165. this.$nextTick(() => {
  166. var list = deepClone(this.value)
  167. this.$emit("on-change", list);
  168. })
  169. },
  170. // 颜色
  171. updateColor() {
  172. this.onChange()
  173. },
  174. colorInput() {
  175. this.onChange()
  176. },
  177. // size
  178. getSizeText(item) {
  179. var unit = item.unit ? item.unit : "px";
  180. return `${item.value}${unit}`
  181. },
  182. getRange(item, type) {
  183. if (item[type]) {
  184. return item[type]
  185. } else {
  186. this[type]
  187. }
  188. },
  189. updateSize() {
  190. this.onChange()
  191. },
  192. // 图片
  193. selecImg(event, item) {
  194. item.value = event[0].url
  195. this.onChange()
  196. },
  197. mouseOverImg(item) {
  198. this.$set(item, "showIcon", true)
  199. },
  200. mouseLeaveImg(item) {
  201. this.$set(item, "showIcon", false)
  202. },
  203. delImg(item) {
  204. item.value = "";
  205. this.onChange()
  206. },
  207. // label样式
  208. labelStyle(item) {
  209. var labelWidth = item.labelWidth ? item.labelWidth : this.labelWidth
  210. var labelAlign = item.labelAlign ? item.labelAlign : this.labelAlign
  211. return {
  212. width: `${labelWidth}px`,
  213. 'text-align': labelAlign
  214. }
  215. },
  216. }
  217. });
  218. </script>
  219. <style scoped>
  220. .item-row {
  221. min-height: 40px;
  222. display: flex;
  223. align-items: center;
  224. }
  225. .diy-operation-image {
  226. display: flex;
  227. align-items: center;
  228. }
  229. .diy-operation-input .diy-input {
  230. flex: 1;
  231. height: 30px;
  232. }
  233. .diy-operation-input .el-input__inner {
  234. height: 100%;
  235. }
  236. .flex-1 {
  237. flex: 1;
  238. }
  239. /* .diy-operation-color,
  240. .diy-operation-size,
  241. .diy-operation-radio,
  242. .diy-operation-checkbox {} */
  243. .diy-operation-name {
  244. margin-right: 15px;
  245. text-align: end;
  246. color: #999999;
  247. }
  248. .diy-color-input {
  249. width: 74px;
  250. background: #F4F3F7;
  251. border-radius: 3px;
  252. font-size: 14px;
  253. color: #666666;
  254. text-align: center;
  255. margin-left: 15px;
  256. height: 28px;
  257. }
  258. .diy-color-input .el-input__inner {
  259. height: 100%;
  260. padding: 0 6px;
  261. }
  262. .diy-operation-color .el-color-picker {
  263. height: 30px;
  264. }
  265. .diy-operation-size .el-slider {
  266. width: 153px;
  267. margin-right: 15px;
  268. }
  269. </style>