forked from usablica/intro.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro.js
346 lines (294 loc) · 10.3 KB
/
intro.js
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
/**
* Intro.js v0.1.0
* https://github.com/usablica/intro.js
* MIT licensed
*
* Copyright (C) 2013 usabli.ca - A weekend project by Afshin Mehrabani (@afshinmeh)
*/
(function () {
//Default config/variables
var VERSION = "0.1.0";
/**
* IntroJs main class
*
* @class IntroJs
*/
function IntroJs(obj) {
this._targetElement = obj;
}
/**
* Initiate a new introduction/guide from an element in the page
*
* @api private
* @method _introForElement
* @param {Object} targetElm
* @returns {Boolean} Success or not?
*/
function _introForElement(targetElm) {
var allIntroSteps = targetElm.querySelectorAll("*[data-intro]"),
introItems = [],
self = this;
//if there's no element to intro
if(allIntroSteps.length < 1) {
return;
}
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
var currentElement = allIntroSteps[i];
introItems.push({
element: currentElement,
intro: currentElement.getAttribute("data-intro"),
step: parseInt(currentElement.getAttribute("data-step"))
});
}
//Ok, sort all items with given steps
introItems.sort(function (a, b) {
return a.step - b.step;
});
//set it to the introJs object
self._introItems = introItems;
//add overlay layer to the page
if(_addOverlayLayer(targetElm)) {
//then, start the show
_nextStep.call(self);
var skipButton = targetElm.querySelector(".introjs-skipbutton"),
nextStepButton = targetElm.querySelector(".introjs-nextbutton");
targetElm.onkeydown = function(e) {
if(e.keyCode == 27) {
//escape key pressed, exit the intro
_exitIntro(targetElm);
}
if([37, 39].indexOf(e.keyCode) >= 0) {
if(e.keyCode == 37) {
//left arrow
_previousStep.call(self);
} else if (e.keyCode == 39) {
//right arrow
_nextStep.call(self);
}
};
}
}
return false;
}
/**
* Go to next step on intro
*
* @api private
* @method _nextStep
*/
function _nextStep() {
if(this._currentStep == undefined) {
this._currentStep = 0;
} else {
++this._currentStep;
}
if((this._introItems.length) <= this._currentStep) {
//end of the intro
_exitIntro(this._targetElement);
return;
}
_showElement.call(this, this._introItems[this._currentStep].element);
}
/**
* Go to previous step on intro
*
* @api private
* @method _nextStep
*/
function _previousStep() {
if(this._currentStep == 0)
return;
_showElement.call(this, this._introItems[--this._currentStep].element);
}
/**
* Exit from intro
*
* @api private
* @method _exitIntro
* @param {Object} targetElement
*/
function _exitIntro(targetElement) {
//remove overlay layer from the page
var overlayLayer = targetElement.querySelector(".introjs-overlay");
//for fade-out animation
overlayLayer.style.opacity = 0;
setTimeout(function () {
overlayLayer.parentNode.removeChild(overlayLayer);
}, 500);
//remove all helper layers
var helperLayer = targetElement.querySelector(".introjs-helperLayer");
helperLayer.parentNode.removeChild(helperLayer);
//remove `introjs-showElement` class from the element
var showElement = document.querySelector(".introjs-showElement");
showElement.className = showElement.className.replace(/introjs-showElement/,'').trim();
//clean listeners
targetElement.onkeydown = null;
}
/**
* Show an element on the page
*
* @api private
* @method _showElement
* @param {Object} targetElement
*/
function _showElement(targetElement) {
var self = this,
oldHelperLayer = document.querySelector(".introjs-helperLayer"),
elementPosition = _getOffset(targetElement);
//targetElement.scrollIntoView();
if(oldHelperLayer != null) {
var oldHelperNumberLayer = oldHelperLayer.querySelector(".introjs-helperNumberLayer"),
oldtooltipLayer = oldHelperLayer.querySelector(".introjs-tooltiptext"),
oldtooltipContainer = oldHelperLayer.querySelector(".introjs-tooltip")
//set new position to helper layer
oldHelperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
"height:" + (elementPosition.height + 10) + "px; " +
"top:" + (elementPosition.top - 5) + "px;" +
"left: " + (elementPosition.left - 5) + "px;");
//set current step to the label
oldHelperNumberLayer.innerHTML = targetElement.getAttribute("data-step");
//set current tooltip text
oldtooltipLayer.innerHTML = targetElement.getAttribute("data-intro");
var oldShowElement = document.querySelector(".introjs-showElement");
oldShowElement.className = oldShowElement.className.replace(/introjs-showElement/,'').trim();
//change to new intro item
targetElement.className += " introjs-showElement";
//wait until the animation is completed
setTimeout(function() {
oldtooltipContainer.style.bottom = "-" + (_getOffset(oldtooltipContainer).height + 10) + "px";
}, 300);
} else {
targetElement.className += " introjs-showElement";
var helperLayer = document.createElement("div"),
helperNumberLayer = document.createElement("span"),
tooltipLayer = document.createElement("div");
helperLayer.className = "introjs-helperLayer";
helperLayer.setAttribute("style", "width: " + (elementPosition.width + 10) + "px; " +
"height:" + (elementPosition.height + 10) + "px; " +
"top:" + (elementPosition.top - 5) + "px;" +
"left: " + (elementPosition.left - 5) + "px;");
document.body.appendChild(helperLayer);
helperNumberLayer.className = "introjs-helperNumberLayer";
tooltipLayer.className = "introjs-tooltip";
helperNumberLayer.innerHTML = targetElement.getAttribute("data-step");
tooltipLayer.innerHTML = "<div class='introjs-tooltiptext'>" + targetElement.getAttribute("data-intro") + "</div><div class='introjs-tooltipbuttons'></div>";
helperLayer.appendChild(helperNumberLayer);
helperLayer.appendChild(tooltipLayer);
var skipTooltipButton = document.createElement("a");
skipTooltipButton.className = "introjs-skipbutton";
skipTooltipButton.href = "javascript:void(0);";
skipTooltipButton.innerHTML = "Skip";
var nextTooltipButton = document.createElement("a");
nextTooltipButton.onclick = function() {
_nextStep.call(self);
};
nextTooltipButton.className = "introjs-nextbutton";
nextTooltipButton.href = "javascript:void(0);";
nextTooltipButton.innerHTML = "Next →";
skipTooltipButton.onclick = function() {
_exitIntro(self._targetElement);
};
var tooltipButtonsLayer = tooltipLayer.querySelector('.introjs-tooltipbuttons');
tooltipButtonsLayer.appendChild(skipTooltipButton);
tooltipButtonsLayer.appendChild(nextTooltipButton);
//set proper position
tooltipLayer.style.bottom = "-" + (_getOffset(tooltipLayer).height + 10) + "px";
}
//scroll the page to the element position
if(typeof(targetElement.scrollIntoViewIfNeeded) === "function") {
//awesome method guys: https://bugzilla.mozilla.org/show_bug.cgi?id=403510
//but I think this method has some problems with IE < 7.0, I should find a proper failover way
targetElement.scrollIntoViewIfNeeded();
}
}
/**
* Add overlay layer to the page
*
* @api private
* @method _addOverlayLayer
* @param {Object} targetElm
*/
function _addOverlayLayer(targetElm) {
var overlayLayer = document.createElement("div"),
styleText = "";
//set css class name
overlayLayer.className = "introjs-overlay";
//set overlay layer position
var elementPosition = _getOffset(targetElm);
if(elementPosition) {
styleText += "width: " + elementPosition.width + "px; height:" + elementPosition.height + "px; top:" + elementPosition.top + "px;left: " + elementPosition.left + "px;";
overlayLayer.setAttribute("style", styleText);
}
targetElm.appendChild(overlayLayer);
overlayLayer.onclick = function() {
_exitIntro(targetElm);
};
setTimeout(function() {
styleText += "opacity: .5;";
overlayLayer.setAttribute("style", styleText);
}, 10);
return true;
}
/**
* Get an element position on the page
* Thanks to `meouw`: http://stackoverflow.com/a/442474/375966
*
* @api private
* @method _getOffset
* @param {Object} element
* @returns Element's position info
*/
function _getOffset(element) {
var elementPosition = {};
//set width
elementPosition.width = element.offsetWidth;
//set height
elementPosition.height = element.offsetHeight;
//calculate element top and left
var _x = 0;
var _y = 0;
while(element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop)) {
_x += element.offsetLeft;
_y += element.offsetTop;
element = element.offsetParent;
}
//set top
elementPosition.top = _y;
//set left
elementPosition.left = _x;
return elementPosition;
}
var introJs = function (targetElm) {
if (typeof (targetElm) === "object") {
//Ok, create a new instance
return new IntroJs(targetElm);
} else if (typeof (targetElm) === "string") {
//select the target element with query selector
var targetElement = document.querySelector(targetElm);
if(targetElement) {
return new IntroJs(targetElement);
} else {
throw new Error("There's no element with given selector.");
}
} else {
return new IntroJs(document.body);
}
};
/**
* Current IntroJs version
*
* @property version
* @type String
*/
introJs.version = VERSION;
//Prototype
introJs.fn = IntroJs.prototype = {
clone: function () {
return IntroJs(this);
},
start: function () {
return _introForElement.call(this, this._targetElement);
}
};
this['introJs'] = introJs;
})();