intro.js 80 KB

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