fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<template>
  <a-collapse v-model:activeKey="styleCollapse">
    <a-collapse-panel v-if="props.styleConfig" key="1" :showArrow="false" header="+ 样式信息">
      <table class="mars-primary-table" border="1" bordercolor="#ffffff" cellspacing="0" cellpadding="0">
        <tr>
          <td>所在图层</td>
          <td>{{ layerName || "默认分组" }}</td>
        </tr>
        <tr>
          <td>标号类型</td>
          <td>{{ entityName }}</td>
        </tr>
        <template v-for="(item, i) in props.styleConfig.style" :key="i">
          <tr v-if="styleValue && styleShowIt(item)">
            <td>{{ item.label }}</td>
            <td>
              <span v-if="item.type === 'label'" class="attr-editor-label">{{ styleValue[item.name] }}</span>
              <component
                v-else
                :is="getComponent(item.type)"
                size="small"
                v-model:value="styleValue[item.name]"
                :min="item.min || item.min === 0 ? item.min : -Infinity"
                :max="item.max || item.max === 0 ? item.max : Infinity"
                :step="item.step || 0.1"
                :options="item.data || []"
                @change="unionChange(item)"
              >
              </component>
            </td>
          </tr>
        </template>
      </table>
    </a-collapse-panel>
    <a-collapse-panel v-if="styleValue && styleValue.label" key="2" :showArrow="false" header="+ 注记信息">
      <table class="mars-primary-table" border="1" bordercolor="#ffffff" cellspacing="0" cellpadding="0">
        <template v-for="(item, i) in labelConfig.style" :key="i">
          <tr v-if="labelShowIt(item)">
            <td>{{ item.label }}</td>
            <td>
              <component
                :is="getComponent(item.type)"
                size="small"
                v-model:value="styleValue.label[item.name]"
                :min="item.min || item.min === 0 ? item.min : -Infinity"
                :max="item.max || item.max === 0 ? item.max : Infinity"
                :step="item.step || 0.1"
                :options="item.data || []"
                @change="unionLabelChange"
              ></component>
            </td>
          </tr>
        </template>
      </table>
    </a-collapse-panel>
  </a-collapse>
</template>
 
<script lang="ts" setup>
import { inject, ref, watch } from "vue"
import _ from "lodash"
import { $message } from "@mars/components/mars-ui/index"
 
const props = defineProps<{
  styleConfig: any
  style: any
}>()
 
const emit = defineEmits(["styleChange"])
 
const getGraphicAttr = inject<() => any>("getGraphicAttr")!
const getGraphic = inject<() => any>("getGraphic")!
 
const layerName = ref("")
const entityName = ref("")
const styleValue = ref<any>(null)
const showImpacts = ref<any[]>([])
const showLabelImpacts = ref<any[]>([])
const labelConfig = getLabelConfig()
 
let graphicType: string
 
watch(
  props,
  () => {
    if (props && props.style) {
      styleValue.value = _.cloneDeep(props.style)
      setDefault()
      const gp = getGraphic()
      if (gp) {
        layerName.value = gp._layer.name
        entityName.value = gp.name || props.styleConfig.name
        graphicType = gp.type
      }
      setImpacts()
      if (props.style.label) {
        setLabelDefault()
        setLabelImpacts()
      }
    }
 
    getLabelConfig()
  },
  {
    immediate: true
  }
)
 
const styleCollapse = ref(["1", "2"])
 
const components = {
  number: "mars-input-number",
  radio: "mars-switch",
  slider: "mars-slider",
  color: "mars-color-picker",
  combobox: "mars-select",
  textarea: "mars-textarea",
  label: "span"
}
function getComponent(type: keyof typeof components) {
  return components[type]
}
 
function getLabelConfig() {
  const graphicAttr = getGraphicAttr()
 
  return graphicAttr.label
}
 
