intro.js 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. /**
  2. * Intro.js v2.9.3
  3. * https://github.com/usablica/intro.js
  4. *
  5. * Copyright (C) 2017 Afshin Mehrabani (@afshinmeh)
  6. */
  7. (function(f) {
  8. if (typeof exports === "object" && typeof module !== "undefined") {
  9. module.exports = f();
  10. // deprecated function
  11. // @since 2.8.0
  12. module.exports.introJs = function () {
  13. console.warn('Deprecated: please use require("intro.js") directly, instead of the introJs method of the function');
  14. // introJs()
  15. return f().apply(this, arguments);
  16. };
  17. } else if (typeof define === "function" && define.amd) {
  18. define([], f);
  19. } else {
  20. var g;
  21. if (typeof window !== "undefined") {
  22. g = window;
  23. } else if (typeof global !== "undefined") {
  24. g = global;
  25. } else if (typeof self !== "undefined") {
  26. g = self;
  27. } else {
  28. g = this;
  29. }
  30. g.introJs = f();
  31. }
  32. })(function () {
  33. //Default config/variables
  34. var VERSION = '2.9.3';
  35. /**
  36. * IntroJs main class
  37. *
  38. * @class IntroJs
  39. */
  40. function IntroJs(obj) {
  41. this._targetElement = obj;
  42. this._introItems = [];
  43. this._options = {
  44. /* Next button label in tooltip box */
  45. nextLabel: 'Next →',
  46. /* Previous button label in tooltip box */
  47. prevLabel: '← Back',
  48. /* Skip button label in tooltip box */
  49. skipLabel: 'Skip',
  50. /* Done button label in tooltip box */
  51. doneLabel: 'Done',
  52. /* Hide previous button in the first step? Otherwise, it will be disabled button. */
  53. hidePrev: false,
  54. /* Hide next button in the last step? Otherwise, it will be disabled button. */
  55. hideNext: false,
  56. /* Default tooltip box position */
  57. tooltipPosition: 'bottom',
  58. /* Next CSS class for tooltip boxes */
  59. tooltipClass: '',
  60. /* CSS class that is added to the helperLayer */
  61. highlightClass: '',
  62. /* Close introduction when pressing Escape button? */
  63. exitOnEsc: true,
  64. /* Close introduction when clicking on overlay layer? */
  65. exitOnOverlayClick: true,
  66. /* Show step numbers in introduction? */
  67. showStepNumbers: true,
  68. /* Let user use keyboard to navigate the tour? */
  69. keyboardNavigation: true,
  70. /* Show tour control buttons? */
  71. showButtons: true,
  72. /* Show tour bullets? */
  73. showBullets: true,
  74. /* Show tour progress? */
  75. showProgress: false,
  76. /* Scroll to highlighted element? */
  77. scrollToElement: true,
  78. /*
  79. * Should we scroll the tooltip or target element?
  80. *
  81. * Options are: 'element' or 'tooltip'
  82. */
  83. scrollTo: 'element',
  84. /* Padding to add after scrolling when element is not in the viewport (in pixels) */
  85. scrollPadding: 30,
  86. /* Set the overlay opacity */
  87. overlayOpacity: 0.8,
  88. /* Precedence of positions, when auto is enabled */
  89. positionPrecedence: ["bottom", "top", "right", "left"],
  90. /* Disable an interaction with element? */
  91. disableInteraction: false,
  92. /* Set how much padding to be used around helper element */
  93. helperElementPadding: 10,
  94. /* Default hint position */
  95. hintPosition: 'top-middle',
  96. /* Hint button label */
  97. hintButtonLabel: 'Got it',
  98. /* Adding animation to hints? */
  99. hintAnimation: true,
  100. /* additional classes to put on the buttons */
  101. buttonClass: "introjs-button"
  102. };
  103. }
  104. /**
  105. * Initiate a new introduction/guide from an element in the page
  106. *
  107. * @api private
  108. * @method _introForElement
  109. * @param {Object} targetElm
  110. * @param {String} group
  111. * @returns {Boolean} Success or not?
  112. */
  113. function _introForElement(targetElm, group) {
  114. var allIntroSteps = targetElm.querySelectorAll("*[data-intro]"),
  115. introItems = [];
  116. if (this._options.steps) {
  117. //use steps passed programmatically
  118. _forEach(this._options.steps, function (step) {
  119. var currentItem = _cloneObject(step);
  120. //set the step
  121. currentItem.step = introItems.length + 1;
  122. //use querySelector function only when developer used CSS selector
  123. if (typeof (currentItem.element) === 'string') {
  124. //grab the element with given selector from the page
  125. currentItem.element = document.querySelector(currentItem.element);
  126. }
  127. //intro without element
  128. if (typeof (currentItem.element) === 'undefined' || currentItem.element === null) {
  129. var floatingElementQuery = document.querySelector(".introjsFloatingElement");
  130. if (floatingElementQuery === null) {
  131. floatingElementQuery = document.createElement('div');
  132. floatingElementQuery.className = 'introjsFloatingElement';
  133. document.body.appendChild(floatingElementQuery);
  134. }
  135. currentItem.element = floatingElementQuery;
  136. currentItem.position = 'floating';
  137. }
  138. currentItem.scrollTo = currentItem.scrollTo || this._options.scrollTo;
  139. if (typeof (currentItem.disableInteraction) === 'undefined') {
  140. currentItem.disableInteraction = this._options.disableInteraction;
  141. }
  142. if (currentItem.element !== null) {
  143. introItems.push(currentItem);
  144. }
  145. }.bind(this));
  146. } else {
  147. //use steps from data-* annotations
  148. var elmsLength = allIntroSteps.length;
  149. var disableInteraction;
  150. //if there's no element to intro
  151. if (elmsLength < 1) {
  152. return false;
  153. }
  154. _forEach(allIntroSteps, function (currentElement) {
  155. // PR #80
  156. // start intro for groups of elements
  157. if (group && (currentElement.getAttribute("data-intro-group") !== group)) {
  158. return;
  159. }
  160. // skip hidden elements
  161. if (currentElement.style.display === 'none') {
  162. return;
  163. }
  164. var step = parseInt(currentElement.getAttribute('data-step'), 10);
  165. if (typeof (currentElement.getAttribute('data-disable-interaction')) !== 'undefined') {
  166. disableInteraction = !!currentElement.getAttribute('data-disable-interaction');
  167. } else {
  168. disableInteraction = this._options.disableInteraction;
  169. }
  170. if (step > 0) {
  171. introItems[step - 1] = {
  172. element: currentElement,
  173. intro: currentElement.getAttribute('data-intro'),
  174. step: parseInt(currentElement.getAttribute('data-step'), 10),
  175. tooltipClass: currentElement.getAttribute('data-tooltipclass'),
  176. highlightClass: currentElement.getAttribute('data-highlightclass'),
  177. position: currentElement.getAttribute('data-position') || this._options.tooltipPosition,
  178. scrollTo: currentElement.getAttribute('data-scrollto') || this._options.scrollTo,
  179. disableInteraction: disableInteraction
  180. };
  181. }
  182. }.bind(this));
  183. //next add intro items without data-step
  184. //todo: we need a cleanup here, two loops are redundant
  185. var nextStep = 0;
  186. _forEach(allIntroSteps, function (currentElement) {
  187. // PR #80
  188. // start intro for groups of elements
  189. if (group && (currentElement.getAttribute("data-intro-group") !== group)) {
  190. return;
  191. }
  192. if (currentElement.getAttribute('data-step') === null) {
  193. while (true) {
  194. if (typeof introItems[nextStep] === 'undefined') {
  195. break;
  196. } else {
  197. nextStep++;
  198. }
  199. }
  200. if (typeof (currentElement.getAttribute('data-disable-interaction')) !== 'undefined') {
  201. disableInteraction = !!currentElement.getAttribute('data-disable-interaction');
  202. } else {
  203. disableInteraction = this._options.disableInteraction;
  204. }
  205. introItems[nextStep] = {
  206. element: currentElement,
  207. intro: currentElement.getAttribute('data-intro'),
  208. step: nextStep + 1,
  209. tooltipClass: currentElement.getAttribute('data-tooltipclass'),
  210. highlightClass: currentElement.getAttribute('data-highlightclass'),
  211. position: currentElement.getAttribute('data-position') || this._options.tooltipPosition,
  212. scrollTo: currentElement.getAttribute('data-scrollto') || this._options.scrollTo,
  213. disableInteraction: disableInteraction
  214. };
  215. }
  216. }.bind(this));
  217. }
  218. //removing undefined/null elements
  219. var tempIntroItems = [];
  220. for (var z = 0; z < introItems.length; z++) {
  221. if (introItems[z]) {
  222. // copy non-falsy values to the end of the array
  223. tempIntroItems.push(introItems[z]);
  224. }
  225. }
  226. introItems = tempIntroItems;
  227. //Ok, sort all items with given steps
  228. introItems.sort(function (a, b) {
  229. return a.step - b.step;
  230. });
  231. //set it to the introJs object
  232. this._introItems = introItems;
  233. //add overlay layer to the page
  234. if(_addOverlayLayer.call(this, targetElm)) {
  235. //then, start the show
  236. _nextStep.call(this);
  237. if (this._options.keyboardNavigation) {
  238. DOMEvent.on(window, 'keydown', _onKeyDown, this, true);
  239. }
  240. //for window resize
  241. DOMEvent.on(window, 'resize', _onResize, this, true);
  242. }
  243. return false;
  244. }
  245. function _onResize () {
  246. this.refresh.call(this);
  247. }
  248. /**
  249. * on keyCode:
  250. * https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
  251. * This feature has been removed from the Web standards.
  252. * Though some browsers may still support it, it is in
  253. * the process of being dropped.
  254. * Instead, you should use KeyboardEvent.code,
  255. * if it's implemented.
  256. *
  257. * jQuery's approach is to test for
  258. * (1) e.which, then
  259. * (2) e.charCode, then
  260. * (3) e.keyCode
  261. * https://github.com/jquery/jquery/blob/a6b0705294d336ae2f63f7276de0da1195495363/src/event.js#L638
  262. *
  263. * @param type var
  264. * @return type
  265. */
  266. function _onKeyDown (e) {
  267. var code = (e.code === null) ? e.which : e.code;
  268. // if code/e.which is null
  269. if (code === null) {
  270. code = (e.charCode === null) ? e.keyCode : e.charCode;
  271. }
  272. if ((code === 'Escape' || code === 27) && this._options.exitOnEsc === true) {
  273. //escape key pressed, exit the intro
  274. //check if exit callback is defined
  275. _exitIntro.call(this, this._targetElement);
  276. } else if (code === 'ArrowLeft' || code === 37) {
  277. //left arrow
  278. _previousStep.call(this);
  279. } else if (code === 'ArrowRight' || code === 39) {
  280. //right arrow
  281. _nextStep.call(this);
  282. } else if (code === 'Enter' || code === 13) {
  283. //srcElement === ie
  284. var target = e.target || e.srcElement;
  285. if (target && target.className.match('introjs-prevbutton')) {
  286. //user hit enter while focusing on previous button
  287. _previousStep.call(this);
  288. } else if (target && target.className.match('introjs-skipbutton')) {
  289. //user hit enter while focusing on skip button
  290. if (this._introItems.length - 1 === this._currentStep && typeof (this._introCompleteCallback) === 'function') {
  291. this._introCompleteCallback.call(this);
  292. }
  293. _exitIntro.call(this, this._targetElement);
  294. } else if (target && target.getAttribute('data-stepnumber')) {
  295. // user hit enter while focusing on step bullet
  296. target.click();
  297. } else {
  298. //default behavior for responding to enter
  299. _nextStep.call(this);
  300. }
  301. //prevent default behaviour on hitting Enter, to prevent steps being skipped in some browsers
  302. if(e.preventDefault) {
  303. e.preventDefault();
  304. } else {
  305. e.returnValue = false;
  306. }
  307. }
  308. }
  309. /*
  310. * makes a copy of the object
  311. * @api private
  312. * @method _cloneObject
  313. */
  314. function _cloneObject(object) {
  315. if (object === null || typeof (object) !== 'object' || typeof (object.nodeType) !== 'undefined') {
  316. return object;
  317. }
  318. var temp = {};
  319. for (var key in object) {
  320. if (typeof(window.jQuery) !== 'undefined' && object[key] instanceof window.jQuery) {
  321. temp[key] = object[key];
  322. } else {
  323. temp[key] = _cloneObject(object[key]);
  324. }
  325. }
  326. return temp;
  327. }
  328. /**
  329. * Go to specific step of introduction
  330. *
  331. * @api private
  332. * @method _goToStep
  333. */
  334. function _goToStep(step) {
  335. //because steps starts with zero
  336. this._currentStep = step - 2;
  337. if (typeof (this._introItems) !== 'undefined') {
  338. _nextStep.call(this);
  339. }
  340. }
  341. /**
  342. * Go to the specific step of introduction with the explicit [data-step] number
  343. *
  344. * @api private
  345. * @method _goToStepNumber
  346. */
  347. function _goToStepNumber(step) {
  348. this._currentStepNumber = step;
  349. if (typeof (this._introItems) !== 'undefined') {
  350. _nextStep.call(this);
  351. }
  352. }
  353. /**
  354. * Go to next step on intro
  355. *
  356. * @api private
  357. * @method _nextStep
  358. */
  359. function _nextStep() {
  360. this._direction = 'forward';
  361. if (typeof (this._currentStepNumber) !== 'undefined') {
  362. _forEach(this._introItems, function (item, i) {
  363. if( item.step === this._currentStepNumber ) {
  364. this._currentStep = i - 1;
  365. this._currentStepNumber = undefined;
  366. }
  367. }.bind(this));
  368. }
  369. if (typeof (this._currentStep) === 'undefined') {
  370. this._currentStep = 0;
  371. } else {
  372. ++this._currentStep;
  373. }
  374. var nextStep = this._introItems[this._currentStep];
  375. var continueStep = true;
  376. if (typeof (this._introBeforeChangeCallback) !== 'undefined') {
  377. continueStep = this._introBeforeChangeCallback.call(this, nextStep.element);
  378. }
  379. // if `onbeforechange` returned `false`, stop displaying the element
  380. if (continueStep === false) {
  381. --this._currentStep;
  382. return false;
  383. }
  384. if ((this._introItems.length) <= this._currentStep) {
  385. //end of the intro
  386. //check if any callback is defined
  387. if (typeof (this._introCompleteCallback) === 'function') {
  388. this._introCompleteCallback.call(this);
  389. }
  390. _exitIntro.call(this, this._targetElement);
  391. return;
  392. }
  393. _showElement.call(this, nextStep);
  394. }
  395. /**
  396. * Go to previous step on intro
  397. *
  398. * @api private
  399. * @method _previousStep
  400. */
  401. function _previousStep() {
  402. this._direction = 'backward';
  403. if (this._currentStep === 0) {
  404. return false;
  405. }
  406. --this._currentStep;
  407. var nextStep = this._introItems[this._currentStep];
  408. var continueStep = true;
  409. if (typeof (this._introBeforeChangeCallback) !== 'undefined') {
  410. continueStep = this._introBeforeChangeCallback.call(this, nextStep.element);
  411. }
  412. // if `onbeforechange` returned `false`, stop displaying the element
  413. if (continueStep === false) {
  414. ++this._currentStep;
  415. return false;
  416. }
  417. _showElement.call(this, nextStep);
  418. }
  419. /**
  420. * Update placement of the intro objects on the screen
  421. * @api private
  422. */
  423. function _refresh() {
  424. // re-align intros
  425. _setHelperLayerPosition.call(this, document.querySelector('.introjs-helperLayer'));
  426. _setHelperLayerPosition.call(this, document.querySelector('.introjs-tooltipReferenceLayer'));
  427. _setHelperLayerPosition.call(this, document.querySelector('.introjs-disableInteraction'));
  428. // re-align tooltip
  429. if(this._currentStep !== undefined && this._currentStep !== null) {
  430. var oldHelperNumberLayer = document.querySelector('.introjs-helperNumberLayer'),
  431. oldArrowLayer = document.querySelector('.introjs-arrow'),
  432. oldtooltipContainer = document.querySelector('.introjs-tooltip');
  433. _placeTooltip.call(this, this._introItems[this._currentStep].element, oldtooltipContainer, oldArrowLayer, oldHelperNumberLayer);
  434. }
  435. //re-align hints
  436. _reAlignHints.call(this);
  437. return this;
  438. }
  439. /**
  440. * Exit from intro
  441. *
  442. * @api private
  443. * @method _exitIntro
  444. * @param {Object} targetElement
  445. * @param {Boolean} force - Setting to `true` will skip the result of beforeExit callback
  446. */
  447. function _exitIntro(targetElement, force) {
  448. var continueExit = true;
  449. // calling onbeforeexit callback
  450. //
  451. // If this callback return `false`, it would halt the process
  452. if (this._introBeforeExitCallback !== undefined) {
  453. continueExit = this._introBeforeExitCallback.call(this);
  454. }
  455. // skip this check if `force` parameter is `true`
  456. // otherwise, if `onbeforeexit` returned `false`, don't exit the intro
  457. if (!force && continueExit === false) return;
  458. //remove overlay layers from the page
  459. var overlayLayers = targetElement.querySelectorAll('.introjs-overlay');
  460. if (overlayLayers && overlayLayers.length) {
  461. _forEach(overlayLayers, function (overlayLayer) {
  462. overlayLayer.style.opacity = 0;
  463. window.setTimeout(function () {
  464. if (this.parentNode) {
  465. this.parentNode.removeChild(this);
  466. }
  467. }.bind(overlayLayer), 500);
  468. }.bind(this));
  469. }
  470. //remove all helper layers
  471. var helperLayer = targetElement.querySelector('.introjs-helperLayer');
  472. if (helperLayer) {
  473. helperLayer.parentNode.removeChild(helperLayer);
  474. }
  475. var referenceLayer = targetElement.querySelector('.introjs-tooltipReferenceLayer');
  476. if (referenceLayer) {
  477. referenceLayer.parentNode.removeChild(referenceLayer);
  478. }
  479. //remove disableInteractionLayer
  480. var disableInteractionLayer = targetElement.querySelector('.introjs-disableInteraction');
  481. if (disableInteractionLayer) {
  482. disableInteractionLayer.parentNode.removeChild(disableInteractionLayer);
  483. }
  484. //remove intro floating element
  485. var floatingElement = document.querySelector('.introjsFloatingElement');
  486. if (floatingElement) {
  487. floatingElement.parentNode.removeChild(floatingElement);
  488. }
  489. _removeShowElement();
  490. //remove `introjs-fixParent` class from the elements
  491. var fixParents = document.querySelectorAll('.introjs-fixParent');
  492. _forEach(fixParents, function (parent) {
  493. _removeClass(parent, /introjs-fixParent/g);
  494. });
  495. //clean listeners
  496. DOMEvent.off(window, 'keydown', _onKeyDown, this, true);
  497. DOMEvent.off(window, 'resize', _onResize, this, true);
  498. //check if any callback is defined
  499. if (this._introExitCallback !== undefined) {
  500. this._introExitCallback.call(this);
  501. }
  502. //set the step to zero
  503. this._currentStep = undefined;
  504. }
  505. /**
  506. * Render tooltip box in the page
  507. *
  508. * @api private
  509. * @method _placeTooltip
  510. * @param {HTMLElement} targetElement
  511. * @param {HTMLElement} tooltipLayer
  512. * @param {HTMLElement} arrowLayer
  513. * @param {HTMLElement} helperNumberLayer
  514. * @param {Boolean} hintMode
  515. */
  516. function _placeTooltip(targetElement, tooltipLayer, arrowLayer, helperNumberLayer, hintMode) {
  517. var tooltipCssClass = '',
  518. currentStepObj,
  519. tooltipOffset,
  520. targetOffset,
  521. windowSize,
  522. currentTooltipPosition;
  523. hintMode = hintMode || false;
  524. //reset the old style
  525. tooltipLayer.style.top = null;
  526. tooltipLayer.style.right = null;
  527. tooltipLayer.style.bottom = null;
  528. tooltipLayer.style.left = null;
  529. tooltipLayer.style.marginLeft = null;
  530. tooltipLayer.style.marginTop = null;
  531. arrowLayer.style.display = 'inherit';
  532. if (typeof(helperNumberLayer) !== 'undefined' && helperNumberLayer !== null) {
  533. helperNumberLayer.style.top = null;
  534. helperNumberLayer.style.left = null;
  535. }
  536. //prevent error when `this._currentStep` is undefined
  537. if (!this._introItems[this._currentStep]) return;
  538. //if we have a custom css class for each step
  539. currentStepObj = this._introItems[this._currentStep];
  540. if (typeof (currentStepObj.tooltipClass) === 'string') {
  541. tooltipCssClass = currentStepObj.tooltipClass;
  542. } else {
  543. tooltipCssClass = this._options.tooltipClass;
  544. }
  545. tooltipLayer.className = ('introjs-tooltip ' + tooltipCssClass).replace(/^\s+|\s+$/g, '');
  546. tooltipLayer.setAttribute('role', 'dialog');
  547. currentTooltipPosition = this._introItems[this._currentStep].position;
  548. // Floating is always valid, no point in calculating
  549. if (currentTooltipPosition !== "floating") {
  550. currentTooltipPosition = _determineAutoPosition.call(this, targetElement, tooltipLayer, currentTooltipPosition);
  551. }
  552. var tooltipLayerStyleLeft;
  553. targetOffset = _getOffset(targetElement);
  554. tooltipOffset = _getOffset(tooltipLayer);
  555. windowSize = _getWinSize();
  556. _addClass(tooltipLayer, 'introjs-' + currentTooltipPosition);
  557. switch (currentTooltipPosition) {
  558. case 'top-right-aligned':
  559. arrowLayer.className = 'introjs-arrow bottom-right';
  560. var tooltipLayerStyleRight = 0;
  561. _checkLeft(targetOffset, tooltipLayerStyleRight, tooltipOffset, tooltipLayer);
  562. tooltipLayer.style.bottom = (targetOffset.height + 20) + 'px';
  563. break;
  564. case 'top-middle-aligned':
  565. arrowLayer.className = 'introjs-arrow bottom-middle';
  566. var tooltipLayerStyleLeftRight = targetOffset.width / 2 - tooltipOffset.width / 2;
  567. // a fix for middle aligned hints
  568. if (hintMode) {
  569. tooltipLayerStyleLeftRight += 5;
  570. }
  571. if (_checkLeft(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, tooltipLayer)) {
  572. tooltipLayer.style.right = null;
  573. _checkRight(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, windowSize, tooltipLayer);
  574. }
  575. tooltipLayer.style.bottom = (targetOffset.height + 20) + 'px';
  576. break;
  577. case 'top-left-aligned':
  578. // top-left-aligned is the same as the default top
  579. case 'top':
  580. arrowLayer.className = 'introjs-arrow bottom';
  581. tooltipLayerStyleLeft = (hintMode) ? 0 : 15;
  582. _checkRight(targetOffset, tooltipLayerStyleLeft, tooltipOffset, windowSize, tooltipLayer);
  583. tooltipLayer.style.bottom = (targetOffset.height + 20) + 'px';
  584. break;
  585. case 'right':
  586. tooltipLayer.style.left = (targetOffset.width + 20) + 'px';
  587. if (targetOffset.top + tooltipOffset.height > windowSize.height) {
  588. // In this case, right would have fallen below the bottom of the screen.
  589. // Modify so that the bottom of the tooltip connects with the target
  590. arrowLayer.className = "introjs-arrow left-bottom";
  591. tooltipLayer.style.top = "-" + (tooltipOffset.height - targetOffset.height - 20) + "px";
  592. } else {
  593. arrowLayer.className = 'introjs-arrow left';
  594. }
  595. break;
  596. case 'left':
  597. if (!hintMode && this._options.showStepNumbers === true) {
  598. tooltipLayer.style.top = '15px';
  599. }
  600. if (targetOffset.top + tooltipOffset.height > windowSize.height) {
  601. // In this case, left would have fallen below the bottom of the screen.
  602. // Modify so that the bottom of the tooltip connects with the target
  603. tooltipLayer.style.top = "-" + (tooltipOffset.height - targetOffset.height - 20) + "px";
  604. arrowLayer.className = 'introjs-arrow right-bottom';
  605. } else {
  606. arrowLayer.className = 'introjs-arrow right';
  607. }
  608. tooltipLayer.style.right = (targetOffset.width + 20) + 'px';
  609. break;
  610. case 'floating':
  611. arrowLayer.style.display = 'none';
  612. //we have to adjust the top and left of layer manually for intro items without element
  613. tooltipLayer.style.left = '50%';
  614. tooltipLayer.style.top = '50%';
  615. tooltipLayer.style.marginLeft = '-' + (tooltipOffset.width / 2) + 'px';
  616. tooltipLayer.style.marginTop = '-' + (tooltipOffset.height / 2) + 'px';
  617. if (typeof(helperNumberLayer) !== 'undefined' && helperNumberLayer !== null) {
  618. helperNumberLayer.style.left = '-' + ((tooltipOffset.width / 2) + 18) + 'px';
  619. helperNumberLayer.style.top = '-' + ((tooltipOffset.height / 2) + 18) + 'px';
  620. }
  621. break;
  622. case 'bottom-right-aligned':
  623. arrowLayer.className = 'introjs-arrow top-right';
  624. tooltipLayerStyleRight = 0;
  625. _checkLeft(targetOffset, tooltipLayerStyleRight, tooltipOffset, tooltipLayer);
  626. tooltipLayer.style.top = (targetOffset.height + 20) + 'px';
  627. break;
  628. case 'bottom-middle-aligned':
  629. arrowLayer.className = 'introjs-arrow top-middle';
  630. tooltipLayerStyleLeftRight = targetOffset.width / 2 - tooltipOffset.width / 2;
  631. // a fix for middle aligned hints
  632. if (hintMode) {
  633. tooltipLayerStyleLeftRight += 5;
  634. }
  635. if (_checkLeft(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, tooltipLayer)) {
  636. tooltipLayer.style.right = null;
  637. _checkRight(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, windowSize, tooltipLayer);
  638. }
  639. tooltipLayer.style.top = (targetOffset.height + 20) + 'px';
  640. break;
  641. // case 'bottom-left-aligned':
  642. // Bottom-left-aligned is the same as the default bottom
  643. // case 'bottom':
  644. // Bottom going to follow the default behavior
  645. default:
  646. arrowLayer.className = 'introjs-arrow top';
  647. tooltipLayerStyleLeft = 0;
  648. _checkRight(targetOffset, tooltipLayerStyleLeft, tooltipOffset, windowSize, tooltipLayer);
  649. tooltipLayer.style.top = (targetOffset.height + 20) + 'px';
  650. }
  651. }
  652. /**
  653. * Set tooltip left so it doesn't go off the right side of the window
  654. *
  655. * @return boolean true, if tooltipLayerStyleLeft is ok. false, otherwise.
  656. */
  657. function _checkRight(targetOffset, tooltipLayerStyleLeft, tooltipOffset, windowSize, tooltipLayer) {
  658. if (targetOffset.left + tooltipLayerStyleLeft + tooltipOffset.width > windowSize.width) {
  659. // off the right side of the window
  660. tooltipLayer.style.left = (windowSize.width - tooltipOffset.width - targetOffset.left) + 'px';
  661. return false;
  662. }
  663. tooltipLayer.style.left = tooltipLayerStyleLeft + 'px';
  664. return true;
  665. }
  666. /**
  667. * Set tooltip right so it doesn't go off the left side of the window
  668. *
  669. * @return boolean true, if tooltipLayerStyleRight is ok. false, otherwise.
  670. */
  671. function _checkLeft(targetOffset, tooltipLayerStyleRight, tooltipOffset, tooltipLayer) {
  672. if (targetOffset.left + targetOffset.width - tooltipLayerStyleRight - tooltipOffset.width < 0) {
  673. // off the left side of the window
  674. tooltipLayer.style.left = (-targetOffset.left) + 'px';
  675. return false;
  676. }
  677. tooltipLayer.style.right = tooltipLayerStyleRight + 'px';
  678. return true;
  679. }
  680. /**
  681. * Determines the position of the tooltip based on the position precedence and availability
  682. * of screen space.
  683. *
  684. * @param {Object} targetElement
  685. * @param {Object} tooltipLayer
  686. * @param {String} desiredTooltipPosition
  687. * @return {String} calculatedPosition
  688. */
  689. function _determineAutoPosition(targetElement, tooltipLayer, desiredTooltipPosition) {
  690. // Take a clone of position precedence. These will be the available
  691. var possiblePositions = this._options.positionPrecedence.slice();
  692. var windowSize = _getWinSize();
  693. var tooltipHeight = _getOffset(tooltipLayer).height + 10;
  694. var tooltipWidth = _getOffset(tooltipLayer).width + 20;
  695. var targetElementRect = targetElement.getBoundingClientRect();
  696. // If we check all the possible areas, and there are no valid places for the tooltip, the element
  697. // must take up most of the screen real estate. Show the tooltip floating in the middle of the screen.
  698. var calculatedPosition = "floating";
  699. /*
  700. * auto determine position
  701. */
  702. // Check for space below
  703. if (targetElementRect.bottom + tooltipHeight + tooltipHeight > windowSize.height) {
  704. _removeEntry(possiblePositions, "bottom");
  705. }
  706. // Check for space above
  707. if (targetElementRect.top - tooltipHeight < 0) {
  708. _removeEntry(possiblePositions, "top");
  709. }
  710. // Check for space to the right
  711. if (targetElementRect.right + tooltipWidth > windowSize.width) {
  712. _removeEntry(possiblePositions, "right");
  713. }
  714. // Check for space to the left
  715. if (targetElementRect.left - tooltipWidth < 0) {
  716. _removeEntry(possiblePositions, "left");
  717. }
  718. // @var {String} ex: 'right-aligned'
  719. var desiredAlignment = (function (pos) {
  720. var hyphenIndex = pos.indexOf('-');
  721. if (hyphenIndex !== -1) {
  722. // has alignment
  723. return pos.substr(hyphenIndex);
  724. }
  725. return '';
  726. })(desiredTooltipPosition || '');
  727. // strip alignment from position
  728. if (desiredTooltipPosition) {
  729. // ex: "bottom-right-aligned"
  730. // should return 'bottom'
  731. desiredTooltipPosition = desiredTooltipPosition.split('-')[0];
  732. }
  733. if (possiblePositions.length) {
  734. if (desiredTooltipPosition !== "auto" &&
  735. possiblePositions.indexOf(desiredTooltipPosition) > -1) {
  736. // If the requested position is in the list, choose that
  737. calculatedPosition = desiredTooltipPosition;
  738. } else {
  739. // Pick the first valid position, in order
  740. calculatedPosition = possiblePositions[0];
  741. }
  742. }
  743. // only top and bottom positions have optional alignments
  744. if (['top', 'bottom'].indexOf(calculatedPosition) !== -1) {
  745. calculatedPosition += _determineAutoAlignment(targetElementRect.left, tooltipWidth, windowSize, desiredAlignment);
  746. }
  747. return calculatedPosition;
  748. }
  749. /**
  750. * auto-determine alignment
  751. * @param {Integer} offsetLeft
  752. * @param {Integer} tooltipWidth
  753. * @param {Object} windowSize
  754. * @param {String} desiredAlignment
  755. * @return {String} calculatedAlignment
  756. */
  757. function _determineAutoAlignment (offsetLeft, tooltipWidth, windowSize, desiredAlignment) {
  758. var halfTooltipWidth = tooltipWidth / 2,
  759. winWidth = Math.min(windowSize.width, window.screen.width),
  760. possibleAlignments = ['-left-aligned', '-middle-aligned', '-right-aligned'],
  761. calculatedAlignment = '';
  762. // valid left must be at least a tooltipWidth
  763. // away from right side
  764. if (winWidth - offsetLeft < tooltipWidth) {
  765. _removeEntry(possibleAlignments, '-left-aligned');
  766. }
  767. // valid middle must be at least half
  768. // width away from both sides
  769. if (offsetLeft < halfTooltipWidth ||
  770. winWidth - offsetLeft < halfTooltipWidth) {
  771. _removeEntry(possibleAlignments, '-middle-aligned');
  772. }
  773. // valid right must be at least a tooltipWidth
  774. // width away from left side
  775. if (offsetLeft < tooltipWidth) {
  776. _removeEntry(possibleAlignments, '-right-aligned');
  777. }
  778. if (possibleAlignments.length) {
  779. if (possibleAlignments.indexOf(desiredAlignment) !== -1) {
  780. // the desired alignment is valid
  781. calculatedAlignment = desiredAlignment;
  782. } else {
  783. // pick the first valid position, in order
  784. calculatedAlignment = possibleAlignments[0];
  785. }
  786. } else {
  787. // if screen width is too small
  788. // for ANY alignment, middle is
  789. // probably the best for visibility
  790. calculatedAlignment = '-middle-aligned';
  791. }
  792. return calculatedAlignment;
  793. }
  794. /**
  795. * Remove an entry from a string array if it's there, does nothing if it isn't there.
  796. *
  797. * @param {Array} stringArray
  798. * @param {String} stringToRemove
  799. */
  800. function _removeEntry(stringArray, stringToRemove) {
  801. if (stringArray.indexOf(stringToRemove) > -1) {
  802. stringArray.splice(stringArray.indexOf(stringToRemove), 1);
  803. }
  804. }
  805. /**
  806. * Update the position of the helper layer on the screen
  807. *
  808. * @api private
  809. * @method _setHelperLayerPosition
  810. * @param {Object} helperLayer
  811. */
  812. function _setHelperLayerPosition(helperLayer) {
  813. if (helperLayer) {
  814. //prevent error when `this._currentStep` in undefined
  815. if (!this._introItems[this._currentStep]) return;
  816. var currentElement = this._introItems[this._currentStep],
  817. elementPosition = _getOffset(currentElement.element),
  818. widthHeightPadding = this._options.helperElementPadding;
  819. // If the target element is fixed, the tooltip should be fixed as well.
  820. // Otherwise, remove a fixed class that may be left over from the previous
  821. // step.
  822. if (_isFixed(currentElement.element)) {
  823. _addClass(helperLayer, 'introjs-fixedTooltip');
  824. } else {
  825. _removeClass(helperLayer, 'introjs-fixedTooltip');
  826. }
  827. if (currentElement.position === 'floating') {
  828. widthHeightPadding = 0;
  829. }
  830. //set new position to helper layer
  831. helperLayer.style.cssText = 'width: ' + (elementPosition.width + widthHeightPadding) + 'px; ' +
  832. 'height:' + (elementPosition.height + widthHeightPadding) + 'px; ' +
  833. 'top:' + (elementPosition.top - widthHeightPadding / 2) + 'px;' +
  834. 'left: ' + (elementPosition.left - widthHeightPadding / 2) + 'px;';
  835. }
  836. }
  837. /**
  838. * Add disableinteraction layer and adjust the size and position of the layer
  839. *
  840. * @api private
  841. * @method _disableInteraction
  842. */
  843. function _disableInteraction() {
  844. var disableInteractionLayer = document.querySelector('.introjs-disableInteraction');
  845. if (disableInteractionLayer === null) {
  846. disableInteractionLayer = document.createElement('div');
  847. disableInteractionLayer.className = 'introjs-disableInteraction';
  848. this._targetElement.appendChild(disableInteractionLayer);
  849. }
  850. _setHelperLayerPosition.call(this, disableInteractionLayer);
  851. }
  852. /**
  853. * Setting anchors to behave like buttons
  854. *
  855. * @api private
  856. * @method _setAnchorAsButton
  857. */
  858. function _setAnchorAsButton(anchor){
  859. anchor.setAttribute('role', 'button');
  860. anchor.tabIndex = 0;
  861. }
  862. /**
  863. * Show an element on the page
  864. *
  865. * @api private
  866. * @method _showElement
  867. * @param {Object} targetElement
  868. */
  869. function _showElement(targetElement) {
  870. if (typeof (this._introChangeCallback) !== 'undefined') {
  871. this._introChangeCallback.call(this, targetElement.element);
  872. }
  873. var self = this,
  874. oldHelperLayer = document.querySelector('.introjs-helperLayer'),
  875. oldReferenceLayer = document.querySelector('.introjs-tooltipReferenceLayer'),
  876. highlightClass = 'introjs-helperLayer',
  877. nextTooltipButton,
  878. prevTooltipButton,
  879. skipTooltipButton,
  880. scrollParent;
  881. //check for a current step highlight class
  882. if (typeof (targetElement.highlightClass) === 'string') {
  883. highlightClass += (' ' + targetElement.highlightClass);
  884. }
  885. //check for options highlight class
  886. if (typeof (this._options.highlightClass) === 'string') {
  887. highlightClass += (' ' + this._options.highlightClass);
  888. }
  889. if (oldHelperLayer !== null) {
  890. var oldHelperNumberLayer = oldReferenceLayer.querySelector('.introjs-helperNumberLayer'),
  891. oldtooltipLayer = oldReferenceLayer.querySelector('.introjs-tooltiptext'),
  892. oldArrowLayer = oldReferenceLayer.querySelector('.introjs-arrow'),
  893. oldtooltipContainer = oldReferenceLayer.querySelector('.introjs-tooltip');
  894. skipTooltipButton = oldReferenceLayer.querySelector('.introjs-skipbutton');
  895. prevTooltipButton = oldReferenceLayer.querySelector('.introjs-prevbutton');
  896. nextTooltipButton = oldReferenceLayer.querySelector('.introjs-nextbutton');
  897. //update or reset the helper highlight class
  898. oldHelperLayer.className = highlightClass;
  899. //hide the tooltip
  900. oldtooltipContainer.style.opacity = 0;
  901. oldtooltipContainer.style.display = "none";
  902. if (oldHelperNumberLayer !== null) {
  903. var lastIntroItem = this._introItems[(targetElement.step - 2 >= 0 ? targetElement.step - 2 : 0)];
  904. if (lastIntroItem !== null && (this._direction === 'forward' && lastIntroItem.position === 'floating') || (this._direction === 'backward' && targetElement.position === 'floating')) {
  905. oldHelperNumberLayer.style.opacity = 0;
  906. }
  907. }
  908. // scroll to element
  909. scrollParent = _getScrollParent( targetElement.element );
  910. if (scrollParent !== document.body) {
  911. // target is within a scrollable element
  912. _scrollParentToElement(scrollParent, targetElement.element);
  913. }
  914. // set new position to helper layer
  915. _setHelperLayerPosition.call(self, oldHelperLayer);
  916. _setHelperLayerPosition.call(self, oldReferenceLayer);
  917. //remove `introjs-fixParent` class from the elements
  918. var fixParents = document.querySelectorAll('.introjs-fixParent');
  919. _forEach(fixParents, function (parent) {
  920. _removeClass(parent, /introjs-fixParent/g);
  921. });
  922. //remove old classes if the element still exist
  923. _removeShowElement();
  924. //we should wait until the CSS3 transition is competed (it's 0.3 sec) to prevent incorrect `height` and `width` calculation
  925. if (self._lastShowElementTimer) {
  926. window.clearTimeout(self._lastShowElementTimer);
  927. }
  928. self._lastShowElementTimer = window.setTimeout(function() {
  929. //set current step to the label
  930. if (oldHelperNumberLayer !== null) {
  931. oldHelperNumberLayer.innerHTML = targetElement.step;
  932. }
  933. //set current tooltip text
  934. oldtooltipLayer.innerHTML = targetElement.intro;
  935. //set the tooltip position
  936. oldtooltipContainer.style.display = "block";
  937. _placeTooltip.call(self, targetElement.element, oldtooltipContainer, oldArrowLayer, oldHelperNumberLayer);
  938. //change active bullet
  939. if (self._options.showBullets) {
  940. oldReferenceLayer.querySelector('.introjs-bullets li > a.active').className = '';
  941. oldReferenceLayer.querySelector('.introjs-bullets li > a[data-stepnumber="' + targetElement.step + '"]').className = 'active';
  942. }
  943. oldReferenceLayer.querySelector('.introjs-progress .introjs-progressbar').style.cssText = 'width:' + _getProgress.call(self) + '%;';
  944. oldReferenceLayer.querySelector('.introjs-progress .introjs-progressbar').setAttribute('aria-valuenow', _getProgress.call(self));
  945. //show the tooltip
  946. oldtooltipContainer.style.opacity = 1;
  947. if (oldHelperNumberLayer) oldHelperNumberLayer.style.opacity = 1;
  948. //reset button focus
  949. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null && /introjs-donebutton/gi.test(skipTooltipButton.className)) {
  950. // skip button is now "done" button
  951. skipTooltipButton.focus();
  952. } else if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  953. //still in the tour, focus on next
  954. nextTooltipButton.focus();
  955. }
  956. // change the scroll of the window, if needed
  957. _scrollTo.call(self, targetElement.scrollTo, targetElement, oldtooltipLayer);
  958. }, 350);
  959. // end of old element if-else condition
  960. } else {
  961. var helperLayer = document.createElement('div'),
  962. referenceLayer = document.createElement('div'),
  963. arrowLayer = document.createElement('div'),
  964. tooltipLayer = document.createElement('div'),
  965. tooltipTextLayer = document.createElement('div'),
  966. bulletsLayer = document.createElement('div'),
  967. progressLayer = document.createElement('div'),
  968. buttonsLayer = document.createElement('div');
  969. helperLayer.className = highlightClass;
  970. referenceLayer.className = 'introjs-tooltipReferenceLayer';
  971. // scroll to element
  972. scrollParent = _getScrollParent( targetElement.element );
  973. if (scrollParent !== document.body) {
  974. // target is within a scrollable element
  975. _scrollParentToElement(scrollParent, targetElement.element);
  976. }
  977. //set new position to helper layer
  978. _setHelperLayerPosition.call(self, helperLayer);
  979. _setHelperLayerPosition.call(self, referenceLayer);
  980. //add helper layer to target element
  981. this._targetElement.appendChild(helperLayer);
  982. this._targetElement.appendChild(referenceLayer);
  983. arrowLayer.className = 'introjs-arrow';
  984. tooltipTextLayer.className = 'introjs-tooltiptext';
  985. tooltipTextLayer.innerHTML = targetElement.intro;
  986. bulletsLayer.className = 'introjs-bullets';
  987. if (this._options.showBullets === false) {
  988. bulletsLayer.style.display = 'none';
  989. }
  990. var ulContainer = document.createElement('ul');
  991. ulContainer.setAttribute('role', 'tablist');
  992. var anchorClick = function () {
  993. self.goToStep(this.getAttribute('data-stepnumber'));
  994. };
  995. _forEach(this._introItems, function (item, i) {
  996. var innerLi = document.createElement('li');
  997. var anchorLink = document.createElement('a');
  998. innerLi.setAttribute('role', 'presentation');
  999. anchorLink.setAttribute('role', 'tab');
  1000. anchorLink.onclick = anchorClick;
  1001. if (i === (targetElement.step-1)) {
  1002. anchorLink.className = 'active';
  1003. }
  1004. _setAnchorAsButton(anchorLink);
  1005. anchorLink.innerHTML = "&nbsp;";
  1006. anchorLink.setAttribute('data-stepnumber', item.step);
  1007. innerLi.appendChild(anchorLink);
  1008. ulContainer.appendChild(innerLi);
  1009. });
  1010. bulletsLayer.appendChild(ulContainer);
  1011. progressLayer.className = 'introjs-progress';
  1012. if (this._options.showProgress === false) {
  1013. progressLayer.style.display = 'none';
  1014. }
  1015. var progressBar = document.createElement('div');
  1016. progressBar.className = 'introjs-progressbar';
  1017. progressBar.setAttribute('role', 'progress');
  1018. progressBar.setAttribute('aria-valuemin', 0);
  1019. progressBar.setAttribute('aria-valuemax', 100);
  1020. progressBar.setAttribute('aria-valuenow', _getProgress.call(this));
  1021. progressBar.style.cssText = 'width:' + _getProgress.call(this) + '%;';
  1022. progressLayer.appendChild(progressBar);
  1023. buttonsLayer.className = 'introjs-tooltipbuttons';
  1024. if (this._options.showButtons === false) {
  1025. buttonsLayer.style.display = 'none';
  1026. }
  1027. tooltipLayer.className = 'introjs-tooltip';
  1028. tooltipLayer.appendChild(tooltipTextLayer);
  1029. tooltipLayer.appendChild(bulletsLayer);
  1030. tooltipLayer.appendChild(progressLayer);
  1031. //add helper layer number
  1032. var helperNumberLayer = document.createElement('span');
  1033. if (this._options.showStepNumbers === true) {
  1034. helperNumberLayer.className = 'introjs-helperNumberLayer';
  1035. helperNumberLayer.innerHTML = targetElement.step;
  1036. referenceLayer.appendChild(helperNumberLayer);
  1037. }
  1038. tooltipLayer.appendChild(arrowLayer);
  1039. referenceLayer.appendChild(tooltipLayer);
  1040. //next button
  1041. nextTooltipButton = document.createElement('a');
  1042. nextTooltipButton.onclick = function() {
  1043. if (self._introItems.length - 1 !== self._currentStep) {
  1044. _nextStep.call(self);
  1045. }
  1046. };
  1047. _setAnchorAsButton(nextTooltipButton);
  1048. nextTooltipButton.innerHTML = this._options.nextLabel;
  1049. //previous button
  1050. prevTooltipButton = document.createElement('a');
  1051. prevTooltipButton.onclick = function() {
  1052. if (self._currentStep !== 0) {
  1053. _previousStep.call(self);
  1054. }
  1055. };
  1056. _setAnchorAsButton(prevTooltipButton);
  1057. prevTooltipButton.innerHTML = this._options.prevLabel;
  1058. //skip button
  1059. skipTooltipButton = document.createElement('a');
  1060. skipTooltipButton.className = this._options.buttonClass + ' introjs-skipbutton ';
  1061. _setAnchorAsButton(skipTooltipButton);
  1062. skipTooltipButton.innerHTML = this._options.skipLabel;
  1063. skipTooltipButton.onclick = function() {
  1064. if (self._introItems.length - 1 === self._currentStep && typeof (self._introCompleteCallback) === 'function') {
  1065. self._introCompleteCallback.call(self);
  1066. }
  1067. if (self._introItems.length - 1 !== self._currentStep && typeof (self._introExitCallback) === 'function') {
  1068. self._introExitCallback.call(self);
  1069. }
  1070. if (typeof(self._introSkipCallback) === 'function') {
  1071. self._introSkipCallback.call(self);
  1072. }
  1073. _exitIntro.call(self, self._targetElement);
  1074. };
  1075. buttonsLayer.appendChild(skipTooltipButton);
  1076. //in order to prevent displaying next/previous button always
  1077. if (this._introItems.length > 1) {
  1078. buttonsLayer.appendChild(prevTooltipButton);
  1079. buttonsLayer.appendChild(nextTooltipButton);
  1080. }
  1081. tooltipLayer.appendChild(buttonsLayer);
  1082. //set proper position
  1083. _placeTooltip.call(self, targetElement.element, tooltipLayer, arrowLayer, helperNumberLayer);
  1084. // change the scroll of the window, if needed
  1085. _scrollTo.call(this, targetElement.scrollTo, targetElement, tooltipLayer);
  1086. //end of new element if-else condition
  1087. }
  1088. // removing previous disable interaction layer
  1089. var disableInteractionLayer = self._targetElement.querySelector('.introjs-disableInteraction');
  1090. if (disableInteractionLayer) {
  1091. disableInteractionLayer.parentNode.removeChild(disableInteractionLayer);
  1092. }
  1093. //disable interaction
  1094. if (targetElement.disableInteraction) {
  1095. _disableInteraction.call(self);
  1096. }
  1097. // when it's the first step of tour
  1098. if (this._currentStep === 0 && this._introItems.length > 1) {
  1099. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null) {
  1100. skipTooltipButton.className = this._options.buttonClass + ' introjs-skipbutton';
  1101. }
  1102. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1103. nextTooltipButton.className = this._options.buttonClass + ' introjs-nextbutton';
  1104. }
  1105. if (this._options.hidePrev === true) {
  1106. if (typeof prevTooltipButton !== "undefined" && prevTooltipButton !== null) {
  1107. prevTooltipButton.className = this._options.buttonClass + ' introjs-prevbutton introjs-hidden';
  1108. }
  1109. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1110. _addClass(nextTooltipButton, 'introjs-fullbutton');
  1111. }
  1112. } else {
  1113. if (typeof prevTooltipButton !== "undefined" && prevTooltipButton !== null) {
  1114. prevTooltipButton.className = this._options.buttonClass + ' introjs-prevbutton introjs-disabled';
  1115. }
  1116. }
  1117. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null) {
  1118. skipTooltipButton.innerHTML = this._options.skipLabel;
  1119. }
  1120. } else if (this._introItems.length - 1 === this._currentStep || this._introItems.length === 1) {
  1121. // last step of tour
  1122. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null) {
  1123. skipTooltipButton.innerHTML = this._options.doneLabel;
  1124. // adding donebutton class in addition to skipbutton
  1125. _addClass(skipTooltipButton, 'introjs-donebutton');
  1126. }
  1127. if (typeof prevTooltipButton !== "undefined" && prevTooltipButton !== null) {
  1128. prevTooltipButton.className = this._options.buttonClass + ' introjs-prevbutton';
  1129. }
  1130. if (this._options.hideNext === true) {
  1131. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1132. nextTooltipButton.className = this._options.buttonClass + ' introjs-nextbutton introjs-hidden';
  1133. }
  1134. if (typeof prevTooltipButton !== "undefined" && prevTooltipButton !== null) {
  1135. _addClass(prevTooltipButton, 'introjs-fullbutton');
  1136. }
  1137. } else {
  1138. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1139. nextTooltipButton.className = this._options.buttonClass + ' introjs-nextbutton introjs-disabled';
  1140. }
  1141. }
  1142. } else {
  1143. // steps between start and end
  1144. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null) {
  1145. skipTooltipButton.className = this._options.buttonClass + ' introjs-skipbutton';
  1146. }
  1147. if (typeof prevTooltipButton !== "undefined" && prevTooltipButton !== null) {
  1148. prevTooltipButton.className = this._options.buttonClass + ' introjs-prevbutton';
  1149. }
  1150. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1151. nextTooltipButton.className = this._options.buttonClass + ' introjs-nextbutton';
  1152. }
  1153. if (typeof skipTooltipButton !== "undefined" && skipTooltipButton !== null) {
  1154. skipTooltipButton.innerHTML = this._options.skipLabel;
  1155. }
  1156. }
  1157. prevTooltipButton.setAttribute('role', 'button');
  1158. nextTooltipButton.setAttribute('role', 'button');
  1159. skipTooltipButton.setAttribute('role', 'button');
  1160. //Set focus on "next" button, so that hitting Enter always moves you onto the next step
  1161. if (typeof nextTooltipButton !== "undefined" && nextTooltipButton !== null) {
  1162. nextTooltipButton.focus();
  1163. }
  1164. _setShowElement(targetElement);
  1165. if (typeof (this._introAfterChangeCallback) !== 'undefined') {
  1166. this._introAfterChangeCallback.call(this, targetElement.element);
  1167. }
  1168. }
  1169. /**
  1170. * To change the scroll of `window` after highlighting an element
  1171. *
  1172. * @api private
  1173. * @method _scrollTo
  1174. * @param {String} scrollTo
  1175. * @param {Object} targetElement
  1176. * @param {Object} tooltipLayer
  1177. */
  1178. function _scrollTo(scrollTo, targetElement, tooltipLayer) {
  1179. if (scrollTo === 'off') return;
  1180. var rect;
  1181. if (!this._options.scrollToElement) return;
  1182. if (scrollTo === 'tooltip') {
  1183. rect = tooltipLayer.getBoundingClientRect();
  1184. } else {
  1185. rect = targetElement.element.getBoundingClientRect();
  1186. }
  1187. if (!_elementInViewport(targetElement.element)) {
  1188. var winHeight = _getWinSize().height;
  1189. var top = rect.bottom - (rect.bottom - rect.top);
  1190. // TODO (afshinm): do we need scroll padding now?
  1191. // I have changed the scroll option and now it scrolls the window to
  1192. // the center of the target element or tooltip.
  1193. if (top < 0 || targetElement.element.clientHeight > winHeight) {
  1194. window.scrollBy(0, rect.top - ((winHeight / 2) - (rect.height / 2)) - this._options.scrollPadding); // 30px padding from edge to look nice
  1195. //Scroll down
  1196. } else {
  1197. window.scrollBy(0, rect.top - ((winHeight / 2) - (rect.height / 2)) + this._options.scrollPadding); // 30px padding from edge to look nice
  1198. }
  1199. }
  1200. }
  1201. /**
  1202. * To remove all show element(s)
  1203. *
  1204. * @api private
  1205. * @method _removeShowElement
  1206. */
  1207. function _removeShowElement() {
  1208. var elms = document.querySelectorAll('.introjs-showElement');
  1209. _forEach(elms, function (elm) {
  1210. _removeClass(elm, /introjs-[a-zA-Z]+/g);
  1211. });
  1212. }
  1213. /**
  1214. * To set the show element
  1215. * This function set a relative (in most cases) position and changes the z-index
  1216. *
  1217. * @api private
  1218. * @method _setShowElement
  1219. * @param {Object} targetElement
  1220. */
  1221. function _setShowElement(targetElement) {
  1222. var parentElm;
  1223. // we need to add this show element class to the parent of SVG elements
  1224. // because the SVG elements can't have independent z-index
  1225. if (targetElement.element instanceof SVGElement) {
  1226. parentElm = targetElement.element.parentNode;
  1227. while (targetElement.element.parentNode !== null) {
  1228. if (!parentElm.tagName || parentElm.tagName.toLowerCase() === 'body') break;
  1229. if (parentElm.tagName.toLowerCase() === 'svg') {
  1230. _addClass(parentElm, 'introjs-showElement introjs-relativePosition');
  1231. }
  1232. parentElm = parentElm.parentNode;
  1233. }
  1234. }
  1235. _addClass(targetElement.element, 'introjs-showElement');
  1236. var currentElementPosition = _getPropValue(targetElement.element, 'position');
  1237. if (currentElementPosition !== 'absolute' &&
  1238. currentElementPosition !== 'relative' &&
  1239. currentElementPosition !== 'fixed') {
  1240. //change to new intro item
  1241. _addClass(targetElement.element, 'introjs-relativePosition');
  1242. }
  1243. parentElm = targetElement.element.parentNode;
  1244. while (parentElm !== null) {
  1245. if (!parentElm.tagName || parentElm.tagName.toLowerCase() === 'body') break;
  1246. //fix The Stacking Context problem.
  1247. //More detail: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context
  1248. var zIndex = _getPropValue(parentElm, 'z-index');
  1249. var opacity = parseFloat(_getPropValue(parentElm, 'opacity'));
  1250. var transform = _getPropValue(parentElm, 'transform') || _getPropValue(parentElm, '-webkit-transform') || _getPropValue(parentElm, '-moz-transform') || _getPropValue(parentElm, '-ms-transform') || _getPropValue(parentElm, '-o-transform');
  1251. if (/[0-9]+/.test(zIndex) || opacity < 1 || (transform !== 'none' && transform !== undefined)) {
  1252. _addClass(parentElm, 'introjs-fixParent');
  1253. }
  1254. parentElm = parentElm.parentNode;
  1255. }
  1256. }
  1257. /**
  1258. * Iterates arrays
  1259. *
  1260. * @param {Array} arr
  1261. * @param {Function} forEachFnc
  1262. * @param {Function} completeFnc
  1263. * @return {Null}
  1264. */
  1265. function _forEach(arr, forEachFnc, completeFnc) {
  1266. // in case arr is an empty query selector node list
  1267. if (arr) {
  1268. for (var i = 0, len = arr.length; i < len; i++) {
  1269. forEachFnc(arr[i], i);
  1270. }
  1271. }
  1272. if (typeof(completeFnc) === 'function') {
  1273. completeFnc();
  1274. }
  1275. }
  1276. /**
  1277. * Mark any object with an incrementing number
  1278. * used for keeping track of objects
  1279. *
  1280. * @param Object obj Any object or DOM Element
  1281. * @param String key
  1282. * @return Object
  1283. */
  1284. var _stamp = (function () {
  1285. var keys = {};
  1286. return function stamp (obj, key) {
  1287. // get group key
  1288. key = key || 'introjs-stamp';
  1289. // each group increments from 0
  1290. keys[key] = keys[key] || 0;
  1291. // stamp only once per object
  1292. if (obj[key] === undefined) {
  1293. // increment key for each new object
  1294. obj[key] = keys[key]++;
  1295. }
  1296. return obj[key];
  1297. };
  1298. })();
  1299. /**
  1300. * DOMEvent Handles all DOM events
  1301. *
  1302. * methods:
  1303. *
  1304. * on - add event handler
  1305. * off - remove event
  1306. */
  1307. var DOMEvent = (function () {
  1308. function DOMEvent () {
  1309. var events_key = 'introjs_event';
  1310. /**
  1311. * Gets a unique ID for an event listener
  1312. *
  1313. * @param Object obj
  1314. * @param String type event type
  1315. * @param Function listener
  1316. * @param Object context
  1317. * @return String
  1318. */
  1319. this._id = function (obj, type, listener, context) {
  1320. return type + _stamp(listener) + (context ? '_' + _stamp(context) : '');
  1321. };
  1322. /**
  1323. * Adds event listener
  1324. *
  1325. * @param Object obj
  1326. * @param String type event type
  1327. * @param Function listener
  1328. * @param Object context
  1329. * @param Boolean useCapture
  1330. * @return null
  1331. */
  1332. this.on = function (obj, type, listener, context, useCapture) {
  1333. var id = this._id.apply(this, arguments),
  1334. handler = function (e) {
  1335. return listener.call(context || obj, e || window.event);
  1336. };
  1337. if ('addEventListener' in obj) {
  1338. obj.addEventListener(type, handler, useCapture);
  1339. } else if ('attachEvent' in obj) {
  1340. obj.attachEvent('on' + type, handler);
  1341. }
  1342. obj[events_key] = obj[events_key] || {};
  1343. obj[events_key][id] = handler;
  1344. };
  1345. /**
  1346. * Removes event listener
  1347. *
  1348. * @param Object obj
  1349. * @param String type event type
  1350. * @param Function listener
  1351. * @param Object context
  1352. * @param Boolean useCapture
  1353. * @return null
  1354. */
  1355. this.off = function (obj, type, listener, context, useCapture) {
  1356. var id = this._id.apply(this, arguments),
  1357. handler = obj[events_key] && obj[events_key][id];
  1358. if (!handler) {
  1359. return;
  1360. }
  1361. if ('removeEventListener' in obj) {
  1362. obj.removeEventListener(type, handler, useCapture);
  1363. } else if ('detachEvent' in obj) {
  1364. obj.detachEvent('on' + type, handler);
  1365. }
  1366. obj[events_key][id] = null;
  1367. };
  1368. }
  1369. return new DOMEvent();
  1370. })();
  1371. /**
  1372. * Append a class to an element
  1373. *
  1374. * @api private
  1375. * @method _addClass
  1376. * @param {Object} element
  1377. * @param {String} className
  1378. * @returns null
  1379. */
  1380. function _addClass(element, className) {
  1381. if (element instanceof SVGElement) {
  1382. // svg
  1383. var pre = element.getAttribute('class') || '';
  1384. element.setAttribute('class', pre + ' ' + className);
  1385. } else {
  1386. if (element.classList !== undefined) {
  1387. // check for modern classList property
  1388. var classes = className.split(' ');
  1389. _forEach(classes, function (cls) {
  1390. element.classList.add( cls );
  1391. });
  1392. } else if (!element.className.match( className )) {
  1393. // check if element doesn't already have className
  1394. element.className += ' ' + className;
  1395. }
  1396. }
  1397. }
  1398. /**
  1399. * Remove a class from an element
  1400. *
  1401. * @api private
  1402. * @method _removeClass
  1403. * @param {Object} element
  1404. * @param {RegExp|String} classNameRegex can be regex or string
  1405. * @returns null
  1406. */
  1407. function _removeClass(element, classNameRegex) {
  1408. if (element instanceof SVGElement) {
  1409. var pre = element.getAttribute('class') || '';
  1410. element.setAttribute('class', pre.replace(classNameRegex, '').replace(/^\s+|\s+$/g, ''));
  1411. } else {
  1412. element.className = element.className.replace(classNameRegex, '').replace(/^\s+|\s+$/g, '');
  1413. }
  1414. }
  1415. /**
  1416. * Get an element CSS property on the page
  1417. * Thanks to JavaScript Kit: http://www.javascriptkit.com/dhtmltutors/dhtmlcascade4.shtml
  1418. *
  1419. * @api private
  1420. * @method _getPropValue
  1421. * @param {Object} element
  1422. * @param {String} propName
  1423. * @returns Element's property value
  1424. */
  1425. function _getPropValue (element, propName) {
  1426. var propValue = '';
  1427. if (element.currentStyle) { //IE
  1428. propValue = element.currentStyle[propName];
  1429. } else if (document.defaultView && document.defaultView.getComputedStyle) { //Others
  1430. propValue = document.defaultView.getComputedStyle(element, null).getPropertyValue(propName);
  1431. }
  1432. //Prevent exception in IE
  1433. if (propValue && propValue.toLowerCase) {
  1434. return propValue.toLowerCase();
  1435. } else {
  1436. return propValue;
  1437. }
  1438. }
  1439. /**
  1440. * Checks to see if target element (or parents) position is fixed or not
  1441. *
  1442. * @api private
  1443. * @method _isFixed
  1444. * @param {Object} element
  1445. * @returns Boolean
  1446. */
  1447. function _isFixed (element) {
  1448. var p = element.parentNode;
  1449. if (!p || p.nodeName === 'HTML') {
  1450. return false;
  1451. }
  1452. if (_getPropValue(element, 'position') === 'fixed') {
  1453. return true;
  1454. }
  1455. return _isFixed(p);
  1456. }
  1457. /**
  1458. * Provides a cross-browser way to get the screen dimensions
  1459. * via: http://stackoverflow.com/questions/5864467/internet-explorer-innerheight
  1460. *
  1461. * @api private
  1462. * @method _getWinSize
  1463. * @returns {Object} width and height attributes
  1464. */
  1465. function _getWinSize() {
  1466. if (window.innerWidth !== undefined) {
  1467. return { width: window.innerWidth, height: window.innerHeight };
  1468. } else {
  1469. var D = document.documentElement;
  1470. return { width: D.clientWidth, height: D.clientHeight };
  1471. }
  1472. }
  1473. /**
  1474. * Check to see if the element is in the viewport or not
  1475. * http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
  1476. *
  1477. * @api private
  1478. * @method _elementInViewport
  1479. * @param {Object} el
  1480. */
  1481. function _elementInViewport(el) {
  1482. var rect = el.getBoundingClientRect();
  1483. return (
  1484. rect.top >= 0 &&
  1485. rect.left >= 0 &&
  1486. (rect.bottom+80) <= window.innerHeight && // add 80 to get the text right
  1487. rect.right <= window.innerWidth
  1488. );
  1489. }
  1490. /**
  1491. * Add overlay layer to the page
  1492. *
  1493. * @api private
  1494. * @method _addOverlayLayer
  1495. * @param {Object} targetElm
  1496. */
  1497. function _addOverlayLayer(targetElm) {
  1498. var overlayLayer = document.createElement('div'),
  1499. styleText = '',
  1500. self = this;
  1501. //set css class name
  1502. overlayLayer.className = 'introjs-overlay';
  1503. //check if the target element is body, we should calculate the size of overlay layer in a better way
  1504. if (!targetElm.tagName || targetElm.tagName.toLowerCase() === 'body') {
  1505. styleText += 'top: 0;bottom: 0; left: 0;right: 0;position: fixed;';
  1506. overlayLayer.style.cssText = styleText;
  1507. } else {
  1508. //set overlay layer position
  1509. var elementPosition = _getOffset(targetElm);
  1510. if (elementPosition) {
  1511. styleText += 'width: ' + elementPosition.width + 'px; height:' + elementPosition.height + 'px; top:' + elementPosition.top + 'px;left: ' + elementPosition.left + 'px;';
  1512. overlayLayer.style.cssText = styleText;
  1513. }
  1514. }
  1515. targetElm.appendChild(overlayLayer);
  1516. overlayLayer.onclick = function() {
  1517. if (self._options.exitOnOverlayClick === true) {
  1518. _exitIntro.call(self, targetElm);
  1519. }
  1520. };
  1521. window.setTimeout(function() {
  1522. styleText += 'opacity: ' + self._options.overlayOpacity.toString() + ';';
  1523. overlayLayer.style.cssText = styleText;
  1524. }, 10);
  1525. return true;
  1526. }
  1527. /**
  1528. * Removes open hint (tooltip hint)
  1529. *
  1530. * @api private
  1531. * @method _removeHintTooltip
  1532. */
  1533. function _removeHintTooltip() {
  1534. var tooltip = document.querySelector('.introjs-hintReference');
  1535. if (tooltip) {
  1536. var step = tooltip.getAttribute('data-step');
  1537. tooltip.parentNode.removeChild(tooltip);
  1538. return step;
  1539. }
  1540. }
  1541. /**
  1542. * Start parsing hint items
  1543. *
  1544. * @api private
  1545. * @param {Object} targetElm
  1546. * @method _startHint
  1547. */
  1548. function _populateHints(targetElm) {
  1549. this._introItems = [];
  1550. if (this._options.hints) {
  1551. _forEach(this._options.hints, function (hint) {
  1552. var currentItem = _cloneObject(hint);
  1553. if (typeof(currentItem.element) === 'string') {
  1554. //grab the element with given selector from the page
  1555. currentItem.element = document.querySelector(currentItem.element);
  1556. }
  1557. currentItem.hintPosition = currentItem.hintPosition || this._options.hintPosition;
  1558. currentItem.hintAnimation = currentItem.hintAnimation || this._options.hintAnimation;
  1559. if (currentItem.element !== null) {
  1560. this._introItems.push(currentItem);
  1561. }
  1562. }.bind(this));
  1563. } else {
  1564. var hints = targetElm.querySelectorAll('*[data-hint]');
  1565. if (!hints || !hints.length) {
  1566. return false;
  1567. }
  1568. //first add intro items with data-step
  1569. _forEach(hints, function (currentElement) {
  1570. // hint animation
  1571. var hintAnimation = currentElement.getAttribute('data-hintanimation');
  1572. if (hintAnimation) {
  1573. hintAnimation = (hintAnimation === 'true');
  1574. } else {
  1575. hintAnimation = this._options.hintAnimation;
  1576. }
  1577. this._introItems.push({
  1578. element: currentElement,
  1579. hint: currentElement.getAttribute('data-hint'),
  1580. hintPosition: currentElement.getAttribute('data-hintposition') || this._options.hintPosition,
  1581. hintAnimation: hintAnimation,
  1582. tooltipClass: currentElement.getAttribute('data-tooltipclass'),
  1583. position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
  1584. });
  1585. }.bind(this));
  1586. }
  1587. _addHints.call(this);
  1588. /*
  1589. todo:
  1590. these events should be removed at some point
  1591. */
  1592. DOMEvent.on(document, 'click', _removeHintTooltip, this, false);
  1593. DOMEvent.on(window, 'resize', _reAlignHints, this, true);
  1594. }
  1595. /**
  1596. * Re-aligns all hint elements
  1597. *
  1598. * @api private
  1599. * @method _reAlignHints
  1600. */
  1601. function _reAlignHints() {
  1602. _forEach(this._introItems, function (item) {
  1603. if (typeof(item.targetElement) === 'undefined') {
  1604. return;
  1605. }
  1606. _alignHintPosition.call(this, item.hintPosition, item.element, item.targetElement);
  1607. }.bind(this));
  1608. }
  1609. /**
  1610. * Get a queryselector within the hint wrapper
  1611. *
  1612. * @param {String} selector
  1613. * @return {NodeList|Array}
  1614. */
  1615. function _hintQuerySelectorAll(selector) {
  1616. var hintsWrapper = document.querySelector('.introjs-hints');
  1617. return (hintsWrapper) ? hintsWrapper.querySelectorAll(selector) : [];
  1618. }
  1619. /**
  1620. * Hide a hint
  1621. *
  1622. * @api private
  1623. * @method _hideHint
  1624. */
  1625. function _hideHint(stepId) {
  1626. var hint = _hintQuerySelectorAll('.introjs-hint[data-step="' + stepId + '"]')[0];
  1627. _removeHintTooltip.call(this);
  1628. if (hint) {
  1629. _addClass(hint, 'introjs-hidehint');
  1630. }
  1631. // call the callback function (if any)
  1632. if (typeof (this._hintCloseCallback) !== 'undefined') {
  1633. this._hintCloseCallback.call(this, stepId);
  1634. }
  1635. }
  1636. /**
  1637. * Hide all hints
  1638. *
  1639. * @api private
  1640. * @method _hideHints
  1641. */
  1642. function _hideHints() {
  1643. var hints = _hintQuerySelectorAll('.introjs-hint');
  1644. _forEach(hints, function (hint) {
  1645. _hideHint.call(this, hint.getAttribute('data-step'));
  1646. }.bind(this));
  1647. }
  1648. /**
  1649. * Show all hints
  1650. *
  1651. * @api private
  1652. * @method _showHints
  1653. */
  1654. function _showHints() {
  1655. var hints = _hintQuerySelectorAll('.introjs-hint');
  1656. if (hints && hints.length) {
  1657. _forEach(hints, function (hint) {
  1658. _showHint.call(this, hint.getAttribute('data-step'));
  1659. }.bind(this));
  1660. } else {
  1661. _populateHints.call(this, this._targetElement);
  1662. }
  1663. }
  1664. /**
  1665. * Show a hint
  1666. *
  1667. * @api private
  1668. * @method _showHint
  1669. */
  1670. function _showHint(stepId) {
  1671. var hint = _hintQuerySelectorAll('.introjs-hint[data-step="' + stepId + '"]')[0];
  1672. if (hint) {
  1673. _removeClass(hint, /introjs-hidehint/g);
  1674. }
  1675. }
  1676. /**
  1677. * Removes all hint elements on the page
  1678. * Useful when you want to destroy the elements and add them again (e.g. a modal or popup)
  1679. *
  1680. * @api private
  1681. * @method _removeHints
  1682. */
  1683. function _removeHints() {
  1684. var hints = _hintQuerySelectorAll('.introjs-hint');
  1685. _forEach(hints, function (hint) {
  1686. _removeHint.call(this, hint.getAttribute('data-step'));
  1687. }.bind(this));
  1688. }
  1689. /**
  1690. * Remove one single hint element from the page
  1691. * Useful when you want to destroy the element and add them again (e.g. a modal or popup)
  1692. * Use removeHints if you want to remove all elements.
  1693. *
  1694. * @api private
  1695. * @method _removeHint
  1696. */
  1697. function _removeHint(stepId) {
  1698. var hint = _hintQuerySelectorAll('.introjs-hint[data-step="' + stepId + '"]')[0];
  1699. if (hint) {
  1700. hint.parentNode.removeChild(hint);
  1701. }
  1702. }
  1703. /**
  1704. * Add all available hints to the page
  1705. *
  1706. * @api private
  1707. * @method _addHints
  1708. */
  1709. function _addHints() {
  1710. var self = this;
  1711. var hintsWrapper = document.querySelector('.introjs-hints');
  1712. if (hintsWrapper === null) {
  1713. hintsWrapper = document.createElement('div');
  1714. hintsWrapper.className = 'introjs-hints';
  1715. }
  1716. /**
  1717. * Returns an event handler unique to the hint iteration
  1718. *
  1719. * @param {Integer} i
  1720. * @return {Function}
  1721. */
  1722. var getHintClick = function (i) {
  1723. return function(e) {
  1724. var evt = e ? e : window.event;
  1725. if (evt.stopPropagation) {
  1726. evt.stopPropagation();
  1727. }
  1728. if (evt.cancelBubble !== null) {
  1729. evt.cancelBubble = true;
  1730. }
  1731. _showHintDialog.call(self, i);
  1732. };
  1733. };
  1734. _forEach(this._introItems, function(item, i) {
  1735. // avoid append a hint twice
  1736. if (document.querySelector('.introjs-hint[data-step="' + i + '"]')) {
  1737. return;
  1738. }
  1739. var hint = document.createElement('a');
  1740. _setAnchorAsButton(hint);
  1741. hint.onclick = getHintClick(i);
  1742. hint.className = 'introjs-hint';
  1743. if (!item.hintAnimation) {
  1744. _addClass(hint, 'introjs-hint-no-anim');
  1745. }
  1746. // hint's position should be fixed if the target element's position is fixed
  1747. if (_isFixed(item.element)) {
  1748. _addClass(hint, 'introjs-fixedhint');
  1749. }
  1750. var hintDot = document.createElement('div');
  1751. hintDot.className = 'introjs-hint-dot';
  1752. var hintPulse = document.createElement('div');
  1753. hintPulse.className = 'introjs-hint-pulse';
  1754. hint.appendChild(hintDot);
  1755. hint.appendChild(hintPulse);
  1756. hint.setAttribute('data-step', i);
  1757. // we swap the hint element with target element
  1758. // because _setHelperLayerPosition uses `element` property
  1759. item.targetElement = item.element;
  1760. item.element = hint;
  1761. // align the hint position
  1762. _alignHintPosition.call(this, item.hintPosition, hint, item.targetElement);
  1763. hintsWrapper.appendChild(hint);
  1764. }.bind(this));
  1765. // adding the hints wrapper
  1766. document.body.appendChild(hintsWrapper);
  1767. // call the callback function (if any)
  1768. if (typeof (this._hintsAddedCallback) !== 'undefined') {
  1769. this._hintsAddedCallback.call(this);
  1770. }
  1771. }
  1772. /**
  1773. * Aligns hint position
  1774. *
  1775. * @api private
  1776. * @method _alignHintPosition
  1777. * @param {String} position
  1778. * @param {Object} hint
  1779. * @param {Object} element
  1780. */
  1781. function _alignHintPosition(position, hint, element) {
  1782. // get/calculate offset of target element
  1783. var offset = _getOffset.call(this, element);
  1784. var iconWidth = 20;
  1785. var iconHeight = 20;
  1786. // align the hint element
  1787. switch (position) {
  1788. default:
  1789. case 'top-left':
  1790. hint.style.left = offset.left + 'px';
  1791. hint.style.top = offset.top + 'px';
  1792. break;
  1793. case 'top-right':
  1794. hint.style.left = (offset.left + offset.width - iconWidth) + 'px';
  1795. hint.style.top = offset.top + 'px';
  1796. break;
  1797. case 'bottom-left':
  1798. hint.style.left = offset.left + 'px';
  1799. hint.style.top = (offset.top + offset.height - iconHeight) + 'px';
  1800. break;
  1801. case 'bottom-right':
  1802. hint.style.left = (offset.left + offset.width - iconWidth) + 'px';
  1803. hint.style.top = (offset.top + offset.height - iconHeight) + 'px';
  1804. break;
  1805. case 'middle-left':
  1806. hint.style.left = offset.left + 'px';
  1807. hint.style.top = (offset.top + (offset.height - iconHeight) / 2) + 'px';
  1808. break;
  1809. case 'middle-right':
  1810. hint.style.left = (offset.left + offset.width - iconWidth) + 'px';
  1811. hint.style.top = (offset.top + (offset.height - iconHeight) / 2) + 'px';
  1812. break;
  1813. case 'middle-middle':
  1814. hint.style.left = (offset.left + (offset.width - iconWidth) / 2) + 'px';
  1815. hint.style.top = (offset.top + (offset.height - iconHeight) / 2) + 'px';
  1816. break;
  1817. case 'bottom-middle':
  1818. hint.style.left = (offset.left + (offset.width - iconWidth) / 2) + 'px';
  1819. hint.style.top = (offset.top + offset.height - iconHeight) + 'px';
  1820. break;
  1821. case 'top-middle':
  1822. hint.style.left = (offset.left + (offset.width - iconWidth) / 2) + 'px';
  1823. hint.style.top = offset.top + 'px';
  1824. break;
  1825. }
  1826. }
  1827. /**
  1828. * Triggers when user clicks on the hint element
  1829. *
  1830. * @api private
  1831. * @method _showHintDialog
  1832. * @param {Number} stepId
  1833. */
  1834. function _showHintDialog(stepId) {
  1835. var hintElement = document.querySelector('.introjs-hint[data-step="' + stepId + '"]');
  1836. var item = this._introItems[stepId];
  1837. // call the callback function (if any)
  1838. if (typeof (this._hintClickCallback) !== 'undefined') {
  1839. this._hintClickCallback.call(this, hintElement, item, stepId);
  1840. }
  1841. // remove all open tooltips
  1842. var removedStep = _removeHintTooltip.call(this);
  1843. // to toggle the tooltip
  1844. if (parseInt(removedStep, 10) === stepId) {
  1845. return;
  1846. }
  1847. var tooltipLayer = document.createElement('div');
  1848. var tooltipTextLayer = document.createElement('div');
  1849. var arrowLayer = document.createElement('div');
  1850. var referenceLayer = document.createElement('div');
  1851. tooltipLayer.className = 'introjs-tooltip';
  1852. tooltipLayer.onclick = function (e) {
  1853. //IE9 & Other Browsers
  1854. if (e.stopPropagation) {
  1855. e.stopPropagation();
  1856. }
  1857. //IE8 and Lower
  1858. else {
  1859. e.cancelBubble = true;
  1860. }
  1861. };
  1862. tooltipTextLayer.className = 'introjs-tooltiptext';
  1863. var tooltipWrapper = document.createElement('p');
  1864. tooltipWrapper.innerHTML = item.hint;
  1865. var closeButton = document.createElement('a');
  1866. closeButton.className = this._options.buttonClass;
  1867. closeButton.setAttribute('role', 'button');
  1868. closeButton.innerHTML = this._options.hintButtonLabel;
  1869. closeButton.onclick = _hideHint.bind(this, stepId);
  1870. tooltipTextLayer.appendChild(tooltipWrapper);
  1871. tooltipTextLayer.appendChild(closeButton);
  1872. arrowLayer.className = 'introjs-arrow';
  1873. tooltipLayer.appendChild(arrowLayer);
  1874. tooltipLayer.appendChild(tooltipTextLayer);
  1875. // set current step for _placeTooltip function
  1876. this._currentStep = hintElement.getAttribute('data-step');
  1877. // align reference layer position
  1878. referenceLayer.className = 'introjs-tooltipReferenceLayer introjs-hintReference';
  1879. referenceLayer.setAttribute('data-step', hintElement.getAttribute('data-step'));
  1880. _setHelperLayerPosition.call(this, referenceLayer);
  1881. referenceLayer.appendChild(tooltipLayer);
  1882. document.body.appendChild(referenceLayer);
  1883. //set proper position
  1884. _placeTooltip.call(this, hintElement, tooltipLayer, arrowLayer, null, true);
  1885. }
  1886. /**
  1887. * Get an element position on the page
  1888. * Thanks to `meouw`: http://stackoverflow.com/a/442474/375966
  1889. *
  1890. * @api private
  1891. * @method _getOffset
  1892. * @param {Object} element
  1893. * @returns Element's position info
  1894. */
  1895. function _getOffset(element) {
  1896. var body = document.body;
  1897. var docEl = document.documentElement;
  1898. var scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop;
  1899. var scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft;
  1900. var x = element.getBoundingClientRect();
  1901. return {
  1902. top: x.top + scrollTop,
  1903. width: x.width,
  1904. height: x.height,
  1905. left: x.left + scrollLeft
  1906. };
  1907. }
  1908. /**
  1909. * Find the nearest scrollable parent
  1910. * copied from https://stackoverflow.com/questions/35939886/find-first-scrollable-parent
  1911. *
  1912. * @param Element element
  1913. * @return Element
  1914. */
  1915. function _getScrollParent(element) {
  1916. var style = window.getComputedStyle(element);
  1917. var excludeStaticParent = (style.position === "absolute");
  1918. var overflowRegex = /(auto|scroll)/;
  1919. if (style.position === "fixed") return document.body;
  1920. for (var parent = element; (parent = parent.parentElement);) {
  1921. style = window.getComputedStyle(parent);
  1922. if (excludeStaticParent && style.position === "static") {
  1923. continue;
  1924. }
  1925. if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;
  1926. }
  1927. return document.body;
  1928. }
  1929. /**
  1930. * scroll a scrollable element to a child element
  1931. *
  1932. * @param Element parent
  1933. * @param Element element
  1934. * @return Null
  1935. */
  1936. function _scrollParentToElement (parent, element) {
  1937. parent.scrollTop = element.offsetTop - parent.offsetTop;
  1938. }
  1939. /**
  1940. * Gets the current progress percentage
  1941. *
  1942. * @api private
  1943. * @method _getProgress
  1944. * @returns current progress percentage
  1945. */
  1946. function _getProgress() {
  1947. // Steps are 0 indexed
  1948. var currentStep = parseInt((this._currentStep + 1), 10);
  1949. return ((currentStep / this._introItems.length) * 100);
  1950. }
  1951. /**
  1952. * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
  1953. * via: http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically
  1954. *
  1955. * @param obj1
  1956. * @param obj2
  1957. * @returns obj3 a new object based on obj1 and obj2
  1958. */
  1959. function _mergeOptions(obj1,obj2) {
  1960. var obj3 = {},
  1961. attrname;
  1962. for (attrname in obj1) { obj3[attrname] = obj1[attrname]; }
  1963. for (attrname in obj2) { obj3[attrname] = obj2[attrname]; }
  1964. return obj3;
  1965. }
  1966. var introJs = function (targetElm) {
  1967. var instance;
  1968. if (typeof (targetElm) === 'object') {
  1969. //Ok, create a new instance
  1970. instance = new IntroJs(targetElm);
  1971. } else if (typeof (targetElm) === 'string') {
  1972. //select the target element with query selector
  1973. var targetElement = document.querySelector(targetElm);
  1974. if (targetElement) {
  1975. instance = new IntroJs(targetElement);
  1976. } else {
  1977. throw new Error('There is no element with given selector.');
  1978. }
  1979. } else {
  1980. instance = new IntroJs(document.body);
  1981. }
  1982. // add instance to list of _instances
  1983. // passing group to _stamp to increment
  1984. // from 0 onward somewhat reliably
  1985. introJs.instances[ _stamp(instance, 'introjs-instance') ] = instance;
  1986. return instance;
  1987. };
  1988. /**
  1989. * Current IntroJs version
  1990. *
  1991. * @property version
  1992. * @type String
  1993. */
  1994. introJs.version = VERSION;
  1995. /**
  1996. * key-val object helper for introJs instances
  1997. *
  1998. * @property instances
  1999. * @type Object
  2000. */
  2001. introJs.instances = {};
  2002. //Prototype
  2003. introJs.fn = IntroJs.prototype = {
  2004. clone: function () {
  2005. return new IntroJs(this);
  2006. },
  2007. setOption: function(option, value) {
  2008. this._options[option] = value;
  2009. return this;
  2010. },
  2011. setOptions: function(options) {
  2012. this._options = _mergeOptions(this._options, options);
  2013. return this;
  2014. },
  2015. start: function (group) {
  2016. _introForElement.call(this, this._targetElement, group);
  2017. return this;
  2018. },
  2019. goToStep: function(step) {
  2020. _goToStep.call(this, step);
  2021. return this;
  2022. },
  2023. addStep: function(options) {
  2024. if (!this._options.steps) {
  2025. this._options.steps = [];
  2026. }
  2027. this._options.steps.push(options);
  2028. return this;
  2029. },
  2030. addSteps: function(steps) {
  2031. if (!steps.length) return;
  2032. for(var index = 0; index < steps.length; index++) {
  2033. this.addStep(steps[index]);
  2034. }
  2035. return this;
  2036. },
  2037. goToStepNumber: function(step) {
  2038. _goToStepNumber.call(this, step);
  2039. return this;
  2040. },
  2041. nextStep: function() {
  2042. _nextStep.call(this);
  2043. return this;
  2044. },
  2045. previousStep: function() {
  2046. _previousStep.call(this);
  2047. return this;
  2048. },
  2049. exit: function(force) {
  2050. _exitIntro.call(this, this._targetElement, force);
  2051. return this;
  2052. },
  2053. refresh: function() {
  2054. _refresh.call(this);
  2055. return this;
  2056. },
  2057. onbeforechange: function(providedCallback) {
  2058. if (typeof (providedCallback) === 'function') {
  2059. this._introBeforeChangeCallback = providedCallback;
  2060. } else {
  2061. throw new Error('Provided callback for onbeforechange was not a function');
  2062. }
  2063. return this;
  2064. },
  2065. onchange: function(providedCallback) {
  2066. if (typeof (providedCallback) === 'function') {
  2067. this._introChangeCallback = providedCallback;
  2068. } else {
  2069. throw new Error('Provided callback for onchange was not a function.');
  2070. }
  2071. return this;
  2072. },
  2073. onafterchange: function(providedCallback) {
  2074. if (typeof (providedCallback) === 'function') {
  2075. this._introAfterChangeCallback = providedCallback;
  2076. } else {
  2077. throw new Error('Provided callback for onafterchange was not a function');
  2078. }
  2079. return this;
  2080. },
  2081. oncomplete: function(providedCallback) {
  2082. if (typeof (providedCallback) === 'function') {
  2083. this._introCompleteCallback = providedCallback;
  2084. } else {
  2085. throw new Error('Provided callback for oncomplete was not a function.');
  2086. }
  2087. return this;
  2088. },
  2089. onhintsadded: function(providedCallback) {
  2090. if (typeof (providedCallback) === 'function') {
  2091. this._hintsAddedCallback = providedCallback;
  2092. } else {
  2093. throw new Error('Provided callback for onhintsadded was not a function.');
  2094. }
  2095. return this;
  2096. },
  2097. onhintclick: function(providedCallback) {
  2098. if (typeof (providedCallback) === 'function') {
  2099. this._hintClickCallback = providedCallback;
  2100. } else {
  2101. throw new Error('Provided callback for onhintclick was not a function.');
  2102. }
  2103. return this;
  2104. },
  2105. onhintclose: function(providedCallback) {
  2106. if (typeof (providedCallback) === 'function') {
  2107. this._hintCloseCallback = providedCallback;
  2108. } else {
  2109. throw new Error('Provided callback for onhintclose was not a function.');
  2110. }
  2111. return this;
  2112. },
  2113. onexit: function(providedCallback) {
  2114. if (typeof (providedCallback) === 'function') {
  2115. this._introExitCallback = providedCallback;
  2116. } else {
  2117. throw new Error('Provided callback for onexit was not a function.');
  2118. }
  2119. return this;
  2120. },
  2121. onskip: function(providedCallback) {
  2122. if (typeof (providedCallback) === 'function') {
  2123. this._introSkipCallback = providedCallback;
  2124. } else {
  2125. throw new Error('Provided callback for onskip was not a function.');
  2126. }
  2127. return this;
  2128. },
  2129. onbeforeexit: function(providedCallback) {
  2130. if (typeof (providedCallback) === 'function') {
  2131. this._introBeforeExitCallback = providedCallback;
  2132. } else {
  2133. throw new Error('Provided callback for onbeforeexit was not a function.');
  2134. }
  2135. return this;
  2136. },
  2137. addHints: function() {
  2138. _populateHints.call(this, this._targetElement);
  2139. return this;
  2140. },
  2141. hideHint: function (stepId) {
  2142. _hideHint.call(this, stepId);
  2143. return this;
  2144. },
  2145. hideHints: function () {
  2146. _hideHints.call(this);
  2147. return this;
  2148. },
  2149. showHint: function (stepId) {
  2150. _showHint.call(this, stepId);
  2151. return this;
  2152. },
  2153. showHints: function () {
  2154. _showHints.call(this);
  2155. return this;
  2156. },
  2157. removeHints: function () {
  2158. _removeHints.call(this);
  2159. return this;
  2160. },
  2161. removeHint: function (stepId) {
  2162. _removeHint.call(this, stepId);
  2163. return this;
  2164. },
  2165. showHintDialog: function (stepId) {
  2166. _showHintDialog.call(this, stepId);
  2167. return this;
  2168. }
  2169. };
  2170. return introJs;
  2171. });