From 520db2c32e24c002e37d523af160c5b4496e4b5d Mon Sep 17 00:00:00 2001 From: chylex <info@chylex.com> Date: Sun, 4 Sep 2016 04:33:55 +0200 Subject: [PATCH] Rewrite loadConfigurationFile in plugins.js to accept default config file --- Resources/Scripts/plugins.js | 61 ++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/Resources/Scripts/plugins.js b/Resources/Scripts/plugins.js index 7a720412..6f3fde40 100644 --- a/Resources/Scripts/plugins.js +++ b/Resources/Scripts/plugins.js @@ -2,25 +2,52 @@ // // Block: Setup a simple JavaScript object configuration loader. // - window.TDPF_loadConfigurationFile = function(pluginObject, fileName, onSuccess, onFailure){ - $TDP.readFile(pluginObject.$token, fileName, true).then(contents => { - var obj; + (function(){ + var continueLoading = function(token, identifier, fileName, onSuccess, onFailure){ + $TDP.readFile(token, fileName, true).then(contents => { + var obj; - try{ - obj = eval("("+contents+")"); - }catch(err){ - if (!(onFailure && onFailure(err.message))){ - alert("Problem loading '"+fileName+"' file for '"+pluginObject.$id+"' plugin, the JavaScript syntax is invalid: "+err.message); + try{ + obj = eval("("+contents+")"); + }catch(err){ + if (!(onFailure && onFailure(err.message))){ + alert("Problem loading '"+fileName+"' file for '"+identifier+"' plugin, the JavaScript syntax is invalid: "+err.message); + } + + return; } - return; - } + onSuccess && onSuccess(obj); + }).catch(err => { + if (!(onFailure && onFailure(err))){ + alert("Problem loading '"+fileName+"' file for '"+identifier+"' plugin: "+err); + } + }); + }; + + window.TDPF_loadConfigurationFile = function(pluginObject, fileNameUser, fileNameDefault, onSuccess, onFailure){ + var identifier = pluginObject.$id; + var token = pluginObject.$token; - onSuccess && onSuccess(obj); - }).catch(err => { - if (!(onFailure && onFailure(err))){ - alert("Problem loading '"+fileName+"' file for '"+pluginObject.$id+"' plugin: "+err); - } - }); - }; + $TDP.checkFileExists(token, fileNameUser).then(exists => { + if (!exists){ + $TDP.readFile(token, fileNameDefault, true).then(contents => { + $TDP.writeFile(token, fileNameUser, contents); + continueLoading(token, identifier, fileNameUser, onSuccess, onFailure); + }).catch(err => { + if (!(onFailure && onFailure(err))){ + alert("Problem generating '"+fileNameUser+"' file for '"+identifier+"' plugin: "+err); + } + }); + } + else{ + continueLoading(token, identifier, fileNameUser, onSuccess, onFailure); + } + }).catch(err => { + if (!(onFailure && onFailure(err))){ + alert("Problem checking '"+fileNameUser+"' file for '"+identifier+"' plugin: "+err); + } + }); + }; + })(); })($TDP); \ No newline at end of file