/**
* Created by dev013 on 9/9/14.
*/
var FileLoadTools = (function () {
var my = {};
var htmlFile = [];// array for storing name of files loaded
var jsFile = [];
var cssFile = [];
/**
* @param {Object} file filename
* @param {Object} fileArray
* function for judging if the file is loaded or not
*/
function isLoaded(file, fileArray) {
for (var i = 0; i < fileArray.length; i++) {
if (fileArray[i] == file) {
return true;
}
}
return false;
}
function getLastArray(array) {
return array[array.length - 1];
}
/**
* @param {Object} baseurl
* @param {Object} names
* function to load js files by names
*/
my.loadJsFile = function (names) {
var name = getLastArray(names);
if (!isLoaded(name, jsFile)) {
var url = g_applicationContext + g_jsPath + names.join("/") + ".js";
$("head").append("<script src='" + url + "'></script>");
jsFile.push(name);
}
};
/**
* load js files by jQuery ,support callback after js file loaded.
* @method loadJsFileByJQ
* @param {Array} names
* @param {Function} onload
*/
my.loadJsFileByJQ = function (names, onload) {
var name = getLastArray(names);
if (!isLoaded(name, jsFile)) {
var url = g_applicationContext + g_jsPath + names.join("/") + ".js";
jsFile.push(name);
$.getScript(url, function (data, textStatus, jqxhr) {
if (typeof onload == 'function') {
onload.apply();
}
});
}
};
my.loadMock = function (name, onload) {
if (!isLoaded(name, jsFile)) {
var url = g_applicationContext + g_mockPath + name + ".js";
jsFile.push(name);
$("head").append("<script src='" + url + "'></script>");
}
}
my.loadCssFile = function (names) {
var name = names[0];
if (!isLoaded(name, cssFile)) {
var url = g_applicationContext + g_cssPath + name + ".css";
$("head").append("<link type='text/css' rel='stylesheet' href='" + url + "'>");
cssFile.push(name);
}
};
my.loadRqrFiles = function (names, jqObj) {
var name = getLastArray(names);
if (!isLoaded(name, htmlFile)) {
var url = g_applicationContext + g_htmlPath + names.join("/") + ".html";
var func = function (data) {
//after the html file loaded,load the js and css files
jqObj.append(data);
htmlFile.push(name);
my.loadJsFile(names);
my.loadCssFile(names);
if (names.length == 1) {
populateThePageWithLanguageSetting(names.join("-") + "Page");
} else {
populateThePageWithLanguageSetting(names.join("-"));
}
};
var errorFunc = function (xhr) {
alert(xhr.statusText);
};
$.ajax({
async: false,
url: url,
type: "GET",
success: func,
error: errorFunc
});
}
};
return my;
}());
/*
*function for load file needed when menu item clicked
**/
$.fn.loadPageToContent = function (names) {
FileLoadTools.loadRqrFiles(names, $(this));
};