Skip to content

Commit 69d2677

Browse files
authored
Merge pull request usablica#761 from usablica/bozdoz-master
Clean up NPM Package
2 parents 55f6bb7 + cfb1acd commit 69d2677

File tree

2 files changed

+80
-34
lines changed

2 files changed

+80
-34
lines changed

intro.js

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,32 @@
55
* Copyright (C) 2017 Afshin Mehrabani (@afshinmeh)
66
*/
77

8-
(function (root, factory) {
9-
if (typeof exports === 'object') {
10-
// CommonJS
11-
factory(exports);
12-
} else if (typeof define === 'function' && define.amd) {
13-
// AMD. Register as an anonymous module.
14-
define(['exports'], factory);
15-
} else {
16-
// Browser globals
17-
factory(root);
18-
}
19-
} (this, function (exports) {
8+
(function(f) {
9+
if (typeof exports === "object" && typeof module !== "undefined") {
10+
module.exports = f();
11+
// deprecated function
12+
// @since 2.8.0
13+
module.exports.introJs = function () {
14+
console.warn('Deprecated: please use require("intro.js") directly, instead of the introJs method of the function');
15+
// introJs()
16+
return f().apply(this, arguments);
17+
};
18+
} else if (typeof define === "function" && define.amd) {
19+
define([], f);
20+
} else {
21+
var g;
22+
if (typeof window !== "undefined") {
23+
g = window;
24+
} else if (typeof global !== "undefined") {
25+
g = global;
26+
} else if (typeof self !== "undefined") {
27+
g = self;
28+
} else {
29+
g = this;
30+
}
31+
g.introJs = f();
32+
}
33+
})(function () {
2034
//Default config/variables
2135
var VERSION = '2.8.0-alpha.1';
2236

@@ -276,10 +290,10 @@
276290
} else if (code === 'Enter' || code === 13) {
277291
//srcElement === ie
278292
var target = e.target || e.srcElement;
279-
if (target && target.className.indexOf('introjs-prevbutton') > 0) {
293+
if (target && target.className.match('introjs-prevbutton')) {
280294
//user hit enter while focusing on previous button
281295
_previousStep.call(self);
282-
} else if (target && target.className.indexOf('introjs-skipbutton') > 0) {
296+
} else if (target && target.className.match('introjs-skipbutton')) {
283297
//user hit enter while focusing on skip button
284298
if (self._introItems.length - 1 == self._currentStep && typeof (self._introCompleteCallback) === 'function') {
285299
self._introCompleteCallback.call(self);
@@ -537,7 +551,7 @@
537551
var fixParents = document.querySelectorAll('.introjs-fixParent');
538552
if (fixParents && fixParents.length > 0) {
539553
for (var i = fixParents.length - 1; i >= 0; i--) {
540-
fixParents[i].className = fixParents[i].className.replace(/introjs-fixParent/g, '').replace(/^\s+|\s+$/g, '');
554+
_removeClass(fixParents[i], /introjs-fixParent/g);
541555
}
542556
}
543557

@@ -842,9 +856,9 @@
842856
// Otherwise, remove a fixed class that may be left over from the previous
843857
// step.
844858
if (_isFixed(currentElement.element)) {
845-
helperLayer.className += ' introjs-fixedTooltip';
859+
_addClass(helperLayer, 'introjs-fixedTooltip');
846860
} else {
847-
helperLayer.className = helperLayer.className.replace(' introjs-fixedTooltip', '');
861+
_removeClass(helperLayer, 'introjs-fixedTooltip');
848862
}
849863

850864
if (currentElement.position == 'floating') {
@@ -947,7 +961,7 @@
947961
var fixParents = document.querySelectorAll('.introjs-fixParent');
948962
if (fixParents && fixParents.length > 0) {
949963
for (var i = fixParents.length - 1; i >= 0; i--) {
950-
fixParents[i].className = fixParents[i].className.replace(/introjs-fixParent/g, '').replace(/^\s+|\s+$/g, '');
964+
_removeClass(fixParents[i], /introjs-fixParent/g);
951965
};
952966
}
953967

@@ -1178,7 +1192,7 @@
11781192
prevTooltipButton.className = 'introjs-button introjs-prevbutton introjs-hidden';
11791193
}
11801194
if (typeof nextTooltipButton !== "undefined" && nextTooltipButton != null) {
1181-
nextTooltipButton.className += ' introjs-fullbutton';
1195+
_addClass(nextTooltipButton, 'introjs-fullbutton');
11821196
}
11831197
} else {
11841198
if (typeof prevTooltipButton !== "undefined" && prevTooltipButton != null) {
@@ -1197,7 +1211,7 @@
11971211
if (typeof skipTooltipButton !== "undefined" && skipTooltipButton != null) {
11981212
skipTooltipButton.innerHTML = this._options.doneLabel;
11991213
// adding donebutton class in addition to skipbutton
1200-
skipTooltipButton.className += ' introjs-donebutton';
1214+
_addClass(skipTooltipButton, 'introjs-donebutton');
12011215
}
12021216
if (typeof prevTooltipButton !== "undefined" && prevTooltipButton != null) {
12031217
prevTooltipButton.className = 'introjs-button introjs-prevbutton';
@@ -1208,7 +1222,7 @@
12081222
nextTooltipButton.className = 'introjs-button introjs-nextbutton introjs-hidden';
12091223
}
12101224
if (typeof prevTooltipButton !== "undefined" && prevTooltipButton != null) {
1211-
prevTooltipButton.className += ' introjs-fullbutton';
1225+
_addClass(prevTooltipButton, 'introjs-fullbutton');
12121226
}
12131227
} else {
12141228
if (typeof nextTooltipButton !== "undefined" && nextTooltipButton != null) {
@@ -1321,22 +1335,21 @@
13211335
if (!parentElm.tagName || parentElm.tagName.toLowerCase() === 'body') break;
13221336

13231337
if (parentElm.tagName.toLowerCase() === 'svg') {
1324-
_setClass(parentElm, 'introjs-showElement introjs-relativePosition');
1338+
_addClass(parentElm, 'introjs-showElement introjs-relativePosition');
13251339
}
13261340

13271341
parentElm = parentElm.parentNode;
13281342
}
13291343
}
13301344

1331-
_setClass(targetElement.element, 'introjs-showElement');
1345+
_addClass(targetElement.element, 'introjs-showElement');
13321346

13331347
var currentElementPosition = _getPropValue(targetElement.element, 'position');
13341348
if (currentElementPosition !== 'absolute' &&
13351349
currentElementPosition !== 'relative' &&
13361350
currentElementPosition !== 'fixed') {
13371351
//change to new intro item
1338-
//targetElement.element.className += ' introjs-relativePosition';
1339-
_setClass(targetElement.element, 'introjs-relativePosition')
1352+
_addClass(targetElement.element, 'introjs-relativePosition')
13401353
}
13411354

13421355
var parentElm = targetElement.element.parentNode;
@@ -1349,23 +1362,51 @@
13491362
var opacity = parseFloat(_getPropValue(parentElm, 'opacity'));
13501363
var transform = _getPropValue(parentElm, 'transform') || _getPropValue(parentElm, '-webkit-transform') || _getPropValue(parentElm, '-moz-transform') || _getPropValue(parentElm, '-ms-transform') || _getPropValue(parentElm, '-o-transform');
13511364
if (/[0-9]+/.test(zIndex) || opacity < 1 || (transform !== 'none' && transform !== undefined)) {
1352-
parentElm.className += ' introjs-fixParent';
1365+
_addClass(parentElm, 'introjs-fixParent');
13531366
}
13541367

13551368
parentElm = parentElm.parentNode;
13561369
}
13571370
}
13581371

1359-
function _setClass(element, className) {
1372+
/**
1373+
* Append a class to an element
1374+
*
1375+
* @api private
1376+
* @method _addClass
1377+
* @param {Object} element
1378+
* @param {String} className
1379+
* @returns null
1380+
*/
1381+
function _addClass(element, className) {
13601382
if (element instanceof SVGElement) {
1383+
// svg
13611384
var pre = element.getAttribute('class') || '';
13621385

13631386
element.setAttribute('class', pre + ' ' + className);
13641387
} else {
1365-
element.className += ' ' + className;
1388+
if (element.classList !== undefined) {
1389+
// check for modern classList property
1390+
var classes = className.split(' ');
1391+
for (var i = 0, len = classes.length; i < len; i++) {
1392+
element.classList.add( classes[i] );
1393+
}
1394+
} else if (!element.className.match( className )) {
1395+
// check if element doesn't already have className
1396+
element.className += ' ' + className;
1397+
}
13661398
}
13671399
}
13681400

1401+
/**
1402+
* Remove a class from an element
1403+
*
1404+
* @api private
1405+
* @method _removeClass
1406+
* @param {Object} element
1407+
* @param {RegExp|String} classNameRegex can be regex or string
1408+
* @returns null
1409+
*/
13691410
function _removeClass(element, classNameRegex) {
13701411
if (element instanceof SVGElement) {
13711412
var pre = element.getAttribute('class') || '';
@@ -1618,7 +1659,7 @@
16181659
var hint = this._targetElement.querySelector('.introjs-hint[data-step="' + stepId + '"]');
16191660

16201661
if (hint) {
1621-
hint.className += ' introjs-hidehint';
1662+
_addClass(hint, 'introjs-hidehint');
16221663
}
16231664

16241665
// call the callback function (if any)
@@ -1671,7 +1712,7 @@
16711712
var hint = this._targetElement.querySelector('.introjs-hint[data-step="' + stepId + '"]');
16721713

16731714
if (hint) {
1674-
hint.className = hint.className.replace(/introjs\-hidehint/g, '');
1715+
_removeClass(hint, /introjs-hidehint/g);
16751716
}
16761717
};
16771718

@@ -1750,12 +1791,12 @@
17501791
hint.className = 'introjs-hint';
17511792

17521793
if (!item.hintAnimation) {
1753-
hint.className += ' introjs-hint-no-anim';
1794+
_addClass(hint, 'introjs-hint-no-anim');
17541795
}
17551796

17561797
// hint's position should be fixed if the target element's position is fixed
17571798
if (_isFixed(item.element)) {
1758-
hint.className += ' introjs-fixedhint';
1799+
_addClass(hint, 'introjs-fixedhint');
17591800
}
17601801

17611802
var hintDot = document.createElement('div');
@@ -2189,6 +2230,5 @@
21892230
}
21902231
};
21912232

2192-
exports.introJs = introJs;
21932233
return introJs;
2194-
}));
2234+
});

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
"node >=0.1.90"
1616
],
1717
"main": "intro.js",
18+
"files": [
19+
"*.js",
20+
"*.css",
21+
"license.md",
22+
"themes/*"
23+
],
1824
"license": "AGPL-3.0",
1925
"spm": {
2026
"main": "intro.js",

0 commit comments

Comments
 (0)