yellow/system/core/core-webinterface.js

416 lines
14 KiB
JavaScript
Raw Normal View History

2014-02-17 14:36:05 +00:00
// Copyright (c) 2013-2014 Datenstrom, http://datenstrom.se
2013-04-14 22:41:04 +00:00
// This file may be used and distributed under the terms of the public license.
// Yellow main API
var yellow =
{
2014-02-17 14:36:05 +00:00
version: "0.2.3",
2013-05-01 20:16:05 +00:00
onClick: function(e) { yellow.webinterface.hidePanesOnClick(yellow.toolbox.getEventElement(e)); },
onKeydown: function(e) { yellow.webinterface.hidePanesOnKeydown(yellow.toolbox.getEventKeycode(e)); },
2013-04-14 22:41:04 +00:00
onResize: function() { yellow.webinterface.resizePanes(); },
2013-05-01 20:16:05 +00:00
onShow: function(id) { yellow.webinterface.showPane(id); },
2013-12-05 22:30:45 +00:00
onLogout: function() { yellow.toolbox.submitForm({"action":"logout"}); },
2014-02-17 14:36:05 +00:00
webinterface:{}, page:{}, toolbox:{}, config:{}, text:{}
2013-04-14 22:41:04 +00:00
}
// Yellow web interface
yellow.webinterface =
{
created: false, //interface created? (boolean)
2013-05-23 06:24:50 +00:00
intervalId: 0, //interface timer interval ID
2013-04-14 22:41:04 +00:00
// Initialise web interface
init: function()
{
this.intervalId = setInterval("yellow.webinterface.create()", 1);
2013-05-01 20:16:05 +00:00
yellow.toolbox.addEvent(window, "resize", yellow.onResize);
yellow.toolbox.addEvent(document, "click", yellow.onClick);
yellow.toolbox.addEvent(document, "keydown", yellow.onKeydown);
2013-04-14 22:41:04 +00:00
},
2013-05-23 06:24:50 +00:00
// Create web interface
2013-04-14 22:41:04 +00:00
create: function()
{
var body = document.getElementsByTagName("body")[0];
2013-05-23 06:24:50 +00:00
if(body && body.firstChild && !this.created)
2013-04-14 22:41:04 +00:00
{
2013-05-23 06:24:50 +00:00
this.created = true;
if(yellow.debug) console.log("yellow.webinterface.create email:"+yellow.config.userEmail+" "+yellow.config.userName);
if(yellow.config.userEmail)
{
yellow.toolbox.insertBefore(this.createBar("yellow-bar"), body.firstChild);
2013-12-05 22:30:45 +00:00
yellow.toolbox.insertAfter(this.createPane("yellow-pane-edit"), body.firstChild);
yellow.toolbox.insertAfter(this.createPane("yellow-pane-user"), body.firstChild);
yellow.toolbox.setText(document.getElementById("yellow-edit-text"), yellow.page.rawData);
2013-05-23 06:24:50 +00:00
} else {
yellow.toolbox.insertBefore(this.createBar("yellow-bar", true), body.firstChild);
2013-12-05 22:30:45 +00:00
yellow.toolbox.insertAfter(this.createPane("yellow-pane-login", true), body.firstChild);
this.showPane("yellow-pane-login");
2013-05-23 06:24:50 +00:00
}
clearInterval(this.intervalId);
}
},
// Create bar
createBar: function(id, simple)
{
if(yellow.debug) console.log("yellow.webinterface.createBar id:"+id);
var elementBar = document.createElement("div");
elementBar.className = "yellow-bar yellow";
elementBar.setAttribute("id", id);
if(!simple)
{
var location = yellow.config.serverBase+yellow.config.pluginLocation;
2013-05-23 06:24:50 +00:00
elementBar.innerHTML =
2013-12-05 22:30:45 +00:00
"<div class=\"yellow-bar-left\">"+
2014-02-17 14:36:05 +00:00
"<a href=\"https://github.com/markseu/yellowcms-extensions/blob/master/documentation/README.md\" target=\"_blank\"><i class=\"yellow-icon\"></i> Yellow</a>"+
2013-12-05 22:30:45 +00:00
"<a href=\"#\" onclick=\"yellow.onShow('yellow-pane-edit'); return false;\">"+this.getText("Edit")+"</a>"+
2013-04-14 22:41:04 +00:00
"</div>"+
2013-12-05 22:30:45 +00:00
"<div class=\"yellow-bar-right\">"+
2014-02-17 14:36:05 +00:00
"<a href=\"#\" onclick=\"yellow.onShow('yellow-pane-user'); return false;\" id=\"yellow-username\">"+yellow.config.userName+" &#9662;</a>"+
2013-04-14 22:41:04 +00:00
"</div>";
}
2013-05-23 06:24:50 +00:00
return elementBar;
2013-04-14 22:41:04 +00:00
},
// Create pane
2013-05-23 06:24:50 +00:00
createPane: function(id, simple)
2013-04-14 22:41:04 +00:00
{
if(yellow.debug) console.log("yellow.webinterface.createPane id:"+id);
2013-05-23 06:24:50 +00:00
var elementPane = document.createElement("div");
2013-12-05 22:30:45 +00:00
elementPane.className = simple ? "yellow-pane" : "yellow-pane yellow-pane-bubble";
2013-05-23 06:24:50 +00:00
elementPane.setAttribute("id", id);
elementPane.style.display = "none";
var elementDiv = document.createElement("div");
2013-12-05 22:30:45 +00:00
elementDiv.setAttribute("id", id+"-content");
if(id == "yellow-pane-login")
2013-04-14 22:41:04 +00:00
{
2013-05-23 06:24:50 +00:00
elementDiv.innerHTML =
"<h1>"+this.getText("LoginText")+"</h1>"+
2013-12-05 22:30:45 +00:00
"<form method=\"post\">"+
"<input type=\"hidden\" name=\"action\" value=\"login\" />"+
2013-07-19 03:05:17 +00:00
"<p><label for=\"email\">"+this.getText("LoginEmail")+"</label> <input name=\"email\" id=\"email\" maxlength=\"64\" /></p>"+
"<p><label for=\"password\">"+this.getText("LoginPassword")+"</label> <input type=\"password\" name=\"password\" id=\"password\" maxlength=\"64\" /></p>"+
2013-12-05 22:30:45 +00:00
"<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("LoginButton")+"\" /></p>"+
2013-05-23 06:24:50 +00:00
"</form>";
2013-12-05 22:30:45 +00:00
} else if(id == "yellow-pane-edit") {
2013-05-23 06:24:50 +00:00
elementDiv.innerHTML =
2013-12-05 22:30:45 +00:00
"<form method=\"post\">"+
"<input type=\"hidden\" name=\"action\" value=\"edit\" />"+
"<textarea id=\"yellow-edit-text\" name=\"rawdata\"></textarea>"+
"<div id=\"yellow-edit-info\" /></div>"+
"<div id=\"yellow-edit-buttons\">"+
"<input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("SaveButton")+"\" />"+
2013-04-14 22:41:04 +00:00
"</div>"+
"</form>";
2013-12-05 22:30:45 +00:00
} else if(id == "yellow-pane-user") {
2013-05-23 06:24:50 +00:00
elementDiv.innerHTML =
2013-04-14 22:41:04 +00:00
"<p>"+yellow.config.userEmail+"</p>"+
2013-12-05 22:30:45 +00:00
"<p><a href=\"#\" onclick=\"yellow.onLogout(); return false;\">"+this.getText("UserLogout")+"</a></p>";
2013-04-14 22:41:04 +00:00
}
2013-05-23 06:24:50 +00:00
elementPane.appendChild(elementDiv);
return elementPane;
2013-04-14 22:41:04 +00:00
},
2013-05-23 06:24:50 +00:00
// Show or hide pane
2013-04-14 22:41:04 +00:00
showPane: function(id)
{
2013-05-23 06:24:50 +00:00
if(!yellow.toolbox.isVisible(document.getElementById(id)))
2013-04-14 22:41:04 +00:00
{
this.hidePanes();
if(yellow.debug) console.log("yellow.webinterface.showPane id:"+id);
document.getElementById(id).style.display = "block";
2013-05-23 06:24:50 +00:00
this.resizePanes();
2013-05-01 20:16:05 +00:00
} else {
this.hidePane(id);
2013-04-14 22:41:04 +00:00
}
},
// Hide pane
hidePane: function(id)
{
2013-05-23 06:24:50 +00:00
if(yellow.toolbox.isVisible(document.getElementById(id)))
2013-05-01 20:16:05 +00:00
{
if(yellow.debug) console.log("yellow.webinterface.hidePane id:"+id);
document.getElementById(id).style.display = "none";
}
2013-04-14 22:41:04 +00:00
},
// Hide all panes
hidePanes: function()
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
for(var element=document.getElementById("yellow-bar"); element; element=element.nextSibling)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
if(element.className && element.className.indexOf("yellow-pane")>=0)
2013-04-14 22:41:04 +00:00
{
this.hidePane(element.getAttribute("id"));
}
}
},
2013-05-01 20:16:05 +00:00
// Hide all panes on mouse click outside
hidePanesOnClick: function(element)
2013-04-14 22:41:04 +00:00
{
while(element = element.parentNode)
{
if(element.className)
{
2013-05-01 20:16:05 +00:00
if(element.className.indexOf("yellow-pane")>=0 || element.className.indexOf("yellow-bar")>=0) return;
2013-04-14 22:41:04 +00:00
}
}
this.hidePanes();
},
2013-05-01 20:16:05 +00:00
// Hide all panes on ESC key
hidePanesOnKeydown: function(keycode)
{
if(keycode == 27) this.hidePanes();
},
2013-04-14 22:41:04 +00:00
2013-05-23 06:24:50 +00:00
// Resize panes, recalculate width and height where needed
resizePanes: function()
2013-04-14 22:41:04 +00:00
{
2013-05-23 06:24:50 +00:00
if(document.getElementById("yellow-bar"))
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
var elementBar = document.getElementById("yellow-bar");
2013-05-23 06:24:50 +00:00
var paneTop = yellow.toolbox.getOuterTop(elementBar) + yellow.toolbox.getOuterHeight(elementBar);
var paneWidth = yellow.toolbox.getOuterWidth(elementBar, true);
var paneHeight = yellow.toolbox.getWindowHeight() - paneTop - yellow.toolbox.getOuterHeight(elementBar);
2013-12-05 22:30:45 +00:00
if(yellow.toolbox.isVisible(document.getElementById("yellow-pane-login")))
2013-05-23 06:24:50 +00:00
{
2013-12-05 22:30:45 +00:00
yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-login"), paneWidth);
2013-05-23 06:24:50 +00:00
}
2013-12-05 22:30:45 +00:00
if(yellow.toolbox.isVisible(document.getElementById("yellow-pane-edit")))
2013-05-23 06:24:50 +00:00
{
2013-12-05 22:30:45 +00:00
yellow.toolbox.setOuterTop(document.getElementById("yellow-pane-edit"), paneTop);
yellow.toolbox.setOuterHeight(document.getElementById("yellow-pane-edit"), paneHeight);
yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-edit"), paneWidth);
yellow.toolbox.setOuterWidth(document.getElementById("yellow-edit-text"), yellow.toolbox.getWidth(document.getElementById("yellow-pane-edit")));
var height1 = yellow.toolbox.getHeight(document.getElementById("yellow-pane-edit"));
var height2 = yellow.toolbox.getOuterHeight(document.getElementById("yellow-pane-edit-content"));
var height3 = yellow.toolbox.getOuterHeight(document.getElementById("yellow-edit-text"));
yellow.toolbox.setOuterHeight(document.getElementById("yellow-edit-text"), height1 - height2 + height3);
2013-05-23 06:24:50 +00:00
}
2013-12-05 22:30:45 +00:00
if(yellow.toolbox.isVisible(document.getElementById("yellow-pane-user")))
2013-05-23 06:24:50 +00:00
{
2013-12-05 22:30:45 +00:00
yellow.toolbox.setOuterTop(document.getElementById("yellow-pane-user"), paneTop);
yellow.toolbox.setOuterHeight(document.getElementById("yellow-pane-user"), paneHeight, true);
yellow.toolbox.setOuterLeft(document.getElementById("yellow-pane-user"), paneWidth - yellow.toolbox.getOuterWidth(document.getElementById("yellow-pane-user")), true);
2013-05-23 06:24:50 +00:00
}
if(yellow.debug) console.log("yellow.webinterface.resizePanes bar:"+elementBar.offsetWidth+"/"+elementBar.offsetHeight);
2013-04-14 22:41:04 +00:00
}
},
// Return text string
getText: function(key)
{
return ("webinterface"+key in yellow.text) ? yellow.text["webinterface"+key] : "[webinterface"+key+"]";
}
}
// Yellow toolbox with helpers
yellow.toolbox =
{
// Set element text
setText: function(element, text)
{
2013-05-23 06:24:50 +00:00
while(element.firstChild !== null) element.removeChild(element.firstChild);
2013-04-14 22:41:04 +00:00
element.appendChild(document.createTextNode(text));
},
2013-05-23 06:24:50 +00:00
// Insert element before element
insertBefore: function(newElement, referenceElement)
{
referenceElement.parentNode.insertBefore(newElement, referenceElement);
},
2013-04-14 22:41:04 +00:00
// Insert element after element
insertAfter: function(newElement, referenceElement)
{
referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
},
2013-05-01 20:16:05 +00:00
// Add event handler
addEvent: function(element, type, handler)
{
if(element.addEventListener) element.addEventListener(type, handler, false);
else element.attachEvent('on'+type, handler);
},
// Return element of event
getEventElement: function(e)
{
e = e ? e : window.event;
return e.target ? e.target : e.srcElement;
2013-05-01 20:16:05 +00:00
},
// Return keycode of event
getEventKeycode: function(e)
{
e = e ? e : window.event;
return e.keyCode
2013-05-23 06:24:50 +00:00
},
// Set element width/height in pixel, including padding and border
setOuterWidth: function(element, width, maxWidth)
{
width -= this.getBoxSize(element).width;
if(maxWidth)
{
element.style.maxWidth = Math.max(0, width) + "px";
} else {
element.style.width = Math.max(0, width) + "px";
}
},
setOuterHeight: function(element, height, maxHeight)
{
height -=this.getBoxSize(element).height;
if(maxHeight)
{
element.style.maxHeight = Math.max(0, height) + "px";
} else {
element.style.height = Math.max(0, height) + "px";
}
},
// Return element width/height in pixel, including padding and border
getOuterWidth: function(element, includeMargin)
{
width = element.offsetWidth;
if(includeMargin) width += this.getMarginSize(element).width;
return width;
},
getOuterHeight: function(element, includeMargin)
{
height = element.offsetHeight;
if(includeMargin) height += this.getMarginSize(element).height;
return height;
},
// Return element width/height in pixel
getWidth: function(element)
{
return element.offsetWidth - this.getBoxSize(element).width;
},
getHeight: function(element)
{
return element.offsetHeight - this.getBoxSize(element).height;
},
// Set element top/left position in pixel
setOuterTop: function(element, top, marginTop)
{
if(marginTop)
{
element.style.marginTop = Math.max(0, top) + "px";
} else {
element.style.top = Math.max(0, top) + "px";
}
},
setOuterLeft: function(element, left, marginLeft)
{
if(marginLeft)
{
element.style.marginLeft = Math.max(0, left) + "px";
} else {
element.style.left = Math.max(0, left) + "px";
}
},
// Return element top/left position in pixel
getOuterTop: function(element)
{
var top = element.getBoundingClientRect().top;
return top + (window.pageYOffset || document.documentElement.scrollTop);
},
getOuterLeft: function(element)
{
var left = element.getBoundingClientRect().left;
return left + (window.pageXOffset || document.documentElement.scrollLeft);
},
// Return window width/height in pixel
getWindowWidth: function()
{
return window.innerWidth || document.documentElement.clientWidth;
},
getWindowHeight: function()
{
return window.innerHeight || document.documentElement.clientHeight;
},
// Return element CSS property
getStyle: function(element, property)
{
var string = "";
if(window.getComputedStyle)
{
string = window.getComputedStyle(element, null).getPropertyValue(property);
} else {
property = property.replace(/\-(\w)/g, function(match, m) { return m.toUpperCase(); });
string = element.currentStyle[property];
}
return string;
},
// Return element CSS padding and border
getBoxSize: function(element)
{
var paddingLeft = parseFloat(this.getStyle(element, "padding-left")) || 0;
var paddingRight = parseFloat(this.getStyle(element, "padding-right")) || 0;
var borderLeft = parseFloat(this.getStyle(element, "border-left-width")) || 0;
var borderRight = parseFloat(this.getStyle(element, "border-right-width")) || 0;
var width = paddingLeft + paddingRight + borderLeft + borderRight;
var paddingTop = parseFloat(this.getStyle(element, "padding-top")) || 0;
var paddingBottom = parseFloat(this.getStyle(element, "padding-bottom")) || 0;
var borderTop = parseFloat(this.getStyle(element, "border-top-width")) || 0;
var borderBottom = parseFloat(this.getStyle(element, "border-bottom-width")) || 0;
var height = paddingTop + paddingBottom + borderTop + borderBottom;
return { "width":width, "height":height };
},
// Return element CSS margin
getMarginSize: function(element)
{
var marginLeft = parseFloat(this.getStyle(element, "margin-left")) || 0;
var marginRight = parseFloat(this.getStyle(element, "margin-right")) || 0;
var width = marginLeft + marginRight;
var marginTop = parseFloat(this.getStyle(element, "margin-top")) || 0;
var marginBottom = parseFloat(this.getStyle(element, "margin-bottom")) || 0;
var height = marginTop + marginBottom;
return { "width":width, "height":height };
},
// Check if element exists and is visible
isVisible: function(element)
{
return element && element.style.display != "none";
2013-12-05 22:30:45 +00:00
},
// Submit form with post method
submitForm: function(params)
{
var elementForm = document.createElement("form");
elementForm.setAttribute("method", "post");
for(var key in params)
{
if(!params.hasOwnProperty(key)) continue;
var elementInput = document.createElement("input");
elementInput.setAttribute("type", "hidden");
elementInput.setAttribute("name", key);
elementInput.setAttribute("value", params[key]);
elementForm.appendChild(elementInput);
}
document.body.appendChild(elementForm);
elementForm.submit();
2013-04-14 22:41:04 +00:00
}
}
yellow.webinterface.init();