This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
showcaser.js
372 lines (368 loc) · 18.1 KB
/
showcaser.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
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Showcaser = factory());
}(this, (function () { 'use strict';
function __$styleInject(css, returnValue) {
if (typeof document === 'undefined') {
return returnValue;
}
css = css || '';
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
return returnValue;
}
__$styleInject(".showcaser-trap-scroll{overflow:hidden}.showcaser-container{position:fixed;opacity:0;top:0;left:0;bottom:0;right:0;z-index:9999999}.showcaser-container.showcaser-container-delay{-webkit-transition:opacity .25s ease-in-out;transition:opacity .25s ease-in-out}.showcaser{position:absolute;border:1px solid #fff}.showcaser.showcaser-full-screen{width:0;height:0;left:50%;top:50%;border:none}.showcaser-text-container{position:absolute;width:32em;max-width:75vw}.showcaser-text-container.right{left:100%;padding-left:2em}.showcaser-text-container.center{left:50%;-webkit-transform:translateX(-50%);transform:translateX(-50%);text-align:center}.showcaser-text-container.left{text-align:right;right:100%;padding-right:2em}.showcaser-text-container.bottom{top:100%;padding-top:2em}.showcaser-text-container.top{top:0;-webkit-transform:translateY(-100%);transform:translateY(-100%);padding-bottom:1em}.showcaser-text-container.middle{top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%)}.showcaser-text-container.center.middle{top:50%;left:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%)}.showcaser-text-container.center.top{top:0;left:50%;-webkit-transform:translate(-50%,-100%);transform:translate(-50%,-100%)}.showcaser.showcaser-full-screen .showcaser-text-container{top:50%;left:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);text-align:center}.showcaser-text{color:#fff;font-size:1.2em;letter-spacing:.05em}.showcaser-button-container{position:relative;margin-top:1.5em}.showcaser-button{background:0 0;border:none;border-radius:2px;color:#fff;position:relative;height:2.25em;line-height:2.25em;margin:0;min-width:4em;padding:0 1em;display:inline-block;font-family:Roboto,sans-serif;font-size:1em;font-weight:500;text-transform:uppercase;letter-spacing:0;overflow:hidden;will-change:box-shadow;-webkit-transition:box-shadow .2s cubic-bezier(.4,0,1,1),background-color .2s cubic-bezier(.4,0,.2,1),color .2s cubic-bezier(.4,0,.2,1);transition:box-shadow .2s cubic-bezier(.4,0,1,1),background-color .2s cubic-bezier(.4,0,.2,1),color .2s cubic-bezier(.4,0,.2,1);outline:none;cursor:pointer;text-decoration:none;text-align:center;vertical-align:middle}.showcaser-button:hover{background-color:hsla(0,0%,62%,.2)}.showcaser-button:focus:not(:active){background-color:rgba(0,0,0,.12)}.showcaser-button:active{background-color:hsla(0,0%,62%,.4)}.showcaser-skip{color:#fff;cursor:pointer;left:100%;bottom:.3em;margin-left:2em;white-space:nowrap;line-height:1.5;display:inline;font-size:.9em;vertical-align:middle}.showcaser-text-container.left .showcaser-skip{right:100%;left:auto}",undefined);
// import Smoothscroll from "./smoothscroll";
var Showcaser = (function () {
function Showcaser() {
}
Showcaser.showcase = function (text, element, options) {
var args = this._sanitizeArgs(element, text, options);
if (!args) {
// Sanitizing failed. Can't continue on with this part of the showcase
throw new Error("Can't start Showcaser due to invalid options");
}
if (this._isVisible || this._showcaseQueue.length) {
this._showcaseQueue.push(args);
}
else {
this._startShowcase(args);
}
};
Showcaser.close = function () {
this._cancelCheckPositionInterval();
if (this._container) {
document.body.removeChild(this._container);
}
this._releaseTrappedBodyScroll();
if (this._args.options.close) {
this._args.options.close();
}
this._args = null;
this._container = null;
this._showcaser = null;
this._textContainer = null;
this._isVisible = false;
this._lastPosition = null;
if (this._showcaseQueue.length) {
var nextItem = this._showcaseQueue.shift();
this._startShowcase(nextItem);
}
};
Showcaser.skipAll = function () {
// Clear out all remaining queued Showcaser items
this._showcaseQueue = [];
// Args gets cleared out in close() so we need to keep a reference to the skip callback function
var skip = this._args.options.skip;
this.close();
if (skip) {
skip();
}
};
Object.defineProperty(Showcaser, "queueLength", {
get: function () {
return this._showcaseQueue.length;
},
enumerable: true,
configurable: true
});
Showcaser._start = function (args) {
var _this = this;
this._args = args;
this._isVisible = true;
// Create all the DOM necessary to display the showcase
var container = document.createElement("div");
container.className = "showcaser-container";
if (args.options.fadeBackground) {
container.className += " showcaser-container-delay";
}
var showcaser = document.createElement("div");
showcaser.className = "showcaser";
if (!args.element) {
showcaser.className += " showcaser-full-screen";
}
var backgroundColor = args.options.backgroundColor;
var shadowRGBA = "rgba(" + backgroundColor.r + "," + backgroundColor.g + "," + backgroundColor.b + "," + backgroundColor.a + ")";
showcaser.style.boxShadow = "0 0 0 99999px " + shadowRGBA + ", inset 0 2px 16px rgba(0,0,0,.3)";
container.appendChild(showcaser);
var textContainer = document.createElement("div");
showcaser.appendChild(textContainer);
var textElement = document.createElement("div");
textElement.className = "showcaser-text";
textElement.innerHTML = args.text;
textContainer.appendChild(textElement);
var buttonContainer = document.createElement("div");
buttonContainer.className = "showcaser-button-container";
textContainer.appendChild(buttonContainer);
var showcaserButton = document.createElement("button");
showcaserButton.className = "showcaser-button waves-effect waves-light btn";
showcaserButton.textContent = args.options.buttonText;
showcaserButton.onclick = function (event) { _this._nextButtonClick(event); };
buttonContainer.appendChild(showcaserButton);
if (args.options.allowSkip) {
var skipButton = document.createElement("div");
skipButton.className = "showcaser-skip";
skipButton.onclick = function (event) { _this._skipClick(event); };
skipButton.textContent = args.options.skipText || "Skip";
buttonContainer.appendChild(skipButton);
}
// Adding a border-radius breaks the box-shadow background on Safari for handheld/tablet and Mac
if (!this._isSafari()) {
// TODO: Use CSS class instead of inline style
var radiusString = (args.options.shape === "circle" ? "50%" : "5px");
showcaser.style.borderRadius = radiusString;
}
document.body.appendChild(container);
this._container = container;
this._showcaser = showcaser;
this._textContainer = textContainer;
this._trapBodyScroll();
this._scrollAndEnable();
};
Showcaser._isSafari = function () {
return navigator.vendor && navigator.vendor.indexOf("Apple") > -1 &&
navigator.userAgent && !navigator.userAgent.match("CriOS");
};
Showcaser._setupCheckPositionInterval = function () {
var _this = this;
if (this._args.options.positionTracker && this._args.element && !this._checkPositionIntervalToken) {
this._checkPositionIntervalToken = setInterval(function () {
if (_this._isScrolling) {
return;
}
var element = _this._args.element;
if (!element || !_this._isElementVisible(element)) {
_this.close();
return;
}
var newPosition = _this._getElementViewportPosition(element);
var lastPosition = _this._lastPosition;
var newViewportWidth = document.body.clientWidth;
var newViewportHeight = document.body.clientHeight;
// Check if size or position of element has changed since we last re-rendered
if (newPosition.top !== lastPosition.top ||
newPosition.right !== lastPosition.right ||
newPosition.bottom !== lastPosition.bottom ||
newPosition.left !== lastPosition.left ||
newViewportWidth !== _this._lastViewportWidth ||
newViewportHeight !== _this._lastViewportHeight) {
_this._scrollAndEnable();
}
}, 200);
}
};
Showcaser._cancelCheckPositionInterval = function () {
if (this._checkPositionIntervalToken) {
clearInterval(this._checkPositionIntervalToken);
this._checkPositionIntervalToken = null;
}
};
Showcaser._isElementVisible = function (elem) {
return !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length);
};
Showcaser._getElementViewportPosition = function (elem) {
return elem.getBoundingClientRect();
};
Showcaser._getElementDocumentPosition = function (element) {
var rect = element.getBoundingClientRect();
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return { top: rect.top + scrollTop, left: rect.left + scrollLeft };
};
Showcaser._scrollAndEnable = function () {
if (this._args.element) {
var element = this._args.element;
// Position relative to the viewport
var rect = this._getElementViewportPosition(element);
// Make sure element is visible
if (!this._isElementVisible(element)) {
console.warn("Showcaser: Element not visible", this._args.element);
this.close();
return;
}
var windowScrollPosition = window.pageYOffset;
var bufferPx = this._args.options.scrollBufferPx || 15;
var isElAboveViewport = rect.top - bufferPx < 0;
var isElBelowViewport = rect.top + bufferPx + rect.height >
(window.innerHeight || document.documentElement.clientHeight);
// Check if we need to scroll page to be able to see the element. Scroll if necessary
// We check the page's scroll position as we use a 'scrollBuffer' to give additional space
// between the element and the viewport. If the page can't be scrolled,
// there's no reason to wait 500ms.
if (isElAboveViewport && isElBelowViewport) {
// Overflows above and below viewport ¯\_(ツ)_/¯
this._positionAndShow();
}
else if (isElAboveViewport) {
// this._isScrolling = true;
var elPosition = this._getElementDocumentPosition(element);
var scrollTo_1 = Math.max(elPosition.top - bufferPx, 0);
// Smoothscroll(scrollAmount, 500, () => {
// this._isScrolling = false;
// this._positionAndShow();
// });
window.scrollTo(0, scrollTo_1);
this._positionAndShow();
}
else if (isElBelowViewport) {
// this._isScrolling = true;
var elPosition = this._getElementDocumentPosition(element);
var scrollTo_2 = Math.max(elPosition.top - bufferPx, 0);
// Smoothscroll(scrollAmount, 500, () => {
// this._isScrolling = false;
// this._positionAndShow();
// });
window.scrollTo(0, scrollTo_2);
this._positionAndShow();
}
else {
// Fully visible
this._positionAndShow();
}
}
else {
this._positionAndShow();
}
};
Showcaser._positionAndShow = function () {
// If the user closes the showcase before scroll happens (delayed by a timeout)
// then args are gonna be null as the showcase has already been closed
if (!this._args) {
return;
}
var options = this._args.options;
var element = this._args.element;
if (element) {
var rect = this._lastPosition = this._getElementViewportPosition(element);
this._lastViewportWidth = document.body.clientWidth;
this._lastViewportHeight = document.body.clientHeight;
// Element has no dimensions
if (!this._isElementVisible(element)) {
console.warn("Showcaser: Element not visible", this._args.element);
this.close();
return;
}
var padding = typeof options.paddingPx !== "undefined" ? options.paddingPx : 20;
if (options.shape === "circle") {
var dimension = Math.round(rect.width > rect.height ? rect.width : rect.height) + padding;
this._showcaser.style.width = dimension + "px";
this._showcaser.style.height = dimension + "px";
var diff = rect.width > rect.height ? rect.width - rect.height : rect.height - rect.width;
// Height has changed, subtract the diff from top
if (rect.width > rect.height) {
this._showcaser.style.top = Math.round(rect.top - diff / 2 - padding / 2) + "px";
this._showcaser.style.left = Math.round(rect.left - padding / 2) + "px";
}
else {
this._showcaser.style.top = Math.round(rect.top - padding / 2) + "px";
this._showcaser.style.left = Math.round(rect.left - diff / 2 - padding / 2) + "px";
}
}
else if (options.shape === "rectangle") {
this._showcaser.style.width = Math.round(rect.width + padding) + "px";
this._showcaser.style.height = Math.round(rect.height + padding) + "px";
this._showcaser.style.top = Math.round(rect.top - padding / 2) + "px";
this._showcaser.style.left = Math.round(rect.left - padding / 2) + "px";
}
}
// User-defined text position
if (options.position) {
var horizontal = options.position.horizontal || "right";
var vertical = options.position.vertical || "middle";
this._applyPositionStyling(vertical, horizontal);
}
else {
// Apply default positioning, before checking for bleed
var vertical = "top";
var horizontal = "center";
this._applyPositionStyling(vertical, horizontal);
var textElRect = this._getElementViewportPosition(this._textContainer);
// Vertical check: bleeds out from the top
if (textElRect.top < 0) {
vertical = "bottom";
}
// Horizontal check: bleeds out from the right
if (textElRect.left + textElRect.width > document.body.clientWidth) {
horizontal = "left";
}
else if (textElRect.left < 0) {
horizontal = "right";
}
this._applyPositionStyling(vertical, horizontal);
}
this._container.style.opacity = "1";
this._setupCheckPositionInterval();
};
Showcaser._applyPositionStyling = function (vertical, horizontal) {
this._textContainer.className = "showcaser-text-container " + vertical + " " + horizontal;
};
Showcaser._trapBodyScroll = function () {
document.body.className += " showcaser-trap-scroll";
};
Showcaser._releaseTrappedBodyScroll = function () {
document.body.className = document.body.className.replace(/(?:^|\s)showcaser-trap-scroll(?!\S)/g, "");
};
Showcaser._startShowcase = function (args) {
if (args.options.before) {
args.options.before();
}
this._start(args);
};
Showcaser._nextButtonClick = function (event) {
this.close();
event.stopPropagation();
return false;
};
Showcaser._skipClick = function (event) {
this.skipAll();
event.stopPropagation();
return false;
};
Showcaser._sanitizeArgs = function (element, text, options) {
if (!text) {
throw new Error("Must specify text to showcase");
}
if (!options) {
options = {};
}
if (!options.backgroundColor) {
options.backgroundColor = {
r: 0,
g: 0,
b: 0,
};
}
if (!options.backgroundColor.a) {
options.backgroundColor.a = 0.7;
}
if (typeof options.fadeBackground !== "boolean") {
options.fadeBackground = true;
}
if (!options.shape) {
options.shape = "circle";
}
if (!options.buttonText) {
options.buttonText = "Got it";
}
if (typeof options.allowSkip !== "boolean") {
options.allowSkip = true;
}
return {
element: element,
text: text,
options: options,
};
};
return Showcaser;
}());
Showcaser._showcaseQueue = [];
return Showcaser;
})));