2017-06-20 11:10:41 +00:00
|
|
|
function Button(config){
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.id = config.id;
|
|
|
|
|
|
|
|
// Create DOM
|
2017-06-21 21:53:53 +00:00
|
|
|
var button = document.createElement("div");
|
2017-06-20 11:10:41 +00:00
|
|
|
button.className = "object";
|
|
|
|
button.classList.add("fader");
|
2017-06-21 21:53:53 +00:00
|
|
|
button.classList.add("button");
|
2017-06-20 11:10:41 +00:00
|
|
|
self.dom = button;
|
|
|
|
|
2017-06-21 21:53:53 +00:00
|
|
|
// BG
|
|
|
|
var bg = document.createElement("div");
|
|
|
|
bg.id = "background";
|
|
|
|
var text = document.createElement("div");
|
|
|
|
text.id = "text";
|
|
|
|
var hitbox = document.createElement("div");
|
|
|
|
hitbox.id = "hitbox";
|
|
|
|
button.appendChild(bg);
|
|
|
|
button.appendChild(text);
|
|
|
|
button.appendChild(hitbox);
|
|
|
|
|
2017-06-20 11:10:41 +00:00
|
|
|
// Customize DOM
|
|
|
|
button.style.left = config.x+"px";
|
|
|
|
button.style.top = config.y+"px";
|
2017-07-10 15:38:20 +00:00
|
|
|
if(config.fontSize){
|
|
|
|
text.style.fontSize = config.fontSize;
|
|
|
|
text.style.top = 14+(20-config.fontSize);
|
|
|
|
}
|
2017-06-28 14:53:43 +00:00
|
|
|
config.upperCase = (config.upperCase===undefined) ? true : config.upperCase;
|
2017-06-28 20:28:15 +00:00
|
|
|
self.setText = function(text_id){
|
|
|
|
var words = Words.get(text_id);
|
|
|
|
if(config.upperCase) words=words.toUpperCase();
|
|
|
|
text.innerHTML = words;
|
|
|
|
};
|
|
|
|
self.setText(config.text_id);
|
2017-06-21 21:53:53 +00:00
|
|
|
|
|
|
|
// On hover...
|
|
|
|
hitbox.onmouseover = function(){
|
|
|
|
if(self.active) button.setAttribute("hover","yes");
|
|
|
|
};
|
|
|
|
hitbox.onmouseout = function(){
|
|
|
|
if(self.active) button.removeAttribute("hover");
|
|
|
|
};
|
2017-06-20 11:10:41 +00:00
|
|
|
|
|
|
|
// On click...
|
2017-06-21 21:53:53 +00:00
|
|
|
hitbox.onclick = function(){
|
2017-06-28 20:28:15 +00:00
|
|
|
if(self.active){
|
|
|
|
if(config.onclick) config.onclick();
|
|
|
|
if(config.message) publish(config.message);
|
|
|
|
}
|
2017-06-21 21:53:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Activate/Deactivate
|
|
|
|
self.active = true;
|
|
|
|
self.activate = function(){
|
|
|
|
self.active = true;
|
|
|
|
button.removeAttribute("deactivated");
|
|
|
|
};
|
|
|
|
self.deactivate = function(){
|
|
|
|
self.active = false;
|
|
|
|
button.setAttribute("deactivated","yes");
|
|
|
|
button.removeAttribute("hover");
|
2017-06-20 11:10:41 +00:00
|
|
|
};
|
2017-06-21 21:53:53 +00:00
|
|
|
if(config.active===undefined) config.active=true;
|
|
|
|
if(!config.active) self.deactivate();
|
2017-06-20 11:10:41 +00:00
|
|
|
|
2017-07-05 18:19:46 +00:00
|
|
|
// Listeners!
|
|
|
|
subscribe(self.id+"/activate", self.activate);
|
|
|
|
subscribe(self.id+"/deactivate", self.deactivate);
|
|
|
|
|
2017-06-20 11:10:41 +00:00
|
|
|
// Add...
|
|
|
|
self.add = function(INSTANT){
|
|
|
|
return _addFade(self, INSTANT);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove...
|
|
|
|
self.remove = function(INSTANT){
|
|
|
|
return _removeFade(self, INSTANT);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|