drag-base.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="query-area"
  3. @dragover.prevent=""
  4. @dragenter.prevent="handleDragOver($event)"
  5. @drop="handleDrop($event)"
  6. @dragleave="handleDragleave($event)"
  7. >
  8. <template v-for="(it, index) in tagList">
  9. <div :class="(this.index == index?'query-area-item query-area-item-active':'query-area-item')"
  10. draggable="true"
  11. @dragstart="childDragstart($event, index)"
  12. @dragenter.prevent="childDragEnter($event, index)"
  13. @dragleave="childDragleave($event, index)">
  14. <div class="query-area-item-title" @click="handleDisplay(index)">{{it.displayName?it.displayName : it.fieldName}}</div>
  15. <div v-if="(queryType=='measure')" class="query-area-item-select">
  16. <a-select v-model:value="tagList[index].operation" :options="aggregates"></a-select>
  17. </div>
  18. <div class="query-area-item-close" @click="childDelete(index)"><CloseOutlined style="color: white;"/></div>
  19. </div>
  20. </template>
  21. </div>
  22. <a-modal v-model:visible="modelName.open" title="修改显示名称" @ok="handleDisplayOk">
  23. <div style="line-height: 30px;">属性名称:{{modelName.fieldName}}</div>
  24. <div>
  25. <a-input v-model:value="modelName.displayName"
  26. :placeholder="(modelName.fieldName+'...')" />
  27. </div>
  28. </a-modal>
  29. </template>
  30. <script lang="ts">
  31. import {defineComponent, computed} from "vue";
  32. //import {message} from "ant-design-vue";
  33. export default defineComponent ({
  34. props:{
  35. queryType : String,
  36. keyName : String,
  37. subjectId : String
  38. },
  39. emits: ['change'],
  40. name : 'DragBase',
  41. data() {
  42. return{
  43. tagList : [] as any,
  44. counter : 0,
  45. index : -1,
  46. aggregates : [],
  47. modelName : {
  48. open : false,
  49. fieldName: '',
  50. displayName : '',
  51. index : -1
  52. }
  53. }
  54. },
  55. setup(props, context){
  56. console.log("base setup" , props);
  57. const dataChange = (list, obj) => {
  58. context.emit('change', list, obj)
  59. }
  60. const subjectId = computed(() => {
  61. return props.subjectId? props.subjectId : null
  62. });
  63. return{
  64. queryType: props.queryType ? props.queryType : 'dimen',
  65. keyName : props.keyName == null || props.keyName == undefined ? 'tempFeildId' : props.keyName,
  66. dataChange,
  67. subjectId
  68. }
  69. },
  70. mounted(){
  71. console.log("mounted" , "");
  72. },
  73. methods:{
  74. getObject:function (data){
  75. return {
  76. tempFeildId: data.tempFeildId,
  77. tempId: data.tempId ? data.tempId : null,
  78. fieldId: data.fieldId ? data.fieldId : null,
  79. fieldCode: data.fieldCode ? data.fieldCode : null,
  80. dataType : data.dataType ? data.dataType : 'int',
  81. fieldAlias: data.fieldAlias,
  82. fieldName: data.fieldName,
  83. isDrag: 1,
  84. displayName: data.displayName ? data.displayName : null,
  85. operation: data.operation ? data.operation : 'sum',
  86. subId : data.subId? data.subId : null,
  87. subjectName : data.subjectName? data.subjectName : null
  88. };
  89. },
  90. handleDragOver : function (ev) {
  91. ev.preventDefault();
  92. this.counter++;
  93. const data = window["dragData"];
  94. console.log("keyName", this.keyName, data[this.keyName])
  95. const tag = this.findTag(data[this.keyName]);
  96. if(tag != null) return;
  97. this.index = this.tagList.length;
  98. const obj = this.getObject(data);
  99. //检查主题是否合法
  100. console.log("handleDragOver", data, obj, this.tagList);
  101. if(obj.subId==null || obj.subjectName==null) return; //主题不合法
  102. if(this.subjectId && this.subjectId != obj.subId) {
  103. //message.error("无法放置,同一模板只能放置统一主题的维度、度量!");
  104. return;//有主题,但是和当前模板中的主题不同
  105. }
  106. this.tagList.push(obj);
  107. },
  108. handleDrop : function (ev) {
  109. ev.preventDefault();
  110. const data = window["dragData"];
  111. const tag = this.findTag(data[this.keyName]);
  112. console.log("handleDrop", data, tag);
  113. if(tag==null) return;
  114. this.tagList[tag.index]['isDrag'] = 0;
  115. this.index = -1;
  116. this.counter = 0;
  117. this.dataChange(this.getTagList(), this.tagList[tag.index]);
  118. },
  119. handleDragleave : function(ev) {
  120. console.log(ev);
  121. this.counter--;
  122. const data = window["dragData"];
  123. const tag = this.findTag(data[this.keyName]);
  124. console.log("handleDragleave", data, tag, this.counter);
  125. if(tag==null) return;
  126. if(this.counter==0) {
  127. this.tagList.splice(tag.index,1);
  128. this.index = -1;
  129. this.dataChange(this.getTagList(), null);
  130. }
  131. },
  132. childDragEnter : function (ev, index) {
  133. ev.preventDefault();
  134. this.counter++;
  135. //重新排序
  136. if(index == this.index || this.index==-1) return;
  137. const data = window["dragData"];
  138. //检查主题是否合法
  139. if(data.subId==null || data.subjectName==null) return; //主题不合法
  140. if(this.subjectId && this.subjectId != data.subId) return;//有主题,但是和当前模板中的主题不同
  141. this.tagList.splice(this.index,1)
  142. this.index = index;
  143. this.tagList.splice(index, 0, this.getObject(data));
  144. console.log("childDragEnter","位置切换");
  145. this.dataChange(this.getTagList(), this.getObject(data));
  146. },
  147. childDragleave : function (ev) {
  148. ev.preventDefault();
  149. this.counter--;
  150. },
  151. childDragstart : function (ev, index) {
  152. console.log(ev);
  153. window["dragData"] = JSON.parse(JSON.stringify(this.tagList[index]));
  154. this.index = -1;
  155. setTimeout(()=>{
  156. this.tagList.splice(index,1);
  157. console.log("childDragstart", window["dragData"], index, this.tagList);
  158. this.dataChange(this.getTagList(), null);
  159. },20);
  160. },
  161. childDelete : function (index){
  162. this.index = -1;
  163. this.tagList.splice(index,1);
  164. this.dataChange(this.getTagList(), null);
  165. },
  166. findTag : function (key) {
  167. for (let i = 0; i < this.tagList.length; i++) {
  168. if (this.tagList[i][this.keyName] == key) {
  169. console.log("findTag",this.tagList, key, this.tagList[i][this.keyName]);
  170. return {obj: this.tagList[i], index: i}
  171. }
  172. }
  173. console.log("findTag",this.tagList, key, "null");
  174. return null;
  175. },
  176. handleDisplay(index){
  177. this.modelName.index = index;
  178. this.modelName.open = true;
  179. this.modelName.fieldName = this.tagList[index]["fieldName"];
  180. this.modelName.displayName = this.tagList[index]["displayName"]?
  181. this.tagList[index]["displayName"]:this.tagList[index]["fieldName"];
  182. },
  183. handleDisplayOk(){
  184. if(this.modelName.displayName!=null && this.modelName.displayName!=undefined && this.modelName.displayName.length>0) {
  185. this.modelName.open = false;
  186. this.tagList[this.modelName.index]["displayName"] = this.modelName.displayName;
  187. this.modelName.index = -1;
  188. }
  189. },
  190. getTagList(){
  191. this.tagList.forEach((row, index)=>{
  192. row.disOrder = index;
  193. });
  194. return this.tagList;
  195. },
  196. setTagList(list){
  197. this.tagList = list;
  198. }
  199. }
  200. })
  201. </script>
  202. <style lang="less" scoped>
  203. @import 'drag-base.less';
  204. </style>