| 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 |
1x
1x
1x
1x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
| import DA from 'dom-align'
import {
prefix
} from './config';
import placements from "./placements";
const IntroMap = {}
const BACKGROUND_LAYOUT = `${prefix}-intro-overlay`;
const HELP_LAYOUT = `${prefix}-intro-helperLayout`;
const TOOLTIP_LAYOUT = `${prefix}-intro-tooltipReferenceLayer`;
class Controller {
constructor(name, intro) {
this.name = name;
this.step = 0; // The controller current step;
this.prevIntro = null; // previous intro
this.currentIntro = null; // current Intro
this.inited = false; // The controller was loaded in the document;
this.list = [intro]; // intro list
this.warper = null; // append wrap dom
}
// return Intros count
get count() {
return this.list.length;
}
// add Intro
addIntro(intro) {
this.list.push(intro);
}
// update All Intro
updateAllSteps() {
this.list.forEach(v => {
v.setState({});
})
}
// update previous Intro and
updateSteps(currentIntro) {
const {
prevIntro
} = this;
if (prevIntro !== null) {
prevIntro.unsetActive({})
}
this.prevIntro = currentIntro;
currentIntro.setActive({});
}
// go to step by Intro
goToStep = (intro = 0) => {
if (intro === 0) {
this.end();
return;
}
const {
step
} = intro;
this.step = step;
this.updateSteps(intro);
this.setLayoutProps(intro);
// this.step = step.step;
// step.component.el.scrollIntoView()
// this.list.forEach(v=> v.component.setState({}));
}
// set Intros end
end = () => {
this.setp = 0;
this.updateAllSteps();
this.destroyLayout();
}
// destory controller layout
destroyLayout() {
this.backgroundLayout.remove();
this.tooltipLayout.remove();
this.helplerLayout.remove();
this.inited = false;
}
// go to step by Intro step
goToStepNumber = (number) => {
const intro = this.list.find(v => v.step === number);
this.goToStep(intro);
}
// create layout dom
createDom() {
// 初始化背景部分
this.backgroundLayout = document.createElement('div');
this.backgroundLayout.className = BACKGROUND_LAYOUT;
this.backgroundLayout.style = {
top: 0,
bottom: 0,
left: 0,
right: 0,
position: 'fixed',
opacity: 0.8
}
// 初始化弹窗
this.tooltipLayout = document.createElement('div');
this.tooltipLayout.className = TOOLTIP_LAYOUT;
//初始化选区
this.helplerLayout = document.createElement('div');
this.helplerLayout.className = HELP_LAYOUT
}
// get Intro child react
getStepRect(dom) {
if (dom) {
const obj = dom.getBoundingClientRect();
const {
scrollY,
scrollX
} = window;
const {
bottom,
height,
left,
right,
top,
width
} = obj;
const attr = {
bottom,
height,
left: left + scrollX,
right,
top: top + scrollY,
width
};
Object.keys(attr).forEach(v => {
attr[v] = `${attr[v]}px`
});
return attr;
} else {
console.log('error');
}
}
getTooltipProps(hide) {
if (hide) {
return {
visibility: 'hidden'
}
}
return {
visibility: 'visible',
background: '#fff'
}
}
// initialize Intro stage layout
initLayout() {
if (this.inited === true) return;
this.createDom();
const idName = `${prefix}-wrap`
let target = document.getElementById(idName);
if (target === null) {
target = document.createElement('div')
target.id = idName;
document.body.appendChild(target);
}
this.wraper = target;
target.appendChild(this.backgroundLayout);
target.appendChild(this.tooltipLayout);
target.appendChild(this.helplerLayout);
this.helplerLayout.addEventListener('transitionstart', function () {
console.log('transitionrun 1');
});
this.helplerLayout.addEventListener('transitionend', (e) => {
if (e.propertyName === 'top') {
const tooltipProps = this.getTooltipProps();
this.setDomStyle(this.tooltipLayout, tooltipProps);
}
});
this.inited = true;
}
setLayoutProps(intro) {
const helperProps = this.getStepRect(intro.el);
this.setDomStyle(this.helplerLayout, helperProps);
const bgProps = this.getBackgroundLayoutProps();
this.setDomStyle(this.backgroundLayout, bgProps);
const hide = this.getTooltipProps(!this.firstLoad);
this.setDomStyle(this.tooltipLayout, hide);
const {
placement
} = intro.props;
DA(this.tooltipLayout, intro.el, placements[placement]);
}
// start
start = () => {
this.firstLoad = true;
const target = this.list.sort((a, b) => a.step - b.step)[0];
this.initLayout()
this.goToStep(target);
this.firstLoad = false;
}
// map style object and set dom style
setDomStyle(dom, style) {
Object.keys(style).forEach(v => {
dom.style[v] = style[v];
})
}
getBackgroundLayoutProps() {
return {
top: 0,
bottom: 0,
left: 0,
right: 0,
position: 'fixed',
opacity: 0.8
}
}
// go to next Intro
nextStep = () => {
const {
step
} = this;
if (this.count !== step) {
this.goToStepNumber(step + 1)
}
}
// goto previous Into
prevStep = () => {
const {
step
} = this;
if (1 !== step) {
this.goToStepNumber(step - 1)
}
}
oncomplete(providedCallback) {}
onexit(providedCallback) {}
onbeforeexit(providedCallback) {}
onchange(providedCallback) {}
onbeforechange(providedCallback) {}
onafterchange(providedCallback) {}
}
// create a new Controller by Intro name props;
function newController(intro) {
const {
name
} = intro;
Iif (IntroMap[name]) {
IntroMap[name].addIntro(intro);
return IntroMap[name];
} else {
const nc = new Controller(name, intro)
IntroMap[name] = nc;
return nc;
}
}
// return a controller by name, default name global;
function getController(name = 'global') {
Iif (IntroMap[name] === undefined) {
throw new Error(`name ${name} Controller not found`);
}
return IntroMap[name];
}
export default Controller;
export {
Controller,
newController,
IntroMap,
getController
} |