forked from nsjcy/frontEnd/nsjcy

LAPTOP-RI7D261L\Mr Ke
2020-02-26 8f619dde9e2382fc8a4f1e04fafde1418f06165b
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
import React from 'react';
import QuestionnairContent from '../QuestionnairContent';
import QuestionnairEditor from '../QuestionnairEditor';
import QuestionnairSiderbar from '../QuestionnairSiderbar';
import DragSort from '../../libs/DragSort/index.js';
import ShakeTransition from '../../libs/Shake';
import Input from '../../libs/Input';
import uuid from '../../utils/utils';
import './index.scss';
 
class Questionnair extends React.PureComponent {
  constructor(props) {
    super(props);
    this.editorsEl = [];
    this.scaleId = '';
    this.sign = false;
  }
 
  state = {
    editors: [
      // {
      //   questionId: 'afaa9e6d672c56418199a8a62a4152c2d74f',
      //   type: 'radio',
      //   title: '单选题',
      //   required: true,
      //   remark: true,
      //   remarkText: '(此题关于一个很有深度的问题)',
      //   options: ['选项1', '选项2'],
      //   scores: [1, 0],
      //   rows: 1,
      //   textareaHeight: 3,
      //   maxLength: 50,
      //   otherOption: true,
      //   otherOptionForwards: '其他',
      //   otherOptionBackwards: '22222',
      //   completionForwards: '题目:',
      //   completionBackwards: '',
      //   isEditor: false,
      //   isFirst: false,
      //   warnFlag: false,
      //   editorShake: ''
      // }
      ],
    questionnairTitle: '',
    questionnairDescription: '',
    curMoveItem: null,
    drag: false,
    scrollTo: 0,
    newEditor: true
  };
  // UNSAFE_componentWillReceiveProps(nextProps) {
  //   console.log('nextProps', nextProps);
  //   this.setState({
  //     editors: nextProps.editors
  //   });
  // }
  componentDidMount() {
    const { editors } = this.props;
    this.setState({
      editors
    })
  }
 
  updateEditors = callback => {
    this.state.editors.some((data, index) => {
      if (data.isFirst && data.isEditor) {
        this.state.editors.splice(index, 1);
        return true;
      } else if (!data.isFirst && data.isEditor) {
        data.isEditor = false;
        return true;
      }
    });
    callback(this.state.editors);
  };
  /*
   * 判断是否有处于编辑状态的题目, activeEditorIndex // -1,没有处于编辑状态的题目
   * 如果有处于编辑状态的题目,则激活该编辑器抖动
   */
  isThereEditor = () => {
    const activeEditorIndex = this.state.editors.findIndex(
      data => data.isEditor === true
    );
    if (activeEditorIndex !== -1) {
      let editors = JSON.parse(JSON.stringify(this.state.editors));
      editors[activeEditorIndex].editorShake = uuid();
      this.setState({
        editors
      });
      return true;
    } else {
      return false;
    }
  };
 
  createEditor = type => {
    if (this.isThereEditor()) {
      return;
    }
    const editor = {
      questionId: uuid(), //id
      type: type, //类型
      title: '', //题目
      required: false, //是否必填
      remark: true, //是否有备注
      remarkText: '', //备注内容
      options: ['选项', '选项'], //选项(只有radio,checkbox,select有,其余尽量给个空数组)
      scores: ['', ''], //选项对应的分数
      rows: 1, //选项占的行数
      textareaHeight: 3, //多行文本高度
      maxLength: 50, //单行文本限制的字数
      otherOption: false, //是否有其他选项
      otherOptionForwards: '其他', //”其他“项文本(前)
      otherOptionBackwards: '', //”其他“项文本(后)
      completionForwards: '题目:', //填空题文本(前)
      completionBackwards: '', //填空题文本(后)
      isEditor: true, //编辑状态还是已编辑状态
      isFirst: true, //是否是新创建的
      warnFlag: [false, false], //警示标识
      editorShake: ''
    };
    this.setState(prevState => ({
      editors: [...prevState.editors, editor]
    }));
  };
 
  dragEditorByOutline = editors => {
    const { onDrag } = this.props;
    this.setState(
      {
        editors
      },
      () => {
        if (onDrag) {
          this.updateEditors(onDrag);
        }
      }
    );
  };
 
  locateEditor = index => {
    this.setState({
      scrollTo: this.editorsEl[index].offsetTop
    });
  };
 
