Skip to content

Instantly share code, notes, and snippets.

@pzagor2
Last active December 15, 2016 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzagor2/deee89ad3a5df72a2d4a668597919477 to your computer and use it in GitHub Desktop.
Save pzagor2/deee89ad3a5df72a2d4a668597919477 to your computer and use it in GitHub Desktop.
//# sourceURL=RAD.js
var radWrapper = document.getElementById('rad-wrapper');
var inEditor = window.location.href.indexOf('edit-loader.html') > -1 ? true : false;
//Use 'inScreenshot' to change behavior when the creative is loaded for fullbacks (Usually we don't run the animation)
var inScreenshot = window.location.href.indexOf('preview?screenshot=1') > -1 ? true : false;
var ad = Radical.getAdByContainer(window.frameElement);
if (!inEditor) {
(function () {
//get DOM references to elements inside creative
var backgroundElement = document.getElementById('a1');
// array -> array of IMG element we wand to wait for complete load
// function -> callback for images load
// function -> callback for crossing breakpoint defined in the format
RADinitialSetup([], imagesLoadedCallback, breakPointCallback);
//callback function for RADinitialSetup
function imagesLoadedCallback() {
console.log('imagesLoaded');
}
//ever time browser window croses a breakpoint defined in the format this callback will be called with {width, height} object
function breakPointCallback(size) {
console.log('Breakpoint callbak', size);
}
//....................... more CODE here ..................
//########## RAD code you don't need to worry about (most of the time) ####################
//#########################################################################################
function getElementOffsetPosition(element) {
var viewportOffset = element.getBoundingClientRect();
var props = {
left:viewportOffset.left,
top: viewportOffset.top,
}
return props;
}
function getElementCSSProperties(element){
var computed = window.getComputedStyle(element, null);
var props = {
top: computed.getPropertyValue('top'),
left: computed.getPropertyValue('left'),
bottom: computed.getPropertyValue('bottom'),
right: computed.getPropertyValue('right'),
width: element.offsetWidth,
height: element.offsetHeight
}
return props;
}
function RADinitialSetup(imagesLoadArray, _imagesLoadedCallback, _breakPointCallback) {
/*!
* enquire.js v2.1.2 - Awesome Media Queries in JavaScript
* Copyright (c) 2014 Nick Williams - http://wicky.nillia.ms/enquire.js
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
*/
!function(a,b,c){var d=window.matchMedia;"undefined"!=typeof module&&module.exports?module.exports=c(d):"function"==typeof define&&define.amd?define(function(){return b[a]=c(d)}):b[a]=c(d)}("enquire",this,function(a){"use strict";function b(a,b){var c,d=0,e=a.length;for(d;e>d&&(c=b(a[d],d),c!==!1);d++);}function c(a){return"[object Array]"===Object.prototype.toString.apply(a)}function d(a){return"function"==typeof a}function e(a){this.options=a,!a.deferSetup&&this.setup()}function f(b,c){this.query=b,this.isUnconditional=c,this.handlers=[],this.mql=a(b);var d=this;this.listener=function(a){d.mql=a,d.assess()},this.mql.addListener(this.listener)}function g(){if(!a)throw new Error("matchMedia not present, legacy browsers require a polyfill");this.queries={},this.browserIsIncapable=!a("only all").matches}return e.prototype={setup:function(){this.options.setup&&this.options.setup(),this.initialised=!0},on:function(){!this.initialised&&this.setup(),this.options.match&&this.options.match()},off:function(){this.options.unmatch&&this.options.unmatch()},destroy:function(){this.options.destroy?this.options.destroy():this.off()},equals:function(a){return this.options===a||this.options.match===a}},f.prototype={addHandler:function(a){var b=new e(a);this.handlers.push(b),this.matches()&&b.on()},removeHandler:function(a){var c=this.handlers;b(c,function(b,d){return b.equals(a)?(b.destroy(),!c.splice(d,1)):void 0})},matches:function(){return this.mql.matches||this.isUnconditional},clear:function(){b(this.handlers,function(a){a.destroy()}),this.mql.removeListener(this.listener),this.handlers.length=0},assess:function(){var a=this.matches()?"on":"off";b(this.handlers,function(b){b[a]()})}},g.prototype={register:function(a,e,g){var h=this.queries,i=g&&this.browserIsIncapable;return h[a]||(h[a]=new f(a,i)),d(e)&&(e={match:e}),c(e)||(e=[e]),b(e,function(b){d(b)&&(b={match:b}),h[a].addHandler(b)}),this},unregister:function(a,b){var c=this.queries[a];return c&&(b?c.removeHandler(b):(c.clear(),delete this.queries[a])),this}},new g});
//RAD code
var sizes = [];
var matchBreakPoint = debounce(function(size) {
_breakPointCallback(size);
}, 50, false);
//debounde implementation
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var callbackCalled = false;
//image load callback handler
(function() {
function doneLoading() {
for (var i = 0; i < imagesLoadArray.length; i++) {
if (imagesLoadArray[i].complete != true) {
return;
}
}
setTimeout(function() {
radWrapper.style.opacity = '1';
}, 1);
if (!callbackCalled) {
callbackCalled = true;
_imagesLoadedCallback();
}
}
for (var i = 0; i < imagesLoadArray.length; i++) {
(function(checkItem) {
checkItem.addEventListener('load', function() {
doneLoading(checkItem);
});
})(imagesLoadArray[i]);
}
doneLoading();
})();
//get current size from browser window width
function guesSize() {
var currectWidth = window.innerWidth;
// console.log(currectWidth);
for (var i = sizes.length - 1; i >= 0; i--) {
if (currectWidth <= sizes[i].width) {
//we have width return
return sizes[i];
}
};
//take biggest size
return sizes[0];
}
//enqure.js size matcher callback handler
(function crateBreakPointCallbacks() {
sizes = ad.getAdContent().sizes;
for (var i = 0; i < sizes.length; i++) {
var size = sizes[i];
var minSize = sizes[i+1] || {height:0, width:0};
(function(size) {
enquire.register("screen and (max-width:" + size.width + "px) and (min-width:"+ minSize.width +"px)", {
match : function() {
matchBreakPoint(size);
}
});
})(size);
}
})();
window.addEventListener("orientationchange", function(e) {
var size = guesSize();
matchBreakPoint(size);
});
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment