Skip to content

Commit

Permalink
remoteoffers extension v 1.2 with better logging and minified distrib…
Browse files Browse the repository at this point in the history
…ution version
  • Loading branch information
vadymus committed Jun 4, 2016
1 parent e5d6414 commit 99ed646
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 24 additions & 0 deletions dist/remoteoffers/adobe.target.ext.remoteoffers.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* adobe.target.ext.remoteoffers.js v0.1.2
*
* Copyright 1996-2016. Adobe Systems Incorporated. All rights reserved.
*
* Example:
adobe.target.ext.remoteoffers.init(
[
{
'url': '/promotion1.html', //remote offer url (required, must be same domain)
'selector': '.menu a', //CSS selector of element to deliver offer to
'callbackSuccess': function(){}, //successful callback
'callbackError': function(){}, //error handler
'method': 'replace' //method to handle offer injection into DOM: 'append' (default) or 'replace'
},
{
'url': '/promotion2.html',
'selector': '#banner'
}
],
{debug:true}
);
*/
!function(e){"use strict";e.target=e.target||{},e.target.ext=e.target.ext||{},e.target.ext.remoteoffers=e.target.ext.remoteoffers||{},e.target.ext.remoteoffers.init=function(e,t){var r=t.debug&&t.debug===!0&&console&&console.info?function(e){console.info("ATX:"+e)}:function(){},o=function(e,t,o){r("XHR to "+e);var n=new XMLHttpRequest;n.onreadystatechange=function(){4==n.readyState&&(200==n.status?t(n.responseText):o(n.status))},n.open("GET",e,!0),n.send()},n=function(e){var t=document.getElementsByTagName("head")[0];if(t){var r=document.createElement("style");r.setAttribute("type","text/css"),r.styleSheet?r.styleSheet.cssText=e:r.appendChild(document.createTextNode(e)),t.insertBefore(r,t.firstChild)}},a=function(e,t,a,c,s){r("getOffer");var i=t+"{visibility:hidden}";n(i),o(e,function(o){r("Call success, applyOffer "+e+" to "+t);var n=document.querySelector(t);if(n){if("string"==typeof s&&"replace"===s){var c=document.createElement("div");n.parentNode.replaceChild(c,n),n=c,r("method:"+s)}adobe.target.applyOffer({element:n,offer:[{type:"html",content:o}]})}"function"==typeof a&&(r("Success handler"),a())},function(o){r("Error loading content for '"+e+"', status: '"+o);var n=document.querySelector(t);n&&(n.style.visibility="visible"),"function"==typeof c&&(r("Error handler"),c())})};!function(){for(var t=0;t<e.length;t++){var o=e[t];o.url&&o.selector?a(o.url,o.selector,o.callbackSuccess,o.callbackError,o.method):r("Error:missing URL or selector")}}()}}(adobe);
13 changes: 6 additions & 7 deletions src/remoteoffers/adobe.target.ext.remoteoffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
{debug:true}
);
*/

!(function(A){
"use strict";

Expand All @@ -30,7 +29,7 @@
A.target.ext.remoteoffers = A.target.ext.remoteoffers || {};
A.target.ext.remoteoffers.init = function(data, opts){

var log = (opts.debug && opts.debug==true && console && console.info) ? function(msg){console.info('ATX:' + msg);} : function(){};
var log = (opts.debug && opts.debug===true && console && console.info) ? function(msg){console.info('ATX:' + msg);} : function(){};

var makeAjaxCall = function(url,callback,error) {
log('XHR to '+url);
Expand Down Expand Up @@ -65,13 +64,14 @@
var prehide = selector + '{visibility:hidden}';
addCssToHead(prehide);
makeAjaxCall(path, function(result){
log('Call success, applyOffer');
log('Call success, applyOffer '+path+' to '+selector);
var element = document.querySelector(selector);
if(element){
if(typeof method === 'string' && method === 'replace'){
var newNode = document.createElement('div');
element.parentNode.replaceChild(newNode, element);
element = newNode;
log('method:'+method);
}
adobe.target.applyOffer({
'element': element,
Expand All @@ -80,14 +80,13 @@
'content': result
}]
});
//note: element will become visible //element.style.visibility = 'visible';
}
if(typeof callbackSuccess==='function') callbackSuccess();
if(typeof callbackSuccess==='function'){ log('Success handler'); callbackSuccess(); }
}, function(status){
log("Error loading content for '" + path + "', status: '" + status);
var element = document.querySelector(selector);
if(element) element.style.visibility = 'visible';
if(typeof callbackError==='function') callbackError();
if(typeof callbackError==='function') { log('Error handler'); callbackError(); }
});

};
Expand All @@ -104,4 +103,4 @@

};

})(adobe);
})(adobe);

0 comments on commit 99ed646

Please sign in to comment.