introJs.js 91 KB

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