liuxiaolong
2019-05-06 2418a2d61feea858e9e63aa52fd64ddfe17e392a
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<template>
  <div ref="treeContainer" id="tree-container-org" class="clearfix pr" style="padding: 0px 8px;">
    <!-- <div v-if="isSearch">
      <div class="py-2 flex-row-between">
        <b-input
          autocomplete="off"
          v-model="searchValue"
          placeholder="输入机构名称"
          @change.native="handleSearch"
          class="mr10"
        />
        <b-button variant="primary" class="pl-4 pr-4" @click="handleSearch">搜索</b-button>
        <h4 class="py-2 mb-0 font-weight-bold">设备列表</h4>
        <b-button variant="link" class="tree-btn" @click="$emit('toggleOpen')">
          <i class="ion ion-md-swap f20"></i>
        </b-button>
      </div>
      <hr class="m-0 mb-2 bg-grid" />
    </div>-->
    <!-- style="background: rgb(243, 243, 243) bg-white" -->
    <div class="flex-row-between">
      <!-- <div class="d-flex pt-2">
        <h5
          class="py-2 mb-0 font-weight-bold cursor-pointer"
          title="点击清除平台设备选择"
          @click="clearTree"
        >平台设备</h5>
        <div class="py-2 add-tool" v-if="!isLock && editId==='orgAdd'">
          <span class="text-primary pl-2" @click="addNdoe">
            <i class="ion ion-ios-add-circle-outline f18" title="添加组织"></i>
          </span>
          <span class="text-primary pl-1" @click="edit"><i class="far fa-edit f18"></i></span>
          <span class="text-primary pl-1" @click="addDevice({ id: '0' })">
            <i class="fas fa-server f18" title="添加设备"></i>
          </span>
        </div>
      </div>-->
      <!-- <b-button variant="link" class="tree-btn" @click="$emit('toggleOpen')">
          <i class="ion ion-md-swap f20"></i>
      </b-button>-->
      <div class="pt-2" v-show="false">
        <fTemplate authority="device:orgTree:edit">
          <circle-lock :isLock="isLock" @change="isLock=!isLock" class="ml-2 mr-2"/>
        </fTemplate>
        <!-- <b-button variant="link" class="tree-btn" @click="$emit('toggleOpen')">
          <i class="ion ion-md-swap f20"></i>
        </b-button>-->
      </div>
    </div>
    <div class="overflow-auto pb-2 px-2 pr animation" :style="isOpen?'height:38vh':'height:76vh'">
      <Tree
        :data="data"
        :props="option"
        :default-expand-all="isOpenAll"
        @node-click="handleNodeClick"
        highlight-current
        id="tree"
        ref="tree2"
        node-key="id"
        class="float-left"
        style="width: 100%"
        :expand-on-click-node="false"
        :render-content="renderContent"
      />
 
      <!-- <circle-lock @change="setLockState" class="ml-2 mr-2 org-tree-lock"/> -->
    </div>
    <!-- tree-Model start -->
    <div class="tree-Model-bg" v-show="showAdd" @click="resetForm"></div>
    <div class="tree-Model" v-show="showAdd" :style="{'left':clientX+'px','top':clientY+'px'}">
      <el-card :body-style="{padding: '10px' }">
        <div class="pt-2"></div>
        <el-form
          :model="addTreeForm"
          size="mini"
          :rules="rules"
          ref="addTreeForm"
          label-width="70px"
        >
          <el-form-item label="名称:" prop="name">
            <el-input v-model="addTreeForm.name"></el-input>
          </el-form-item>
          <div class="text-center pb-2">
            <b-button variant="primary" class="pl20 pr20 mx-2" @click="submitForm">保存</b-button>
            <b-button class="pl20 pr20 mx-2" @click="resetForm">取消</b-button>
          </div>
        </el-form>
      </el-card>
    </div>
    <!-- tree-Model end -->
    <!-- <add-device-form
        :item="null"
        ref="deviceForm"
        title="新增设备"
        @submit="deviceSubmit"
        :isShowFooter="false"
    />-->
  </div>
</template>
 
