Skip to content

Instantly share code, notes, and snippets.

@yawboakye
Created December 25, 2012 00:56
Show Gist options
  • Save yawboakye/4371204 to your computer and use it in GitHub Desktop.
Save yawboakye/4371204 to your computer and use it in GitHub Desktop.
(function () {
var script = document.getElementById('button');
var options = (decodeURIComponent(script.getAttribute('src').split('?')[1])).split('&');
var params = [];
var button = document.createElement('button');
for (var i=0, _len = options.length; i < _len; ++i) {
options[i] = options[i].split('=');
params[options[i][0]] = options[i][1];
}
button.innerHTML = params.text;
button.className = params.color + " " + params.size;
button.addEventListener('click', function (evt) {
evt.preventDefault();
location.href = "http://www.freelancepro.me/" + params.user;
}, false);
document.body.appendChild(button);
})(this);
var addListener = function (col, evt, fn) {
for (var i=0, _len = col.length; i < _len; ++i) {
col[i].addEventListener(evt, fn, false);
}
};
var serialize = function (obj) {
var keys = Object.keys(obj),
serializedData = "";
for (var i=0, _len = keys.length; i < _len; ++i) {
serializedData += ( i === 0 ? "" : "&") + keys[i] + "=" + obj[keys[i]];
}
return serializedData;
}
var buttonCode = {
init: function (config) {
this._text = document.getElementById(config.textElement);
this._colors = document.getElementsByClassName(config.colors);
this._sizes = document.getElementsByClassName(config.sizes);
this._btnConfig = { user: 'yawboakye' };
var $this = this;
this._text.addEventListener('blur', function (e) {
if(this.value) {
$this._btnConfig['text'] = this.value;
$this.notify();
}
}, false);
this._output = document.getElementById(config.output);
this._output.addEventListener('mouseover', function (evt) {
evt.target.select();
}, false);
this.attachEvents();
},
get: function (property) {
$this = this;
return function () {
$this._btnConfig[property] = event.target.getAttribute("data-"+property);
$this.notify();
}
},
attachEvents: function () {
addListener(this._colors, 'click', this.get('color'));
addListener(this._sizes, 'click', this.get('size'));
},
notify: function () {
var code = "%script type=\"text/javascript\" src=\"_w.js?" + encodeURIComponent(serialize(this._btnConfig)) + "\"%%/script%";
this._output.innerHTML = code.replace(/%(.*)%%(.*)%/g, "<$1><$2>");
}
};
@yawboakye
Copy link
Author

Rough Cut v1.0.0.1 of Link-to-Profile button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment