schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<template>
  <draggable v-model="currentButtons" :options="{group:'button-group', ghostClass: 'ghost', animation:300, chosenClass:'chosen-item', sort:true}" tag="div" class="draggable-main button-container" @add="handleButtonAdd">
    <template v-for="(element, index) in currentButtons">
      <template v-if="element.type == 'button-group'">
        <div :key="element.key" :class="{active: selectWidget && selectWidget.key === element.key}" class="button-group" style="position: relative;" @click.stop="handleSelectButton(index)">
          <draggable v-model="element.buttons" :options="{group:'button-group', ghostClass: 'ghost', animation:300}" tag="div" class="draggable-main button-box" @add="($evt)=>{handleButtonGroupAdd($evt, element)}">
            <manager-form-button v-for="(subBtn, subIndex) in element.buttons" :key="subBtn.key" :element="subBtn" :select.sync="selectWidget" :index="subIndex" :buttons="element.buttons" :config-type.sync="currentConfigType"></manager-form-button>
          </draggable>
          <div class="tool">
            <el-button v-if="selectWidget && selectWidget.key == element.key" title="删除" style="bottom: -20px;" class="widget-action-delete" circle plain type="danger" @click.stop="handleButtonDelete(index)">
              <i class="el-icon-yrt-shanchu2"></i>
            </el-button>
          </div>
        </div>
      </template>
      <template v-else>
        <manager-form-button :key="element.key" :element="element" :select.sync="selectWidget" :index="index" :buttons="currentButtons" :config-type.sync="currentConfigType"></manager-form-button>
      </template>
    </template>
  </draggable>
</template>
 
<script>
import Draggable from "vuedraggable";
import ManagerFormButton from "./ManagerFormButton";
 
export default {
  components: {
    Draggable,
    ManagerFormButton
  },
  props: {
    select: {
      type: Object,
      default: () => {}
    },
    buttons: {
      type: Array,
      default: () => []
    },
    configType: {
      type: String,
      default: "WidgetConfig"
    }
  },
  data() {
    return {
      // selectWidget: this.select
    };
  },
  computed: {
    currentButtons: {
      get: function() {
        return this.buttons;
      },
      set: function(val) {
        this.$emit("update:buttons", val);
      }
    },
    currentConfigType: {
      get: function() {
        return this.configType;
      },
      set: function(val) {
        this.$emit("update:configType", val);
      }
    },
    selectWidget: {
      get: function() {
        return this.select;
      },
      set: function(val) {
        this.$emit("update:select", val);
      }
    }
  },
  watch: {
    select(val) {
      this.selectWidget = val;
    }
  },
  methods: {
    // 按钮添加
    handleButtonAdd(evt) {
      const newIndex = evt.newIndex;
      // 为拖拽到容器的元素添加唯一 key
      const key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
      var button = this.currentButtons[newIndex];
      this.$set(
        this.currentButtons,
        newIndex,
        Object.assign({}, JSON.parse(JSON.stringify(button)), {
          key
        })
      );
 
      this.currentConfigType = "WidgetConfig";
      this.selectWidget = this.currentButtons[newIndex];
    },
    // 按钮组添加
    handleButtonGroupAdd($event, row) {
      const newIndex = $event.newIndex;
      const oldIndex = $event.oldIndex;
      const item = $event.item;
 
      // 防止布局元素的嵌套拖拽
      if (item.className.indexOf("button-group") >= 0) {
        // 如果是列表中拖拽的元素需要还原到原来位置
        item.tagName === "DIV" && this.currentButtons.splice(oldIndex, 0, row.buttons[newIndex]);
 
        row.buttons.splice(newIndex, 1);
 
        return false;
      }
      const key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
 
      var button = row.buttons[newIndex];
      this.$set(
        row.buttons,
        newIndex,
        Object.assign({}, JSON.parse(JSON.stringify(button)), {
          key
        })
      );
 
      this.currentConfigType = "WidgetConfig";
      this.selectWidget = row.buttons[newIndex];
    },
    // 按钮选择
    handleSelectButton(index) {
      this.selectWidget = this.currentButtons[index];
    },
    // 按钮删除
    handleButtonDelete(index) {
      var buttons = this.currentButtons;
      if (buttons.length - 1 === index) {
        if (index === 0) {
          this.selectWidget = {};
        } else {
          this.selectWidget = buttons[index - 1];
        }
      } else {
        this.selectWidget = buttons[index + 1];
      }
 
      this.$nextTick(() => {
        buttons.splice(index, 1);
      });
    }
  }
};
</script>