<script>
import { getContainerRect } from '@/components/common/util.js'
import { Tree, Card, Form, FormItem, Input } from 'element-ui'
// import addDeviceForm from './addDeviceForm'
import circleLock from '@/components/specialButton/circleLock'
import { delDevice } from '@/server/home.js'
import { delDicOrg, saveDicOrg } from '@/server/common.js'
export default {
  data: () => ({
    searchValue: '',
    isLock: true,
    editId: '',
    addTreeForm: {
      name: ''
    },
    rules: {
      name: [
        { required: true, message: '请输入节点名称', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在 2 到 10 个字', trigger: 'blur' }
      ]
    },
    showAdd: false,
    clientX: 0,
    clientY: 0
  }),
  props: {
    data: {
      type: Array,
      default: () => [],
      required: true
    },
    option: {
      type: Object,
      default: () => ({
        children: 'child',
        label: 'name'
      })
    },
    isOpenAll: {
      default: true,
      type: Boolean
    },
    isSearch: {
      default: true,
      type: Boolean
    },
    isOpen: {
      default: false,
      type: Boolean
    }
  },
  methods: {
    /* 接口声明 start */
    async saveDicOrg(json) {
      const res = await saveDicOrg(json)
      if (res.success && res.code === 200 && res.data) {
        /* 成功操作 */
        this.$emit('save-org-success')
        /* 初始化 */
        this.resetForm()
        this.$toast({
          type: 'success',
          message: res && res.msg ? res.msg : '保存组织成功'
        })
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '保存组织失败!'
        })
      }
      console.log(res, 'saveDicOrg---res')
    },
    async delDevice(id) {
      const res = await delDevice({ id })
      if (res.success && res.code === 200) {
        /* 成功操作 */
        this.$emit('del-device-success')
        this.$toast({
          type: 'success',
          message: res && res.msg ? res.msg : '删除设备成功'
        })
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '删除设备失败!'
        })
      }
      console.log(res, 'delDevice---res')
    },
    async delDicOrg(id) {
      const res = await delDicOrg({ id })
      if (res.success && res.code === 200) {
        /* 成功操作 */
        this.$emit('del-org-success')
        this.$toast({
          type: 'success',
          message: res && res.msg ? res.msg : '删除组织成功'
        })
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '删除组织失败!'
        })
      }
      console.log(res, 'delDicOrg---res')
    },
    /* 接口声明 end */
    handleNodeClick(node) {
      this.editId = node.id ? node.id : ''
      this.$emit('currentNode', node)
    },
    // * 设置选中状态
    setCurrentKey(id) {
      this.$nextTick(() => {
        this.$refs.tree2.setCurrentKey(id)
      })
    },
    // 清空选中状态
    removeCurrentNode() {
      this.$refs.tree2.setCurrentKey(null)
    },
    // * 过滤
    handleSearch() {
      this.$refs.tree2.filter(this.searchValue)
    },
    // * 过滤
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // * 获取被选中节点
    getCurrentNode() {
      return this.$refs.tree2.getCurrentNode()
    },
    setLockState(e) {
      console.log(e, 'true 锁定')
    },
    clearTree() {
      this.editId = 'orgAdd'
      this.$emit('currentNode', { id: '0', type: 'MENU', name: '平台设备' })
      this.removeCurrentNode()
    },
    /* 添加节点编辑 */
    submitForm() {
      this.$refs.addTreeForm.validate(valid => {
        if (valid) {
          // const json = { id, parentId, ...this.addTreeForm }
          this.saveDicOrg(this.addTreeForm)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.addTreeForm.resetFields()
      /* 关闭弹窗 */
      this.showAdd = false
      this.addTreeForm = { name: '' }
    },
 
    /* 节点操作 */
    setModelXYShow(event) {
      if (!this.$refs.treeContainer) {
        return null
      }
      let { clientX = 0, clientY = 0 } = event
      console.log(clientX, clientY, ' clientX, clientY')
      const { left = 0, top = 0, width, height } = getContainerRect(
        this.$refs.treeContainer
      )
      console.log(left, top, width, height, 'left, top, width, height')
 
      if (clientX && clientX - left > 0) {
        clientX = clientX - left
      }
      if (clientY && clientY - top > 0) {
        clientY = clientY - top
      }
      clientX = clientX - 50
      clientY = clientY + 10
      console.log(width - clientX, 'width-clientX')
      if (width - clientX - 200 < 0) {
        clientX = width - 200
      }
 
      this.clientX = clientX
      this.clientY = clientY
      this.showAdd = true
      // console.log(
      //   { clientX, clientY, showAdd: true },
      //   '{ clientX, clientY, showAdd: true }'
      // )
      return { clientX, clientY, showAdd: true }
    },
    addNdoe(event, data) {
      if (data) {
        this.addTreeForm.parentId = data.id ? data.id : ''
      } else {
        this.addTreeForm.parentId = '0'
      }
      // submitForm({id = '', parentId =''})
      /* 显示弹窗函数 */
      this.setModelXYShow(event)
    },
    edit(event, data) {
      if (data && data.type === 'MENU') {
        this.addTreeForm.name = data.name ? data.name : ''
        this.addTreeForm.id = data.id ? data.id : ''
        /* 显示弹窗函数 */
        this.setModelXYShow(event)
      } else if (data) {
        // 设备编辑
        this.$emit('add-device', { data, type: 'edit' })
      }
    },
    delNdoe({ id, type }) {
      this.$swal(
        {
          title: '确定删除?',
          text: `你将要删除当前${type === 'MENU' ? '节点' : '设备'}吗!`,
          type: 'warning',
          showCancelButton: true,
          allowOutsideClick: true,
          // confirmButtonColor: '#d9534f',
          confirmButtonText: '确定删除!',
          cancelButtonText: '取消',
          closeOnConfirm: true
        },
        () => {
          type === 'MENU' ? this.delDicOrg(id) : this.delDevice(id)
        }
      )
    },
    addDevice(data) {
      this.$emit('add-device', { data, type: 'add' })
    },
    renderContent(h, { node, data, store }) {
      return (
        <span class="custom-tree-node">
          {data.type !== 'MENU' ? (
            data.isOnMap === 1 ? (
              <span class="text-primary">{data.name}</span>
            ) : (
              <span class="text-purple">{data.name}</span>
            )
          ) : (
            <span>{data.name}</span>
          )}
          {!this.isLock && this.editId === data.id ? (
            <span class="py-2 add-tool" v-show="!isLock && editId === node.id">
              {data.type === 'MENU' ? (
                <span
                  class="text-primary pl-2"
                  on-click={event => {
                    this.addNdoe(event, data)
                  }}
                >
                  <i
                    class="ion ion-ios-add-circle-outline f16"
                    title="添加机构"
                  />
                </span>
              ) : (
                ''
              )}
 
              <span
                class="text-primary pl-1"
                on-click={event => {
                  this.edit(event, data)
                }}
              >
                <i class="far fa-edit f16" title="编辑" />
              </span>
 
              <span
                class="text-primary pl-1"
                on-click={event => {
                  this.delNdoe(data)
                }}
              >
                <i class="far fa-trash-alt f16" title="删除" />
              </span>
              {data.type === 'MENU' ? (
                <span
                  class="text-primary pl-1"
                  on-click={event => {
                    this.addDevice(data)
                  }}
                >
                  <i class="fas fa-server f16" title="添加设备" />
                </span>
              ) : (
                ''
              )}
            </span>
          ) : (
            ''
          )}
        </span>
      )
    },
    deviceSubmit(data) {
      console.log(data, '弹窗提交')
    }
  },
  /* !isLock &&  */
  components: {
    Tree,
    circleLock,
    elCard: Card,
    elForm: Form,
    elFormItem: FormItem,
    elInput: Input
  }
}
</script>
 
<style lang="scss">
#tree-container-org .el-tree {
  overflow: auto;
}
#tree-container-org .el-tree-node {
  > .el-tree-node__children {
    overflow: visible;
    float: left;
    min-width: 100%;
  }
  &:after {
    display: block;
    content: '';
    clear: both;
  }
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  // background: rgb(38, 180, 255);
  // color: #fff;
  color: #35bde7;
  position: relative;
  border-radius: 5px;
  overflow: hidden;
  .el-tree-node__expand-icon {
    color: #35bde7;
  }
  .el-tree-node__expand-icon.is-leaf {
    color: transparent;
    cursor: default;
  }
}
.el-tree-node__content:hover {
  border-radius: 5px;
  overflow: hidden;
}
.tree-btn {
  padding: 6px 8px;
}
.org-tree-lock {
  position: absolute;
  right: 0px;
  top: 2px;
}
.animation {
  transition: all 0.5s;
}
.tree-edit-btn {
  padding: 0px 3px;
  font-size: 14px;
}
.add-tool {
  cursor: pointer;
}
.tree-Model {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 200px;
}
.tree-Model-bg {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  // width: 100%;
  // height: 100%;
  background: rgba(255, 255, 255, 0.6);
}
</style>