pets/old/libs/bluecore.js

128 lines
4.7 KiB
JavaScript

"use strict";
/////////////////////// BlueCore 1.7.2 \\\\\\\\\\\\\\\\\\\\\\\\\\
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.
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) {
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 buttonAdd (ParentID,ID,Label,Action,Class,Element) {
if ( "undefined" == typeof Class ) { Class = ""; }
if ( "undefined" == typeof Element) { Element = "div"; }
(function (button) {
button.innerHTML = Label;
button.onclick = function () { Action(); }
})(elementPlace(ParentID,ID,"button "+Class,Element));
}
function dialogControlsGen(parentDialog,DialogBack,type,returnVar) { //I need to refactor this, these if statments are too similar.
if ( "sub_window" == type ) {
var closeButton = elementPlace(parentDialog,"closeButton",null,"div"); //Also need to change this, to help standardize the look and feel of the UI
closeButton.innerHTML = "X";
closeButton.onclick = function () { document.getElementById("body").removeChild(DialogBack); }
//More controls will go here probably as I flesh out the sub window design
}
if ( "input" == type ) {
var inputArea = elementPlace(parentDialog,"listID","inputField","input",null);
var submitButton = buttonAdd(parentDialog,"SubmitButton","Submit",function () {
returnVar.value = inputArea.value;
document.getElementById("body").removeChild(DialogBack);
},"UI UIbutton");
}
if ( "input_password" == type ) {
var inputArea = elementPlace(parentDialog,"listID","inputField","input",null);
inputArea.type = "password";
var submitButton = buttonAdd(parentDialog,"SubmitButton","Submit",function () {
returnVar.value = inputArea.value;
document.getElementById("body").removeChild(DialogBack);
},"UI UIbutton");
}
if ( "info" == type ) {
var closeButton = elementPlace(parentDialog,"closeButton",null,"div");
closeButton.innerHTML = "Close";
closeButton.onclick = function () { document.getElementById("body").removeChild(DialogBack); }
}
if ( "end" == type ) {
var closeButton = elementPlace(parentDialog,"closeButton",null,"div");
closeButton.innerHTML = "Play Again";
closeButton.onclick = function () { document.getElementById("body").removeChild(DialogBack); gameInit(); }
}
}
function dialogMessage (content,type,returnVar) {
var message = elementPlace("#body","app_message_back","app_Message_Back","div");
var dialog = elementPlace("#app_message_back","app_message","app_Message","div");
dialog.innerHTML = content;
dialogControlsGen("#app_message",message, type, returnVar);
}