(function(n,t){typeof define=="function"&&define.amd?define(["tether"],t):typeof exports=="object"?module.exports=t(require("tether")):n.Shepherd=t(n.Tether)})(this,function(n){"use strict";function c(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function");}function l(n,t){if(typeof t!="function"&&t!==null)throw new TypeError("Super expression must either be null or a function, not "+typeof t);n.prototype=Object.create(t&&t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}});t&&(Object.setPrototypeOf?Object.setPrototypeOf(n,t):n.__proto__=t)}function t(n){return typeof n=="undefined"}function k(n){return n&&n.constructor===Array}function d(n){return n&&n.constructor===Object}function y(n){return typeof n=="object"}function u(n){var t=document.createElement("div");return t.innerHTML=n,t.children[0]}function g(n,i){var r=undefined;return t(n.matches)?t(n.matchesSelector)?t(n.msMatchesSelector)?t(n.webkitMatchesSelector)?t(n.mozMatchesSelector)?t(n.oMatchesSelector)||(r=n.oMatchesSelector):r=n.mozMatchesSelector:r=n.webkitMatchesSelector:r=n.msMatchesSelector:r=n.matchesSelector:r=n.matches,r.call(n,i)}function tt(n){var i,t;return y(n)?n.hasOwnProperty("element")&&n.hasOwnProperty("on")?n:null:(i=nt.exec(n),!i)?null:(t=i[2],t[0]==="["&&(t=t.substring(1,t.length-1)),{element:i[1],on:t})}function it(n,i){var r;if(n===null||t(n)||y(n))return n;var f=n.split(" "),e={},u=i.length-1;for(r=f.length-1;r>=0;r--){if(u===0){e[i[u]]=f.slice(0,r+1).join(" ");break}else e[i[u]]=f[r];u--}return e}var s=function(){function n(n,t){for(var i,r=0;r<t.length;r++)i=t[r],i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(n,i.key,i)}return function(t,i,r){return i&&n(t.prototype,i),r&&n(t,r),t}}(),h=function(n,t,i){var e=!0,r,f,o;n:while(e){var u=n,s=t,h=i;if(e=!1,u===null&&(u=Function.prototype),r=Object.getOwnPropertyDescriptor(u,s),r===undefined){if(f=Object.getPrototypeOf(u),f===null)return undefined;n=f;t=s;i=h;e=!0;r=f=undefined;continue n}else return"value"in r?r.value:(o=r.get,o===undefined)?undefined:o.call(h)}},i=n.Utils,f=i.Evented,a=i.addClass,e=i.extend,w=i.hasClass,v=i.removeClass,b=i.uniqueId,r=new f,p={"top right":"bottom left","top left":"bottom right","top center":"bottom center","middle right":"middle left","middle left":"middle right","middle center":"middle center","bottom left":"top right","bottom right":"top left","bottom center":"top center",top:"bottom center",left:"middle right",right:"middle left",bottom:"top center",center:"middle center",middle:"middle center"},nt=/^(.+) (top|left|right|bottom|center|\[[a-z ]+\])$/,o=function(i){function r(n,t){return c(this,r),h(Object.getPrototypeOf(r.prototype),"constructor",this).call(this,n,t),this.tour=n,this.bindMethods(),this.setOptions(t),this}return l(r,i),s(r,[{key:"bindMethods",value:function(){var n=this;["_show","show","hide","isOpen","cancel","complete","scrollTo","destroy","render"].map(function(t){n[t]=n[t].bind(n)})}},{key:"setOptions",value:function(){var o=arguments.length<=0||arguments[0]===undefined?{}:arguments[0],i,r,f;if(this.options=o,this.destroy(),this.id=this.options.id||this.id||"step-"+b(),i=this.options.when,i)for(r in i)if({}.hasOwnProperty.call(i,r)){f=i[r];this.on(r,f,this)}var n=JSON.stringify(this.options.buttons),u=t(n)||n==="true",e=n==="{}"||n==="[]"||n==="null"||n==="false",s=!u&&k(this.options.buttons),h=!u&&d(this.options.buttons);u?this.options.buttons=[{text:"Next",action:this.tour.next,classes:"btn"}]:!e&&h?this.options.buttons=[this.options.buttons]:(e||!s)&&(this.options.buttons=!1)}},{key:"getTour",value:function(){return this.tour}},{key:"bindAdvance",value:function(){var n=this,i=it(this.options.advanceOn,["selector","event"]),r=i.event,u=i.selector,f=function(i){n.isOpen()&&(t(u)?n.el&&i.target===n.el&&n.tour.next():g(i.target,u)&&n.tour.next())};document.body.addEventListener(r,f);this.on("destroy",function(){return document.body.removeEventListener(r,f)})}},{key:"getAttachTo",value:function(){var n=tt(this.options.attachTo)||{},t=e({},n);return typeof n.element=="string"&&(t.element=document.querySelector(n.element),t.element||console.error("The element for this Shepherd step was not found "+n.element)),t}},{key:"setupTether",value:function(){var i,r,u;if(t(n))throw new Error("Using the attachment feature of Shepherd requires the Tether library");i=this.getAttachTo();r=p[i.on]||p.right;t(i.element)&&(i.element="viewport",r="middle center");u={classPrefix:"shepherd",element:this.el,constraints:[{to:"window",pin:!0,attachment:"together"}],target:i.element,offset:i.offset||"0 0",attachment:r};this.tether&&this.tether.destroy();this.tether=new n(e(u,this.options.tetherOptions))}},{key:"show",value:function(){var i=this,n;if(!t(this.options.beforeShowPromise)&&(n=this.options.beforeShowPromise(),!t(n)))return n.then(function(){return i._show()});this._show()}},{key:"_show",value:function(){var n=this;this.trigger("before-show");this.el||this.render();a(this.el,"shepherd-open");document.body.setAttribute("data-shepherd-step",this.id);this.setupTether();this.options.scrollTo&&setTimeout(function(){n.scrollTo()});this.trigger("show")}},{key:"hide",value:function(){this.trigger("before-hide");v(this.el,"shepherd-open");document.body.removeAttribute("data-shepherd-step");this.tether&&this.tether.destroy();this.tether=null;this.trigger("hide")}},{key:"isOpen",value:function(){return this.el&&w(this.el,"shepherd-open")}},{key:"cancel",value:function(){this.tour.cancel();this.trigger("cancel")}},{key:"complete",value:function(){this.tour.complete();this.trigger("complete")}},{key:"scrollTo",value:function(){var i=this.getAttachTo(),n=i.element;t(this.options.scrollToHandler)?t(n)||n.scrollIntoView():this.options.scrollToHandler(n)}},{key:"destroy",value:function(){!t(this.el)&&this.el.parentNode&&(this.el.parentNode.removeChild(this.el),delete this.el);this.tether&&this.tether.destroy();this.tether=null;this.trigger("destroy")}},{key:"render",value:function(){var i=this,n,r,f;t(this.el)||this.destroy();this.el=u("<div class='shepherd-step "+(this.options.classes||"")+"' data-id='"+this.id+"' "+(this.options.idAttribute?'id="'+this.options.idAttribute+'"':"")+"><\/div>");n=document.createElement("div");n.className="shepherd-content";this.el.appendChild(n);r=document.createElement("header");n.appendChild(r);this.options.title&&(r.innerHTML+="<h3 class='shepherd-title'>"+this.options.title+"<\/h3>",this.el.className+=" shepherd-has-title");this.options.showCancelLink&&(f=u("<a href class='shepherd-cancel-link'>✕<\/a>"),r.appendChild(f),this.el.className+=" shepherd-has-cancel-link",this.bindCancelLink(f));t(this.options.text)||function(){var r=u("<div class='shepherd-text'><\/div>"),t=i.options.text;typeof t=="function"&&(t=t.call(i,r));t instanceof HTMLElement?r.appendChild(t):(typeof t=="string"&&(t=[t]),t.map(function(n){r.innerHTML+="<p>"+n+"<\/p>"}));n.appendChild(r)}();this.options.buttons&&function(){var t=document.createElement("footer"),r=u("<ul class='shepherd-buttons'><\/ul>");i.options.buttons.map(function(n){var t=u("<li><a class='shepherd-button "+(n.classes||"")+"'>"+n.text+"<\/a>");r.appendChild(t);i.bindButtonEvents(n,t.querySelector("a"))});t.appendChild(r);n.appendChild(t)}();document.body.appendChild(this.el);this.setupTether();this.options.advanceOn&&this.bindAdvance()}},{key:"bindCancelLink",value:function(n){var t=this;n.addEventListener("click",function(n){n.preventDefault();t.cancel()})}},{key:"bindButtonEvents",value:function(n,i){var f=this,u,r;n.events=n.events||{};t(n.action)||(n.events.click=n.action);for(u in n.events)({}).hasOwnProperty.call(n.events,u)&&(r=n.events[u],typeof r=="string"&&function(){var n=r;r=function(){return f.tour.show(n)}}(),i.addEventListener(u,r));this.on("destroy",function(){var t,r;for(t in n.events)({}).hasOwnProperty.call(n.events,t)&&(r=n.events[t],i.removeEventListener(t,r))})}}]),r}(f),rt=function(n){function i(){var n=this,t=arguments.length<=0||arguments[0]===undefined?{}:arguments[0],u;return c(this,i),h(Object.getPrototypeOf(i.prototype),"constructor",this).call(this,t),this.bindMethods(),this.options=t,this.steps=this.options.steps||[],u=["complete","cancel","hide","start","show","active","inactive"],u.map(function(t){(function(t){n.on(t,function(i){i=i||{};i.tour=n;r.trigger(t,i)})})(t)}),this}return l(i,n),s(i,[{key:"bindMethods",value:function(){var n=this;["next","back","cancel","complete","hide"].map(function(t){n[t]=n[t].bind(n)})}},{key:"addStep",value:function(n,i){return t(i)&&(i=n),i instanceof o?i.tour=this:((typeof n=="string"||typeof n=="number")&&(i.id=n.toString()),i=e({},this.options.defaults,i),i=new o(this,i)),this.steps.push(i),this}},{key:"removeStep",value:function(n){for(var r=this.getCurrentStep(),i,t=0;t<this.steps.length;++t)if(i=this.steps[t],i.id===n){i.hide();i.destroy();this.steps.splice(t,1);break}r&&r.id===n&&(this.currentStep=undefined,this.steps.length?this.show(0):this.hide())}},{key:"getById",value:function(n){for(var i,t=0;t<this.steps.length;++t)if(i=this.steps[t],i.id===n)return i}},{key:"getCurrentStep",value:function(){return this.currentStep}},{key:"next",value:function(){var n=this.steps.indexOf(this.currentStep);n===this.steps.length-1?(this.hide(n),this.trigger("complete"),this.done()):this.show(n+1,!0)}},{key:"back",value:function(){var n=this.steps.indexOf(this.currentStep);this.show(n-1,!1)}},{key:"cancel",value:function(){this.currentStep&&this.currentStep.hide();this.trigger("cancel");this.done()}},{key:"complete",value:function(){this.currentStep&&this.currentStep.hide();this.trigger("complete");this.done()}},{key:"hide",value:function(){this.currentStep&&this.currentStep.hide();this.trigger("hide");this.done()}},{key:"done",value:function(){r.activeTour=null;v(document.body,"shepherd-active");this.trigger("inactive",{tour:this})}},{key:"show",value:function(){var i=arguments.length<=0||arguments[0]===undefined?0:arguments[0],f=arguments.length<=1||arguments[1]===undefined?!0:arguments[1],n,u,e;this.currentStep?this.currentStep.hide():(a(document.body,"shepherd-active"),this.trigger("active",{tour:this}));r.activeTour=this;n=undefined;n=typeof i=="string"?this.getById(i):this.steps[i];n&&(t(n.options.showOn)||n.options.showOn()?(this.trigger("show",{step:n,previous:this.currentStep}),this.currentStep&&this.currentStep.hide(),this.currentStep=n,n.show()):(u=this.steps.indexOf(n),e=f?u+1:u-1,this.show(e,f)))}},{key:"start",value:function(){this.trigger("start");this.currentStep=null;this.next()}}]),i}(f);return e(r,{Tour:rt,Step:o,Evented:f}),r})