@amplience/dynamic-content-accelerators
Version:
Amplience DC Frontend modules, using HTML Service
1 lines • 12 kB
JavaScript
;!function(t){function e(n){var s=[].slice.call(n.querySelectorAll(".amp-dc-promo-block")),a=2,i=window.innerWidth;if(s.length<2)s[0].classList.add("dc-fade-in");else{s.forEach(function(t){s[a-1].classList.remove("dc-fade-in")}),s[a-1].classList.add("dc-fade-in");setInterval(function(){i=window.innerWidth;var t=[].slice.call(n.querySelectorAll(".dc-fade-in"));if(!(768<i)){var e=(a===s.length?a=1:a+=1,a-1);0<t.length&&t.forEach(function(t){t.classList.remove("dc-fade-in")}),s[e].classList.add("dc-fade-in")}},5e3)}}function n(t){var e={infinite:"true"===t.getAttribute("data-infinite")?1:0,navigation:"true"===t.getAttribute("data-navigation"),autoplay:"true"===t.getAttribute("data-autoplay")},n=(window.lory?window.lory:"function"==typeof require?require("lory.js").lory:null)(t,e);e.infinite||this.disableNavButtons(t,n),e.navigation&&this.navigationDots(t,n,e),this.enableSwipeGesturesOnVideo(t)}function s(t,e){[].slice.call(document.querySelectorAll(t)).forEach(function(t){t.component||(t.component=new e(t))})}var i;function a(){var t=document.getElementById("card-container");if(!t)return!1;var e,n=t.childNodes[0].nextSibling,s=n.clientHeight,a=t.clientHeight;1.5<s/a&&(i&&clearInterval(i),i=setInterval(function(){t.classList.remove("transition"),e=n.style.top||0,e=parseInt(e),Math.abs(parseInt(e))+a<s?(e-=1,e+="px",n.style.top=e):(clearInterval(i),setTimeout(o,1e3))},10))}function o(){var t=document.getElementById("card-container"),e=t.childNodes[0].nextSibling;t.classList.add("with-transition"),setTimeout(function(){e.style.top="0px",t.classList.remove("with-transition"),t.classList.add("transition"),clearInterval(i)},1e3)}function r(){var t;s(".amp-dc-promo-banner",e),s(".amp-dc-slider",n),t=window.poiDefaults||{domain:"//i1.adis.ws",account:"csdemo",containerClass:"amp-dc-poi-image",imgClass:"amp-dc-image-pic",images:[{name:"*",polygonCallbacks:[{target:"*",action:"click",callback:function(t,e){console.log("generic click",e)},initCallback:function(t){console.log("init callback polygon",t)}}],hotspotCallbacks:[{target:"*",action:"click",callback:function(t,e){console.log("yay, i was clicked :)",e)},initCallback:function(t){console.log("init callback hotspot",t)}},{target:"*",action:"mouseover",callback:function(t,e){console.log("yay, i was hovered :)",e)},initCallback:function(t){console.log("init callback hotspot",t)}}]}]},new window.POI(t).init(),setTimeout(a,2e3)}n.prototype.disableNavButtons=function(t,n){var s=Array.prototype.slice.call(t.querySelectorAll(".js_dc_slide"),0),a=t.querySelector(".js_prev"),i=t.querySelector(".js_next"),o="ctrl-disabled";if(!a&&!i)return!1;var e=function(t){var e=n.returnIndex();0===e?(a.classList.add(o),i.classList.remove(o)):e===s.length-1?(a.classList.remove(o),i.classList.add(o)):(a.classList.remove(o),i.classList.remove(o))};0===n.returnIndex()&&a.classList.add(o),t.addEventListener("after.lory.slide",e),t.addEventListener("on.lory.resize",e)},n.prototype.autoslide=function(t,e){var n,s=function(){e.next()};return{start:function(){n=setInterval(s,3e3)},stop:function(){clearTimeout(n)},init:function(){this.start()}}},n.prototype.navigationDots=function(t,n,s){var a=Array.prototype.slice.call(t.querySelectorAll(".js_dc_slider_dot"),0),i=s.autoplay&&new this.autoslide(t,n);a.forEach(function(t,e){0===e&&(t.classList.add("active"),i&&i.init(t)),t.addEventListener("click",function(t){n.slideTo(e)})}),t.addEventListener("after.lory.slide",function(t){a.forEach(function(t,e){t.classList.remove("active")}),console.log(s);var e=s.infinite?t.detail.currentSlide-1:t.detail.currentSlide;a[e].classList.add("active"),i&&i.stop(),i&&i.start()})},n.prototype.enableSwipeGesturesOnVideo=function(t){if(navigator.userAgent.match(/Android/i)){var e=t.querySelectorAll(".amp-dc-slider .amp-dc-video");(e=Array.prototype.slice.call(e,0)).forEach(function(t,e){var n=document.createElement("div");n.style.width=t.clientWidth+"px",n.style.height=t.clientHeight-30+"px",n.style.marginBottom=30-t.clientHeight+"px",n.className="inactive-video",t.parentNode.parentNode.insertBefore(n,t.parentNode),n.addEventListener("click",function(){n.classList.add("no-overlay"),t.play()}),t.addEventListener("pause",function(){n.classList.remove("no-overlay")}),window.addEventListener("resize",function(){n.style.width=t.clientWidth+"px",n.style.height=t.clientHeight-30+"px",n.style.marginBottom=30-t.clientHeight+"px"})})}},t.Utils=t.Utils||{},t.Utils.attachComponents=r,window.addEventListener("load",function(){r()})}(window.AmpCa=window.AmpCa||{}),window.POI=function(t){this.images=[],this.imgsLoaded=0,this.params=t},window.POI.prototype={generateData:function(t,e){var n,s=this.getHotspots(t.data);if(null!==s){var a=this.findImg(t.img,e);n={dimensions:{width:t.data.width,height:t.data.height},$img:a,data:t.img,name:t.img.name,points:s,svg:null},this.images.push(n)}this.imgsLoaded+=1,t.callback(n)},getImgData:function(a){for(var i=this,o=this.params.images,r=document.querySelectorAll("img."+this.params.imgClass),l=Array.apply(null,r),c=function(s){i.ajax.atomic(i.params.domain+"/i/"+i.params.account+"/"+s.name+".json?metadata=true&func=amp.jsonReturn&v="+(new Date).getTime()).then(function(t){var e=t.data,n=i.clone(l);i.findAllImages(s,n).forEach(function(){i.generateData({data:e,img:s,callback:function(t){a(t)}},l)})})},t=0;t<o.length;t++)!function(){var n=t;if("*"===o[n].name)for(var s=0;s<r.length;s++)!function(){var e,t=r[s].getAttribute("src").split("?");if(e=(e=(e=t[0].split(i.params.account+"/"))[e.length-1].split("/"))[0],o.find(function(t){return t.name===e}))return;c({name:e,hotspotCallbacks:o[n].hotspotCallbacks,polygonCallbacks:o[n].polygonCallbacks})}();else o[n].data?i.generateData({data:o[n].data,img:o[n],callback:function(t){a(t)}},l):c(o[n])}()},findAllImages:function(t,e){for(var n=this.params.imgAttribute||"src",s=[],a=0;a<e.length;a++){var i=new RegExp(t.name),o=e[a].getAttribute(n).match(i);o&&0<o.length&&s.push(e[a])}return s},findImg:function(t,e){for(var n=this.params.imgAttribute||"src",s=null,a=0;a<e.length;a++){var i=new RegExp(t.name),o=e[a].getAttribute(n).match(i);if(o&&0<o.length){s=e[a],e.splice(a,1);break}}return s},getHotspots:function(t){var e=null;t.constructor!==Array&&(t=[t]);for(var n=0;n<t.length;n++)t[n]&&t[n].metadata&&t[n].metadata.hotSpots&&t[n].metadata.hotSpots.constructor===Object&&t[n].metadata.hotSpots.hotSpots&&t[n].metadata.hotSpots.hotSpots.constructor===Object&&t[n].metadata.hotSpots.hotSpots.list&&0<t[n].metadata.hotSpots.hotSpots.list.length&&(e=t[n].metadata.hotSpots.hotSpots.list);return e},iteratePoints:function(t){if(t)for(var e=this.hotspots(),n=this.polygons(),s=t.points,a=0;a<s.length;a++)s[a].points.constructor===Array?n.create(s[a],t):e.create(s[a],t)},assignEvents:function(t,n,s,a){if(s&&0<s.length)for(var i=0;i<s.length;i++)!function(){var e=s[i];e.target!==n&&"*"!==e.target||(t.addEventListener(e.action,function(t){e.callback(t,a)},!1),e.initCallback&&e.initCallback(a))}()},init:function(){var e=this;this.getImgData(function(t){e.iteratePoints(t)})},clone:function(t){if(null==t||"object"!=typeof t)return t;var e=t.constructor();for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}},"object"==typeof exports&&(module.exports=POI),POI.prototype.dom={hasClass:function(t,e){return new RegExp("(\\s|^)"+e+"(\\s|$)").test(t.className)},getClosest:function(t,e){var n,s,a=e.charAt(0),i="classList"in document.documentElement;for("["===a&&(1<(n=(e=e.substr(1,e.length-2)).split("=")).length&&(s=!0,n[1]=n[1].replace(/"/g,"").replace(/'/g,"")));t&&t!==document&&1===t.nodeType;t=t.parentNode){if("."===a)if(i){if(t.classList.contains(e.substr(1)))return t}else if(new RegExp("(^|\\s)"+e.substr(1)+"(\\s|$)").test(t.className))return t;if("#"===a&&t.id===e.substr(1))return t;if("["===a&&t.hasAttribute(n[0])){if(!s)return t;if(t.getAttribute(n[0])===n[1])return t}if(t.tagName.toLowerCase()===e)return t}return null}},POI.prototype.hotspots=function(){var c=this;return{create:function(t,e){var n=e.data.hotspotCallbacks,s=document.createElement("div"),a=t.selector,i=t.target;if(a){0===a.indexOf(".")?(a=a.slice(1),s.setAttribute("class",a)):0===a.indexOf("#")?(a=a.slice(1),s.setAttribute("id",a)):s.setAttribute("class",a),s.setAttribute("data-type","poi-hotspot");var o=c.dom.getClosest(e.$img,"."+c.params.containerClass);if(o&&c.dom.hasClass(o,c.params.containerClass)){var r=t.points.x.toString().slice(2);r=r.substr(0,2)+"."+r.substr(2);var l=t.points.y.toString().slice(2);l=l.substr(0,2)+"."+l.substr(2),s.style.position="absolute",s.style.left=r+"%",s.style.top=l+"%",o.style.position="relative",o.appendChild(s),i&&0<i.length&&(s.setAttribute("data-target",i),c.assignEvents(s,i,n,{$image:e.$img,$target:s,$parent:o,hotspot:t,imgInfo:e}))}else console.warn("No parent with specified className "+c.params.containerClass+" was found.")}else console.warn("no selector specified")}}},POI.prototype.polygons=function(){var d=this;return{create:function(t,e){var n=e.data.polygonCallbacks,s=t.selector;if(s){var a,i=t.target,o=d.dom.getClosest(e.$img,"."+d.params.containerClass),r="http://www.w3.org/2000/svg",l=document.createElementNS(r,"g"),c=document.createElementNS(r,"polygon");if(e.svg?a=e.svg:((a=document.createElementNS(r,"svg")).setAttributeNS(null,"viewBox","0 0 "+e.dimensions.width+" "+e.dimensions.height),o.appendChild(a),e.svg=a),0===s.indexOf(".")?(s=s.slice(1),c.setAttributeNS(null,"class",s)):0===s.indexOf("#")?(s=s.slice(1),c.setAttributeNS(null,"id",s)):c.setAttributeNS(null,"class",s),o&&d.dom.hasClass(o,d.params.containerClass)){var u="";t.points.forEach(function(t){u+=e.dimensions.width*t.x+","+e.dimensions.height*t.y+" "}),c.setAttributeNS(null,"points",u),a.appendChild(l),l.appendChild(c),i&&0<i.length&&(c.setAttributeNS(null,"data-target",i),d.assignEvents(l,i,n,{$image:e.$img,$target:l,$parent:o,polygon:t,imgInfo:e}))}else console.warn("No parent with specified className "+d.params.containerClass+" was found.")}else console.warn("no selector specified")}}},POI.prototype.ajax={settings:null,defaults:{method:"GET",username:null,password:null,data:{},headers:{"Content-type":"application/x-www-form-urlencoded"},responseType:"text",timeout:null,withCredentials:!1},supports:function(){return"XMLHttpRequest"in window&&"JSON"in window&&"Promise"in window},extend:function(){for(var n=this,s={},t=0;t<arguments.length;t++){!function(t){for(var e in t)t.hasOwnProperty(e)&&("[object Object]"===Object.prototype.toString.call(t[e])?s[e]=n.extend(s[e],t[e]):s[e]=t[e])}(arguments[t])}return s},parse:function(e){var n;if("text"!==this.settings.responseType&&""!==this.settings.responseType)return{data:e.response,xhr:e};try{n=JSON.parse(e.responseText)}catch(t){n=e.responseText}return{data:n,xhr:e}},param:function(t){if("string"==typeof t||"[object FormData]"===Object.prototype.toString.call(t))return t;if(/application\/json/i.test(this.settings.headers["Content-type"])||"[object Array]"===Object.prototype.toString.call(t))return JSON.stringify(t);var e=[];for(var n in t)t.hasOwnProperty(n)&&e.push(encodeURIComponent(n)+"="+encodeURIComponent(t[n]));return e.join("&")},makeRequest:function(s){var a=new XMLHttpRequest,i=this,t=new Promise(function(t,e){for(var n in a.onreadystatechange=function(){4===a.readyState&&(200<=a.status&&a.status<300?t(i.parse(a)):e({status:a.status,statusText:a.statusText,responseText:a.responseText}))},a.open(i.settings.method,s,!0,i.settings.username,i.settings.password),a.responseType=i.settings.responseType,i.settings.headers)i.settings.headers.hasOwnProperty(n)&&a.setRequestHeader(n,i.settings.headers[n]);i.settings.timeout&&(a.timeout=i.settings.timeout,a.ontimeout=function(t){e({status:408,statusText:"Request timeout"})}),i.settings.withCredentials&&(a.withCredentials=!0),a.send(i.param(i.settings.data))});return t.cancel=function(){a.abort()},t},atomic:function(t,e){if(!this.supports())throw"Atomic: This browser does not support the methods used in this plugin.";return this.settings=this.extend(this.defaults,e||{}),this.makeRequest(t)}};