  cancelEdit = index => {
    let editors = JSON.parse(JSON.stringify(this.state.editors));
    editors[index].isFirst
      ? editors.splice(index, 1)
      : (editors[index].isEditor = false);
    this.setState({
      editors
    });
  };
 
  confirmEdit = (index, newEditor) => {
    const { onConfirm } = this.props;
    let editors = JSON.parse(JSON.stringify(this.state.editors));
    editors.splice(index, 1, newEditor);
    this.setState(
      {
        editors
      },
      () => {
        if (onConfirm) {
          this.updateEditors(onConfirm);
        }
      }
    );
  };
 
  againEdit = index => {
    if (this.isThereEditor()) {
      return;
    }
    let editors = JSON.parse(JSON.stringify(this.state.editors));
    editors[index].isEditor = true;
    this.setState({
      editors
    });
  };
 
  copyEdit = index => {
    const { onCopy } = this.props;
    let editors = JSON.parse(JSON.stringify(this.state.editors));
    const copyEditor = {
      ...this.state.editors[index],
      questionId: uuid()
    };
    editors.splice(index + 1, 0, copyEditor);
    this.setState(
      {
        editors
      },
      () => {
        if (onCopy) {
          this.updateEditors(onCopy);
        }
      }
    );
  };
 
  removeEdit = index => {
    const { onRemove } = this.props;
    let editors = JSON.parse(JSON.stringify(this.state.editors));
    editors.splice(index, 1);
    this.setState(
      {
        editors
      },
      () => {
        if (onRemove) {
          this.updateEditors(onRemove);
        }
      }
    );
  };
 
  handleDragMove = (editors, from, to) => {
    this.setState({
      curMoveItem: to,
      editors,
      drag: true
    });
  };
 
  handleDragEnd = () => {
    const { onDrag } = this.props;
    this.setState(
      {
        curMoveItem: null,
        drag: false
      },
      () => {
        if (onDrag) {
          this.updateEditors(onDrag);
        }
      }
    );
  };
  //标记事件
  handleSgin = sign => {
    const { onSign } = this.props;
    if (onSign) {
      onSign(sign);
    }
  };
  //问卷标题失焦事件
  blurTitle = title => {
    const { onSaveTitle } = this.props;
    if (onSaveTitle) {
      onSaveTitle(title);
    }
  };
 
  render() {
    const {
      editors,
      drag,
      editorShake,
      scrollTo,
      questionnairTitle,
      questionnairDescription
    } = this.state;
 
    const { submitQuestionTemp, btnLoading, initData } = this.props;
 
    //如果有编辑状态的题目则禁止拖动
    const hasEditor = editors.some(data => data.isEditor === true);
    const canDrag = hasEditor ? false : true;
    const isFirst = editors.length !== 0 && editors[editors.length - 1].isFirst;
    const editorsEl = editors.map((editor, index) => {
      return (
        <div
          className="drag-wrapper"
          ref={el => (this.editorsEl[index] = el)}
          key={editor.questionId}
        >
          <QuestionnairEditor
            index={index}
            curMoveItem={this.state.curMoveItem}
            editor={editor}
            drag={drag}
            handleConfirm={this.confirmEdit}
            handleCancel={this.cancelEdit}
            handleEdit={this.againEdit}
            handleRemove={this.removeEdit}
            handleCopy={this.copyEdit}
          />
        </div>
      );
    });
    return (
      <div className="questionnair">
        <QuestionnairSiderbar
          editors={editors}
          onSelectEditor={this.createEditor}
          onDragOutline={this.dragEditorByOutline}
          onClickOutline={this.locateEditor}
          submitQuestionTemp={submitQuestionTemp}
        />
        <QuestionnairContent
          editors={editors}
          isThereEditor={this.isThereEditor}
          isFirst={isFirst}
          scrollTo={scrollTo}
          questionnairSign={this.sign}
          questionnairTitle={questionnairTitle}
          questionnairDescription={questionnairDescription}
          onBlurTitle={this.blurTitle}
          onChangeSign={this.handleSgin}
          submitQuestionTemp={submitQuestionTemp}
          btnLoading={btnLoading}
          initData={initData}
        >
          {editorsEl.length !== 0 && (
            <DragSort
              draggable={canDrag}
              data={editors}
              onDragEnd={this.handleDragEnd}
              onDragMove={this.handleDragMove}
            >
              {editorsEl}
            </DragSort>
          )}
        </QuestionnairContent>
      </div>
    );
  }
}
 
export default Questionnair;