blue.js/libs/bluecore.js

151 lines
5.5 KiB
JavaScript

"use strict";
/////////////////////// BlueCore 1.8.3 \\\\\\\\\\\\\\\\\\\\\\\\\\
function getData(url,dothis,tothis,type,body) {
var DataRequest = new XMLHttpRequest();
if (undefined == type) { type = "GET"; }
if (undefined == body) { body = ""; }
DataRequest.open(type, url, true);
DataRequest.onreadystatechange = function () {
if ( (DataRequest.readyState === XMLHttpRequest.DONE) && (DataRequest.status === 200) ) {
dothis(DataRequest.responseText,tothis);
}
};
DataRequest.send(body);
}
function isEmpty(object) {
console.log(object);
console.log(typeof object);
if ( "object" == typeof object ) {
for ( var propery in object ) {
return false;
}
return true;
} else {
return false;
}
}
function addJavascript(string,action) {
try {
window.eval(string);
if ("function" == typeof action) { action(); }
}
catch (e) { console.log("Error with external script: "+e); }
}
function getJavascript(url,action) {
getData(url,addJavascript,action);
}
function addHTMLfrag(string,target) {
target.innerHTML = string;
}
function getHTMLfrag(url,targetNode) {
var target = document.querySelector(targetNode);
target.innerHTML = "Loading...";
getData(url,addHTMLfrag,target);
}
function elementMake (ID,Class,element) { //new and improved, I can use more than just divs for things now.
ID = "string" == typeof ID ? ID.replace(/ /g,"_") : ID;
return (function (myElement) {
if ("string" == typeof ID) { myElement.id = ID; }
if ("string" == typeof Class) { myElement.className = Class; }
return myElement;
})(document.createElement(element));
}
function elementPlace (parentID, ID, Class, element, position) {
parentID = "string" == typeof parentID ? parentID.replace(/ /g,"_") : parentID;
ID = "string" == typeof ID ? ID.replace(/ /g,"_") : ID;
var newElement = elementMake(ID,Class,element);
if ( (typeof document.querySelector(parentID).append && typeof document.querySelector(parentID).prepend) !== "undefined") { // Are we compliant?
if ("before" == position) {
document.querySelector(parentID).prepend(newElement);
} else {
document.querySelector(parentID).append(newElement);
}
} else { //No? Ok we will use the old way.
if ("before" == position) {
var p = document.querySelector(parentID);
p.insertBefore(newElement,p.firstChild);
} else {
document.querySelector(parentID).appendChild(newElement);
}
}
return newElement;
}
function titledContainer (ParentID,ID,Class,Element,Position,Title) {
var outer = elementPlace(ParentID,"outer_"+ID,"outer "+Class,"div",Position);
outer.innerHTML = '<p class="title">'+Title+'</p>';
return elementPlace("#outer_"+ID,ID,Class,Element);
}
function numberShorten (Value,Accuricy) {
var level = 0;
var number = "number" == typeof Value ? Value : 0;
var places = "number" == typeof Accuricy ? Math.pow(10,Accuricy) : 1000;
var unit = ["","k","M","B","T","q","Q","s","S","O","N","d","Ud","Dd","Td","qd","Qd","sd","Sd","Od","Nd","v","Uv","Dv","Tv","qv","Qv","sv","Sv","Ov","Nv","t","Ut","Dt","Tt","qt","Qt","st","St","Ot","Nv","c","uc","dc","Tc","Dc","Uc","vc","Sc","Oc","Nc","STOP"];
while (999<number) {
level++;
number = Math.round(number)/1000;
}
return (Math.floor(number*places)/places)+unit[level];
}
function buttonAdd (ParentID,ID,Label,Action,Class,Element) {
if ( "undefined" == typeof Class ) { Class = ""; }
if ( "undefined" == typeof Element) { Element = "div"; }
var me = (function (button) {
button.innerHTML = Label;
button.onclick = function () { Action(); }
return button;
})(elementPlace(ParentID,ID,"button "+Class,Element));
return me;
}
function inputDialog (dialogRoot,container,args,callback) {
args = "object" == typeof args ? args : {"content":""};
args.content = "string" == typeof args.content ? args.content : "";
container.innerText += args.content;
var inputArea = elementPlace("#"+container.id,null,"formInput","input");
if ("string" == typeof args.inputType) { inputArea.type = args.inputType; }
buttonAdd("#"+container.id,null,"Submit",function () {
if ("file" == inputArea.type ) {
callback(inputArea.files);
} else {
callback(inputArea.value);
}
dialogRoot.parentNode.removeChild(dialogRoot);
},"dialog_submit");
}
function messageDialog (dialogRoot,container,args) {
args = "object" == typeof args ? args : {"content":""};
args.content = "string" == typeof args.content ? args.content : "";
container.innerText += args.content;
buttonAdd("#"+container.id,null,"Ok",function () {
dialogRoot.parentNode.removeChild(dialogRoot);
},"dialog_submit");
}
function popupDialog (ID, title, closeable, contentGen, genArgs, callback) {
if ("function" != typeof callback) { callback = function () { return 0; }; }
var dialogBack = elementPlace("body",ID+"_back","dialog_back","div");
var dialogWindow = elementPlace("#"+ID+"_back",ID,"dialog_window","div");
var dialogTitle = elementPlace("#"+ID,ID+"_title","dialog_title","div");
if ("string" == typeof title) { dialogTitle.innerText = title; }
if (true == closeable) {buttonAdd("#"+ID+"_title",null,"X",function () {dialogBack.parentNode.removeChild(dialogBack);},"dialog_close");}
var dialogContent = elementPlace("#"+ID,ID+"_content","dialog_content","div");
if ("function" == typeof contentGen) {
contentGen(dialogBack,dialogContent,genArgs,callback);
} else {
dialogContent.innerText = "string" == typeof contentGen ? contentGen : "Empty Dialog";
buttonAdd("#"+ID+"_content",null,"Close",callback);
}
}