Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nhunsaker/f1dec5814f7aea24bc0d88257f19620f to your computer and use it in GitHub Desktop.
Save nhunsaker/f1dec5814f7aea24bc0d88257f19620f to your computer and use it in GitHub Desktop.
window.PH = {
remoteWindow: null,
onWindowOpen: new Event('PH.onWindowOpen'),
onWindowClose: new Event('window.PH.onWindowClose'),
watcherJob: null,
env: null,
props: function() {
return ['bfn', 'url', 'type', 'title', 'description'];
},
ready: function(fn) {
if (document.readyState !== 'loading') {
fn();
} else if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', fn);
} else {
window.attachEvent('onreadystatechange', function() {
if (document.readyState !== 'loading') {
fn();
};
});
}
},
watch: function watch(obj, prop, handler) {
var currval = obj[prop];
function callback() {
if (obj[prop] !== currval) {
var temp = currval;
currval = obj[prop];
handler(temp, currval);
}
}
return callback;
},
onclick: function(e) {
var url = window.PH.env + '/remote?';
var props = window.PH.props();
var params = [];
for (var n = 0; n < props.length; n++) {
var key = 'data-' + props[n];
var value = e.target.getAttribute(key);
params.push(props[n] + '=' + value);
};
var query = url + params.join('&');
var options = 'location=no,toolbar=no,menubar=no,height=660,width=540,';
window.PH.remoteWindow = window.open(query, '_blank', options, false);
document.dispatchEvent(PH.onWindowOpen);
window.PH.watcherJob = setInterval(window.PH.watch(window.PH.remoteWindow, 'window', window.PH.onRemoteChange), 100);
},
onRemoteChange: function() {
document.dispatchEvent(PH.onWindowClose);
},
attach: function(target, attrs) {
var props = window.PH.props();
var btn = document.createElement('button');
var textnode = document.createTextNode('Publish');
var img = document.createElement('img');
var imgStyle = 'position:relative;top:3px;margin-right:3px;';
img.setAttribute('src', window.PH.env + '/static/img/ph-fb-icon.png');
img.setAttribute('style', imgStyle);
btn.appendChild(img);
btn.appendChild(textnode);
btn.setAttribute('class', 'button button--small button--facebook button--icon');
for (var n = 0; n < props.length; n++) {
var key = 'data-' + props[n];
btn.setAttribute(key, attrs[props[n]]);
};
target.appendChild(btn);
target.addEventListener('click', window.PH.onclick, props);
},
init: function() {
var props = window.PH.props();
window.PH.env = 'https://pubhub.stage.buzzfeed.io';
var targets = document.getElementsByClassName('pubhub-button');
for (var i = 0; i < targets.length; i++) {
var attr = {};
for (var n = 0; n < props.length; n++) {
var key = 'data-' + props[n];
attr[props[n]] = targets[i].getAttribute(key)
};
window.PH.attach(targets[i], attr);
targets[i].classList.remove('pubhub-button');
}
},
};
// window.PH.attach( document.getElementsByClassName('foo')[0], {'bfn':'bfn::dev:hive:image:123', 'url':'http://strabo.com/gallery/albums/wallpaper/foo_wallpaper.sized.jpg', 'type':'facebook_image', 'title':'foo', 'description':'foo'})
window.PH.ready(window.PH.init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment