From afe8296fd7590408612888e2e462e6287923ad85 Mon Sep 17 00:00:00 2001 From: Muremwa Date: Thu, 8 Oct 2020 22:12:47 +0300 Subject: [PATCH] added ModelAdmin urls --- extension.js | 2 + extraUrls.js | 14 +++++-- modelAdminLoad.js | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 modelAdminLoad.js diff --git a/extension.js b/extension.js index 462ff5d..d6b386a 100644 --- a/extension.js +++ b/extension.js @@ -67,6 +67,8 @@ function readAndDisplayUrls () { // load pre defined url configurations const extraUrlPatterns = externalUrls(vscode.workspace.rootPath, (error, file) => { vscode.window.showErrorMessage(`The configurations in ${file} are incorrect`); + }, () => { + vscode.window.showErrorMessage('Wrong formart on .vscode/urlConfigs/models.json') }); // merge both patterns diff --git a/extraUrls.js b/extraUrls.js index df0985e..3923731 100644 --- a/extraUrls.js +++ b/extraUrls.js @@ -4,6 +4,7 @@ const fs = require('fs'); const path = require('path'); +const modelAdmin = require('./modelAdminLoad').modelAdmin; const EXTRAURLS = 'extraUrls' const isObj = (obj) => typeof obj === 'object' && obj !== null; @@ -13,7 +14,7 @@ function _loadExtraUrls (files, errorCallBack) { files.forEach((file) => { if (file.match(/\.conf\.json/g)) { - const configs = fs.readFileSync(file); + const configs = fs.readFileSync(file, { encoding: 'utf-8', flag: 'r' }); try { const parsedconfigs = JSON.parse(configs); @@ -58,7 +59,7 @@ function _loadExtraUrls (files, errorCallBack) { * @param {string} home * @returns {Map} */ -function loadUrls (home, handleExternalReadError) { +function loadUrls (home, handleExternalReadError, wrongFormatModels) { // Read json files in extraUrls let userConfigs; @@ -73,7 +74,14 @@ function loadUrls (home, handleExternalReadError) { ...userConfigs ]; - return _loadExtraUrls(jFiles, handleExternalReadError); + const urls = _loadExtraUrls(jFiles, handleExternalReadError); + + if (urls.has('admin')) { + const modelUrls = modelAdmin(home, wrongFormatModels, () => {}); + urls.set('admin', [...urls.get('admin'), ...modelUrls]); + }; + + return urls; }; diff --git a/modelAdminLoad.js b/modelAdminLoad.js new file mode 100644 index 0000000..9252d92 --- /dev/null +++ b/modelAdminLoad.js @@ -0,0 +1,105 @@ +/* + Read *.models.json to load ModelAdmin URL configurations +*/ +const fs = require('fs'); +const path = require('path'); + + +/** +*@param {string} home +*@param {() => void} wrongFomartCallback +*@param {() => void} noModelsCallback +*@returns {Map} +**/ +function _getModels (home, wrongFomartCallback, noModelsCallback) { + const models = new Map(); + + try { + const _models = fs.readFileSync(path.join(home, ".vscode", "urlConfigs", "models.json"), { encoding: 'utf-8', flag: 'r' }); + try { + const __models = JSON.parse(_models); + + if (typeof __models === 'object' && __models !== null) { + for (const app of Object.keys(__models)) { + models.set(app, __models[app]); + }; + }; + + } catch (error) { + wrongFomartCallback(); + } + } catch (error) { + noModelsCallback(); + } + + return models; +}; + + +/** +*@param {string} model +*@param {string} appName +*@param {string} appLabel +*@param {string} extraLabel +*@param {boolean} objectId +*@returns {{ + reverseName: string, + arguments: [], + viewName: string, + hasArgs: boolean +}} +*/ +function createSingleModelUrls (model, appName, appLabel, extraLabel, objectId) { + return url = { + "reverseName": `${appName}:${appLabel}_${model}_${extraLabel}`, + "arguments": objectId? [ + { + "name": "object_id", + "argType": "integer" + } + ]: [], + "viewName": "", + "hasArgs": objectId? true: false + } +}; + + + +/** +*@param {string} home +*@param {() => void} wrongFomartCallback +*@param {() => void} noModelsCallback +*@returns {{ + reverseName: string, + arguments: [], + viewName: string, + hasArgs: boolean +}[]} +**/ +function loadModelAdminConfigs (home, wrongFomartCallback, noModelsCallback) { + let patterns = []; + const models = _getModels(home, wrongFomartCallback, noModelsCallback); + const modelAdminUrls = [ + ['changelist', false], + ['add', false], + ['history', true], + ['delete', true], + ['change', true] + ]; + + for (const model of models.keys()) { + const appModels = models.get(model); + + if (Array.isArray(appModels)) { + patterns = [...patterns, ...appModels.map((appModel) => { + return modelAdminUrls.map((url) => createSingleModelUrls(appModel, 'admin', model, url[0], url[1])) + }).flat()]; + }; + }; + + return patterns; +}; + +module.exports = { + modelAdmin: loadModelAdminConfigs +} \ No newline at end of file