ncase.me-trust/js/core/Button.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

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-07-13 15:07:33 +00:00
if(config.size) button.setAttribute("size", config.size);
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 20:28:15 +00:00
self.setText = function(text_id){
var words = Words.get(text_id);
2017-07-13 15:07:33 +00:00
if(config.uppercase) words = words.toUpperCase();
2017-06-28 20:28:15 +00:00
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!
2017-07-13 15:07:33 +00:00
if(self.id){
listen(self, self.id+"/activate", self.activate);
listen(self, self.id+"/deactivate", self.deactivate);
}
2017-07-05 18:19:46 +00:00
2017-07-13 18:49:49 +00:00
// Add & Remove
self.add = function(){ _add(self); };
self.remove = function(){ _remove(self); };
2017-06-20 11:10:41 +00:00
}