function setDefault() {
  if (props.styleConfig.style) {
    props.styleConfig.style.forEach((item: any) => {
      if (!styleValue.value[item.name] && styleValue.value[item.name] !== 0 && styleValue.value[item.name] !== false) {
        styleValue.value[item.name] = item.defval
      }
    })
  }
}
 
function setImpacts() {
  let allImpacts: any[] = []
  if (props.styleConfig.style) {
    props.styleConfig.style.forEach((item: any) => {
      if (item.impact && styleValue.value[item.name] === true) {
        allImpacts = allImpacts.concat(item.impact)
      }
      if (item.data && _.isArray(item.data)) {
        item.data.forEach((op: any) => {
          if (op.impact && styleValue.value[item.name] === op.value) {
            allImpacts = allImpacts.concat(op.impact)
          }
        })
      }
    })
  }
  showImpacts.value = _.uniq(allImpacts)
}
 
function setLabelDefault() {
  labelConfig.style.forEach((item: any) => {
    if (!styleValue.value.label[item.name] && styleValue.value.label[item.name] !== 0) {
      styleValue.value.label[item.name] = item.defval
    }
  })
}
function setLabelImpacts() {
  let allImpacts: any[] = []
 
  labelConfig.style.forEach((item: any) => {
    if (item.impact && styleValue.value.label[item.name] === true) {
      allImpacts = allImpacts.concat(item.impact)
    }
    if (item.data && _.isArray(item.data)) {
      item.data.forEach((op: any) => {
        if (op.impact && styleValue.value.label[item.name] === op.value) {
          allImpacts = allImpacts.concat(op.impact)
        }
      })
    }
  })
  showLabelImpacts.value = _.uniq(allImpacts)
}
 
function updateStyle(item: any) {
  const data: Record<string, any> = {
    [item.name]: styleValue.value[item.name]
  }
  if (item.impact) {
    item.impact.forEach((im: string) => {
      data[im] = styleValue.value[im]
    })
  }
  if (item.data && Array.isArray(item.data)) {
    item.data.forEach((it: any) => {
      if (it.impact) {
        it.impact.forEach((im: string) => {
          data[im] = styleValue.value[im]
        })
      }
    })
  }
 
  emit("styleChange", data)
}
 
function unionChange(item: any) {
  if (item.name === "fill" || item.name === "outline") {
    if (styleValue.value.fill === false && styleValue.value.outline === false) {
      $message("填充和边框不能同时为否")
      styleValue.value[item.name] = true
      return
    }
  }
  setImpacts()
  updateStyle(item)
}
 
function unionLabelChange(item: any) {
  setLabelDefault()
  setLabelImpacts()
  updateStyle({ name: "label" })
}
 
// style的显示处理
function styleShowIt(item: any) {
  if (item.type === "hidden") {
    return false
  }
  if (item.isImpact && !showImpacts.value.includes(item.name)) {
    return false
  }
 
  const attrName = item.name
  // 以下对样式互斥的处理。
  // 贴地对象
  if (styleValue.value.clampToGround && graphicType !== "model" && graphicType !== "label") {
    if (
      attrName === "fill" || // 不能取消填充。
      attrName === "height" || // 没有高度
      attrName === "outline" ||
      attrName === "outlineWidth" ||
      attrName === "outlineColor" ||
      attrName === "outlineOpacity" ||
      attrName === "hasShadows" ||
      attrName === "diffHeight"
    ) {
      return false
    }
  } else {
    if (attrName === "zIndex") {
      return false
    }
  }
 
  // 三维立体对象
  if (styleValue.value.diffHeight > 0) {
    if (attrName === "clampToGround" || attrName === "outlineWidth") {
      return false
    }
  }
 
  return true
}
 
// 注记属性的显示处理
function labelShowIt(item: any) {
  return item.type !== "hidden" && (!item.isImpact || (item.isImpact && showLabelImpacts.value.includes(item.name)))
}
</script>
 
<style lang="less" scoped>
.attr-editor-label {
  word-break: break-all;
}
</style>