Started template implemenation

This commit is contained in:
Pierre 2017-01-25 16:52:22 +01:00
parent 979940fdb9
commit 5794179800
8 changed files with 140 additions and 17 deletions

View File

@ -11,7 +11,7 @@
* @param {Object} params The params to include in request
* @param {Function} nextAction What to do next
*/
ComunicWeb.common.network.makeAPIrequest = function(apiURI, params, nextAction){
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
//Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI;

View File

@ -26,7 +26,7 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo
nextAction = function(){};
//Send API request
ComunicWeb.common.network.makeAPIrequest(apiURI, params, nextAction);
ComunicWeb.common.api.makeAPIrequest(apiURI, params, nextAction);
}
/**
@ -74,10 +74,18 @@ ComunicWeb.common.error.fatalError = function(errorMessage, errorCode, errorData
/**
* Handle and show a 404 not found error message
*
* @param {Object} additionnalData Additionnal data passed in the method
* @param {element} targetElement Where the template will be applied
* @return {Boolean} True for a success
*/
ComunicWeb.common.error.pageNotFound = function(){
alert("404 not found");
ComunicWeb.common.error.pageNotFound = function(additionnalData, targetElement){
//Show template element
var templateURI = "common/errors/error.tpl";
var dataTemplate = {
};
ComunicWeb.common.page.getAndShowTemplate(targetElement, dataTemplate, templateURI, (function(){}), true);
//Report error
var errorData = {

View File

@ -15,10 +15,9 @@ var ComunicWeb = {
*/
common:{
/**
* Network functions
* API functions
*/
network: {
api: {
/**
* Make an API request
*/
@ -78,14 +77,17 @@ var ComunicWeb = {
/**
* Handle a 404 not found error
*/
pageNotFound: function(){},
pageNotFound: function(additionnalData, targetElement){},
},
/**
* URL functions
*/
url:{
/**
* Return current URL opened on the website
*/
getCurrentWebsiteURL: function(){},
},
/**
@ -115,7 +117,23 @@ var ComunicWeb = {
/**
* Load, parse and show a template
*/
//Not implemented yet
getAndShowTemplate: function(targetElem, dataTemplate, templateURI, nextAction, cleanContener){},
},
/**
* Network common requests
*/
network: {
/**
* @var {object} Cache contener
*/
cache: {},
/**
* Make a get request
*/
getRequest: function(url, cache, GETnextAction){},
},
/**

View File

@ -0,0 +1,65 @@
/**
* Network functions
*
* @author Pierre HUBERT
*/
ComunicWeb.network = {
/**
* @var {object} Cache contener
*/
cache: {},
/**
* Make a GET request
*
* @param {String} url Destination URL
* @param {Boolean} cache Specify if data can be cached or not (optimize network)
* @param {function} GETnextAction What to do next
* @return {Boolean} False if it fails
*/
getRequest: function(url, cache, GETnextAction){
//First, check if it is required to cache the request
if(cache){
//Prepare cache entry name
var cacheEntryName = encodeURIComponent(url);
//Check if entry exists
if(this.cache[cacheEntryName]){
//Call next action with the url contained into the cache
GETnextAction(this.cache[cacheEntryName]);
//Quit function
return true;
}
}
//No cache entry where found or cache is disabled, continue
var xhrRequest = new XMLHttpRequest();
xhrRequest.open("GET", url);
xhrRequest.onreadystatechange = function(){
if(xhrRequest.readyState == 4){
//We check if it is an error
if(xhrRequest.status != 200){
//It's an error, we will quit soon, but debug message before
ComunicWeb.debug.logMessage("GET request failed on " + url + " Got response code " + xhrRequest.status);
return false;
}
//Check if it is required to cache result
if(cache){
ComunicWeb.network.cache[cacheEntryName] = xhrRequest.responseText;
}
//Call next action
GETnextAction(xhrRequest.responseText);
}
}
//Perform request
xhrRequest.send(null);
},
};

View File

@ -98,7 +98,7 @@ ComunicWeb.common.page = {
//If we didn't get anything, clean the page and create a wrapper element
if(!mainContenerElem){
mainConterElem = this.emptyPage(true);
mainContenerElem = this.emptyPage(true);
}
//Check if some additionnal data was specified
@ -106,7 +106,7 @@ ComunicWeb.common.page = {
additionnalData = {};
//Call the method related to the page
eval(pageInfos.methodHandler + ("(additionnalData, mainContenerElem);"));
},
@ -130,9 +130,25 @@ ComunicWeb.common.page = {
* Load, parse and show a template
*
* @param {Object} targetElem The target element where the template will be applied
* @param {Object} ResumeHERE
* @param {Object} dataTemplate Datas to pass to the template (to parse it)
* @param {String} templateURI URI pointing on the template
* @param {function} nextAction What to do once the template is loaded
* @param {Boolean} cleanContener Specify if contener has to be cleaned or not
* @return {Boolean} False if it fails
*/
//getAndShowTemplate: function(){
getAndShowTemplate: function(targetElem, dataTemplate, templateURI, nextAction, cleanContener){
//}
//First, get the template URL
templateURL = ComunicWeb.__config.templatesURL + templateURI;
//Define how to apply the template
var afterDownloadTemplateContent = function(templateContent){
targetElem.innerHTML = (templateContent);
}
//Perform request
if(!ComunicWeb.network.getRequest(templateURL, true, afterDownloadTemplateContent))
//An error occured
return false;
},
};

View File

@ -0,0 +1,7 @@
<div>
<h1 style='text-align: center;'><b>{error_code}</b> <small>{error_title}</small></h1>
<p style='text-align:justify;'>
{error_message}
</p>
</div>

View File

@ -27,8 +27,11 @@ $config['JSfiles'] = array(
"%PATH_ASSETS%adminLTE/plugins/jQuery/jquery-2.2.3.min.js",
"%PATH_ASSETS%adminLTE/plugins/jquery-ui/jquery-ui.min.js",
//App scripts
//Functions schema
"%PATH_ASSETS%js/common/functionsSchema.js",
//App scripts
"%PATH_ASSETS%js/common/network.js",
"%PATH_ASSETS%js/pagesList.js",
"%PATH_ASSETS%js/common/api.js",
"%PATH_ASSETS%js/common/errors.js",
@ -52,3 +55,6 @@ $config['productionMode'] = 0;
//Application version
$config['appVersion'] = "0.1";
//Templates URL
$config['templatesURL'] = $config['pathAssets']."templates/";

View File

@ -47,6 +47,9 @@
//Assets URL
assetsURL: "<?php echo config['pathAssets']; ?>",
//Templates URL
templatesURL : "<?php echo config['templatesURL']; ?>",
//Site URL
siteURL: "<?php echo config['siteURL']; ?>",