Blame view

src/components/funAreaSelectPlus/funAreaSelectPlus.js 9.9 KB
e2460342   Zhang Zhuo   更改 -张卓
1
2
  import React, { Component } from 'react';
  import FunAreaSelect from "../funAreaSelect/funAreaSelect.js"
5290cb53   Zhang Zhuo   更新第一版本 -张卓
3
  import "./funAreaSelectPlus.css"
e2460342   Zhang Zhuo   更改 -张卓
4
5
6
7
8
  export default class FunAreaSelectPlus extends Component {
      constructor() {
          super();
          this.state = {
              id: new Date().getTime() + "waiceng",
e2460342   Zhang Zhuo   更改 -张卓
9
10
11
12
13
14
15
16
17
18
19
20
21
22
              disabled: "false",//判断是否可以移动  false是可以移动
              circleRadius: 5,//圆半径
              circleBorderWidth: 2,//圆边线的半径宽
              circleBorderColor: "#28B1D9",//圆边线的颜色
              circleInColor: "#28B1D9",//圆里里面的颜色
              circleSelectColor: "#FFF",
              lineColor: "#28B1D9",//线的颜色
              lineWidth: 2,//线的宽度
              areaColor: "rgba(40, 177, 217, 0.2)",//选中区域颜色
              disabled: "false",//判断是否可以移动  false是可以移动
              title: "",//文字的内容
              titleColor: "#000",//文字的颜色
              titleFont: "14px bold 黑体",//文字的字体
              titleLineHeight: "",//文字的行高
5290cb53   Zhang Zhuo   更新第一版本 -张卓
23
24
25
26
27
              titleBackgroundColor: "",//字的北京颜色支持rgba
              titlePadding: "10px",//文字的padding
              titleTextAlign: "center",
              titleWidth: 100,
              titleFillColor: "",
e2460342   Zhang Zhuo   更改 -张卓
28
              position: [10, 10],//文字的位置
5290cb53   Zhang Zhuo   更新第一版本 -张卓
29
              inputStyle:{}
e2460342   Zhang Zhuo   更改 -张卓
30
31
32
33
34
35
          }
      }
      static getDerivedStateFromProps(props, state) {
          var obj = {};
          for (var key in props) {
              if (props[key] && props[key] !== state[key]) {
42886855   Zhang Zhuo   更改保存下 -张卓
36
                  if (typeof props[key] === "object") {
e2460342   Zhang Zhuo   更改 -张卓
37
                      obj[key] = JSON.parse(JSON.stringify(props[key]))
42886855   Zhang Zhuo   更改保存下 -张卓
38
                  } else {
e2460342   Zhang Zhuo   更改 -张卓
39
40
                      obj[key] = props[key]
                  }
e2460342   Zhang Zhuo   更改 -张卓
41
42
43
44
45
46
47
48
49
              }
          }
          if (Object.keys(obj).length > 0) {
              return obj;
          } else {
              return null
          }
      }
      componentDidMount() {
5290cb53   Zhang Zhuo   更新第一版本 -张卓
50
51
          let { list } = this.props;
          if (list && list.length > 0) {
42886855   Zhang Zhuo   更改保存下 -张卓
52
53
              this.selectIndex = list[list.length - 1].id;
          }
e2460342   Zhang Zhuo   更改 -张卓
54
55
          this.selectArea();
      }
42886855   Zhang Zhuo   更改保存下 -张卓
56
      clickIt(mode) {
5290cb53   Zhang Zhuo   更新第一版本 -张卓
57
          let { list} = this.props;
42886855   Zhang Zhuo   更改保存下 -张卓
58
          var id = new Date().getTime()
5290cb53   Zhang Zhuo   更新第一版本 -张卓
59
          if (!list || list.length === 0) {
42886855   Zhang Zhuo   更改保存下 -张卓
60
              list = []
e2460342   Zhang Zhuo   更改 -张卓
61
              list.push({
42886855   Zhang Zhuo   更改保存下 -张卓
62
                  id: id,
5290cb53   Zhang Zhuo   更新第一版本 -张卓
63
                  title:this.props.placeholder?this.props.placeholder:"请输入",
42886855   Zhang Zhuo   更改保存下 -张卓
64
                  mode: mode
e2460342   Zhang Zhuo   更改 -张卓
65
              })
42886855   Zhang Zhuo   更改保存下 -张卓
66
              this.selectIndex = id;
e2460342   Zhang Zhuo   更改 -张卓
67
          } else {
42886855   Zhang Zhuo   更改保存下 -张卓
68
              var boo = this[list[list.length - 1].id + list[list.length - 1].mode].isNull()//判断画布是否为空
e2460342   Zhang Zhuo   更改 -张卓
69
              if (boo) {//画布为空
5290cb53   Zhang Zhuo   更新第一版本 -张卓
70
                  this.props.err(false, "画布为空,请先绘制在添加。")
e2460342   Zhang Zhuo   更改 -张卓
71
              } else if (!list[list.length - 1].serviceData) {
5290cb53   Zhang Zhuo   更新第一版本 -张卓
72
                  this.props.err(false, "画布没有保存,请先绘制完成在添加")
e2460342   Zhang Zhuo   更改 -张卓
73
              } else {
42886855   Zhang Zhuo   更改保存下 -张卓
74
                  list.push({
5290cb53   Zhang Zhuo   更新第一版本 -张卓
75
76
                      id: id+"",
                      title:this.props.placeholder?this.props.placeholder:"请输入",
42886855   Zhang Zhuo   更改保存下 -张卓
77
78
                      mode: mode
                  })
5290cb53   Zhang Zhuo   更新第一版本 -张卓
79
                  this.selectIndex = id+"";//这个地方是用来判断当前点击或者添加的第几个,通过这个控制着list父子组件的同时赋值
e2460342   Zhang Zhuo   更改 -张卓
80
81
              }
          }
42886855   Zhang Zhuo   更改保存下 -张卓
82
83
          this.props.change(list)
      }
5290cb53   Zhang Zhuo   更新第一版本 -张卓
84
85
86
87
88
89
      delete() {
          let {list} = this.state;
          var nowList = list.filter(item=>item.id === this.selectIndex)//这个地方this是id不是索引值这里要注意,一开始是索引值但是发现无法匹配所有所以改变了一下
          var limian = this[nowList[0].id + nowList[0].mode];//删除input的输入框
          limian[limian.state.id].removeTags("input",limian.props.listId)
          this.props.delete(this.selectIndex, "发出删除指令")
e2460342   Zhang Zhuo   更改 -张卓
90
91
      }
      change(val) {//保存后把最后一个放入这个list中储存,这样可以判断是否储存,点击的时候是否在点内
42886855   Zhang Zhuo   更改保存下 -张卓
92
          let { list } = this.props;
5290cb53   Zhang Zhuo   更新第一版本 -张卓
93
94
          list = list.map(item => {
              if (item.id === this.selectIndex) {
42886855   Zhang Zhuo   更改保存下 -张卓
95
96
97
98
                  item.serviceData = JSON.parse(JSON.stringify(val));
              }
              return item
          })
e2460342   Zhang Zhuo   更改 -张卓
99
100
101
          this.props.change(list)
      }
      selectArea() {//选中区域z-index应该增大放在最上面
42886855   Zhang Zhuo   更改保存下 -张卓
102
          let { id } = this.state;
e2460342   Zhang Zhuo   更改 -张卓
103
104
          var waiceng = document.getElementById(id);
          waiceng.onclick = (e) => {
42886855   Zhang Zhuo   更改保存下 -张卓
105
106
              // let { list } = this.state;
              let list = this.props.list//这里用里this.list因为有时候在change中setState不好使不能设置list怀疑是this指向问题但是打印出来没什么问题所以还没解决
e2460342   Zhang Zhuo   更改 -张卓
107
108
109
              var left = e.layerX
              var top = e.layerY
              this.indexflag = false;//这里是为了防止点击到重复的区域导致无法选区的情况
5290cb53   Zhang Zhuo   更新第一版本 -张卓
110
              if (list) {
42886855   Zhang Zhuo   更改保存下 -张卓
111
112
113
114
115
116
117
118
119
120
121
122
                  for (var i = 0; i < list.length; i++) {
                      if (list[i].serviceData) {
                          var myRef = this[list[i].id + list[i].mode].state.id;//获取内部id
                          if (this.isInPolygon([left, top], JSON.parse(JSON.stringify(list[i].serviceData)), this[list[i].id + list[i].mode][myRef])) {//选中之后放在最上层
                              var id = this[list[i].id + list[i].mode][myRef].state.id
                              if (this.indexflag) {
                                  document.getElementById(id).style.zIndex = 0
                              } else {
                                  document.getElementById(id).style.zIndex = 99
                                  this.selectIndex = list[i].id;
                                  this.indexflag = true
                              }
e2460342   Zhang Zhuo   更改 -张卓
123
                          } else {
42886855   Zhang Zhuo   更改保存下 -张卓
124
125
                              var id = this[list[i].id + list[i].mode][myRef].state.id
                              document.getElementById(id).style.zIndex = 0
e2460342   Zhang Zhuo   更改 -张卓
126
                          }
e2460342   Zhang Zhuo   更改 -张卓
127
128
129
130
131
                      }
                  }
              }
          }
      }
42886855   Zhang Zhuo   更改保存下 -张卓
132
133
134
135
136
137
138
139
140
      isInPolygon(point, vs, childEl) {//判断多边形是否在点内
          var x = point[0], y = point[1];
          var inside = false;
          for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
              var xi = vs[i][0], yi = vs[i][1];
              var xj = vs[j][0], yj = vs[j][1];
              var intersect = ((yi > y) != (yj > y))
                  && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
              if (intersect) inside = !inside;
e2460342   Zhang Zhuo   更改 -张卓
141
          }
42886855   Zhang Zhuo   更改保存下 -张卓
142
143
          if (!intersect) {
              if (childEl.selectBox(point[0], point[1])) {
e2460342   Zhang Zhuo   更改 -张卓
144
145
                  return true;
              }
e2460342   Zhang Zhuo   更改 -张卓
146
          }
42886855   Zhang Zhuo   更改保存下 -张卓
147
          return inside;
e2460342   Zhang Zhuo   更改 -张卓
148
      }
