Files
PetitTeton/public/admin/js/framework_lite.js

153 lines
5.2 KiB
JavaScript

//
// Requires jquery at a minimum.
//
var framework = new BrainstormFramework();
var brainstormFramework = framework;
function BrainstormFramework() {
//
// Loads the html at the given URL into the container. The container will be emptied of all content prior to loading. Any scripts inside <runonce>..</runonce> tags will be removed and executed as soon as the html is loaded.
// @param containerRef The jquery reference to the container for the html. This can be for example 'body' to reference the body node, or '#body' to reference the node with the ID of 'body'.
// @param url The URL of the html to be loaded.
//
this.append = function(containerRef, url) {
var _this = this;
var container = containerRef ? $(containerRef) : null;
$.ajax({url: url, dataType: 'html', async: false, success: function(data) {
data = _this.extractViewData(data);
if(data.view) {
if(container) {
container.append(data.view);
}
else {
htmlHandler(data.view);
}
}
if(data.script && data.script.length > 0) {
try {
eval(data.script);
} catch(err) {
alert(err);
}
}
}});
}
//
// Loads the html at the given URL into the container. The container will be emptied of all content prior to loading. Any scripts inside <runonce>..</runonce> tags will be removed and executed as soon as the html is loaded.
// @param containerRef The jquery reference to the container for the html. This can be for example 'body' to reference the body node, or '#body' to reference the node with the ID of 'body'.
// @param url The URL of the html to be loaded.
// @param htmlHandler The optional handler to be called to place the html. This may be specified in place of the container ID. The handler will be passed the HTML for the view as a string.
//
this.load = function(containerRef, url, htmlHandler) {
var _this = this;
var container = containerRef ? $(containerRef) : null;
if(container) {
container.empty();
}
$.ajax({url: url, dataType: 'html', async: false, success: function(data) {
data = _this.extractViewData(data);
if(data.view) {
if(container) {
container.html(data.view);
}
else {
htmlHandler(data.view);
}
}
if(data.script && data.script.length > 0) {
try {
eval(data.script);
} catch(err) {
alert(err);
}
}
}});
}
this.extractViewData = function(viewData) {
if(viewData != undefined) {
var data = {script: "", metadata: undefined, view: ""};
var start;
//Remove the escaping that allowed it to be sent as part of a JSON response.//
viewData = this.unescape(viewData);
//Strip out any run-once scripts to be run after loading the html.//
while(viewData.indexOf("<runonce>") != -1) {
//extract the script.//
data.script += viewData.substring(viewData.indexOf("<runonce>") + 9, viewData.indexOf("</runonce>")).replace("<!--", "").replace("//-->", "");
//Remove the script from the view data.//
viewData = viewData.substring(0, viewData.indexOf("<runonce>")) + viewData.substring(viewData.indexOf("</runonce>") + 10);
}
//Detect and remove any metadata.//
if((start = viewData.indexOf('<metadata>')) != -1) {
var end = viewData.indexOf('</metadata>', start + 10);
var metadata = viewData.substring(start, end + 11);
//Remove the metadata from the document.//
viewData = viewData.substring(0, start) + viewData.substring(end + 11);
//Parse the metadata XML.//
data.metadata = $.parseXML(metadata);
}
else if((start = viewData.indexOf('<metadata ')) != -1) {
var end = viewData.indexOf('/>', start + 10);
var metadata = viewData.substring(start, end + 2);
//Remove the metadata from the document.//
viewData = viewData.substring(0, start) + viewData.substring(end + 2);
//Parse the metadata XML.//
data.metadata = $.parseXML(metadata);
}
else if((start = viewData.indexOf('<metadata/>')) != -1) {
viewData = viewData.substring(0, start) + viewData.substring(start + 11);
}
//Strip out any comments.//
while(viewData.indexOf("<!--") != -1) {
//Remove the comment from the view data.//
viewData = viewData.substring(0, viewData.indexOf("<!--")) + viewData.substring(viewData.indexOf("-->") + 3);
}
data.view = viewData;
return data;
}
}
//
// Removes escape characters from text.
// @param text The text whose escape characters are to be removed.
//
this.unescape = function(text) {
var result = text.replace(/\x7C1/g, "\\").replace(/\x7C2/g, "'").replace(/\x7C3/g, "\"").replace(/\x7C4/g, "\x0D").replace(/\x7C5/g, "\x09").replace(/\x7C7/g, "&").replace(/\x7C8/g, "<").replace(/\x7C9/g, ">").replace(/\x7C6/g, "\x7C");
return result;
};
//
// Adds escape characters to text.
// @param text The text whose escape characters are to be added. If this is undefined then the result will be an empty string.
//
this.escape = function(text) {
var result;
if(text) {
result = text.replace(/\x0A\x0D/g, "\n").replace(/\x7C/g, "\x7C6").replace(/\\/g, "\x7C1").replace(/\'/g, "\x7C2").replace(/\"/g, "\x7C3").replace(/\n/g, "\x7C4").replace(/\x09/g, "\x7C5").replace(/%/g, "\x7C6").replace(/&/g, "\x7C7").replace(/\x3C/g, "\x7C8").replace(/\x3E/g, "\x7C9");
}
else {
result = "";
}
return result;
};
}