/** * Intro.js v0.5.0 * https://github.com/usablica/intro.js * MIT licensed * * Copyright (C) 2013 usabli.ca - A weekend project by Afshin Mehrabani (@afshinmeh) */ (function (root, factory) { if (typeof exports === 'object') { // CommonJS factory(exports); } else if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['exports'], factory); } else { // Browser globals factory(root); } } (this, function (exports) { //Default config/variables var VERSION = '0.5.0'; /** * IntroJs main class * * @class IntroJs */ function IntroJs(obj) { this._targetElement = obj; this._options = { /* Next button label in tooltip box */ nextLabel: 'Next →', /* Previous button label in tooltip box */ prevLabel: '← Back', /* Skip button label in tooltip box */ skipLabel: 'Skip', /* Done button label in tooltip box */ doneLabel: 'Done', /* Default tooltip box position */ tooltipPosition: 'bottom', /* Next CSS class for tooltip boxes */ tooltipClass: '', /* Close introduction when pressing Escape button? */ exitOnEsc: true, /* Close introduction when clicking on overlay layer? */ exitOnOverlayClick: true, /* Show step numbers in introduction? */ showStepNumbers: true }; } /** * 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 introItems = [], self = this; if (this._options.steps) { //use steps passed programmatically var allIntroSteps = []; for (var i = 0, stepsLength = this._options.steps.length; i < stepsLength; i++) { var currentItem = this._options.steps[i]; //set the step currentItem.step = i + 1; //use querySelector function only when developer used CSS selector if (typeof(currentItem.element) === 'string') { //grab the element with given selector from the page currentItem.element = document.querySelector(currentItem.element); } introItems.push(currentItem); } } else { //use steps from data-* annotations var allIntroSteps = targetElm.querySelectorAll('*[data-intro]'); //if there's no element to intro if (allIntroSteps.length < 1) { return false; } 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'), 10), tooltipClass: currentElement.getAttribute('data-tooltipClass'), position: currentElement.getAttribute('data-position') || this._options.tooltipPosition }); } } //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.call(self, targetElm)) { //then, start the show _nextStep.call(self); var skipButton = targetElm.querySelector('.introjs-skipbutton'), nextStepButton = targetElm.querySelector('.introjs-nextbutton'); self._onKeyDown = function(e) { if (e.keyCode === 27 && self._options.exitOnEsc == true) { //escape key pressed, exit the intro _exitIntro.call(self, targetElm); //check if any callback is defined if (self._introExitCallback != undefined) { self._introExitCallback.call(self); } } else if(e.keyCode === 37) { //left arrow _previousStep.call(self); } else if (e.keyCode === 39 || e.keyCode === 13) { //right arrow or enter _nextStep.call(self); //prevent default behaviour on hitting Enter, to prevent steps being skipped in some browsers if(e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } } }; self._onResize = function(e) { _setHelperLayerPosition.call(self, document.querySelector('.introjs-helperLayer')); }; if (window.addEventListener) { window.addEventListener('keydown', self._onKeyDown, true); //for window resize window.addEventListener("resize", self._onResize, true); } else if (document.attachEvent) { //IE document.attachEvent('onkeydown', self._onKeyDown); //for window resize document.attachEvent("onresize", self._onResize); } } return false; } /** * Go to specific step of introduction * * @api private * @method _goToStep */ function _goToStep(step) { //because steps starts with zero this._currentStep = step - 2; if(typeof (this._introItems) !== 'undefined') { _nextStep.call(this); } } /** * Go to next step on intro * * @api private * @method _nextStep */ function _nextStep() { if (typeof (this._currentStep) === 'undefined') { this._currentStep = 0; } else { ++this._currentStep; } if((this._introItems.length) <= this._currentStep) { //end of the intro //check if any callback is defined if (typeof (this._introCompleteCallback) === 'function') { this._introCompleteCallback.call(this); } _exitIntro.call(this, this._targetElement); return; } var nextStep = this._introItems[this._currentStep]; if (typeof (this._introBeforeChangeCallback) !== 'undefined') { this._introBeforeChangeCallback.call(this, nextStep.element); } _showElement.call(this, nextStep); } /** * Go to previous step on intro * * @api private * @method _nextStep */ function _previousStep() { if (this._currentStep === 0) { return false; } var nextStep = this._introItems[--this._currentStep]; if (typeof (this._introBeforeChangeCallback) !== 'undefined') { this._introBeforeChangeCallback.call(this, nextStep.element); } _showElement.call(this, nextStep); } /** * 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 () { if (overlayLayer.parentNode) { overlayLayer.parentNode.removeChild(overlayLayer); } }, 500); //remove all helper layers var helperLayer = targetElement.querySelector('.introjs-helperLayer'); if (helperLayer) { helperLayer.parentNode.removeChild(helperLayer); } //remove `introjs-showElement` class from the element var showElement = document.querySelector('.introjs-showElement'); if (showElement) { showElement.className = showElement.className.replace(/introjs-[a-zA-Z]+/g, '').replace(/^\s+|\s+$/g, ''); // This is a manual trim. } //remove `introjs-fixParent` class from the elements var fixParents = document.querySelectorAll('.introjs-fixParent'); if (fixParents && fixParents.length > 0) { for (var i = fixParents.length - 1; i >= 0; i--) { fixParents[i].className = fixParents[i].className.replace(/introjs-fixParent/g, '').replace(/^\s+|\s+$/g, ''); }; } //clean listeners if (window.removeEventListener) { window.removeEventListener('keydown', this._onKeyDown, true); } else if (document.detachEvent) { //IE document.detachEvent('onkeydown', this._onKeyDown); } //set the step to zero this._currentStep = undefined; } /** * Render tooltip box in the page * * @api private * @method _placeTooltip * @param {Object} targetElement * @param {Object} tooltipLayer * @param {Object} arrowLayer */ function _placeTooltip(targetElement, tooltipLayer, arrowLayer) { //reset the old style tooltipLayer.style.top = null; tooltipLayer.style.right = null; tooltipLayer.style.bottom = null; tooltipLayer.style.left = null; //prevent error when `this._currentStep` is undefined if(!this._introItems[this._currentStep]) return; var tooltipCssClass = ''; //if we have a custom css class for each step var currentStepObj = this._introItems[this._currentStep]; if (typeof (currentStepObj.tooltipClass) === 'string') { tooltipCssClass = currentStepObj.tooltipClass; } else { tooltipCssClass = this._options.tooltipClass; } tooltipLayer.className = ('introjs-tooltip ' + tooltipCssClass).replace(/^\s+|\s+$/g, ''); //custom css class for tooltip boxes var tooltipCssClass = this._options.tooltipClass; var currentTooltipPosition = this._introItems[this._currentStep].position; switch (currentTooltipPosition) { case 'top': tooltipLayer.style.left = '15px'; tooltipLayer.style.top = '-' + (_getOffset(tooltipLayer).height + 10) + 'px'; arrowLayer.className = 'introjs-arrow bottom'; break; case 'right': tooltipLayer.style.left = (_getOffset(targetElement).width + 20) + 'px'; arrowLayer.className = 'introjs-arrow left'; break; case 'left': tooltipLayer.style.top = '15px'; tooltipLayer.style.right = (_getOffset(targetElement).width + 20) + 'px'; arrowLayer.className = 'introjs-arrow right'; break; case 'bottom': // Bottom going to follow the default behavior default: tooltipLayer.style.bottom = '-' + (_getOffset(tooltipLayer).height + 10) + 'px'; arrowLayer.className = 'introjs-arrow top'; break; } } /** * Update the position of the helper layer on the screen * * @api private * @method _setHelperLayerPosition * @param {Object} helperLayer */ function _setHelperLayerPosition(helperLayer) { if(helperLayer) { //prevent error when `this._currentStep` in undefined if(!this._introItems[this._currentStep]) return; var elementPosition = _getOffset(this._introItems[this._currentStep].element); //set new position to helper layer helperLayer.setAttribute('style', 'width: ' + (elementPosition.width + 10) + 'px; ' + 'height:' + (elementPosition.height + 10) + 'px; ' + 'top:' + (elementPosition.top - 5) + 'px;' + 'left: ' + (elementPosition.left - 5) + 'px;'); } } /** * Show an element on the page * * @api private * @method _showElement * @param {Object} targetElement */ function _showElement(targetElement) { if (typeof (this._introChangeCallback) !== 'undefined') { this._introChangeCallback.call(this, targetElement.element); } var self = this, oldHelperLayer = document.querySelector('.introjs-helperLayer'), elementPosition = _getOffset(targetElement.element); if(oldHelperLayer != null) { var oldHelperNumberLayer = oldHelperLayer.querySelector('.introjs-helperNumberLayer'), oldtooltipLayer = oldHelperLayer.querySelector('.introjs-tooltiptext'), oldArrowLayer = oldHelperLayer.querySelector('.introjs-arrow'), oldtooltipContainer = oldHelperLayer.querySelector('.introjs-tooltip'), skipTooltipButton = oldHelperLayer.querySelector('.introjs-skipbutton'), prevTooltipButton = oldHelperLayer.querySelector('.introjs-prevbutton'), nextTooltipButton = oldHelperLayer.querySelector('.introjs-nextbutton'); //hide the tooltip oldtooltipContainer.style.opacity = 0; //set new position to helper layer _setHelperLayerPosition.call(self, oldHelperLayer); //remove `introjs-fixParent` class from the elements var fixParents = document.querySelectorAll('.introjs-fixParent'); if (fixParents && fixParents.length > 0) { for (var i = fixParents.length - 1; i >= 0; i--) { fixParents[i].className = fixParents[i].className.replace(/introjs-fixParent/g, '').replace(/^\s+|\s+$/g, ''); }; } //remove old classes var oldShowElement = document.querySelector('.introjs-showElement'); oldShowElement.className = oldShowElement.className.replace(/introjs-[a-zA-Z]+/g, '').replace(/^\s+|\s+$/g, ''); //we should wait until the CSS3 transition is competed (it's 0.3 sec) to prevent incorrect `height` and `width` calculation if (self._lastShowElementTimer) { clearTimeout(self._lastShowElementTimer); } self._lastShowElementTimer = setTimeout(function() { //set current step to the label if(oldHelperNumberLayer != null) { oldHelperNumberLayer.innerHTML = targetElement.step; } //set current tooltip text oldtooltipLayer.innerHTML = targetElement.intro; //set the tooltip position _placeTooltip.call(self, targetElement.element, oldtooltipContainer, oldArrowLayer); //show the tooltip oldtooltipContainer.style.opacity = 1; }, 350); } else { var helperLayer = document.createElement('div'), arrowLayer = document.createElement('div'), tooltipLayer = document.createElement('div'); helperLayer.className = 'introjs-helperLayer'; //set new position to helper layer _setHelperLayerPosition.call(self, helperLayer); //add helper layer to target element this._targetElement.appendChild(helperLayer); arrowLayer.className = 'introjs-arrow'; tooltipLayer.innerHTML = '