5290cb53   Zhang Zhuo   更新第一版本 -张卓
149
150
151
      wenziChange(val,index){//文字发生改变的时候
          this.props.wenziChange(val,index)
      }
e2460342   Zhang Zhuo   更改 -张卓
152
      render() {
42886855   Zhang Zhuo   更改保存下 -张卓
153
          let { id, disabled, circleRadius,
e2460342   Zhang Zhuo   更改 -张卓
154
155
156
157
158
159
160
161
162
163
164
              circleBorderWidth,
              circleBorderColor,
              circleInColor,
              circleSelectColor,
              lineColor,
              lineWidth,
              areaColor,
              title,
              titleColor,
              titleFont,
              titleLineHeight,
5290cb53   Zhang Zhuo   更新第一版本 -张卓
165
166
167
168
169
              titleBackgroundColor,
              titlePadding,
              titleTextAlign,
              titleWidth,
              titleFillColor,
e2460342   Zhang Zhuo   更改 -张卓
170
              position } = this.state;
5290cb53   Zhang Zhuo   更新第一版本 -张卓
171
          let { list } = this.props
e2460342   Zhang Zhuo   更改 -张卓
172
173
          return (
              <div id={id} style={{ width: "100%", height: "100%", position: "relative" }}>
a031df93   Zhang Zhuo   更新视频稳定版本 -张卓
174
175
                  {/* <div className="buttonFunAreaSelectPlus" onClick={()=>this.clickIt("react")} style={{background: "url("+require("./123.png")+") no-repeat center center",backgroundSize:"100% 200%"}}>添加矩形</div>
                  <div className="buttonFunAreaSelectPlus" onClick={()=>this.delete()} style={{background: "url("+require("./123.png")+") no-repeat center center",backgroundSize:"100% 200%"}}>删除</div> */}
5290cb53   Zhang Zhuo   更新第一版本 -张卓
176
177
178
                  {/* <button style={{ position: "absolute", right: "-70px", top: "0" }} onClick={() => this.clickIt("react")}>添加矩形</button> */}
                  {/* <button style={{ position: "absolute", right: "-83px", top: "40px" }} onClick={() => this.clickIt("polygon")}>添加多边形</button> */}
                  {/* <button style={{ position: "absolute", right: "-43px", top: "40px" }} onClick={() => this.delete()}>删除</button> */}
e2460342   Zhang Zhuo   更改 -张卓
179
                  {
5290cb53   Zhang Zhuo   更新第一版本 -张卓
180
                      list && list.map((item, index) => {
e2460342   Zhang Zhuo   更改 -张卓
181
182
183
184
185
186
187
188
189
190
                          return <FunAreaSelect
                              disabled={disabled}
                              circleRadius={circleRadius}
                              circleBorderWidth={circleBorderWidth}
                              circleBorderColor={circleBorderColor}
                              circleSelectColor={circleSelectColor}
                              circleInColor={circleInColor}
                              lineColor={lineColor}
                              lineWidth={lineWidth}
                              areaColor={areaColor}
5290cb53   Zhang Zhuo   更新第一版本 -张卓
191
192
193
194
195
196
197
198
199
200
201
202
                              title={item.title ? item.title : title}
                              titleColor={item.titleColor ? item.titleColor : titleColor}
                              titleFont={item.titleFont ? item.titleFont : titleFont}
                              titleLineHeight={item.titleLineHeight ? item.titleLineHeight : titleLineHeight}
                              titleBackgroundColor={item.titleBackgroundColor ? item.titleBackgroundColor : titleBackgroundColor}
                              titlePadding={item.titlePadding ? item.titlePadding : titlePadding}
                              titleTextAlign={item.titleTextAlign ? item.titleTextAlign : titleTextAlign}
                              titleWidth={item.titleWidth ? item.titleWidth : titleWidth}
                              titleFillColor={item.titleFillColor ? item.titleFillColor : titleFillColor}
                              position={position} key={item.id} listId={item.id} serviceData={item.serviceData} ref={(inst) => { this[item.id + item.mode] = inst }} mode={item.mode}
                              wenziChange={(val)=>this.wenziChange(val,index)}
                              change={(val) => this.change(val)}></FunAreaSelect>
e2460342   Zhang Zhuo   更改 -张卓
203
204
205
206
207
                      })
                  }
              </div>)
      }
  }