Skip to content

Commit

Permalink
added ModelAdmin urls
Browse files Browse the repository at this point in the history
  • Loading branch information
muremwa committed Oct 8, 2020
1 parent 8930761 commit afe8296
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
2 changes: 2 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions extraUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -58,7 +59,7 @@ function _loadExtraUrls (files, errorCallBack) {
* @param {string} home
* @returns {Map<string, []>}
*/
function loadUrls (home, handleExternalReadError) {
function loadUrls (home, handleExternalReadError, wrongFormatModels) {
// Read json files in extraUrls
let userConfigs;

Expand All @@ -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;

};

Expand Down
105 changes: 105 additions & 0 deletions modelAdminLoad.js
Original file line number Diff line number Diff line change
@@ -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<string, string[]>}
**/
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
}

0 comments on commit afe8296

Please sign in to comment.