From 5794179800c6481ba0dfb33f60a689b62b4b5caf Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 25 Jan 2017 16:52:22 +0100 Subject: [PATCH] Started template implemenation --- assets/js/common/api.js | 2 +- assets/js/common/errors.js | 14 +++-- assets/js/common/functionsSchema.js | 30 ++++++++--- assets/js/common/network.js | 65 ++++++++++++++++++++++++ assets/js/common/page.js | 26 ++++++++-- assets/templates/common/errors/error.tpl | 7 +++ corePage/config/dev.config.php | 10 +++- index.php | 3 ++ 8 files changed, 140 insertions(+), 17 deletions(-) create mode 100644 assets/js/common/network.js create mode 100644 assets/templates/common/errors/error.tpl diff --git a/assets/js/common/api.js b/assets/js/common/api.js index afba36db..b2e38b64 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -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; diff --git a/assets/js/common/errors.js b/assets/js/common/errors.js index 3af55804..d9532fda 100644 --- a/assets/js/common/errors.js +++ b/assets/js/common/errors.js @@ -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,11 +74,19 @@ 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 = { pageURL: location.href, diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 47e0e1dc..1e6088e5 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -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){}, }, /** diff --git a/assets/js/common/network.js b/assets/js/common/network.js new file mode 100644 index 00000000..5e0d8b3a --- /dev/null +++ b/assets/js/common/network.js @@ -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); + }, +}; \ No newline at end of file diff --git a/assets/js/common/page.js b/assets/js/common/page.js index ee8c2fd0..ca99e11e 100644 --- a/assets/js/common/page.js +++ b/assets/js/common/page.js @@ -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; + }, }; \ No newline at end of file diff --git a/assets/templates/common/errors/error.tpl b/assets/templates/common/errors/error.tpl new file mode 100644 index 00000000..8aafdd1b --- /dev/null +++ b/assets/templates/common/errors/error.tpl @@ -0,0 +1,7 @@ +
+

{error_code} {error_title}

+ +

+ {error_message} +

+
\ No newline at end of file diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index a52e449a..f5fe946f 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -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", @@ -51,4 +54,7 @@ $config['languagesPath'] = "%PATH_ASSETS%js/langs/"; $config['productionMode'] = 0; //Application version -$config['appVersion'] = "0.1"; \ No newline at end of file +$config['appVersion'] = "0.1"; + +//Templates URL +$config['templatesURL'] = $config['pathAssets']."templates/"; \ No newline at end of file diff --git a/index.php b/index.php index 28bb8d0f..7492c34f 100644 --- a/index.php +++ b/index.php @@ -47,6 +47,9 @@ //Assets URL assetsURL: "", + //Templates URL + templatesURL : "", + //Site URL siteURL: "",