ComunicWeb/assets/js/common/page.js

485 lines
16 KiB
JavaScript
Raw Normal View History

2017-01-21 18:30:27 +00:00
/**
* Page functions
*
* @author Pierre HUBERT
*/
ComunicWeb.common.page = {
/**
* Save the current page url
*/
_current_url: "",
2017-01-21 18:30:27 +00:00
/**
* Empty current page content
2017-01-22 12:03:31 +00:00
*
* @param {Boolean} createWrapper Optionnal, define if it is required to add a wrapper
* container to the page
* @return {Object} Wrapper element if it is created
2017-01-21 18:30:27 +00:00
*/
2017-01-22 12:03:31 +00:00
emptyPage: function(createWrapper){
2017-01-21 18:30:27 +00:00
//Empty body tag
document.body.innerHTML = "";
//Remove body speicific tags
document.body.className = "";
document.body.id = "";
document.body.onclick = "";
//Log message
ComunicWeb.debug.logMessage("Clean the screen.");
2017-01-22 12:03:31 +00:00
//If required, create the wrapper element
if(createWrapper){
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.id = "wrapper";
document.body.appendChild(wrapper);
//Return link to wrapper
return(wrapper);
}
2017-01-21 18:30:27 +00:00
},
/**
* Show a full wait splash screen
2017-05-21 16:26:18 +00:00
*
* @param {String} message A message to explain the reason of the splash screen (optionnal)
2017-01-21 18:30:27 +00:00
*/
2017-05-21 16:26:18 +00:00
showWaitSplashScreen: function(message){
2017-01-21 18:30:27 +00:00
//First, empty the screen
this.emptyPage();
//Log message
2017-06-14 09:46:10 +00:00
ComunicWeb.debug.logMessage("Display a wait splash screen on the screen.");
2017-01-21 18:30:27 +00:00
2017-05-21 16:26:18 +00:00
//Create message element (if required)
if(message){
var messageElem = createElem("div", document.body);
messageElem.className = "text";
messageElem.innerHTML = message;
}
2017-01-21 18:30:27 +00:00
//Create image element
var imgElem = document.createElement("img");
imgElem.src = ComunicWeb.__config.assetsURL+"img/roundProgress.gif";
document.body.appendChild(imgElem);
//Change body className
document.body.className = "waitSplashScreen";
},
2017-02-24 08:57:27 +00:00
/**
* Show a transparent wait splash screen
*
2017-06-07 14:55:47 +00:00
* @param {HTMLElement} target Optionnal, defines the target of the transparent splashscreen
* @returns {HTMLElement} The splash screen element to let it being deleted
2017-02-24 08:57:27 +00:00
*/
2017-06-07 14:55:47 +00:00
showTransparentWaitSplashScreen: function(target){
2017-02-24 08:57:27 +00:00
//Create the element
2017-06-07 14:55:47 +00:00
var waitSplashScreen = createElem("div");
2017-02-24 08:57:27 +00:00
waitSplashScreen.className = "transparentWaitSplashScreen";
//Populate it
2017-06-07 14:55:47 +00:00
var imgElem = createElem("img");
2017-02-24 08:57:27 +00:00
imgElem.src = ComunicWeb.__config.assetsURL+"img/barProgress.gif";
waitSplashScreen.appendChild(imgElem);
//Apply splash screen
2017-06-07 14:55:47 +00:00
if(!target)
document.body.appendChild(waitSplashScreen);
else
target.appendChild(waitSplashScreen);
2017-02-24 08:57:27 +00:00
//Return wait splash screen element
return waitSplashScreen;
},
2017-01-21 18:30:27 +00:00
/**
* Open a page
*
* @param {String} pageURI The URI to the page
2017-01-22 17:46:06 +00:00
* @param {Object} additionnalData Additionnal data to pass to the new page
2017-05-25 11:55:49 +00:00
* @return {Boolean} True for a success
2017-01-21 18:30:27 +00:00
*/
2017-01-22 17:46:06 +00:00
openPage: function(pageURI, additionnalData){
2017-12-24 15:24:31 +00:00
2017-01-21 18:30:27 +00:00
//Log message
ComunicWeb.debug.logMessage("Open the following page: " + pageURI);
2017-01-22 12:03:31 +00:00
2017-12-24 15:24:31 +00:00
//Check if some additionnal data was specified
if(!additionnalData)
additionnalData = {};
2017-01-22 12:03:31 +00:00
//Extract the first part of the URL
2017-02-24 08:57:27 +00:00
var firstPartURI = pageURI.toString();
2017-01-22 12:03:31 +00:00
2017-03-01 14:37:55 +00:00
//Check if there are hashtag for the URL
if(firstPartURI.indexOf("#") != -1){
firstPartURI = firstPartURI.split("#")[0];
}
2017-05-28 11:48:19 +00:00
//Check if there are $_GET parametres included with the URL
2017-12-24 15:24:31 +00:00
if(firstPartURI.includes("?")){
var split = firstPartURI.split("?");
firstPartURI = split[0];
additionnalData.urlArgs = {};
//Process the arguments
var list = split[1].split("&");
for(i in list){
//Check if it is a real argument
if(list[i].length > 0){
if(!list[i].includes("=")){
additionnalData.urlArgs[list[i]] = null;
} else {
//Add the argument to the list
var argument = list[i].split("=");
additionnalData.urlArgs[argument[0]] = argument[1];
}
}
}
2017-05-28 11:48:19 +00:00
}
2017-01-22 12:03:31 +00:00
//Check if pageURI is empty
if(firstPartURI == ""){
firstPartURI = "home";
}
//Check if there is also subfolders
2017-03-01 14:37:55 +00:00
if(firstPartURI.indexOf("/") != -1){
//Save the list of subfolders
var subfoldersURIarray = firstPartURI.split("/");
subfoldersURIarray.shift();
subfoldersURI = subfoldersURIarray.join("/");
//Remove them to find the right page
2017-01-22 12:03:31 +00:00
firstPartURI = firstPartURI.split("/")[0];
} else {
//No subfolder was specified
subfoldersURI = false;
2017-01-22 12:03:31 +00:00
}
//Check if specied page exists
if(ComunicWeb.pagesList[firstPartURI]){
var pageInfos = ComunicWeb.pagesList[firstPartURI];
}
//Else we include the 404 not found page
else{
var pageInfos = ComunicWeb.pagesList.notFound;
}
//Change page title
document.title = pageInfos.pageTitle;
2017-02-24 08:57:27 +00:00
//Change page URL
ComunicWeb.common.url.changeURI(document.title, pageURI);
//Save new url
this._current_url = window.location.href.toString();
2018-03-25 07:43:39 +00:00
//Get the main container of the page
var mainContainerElem = byId("wrapper");
2017-01-22 12:03:31 +00:00
//If we didn't get anything, clean the page and create a wrapper element
2018-03-25 07:43:39 +00:00
if(!mainContainerElem){
var mainContainerElem = this.emptyPage(true);
2017-01-22 12:03:31 +00:00
}
2017-01-22 17:46:06 +00:00
2017-05-21 15:42:04 +00:00
//We check if the page is a full screen page or not
2017-05-21 15:25:41 +00:00
if(pageInfos.disableMenus){
//We force the screen to be cleaned
2018-03-25 07:43:39 +00:00
var mainContainerElem = this.emptyPage(true);
var pageTarget = mainContainerElem; //The page directly goes to the main target
2017-05-21 15:25:41 +00:00
}
//Else
else {
2017-05-21 15:42:04 +00:00
//We try to locate the target of the page
var pageTarget = byId("pageTarget");
//We empty screen if we couldn't rich it
if(!pageTarget){
2018-03-25 07:43:39 +00:00
mainContainerElem.innerHTML = "";
2017-05-21 15:42:04 +00:00
//We create the pagetTarget element
2018-03-25 07:43:39 +00:00
var pageTarget = createElem("div", mainContainerElem);
2017-05-21 15:42:04 +00:00
pageTarget.id = "pageTarget";
}
2017-05-21 16:10:55 +00:00
else{
//We can empty page target (much faster)
pageTarget.innerHTML = "";
}
2017-05-21 15:42:04 +00:00
//Set wrapper class
2017-05-21 15:55:19 +00:00
pageTarget.className = "content-wrapper";
2017-05-21 15:42:04 +00:00
//Ask adminLTE to fix layout
if($.AdminLTE.layout)
$.AdminLTE.layout.fix();
2017-05-21 15:42:04 +00:00
//Set body class
2017-05-25 13:41:27 +00:00
document.body.className="hold-transition fixed skin-blue layout-top-nav";
2017-05-21 15:42:04 +00:00
2017-05-21 15:55:19 +00:00
//We load the menubar
ComunicWeb.components.menuBar.common.display();
2017-05-28 11:48:19 +00:00
//We load specific components for logged in users
if(ComunicWeb.user.userLogin.getUserLoginState()){
2017-06-03 16:18:43 +00:00
//We load friends list (if user is logged in)
2017-05-31 15:12:51 +00:00
ComunicWeb.components.friends.bar.display();
2017-06-03 16:18:43 +00:00
2017-06-07 17:24:48 +00:00
//We load conversations manager (login required)
ComunicWeb.components.conversations.manager.display();
2017-05-28 11:48:19 +00:00
}
2017-05-21 15:25:41 +00:00
}
//Add the subfolder URI (if any)
if(subfoldersURI){
additionnalData.subfolder = subfoldersURI;
}
2017-01-22 17:46:06 +00:00
//Call the method related to the page
2017-05-21 15:25:41 +00:00
eval(pageInfos.methodHandler + ("(additionnalData, pageTarget);"));
2017-01-22 12:03:31 +00:00
2017-05-25 11:55:49 +00:00
//Success
return true;
2017-01-22 12:03:31 +00:00
},
/**
* Refresh the current page
*/
refresh_current_page: function(){
//Get current page URI
var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL();
//Open a page
this.openPage(currentPage);
},
/**
* Inform of page location update
*
* @param {location} new_location The new location of the page
*/
location_updated: function(new_location){
//Check if the url change has already been handled or not
if(new_location.href.toString() != this._current_url)
//Open the page using url detection
this.refresh_current_page();
},
2017-01-22 12:03:31 +00:00
/**
* Prepare a template load by specifiying datas
*
2018-03-25 07:43:39 +00:00
* @return {Object} The object container with all required infos
2017-01-22 12:03:31 +00:00
*/
prepareLoadTemplate: function(){
//Create an object
var obj = {
templateURL: "",
templateDatas: "",
};
//Return object
return obj;
2017-01-22 17:46:06 +00:00
},
2017-01-22 12:03:31 +00:00
/**
2017-02-21 11:37:26 +00:00
* Load, parse and show an HTML template
2017-01-22 12:03:31 +00:00
*
* @param {Object} targetElem The target element where the template will be applied
2017-01-25 15:52:22 +00:00
* @param {Object} dataTemplate Datas to pass to the template (to parse it)
* @param {String} templateURI URI pointing on the template
2017-02-21 11:37:26 +00:00
* @param {function} afterParsingHTMLtemplate What to do once the template is loaded
2018-03-25 07:43:39 +00:00
* @param {Boolean} cleanContainer Specify if container has to be cleaned or not
2017-01-25 15:52:22 +00:00
* @return {Boolean} False if it fails
2017-01-22 12:03:31 +00:00
*/
2018-03-25 07:43:39 +00:00
getAndShowTemplate: function(targetElem, dataTemplate, templateURI, afterParsingHTMLtemplate, cleanContainer){
2017-01-22 17:46:06 +00:00
2017-01-25 15:52:22 +00:00
//First, get the template URL
templateURL = ComunicWeb.__config.templatesURL + templateURI;
//Define how to apply the template
var afterDownloadTemplateContent = function(templateContent){
2017-01-28 13:07:18 +00:00
2018-03-25 07:43:39 +00:00
//If required, clean the container
if(cleanContainer){
2017-02-21 11:37:26 +00:00
targetElem.innerHTML = "";
}
2017-01-28 13:07:18 +00:00
//Apply data templates
for(elemName in dataTemplate){
//We change the template content while it still exists
while(templateContent.indexOf("{"+elemName+"}") != -1){
templateContent = templateContent.replace("{"+elemName+"}", dataTemplate[elemName]);
}
}
//Apply template source
targetElem.innerHTML = templateContent;
2017-02-21 11:37:26 +00:00
2017-02-24 08:57:27 +00:00
//Make a link live
var aElems = targetElem.getElementsByTagName("a");
for(num in aElems){
//Export current element
var currentElement = aElems[num];
//Check if it is a real html elements and if it contains a "target" attribute
if(currentElement.attributes){
if(currentElement.attributes.target){
//Change the onclick behavior of the elements
currentElement.onclick = (function() {
ComunicWeb.common.page.openPage(this.getAttribute("target"));
});
}
}
}
2017-02-21 11:37:26 +00:00
//Perform next action (if there is)
if(afterParsingHTMLtemplate)
afterParsingHTMLtemplate();
};
2017-01-25 15:52:22 +00:00
//Perform request
2017-02-21 11:37:26 +00:00
if(!ComunicWeb.common.network.getRequest(templateURL, true, afterDownloadTemplateContent))
2017-01-25 15:52:22 +00:00
//An error occured
return false;
},
2017-02-21 11:37:26 +00:00
/**
* Convert a JSON object into html elements
*
* @param {Object} parentNodeChilds The parent which contains the childs to convert (an object)
* @param {Object} values Optionnal, fill the template with predefined values
* @returns {HTMLObject} The processed JSON code
*/
convertJSONobjectTOhtmlElement: function(parentNodeChilds, values){
//Create variable
var resultElements = {};
//Process each element of the array
for(elemID in parentNodeChilds){
//Determine object type
var objType = (parentNodeChilds[elemID].nodeType ? parentNodeChilds[elemID].nodeType : elemID);
//Create object
var element = document.createElement(objType);
element.elemID = elemID;
//Populate it with its informations
for(fieldName in parentNodeChilds[elemID]){
if(fieldName == "nodeType"){
//Do nothing
}
//We perform children generation if required
else if(fieldName == "children"){
//Call the function to get the element's childs and apply them
var elemChilds = this.convertJSONobjectTOhtmlElement(parentNodeChilds[elemID][fieldName], values);
for(childID in elemChilds){
element.appendChild(elemChilds[childID]);
}
}
//We check if it is innerHTML filling
else if(fieldName == "innerHTML"){
element.innerHTML = parentNodeChilds[elemID][fieldName];
}
//We check if it is auto filling system which is called
else if (fieldName == "autofill"){
//Check if required value exists in the data
if(values){
if(values[parentNodeChilds[elemID][fieldName]]){
//Then fill field with the value
element.innerHTML = values[parentNodeChilds[elemID][fieldName]];
}
}
}
//For other input, we use "setAttribute"
else{
element.setAttribute(fieldName, parentNodeChilds[elemID][fieldName]);
}
}
//Save element
resultElements[element.elemID] = element;
}
//Return result
return resultElements;
},
/**
* Get and show a JSON template
*
* @param {Object} targetElem The target element where the template will be applied
* @param {String} templateURI URI pointing on the template
* @param {Object} additionalData Additionnal to pass to the template
* @param {function} afterParsingJSONtemplate What to do once JSON template is loaded
2018-03-25 07:43:39 +00:00
* @param {Boolean} cleanContainer Specify wether the template container has to be cleaned or not
2017-02-21 11:37:26 +00:00
* @return {Boolean} Flase if it fails
*/
2018-03-25 07:43:39 +00:00
getAndShowJSONtemplate: function(targetElem, templateURI, additionalData, afterParsingJSONtemplate, cleanContainer){
2017-02-21 11:37:26 +00:00
//Define template URL
var templateURL = ComunicWeb.__config.templatesURL + templateURI;
//Define how to apply the template
var afterTemplateDownload = function(templateContent){
//Decode JSON content
var JSONobject = JSON.parse(templateContent);
//Check if parsing failed
if(!JSONobject){
ComunicWeb.debug.logMessage("Parsing JSON failed with this file: " + templateURL);
return false;
}
//Parse JSON object
var result = ComunicWeb.common.page.convertJSONobjectTOhtmlElement(JSONobject, additionalData);
//Apply each result element
for(elem in result){
targetElem.appendChild(result[elem]);
}
//Perform next action if required
if(afterParsingJSONtemplate){
afterParsingJSONtemplate();
}
//Everything OK
return true;
};
//Perform request
if(!ComunicWeb.common.network.getRequest(templateURL, true, afterTemplateDownload))
//An error occured
return false;
}
2017-01-21 18:30:27 +00:00
};