From 485f940396df04b3d092c6629dd422586febe29a Mon Sep 17 00:00:00 2001 From: mmasoud1 Date: Wed, 10 Jan 2024 11:09:45 -0500 Subject: [PATCH] clean design code --- js/histojs/designFunctionsv4.js | 121 ++++++++++++++++++---- test/histojs_test/designFunctions_test.js | 24 +++++ 2 files changed, 125 insertions(+), 20 deletions(-) diff --git a/js/histojs/designFunctionsv4.js b/js/histojs/designFunctionsv4.js index a3e1985..7d8d1cb 100644 --- a/js/histojs/designFunctionsv4.js +++ b/js/histojs/designFunctionsv4.js @@ -6142,9 +6142,9 @@ * @memberof HistoJS * @since 1.0.0 * @version 1.0.0 - * @param {object} sourceParams - * @param {string} hostApi - * @returns {promise} + * @param {object} sourceParams - e.g. { username: "someName" , password: "somePass" } + * @param {string} hostApi - e.g. "https://styx.neurology.emory.edu/girder/api/v1/" + * @returns {object} e.g. { promise: {…}, .... } */ dsaLogin = (sourceParams, hostApi) => { @@ -6189,6 +6189,15 @@ }); } + /** + * Event fires on before Ajax + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @todo complete + */ + webix.attachEvent("onBeforeAjax", (mode, url, data, request, headers, files, promise) => { let toSearchInUrl = "cdn.webix.com"; let searchedInUrl = url.search(toSearchInUrl); @@ -6198,10 +6207,30 @@ } }); + /** + * Set token into URL + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @param {string} token e.g. "3lQyO1b5R8XQg8TiCBtsTzCuP75SVZfl5ZkOeGb6xZPqaug9dF8bG6LUmE0Xa7VT" + * @param {string} symbol e.g. "?" + * @returns {string} e.g. "?token=3lQyO1b5R8XQg8TiCBtsTzCuP75SVZfl5ZkOeGb6xZPqaug9dF8bG6LUmE0Xa7VT" + */ + setTokenIntoUrl = (token, symbol) => { return token ? `${symbol}token=${token}` : ""; } + /** + * Login to DSA server + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @param {object} params e.g. { username: "someName" , password: "somePass" }; + * @returns {object} e.g. { promise: {…}, .... } + */ login = (params) => { let hostApi = getHostApi(); @@ -6213,6 +6242,13 @@ }); } + /** + * Logout DSA host + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + */ logout = () => { dsaLogout().then(() => { webix.storage.local.remove(`${"user" + "-"}${getHostIndex()}`); @@ -6220,6 +6256,14 @@ }); } + /** + * Get token + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @returns {string} e.g. "3lQyO1b5R8XQg8TiCBtsTzCuP75SVZfl5ZkOeGb6xZPqaug9dF8bG6LUmE0Xa7VT" + */ getToken = () => { const authToken = webix.storage.local.get(`${"authToken" + "-"}${getHostIndex()}`); @@ -6230,29 +6274,66 @@ return authToken.token; } + /** + * Get User info + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @returns {object} e.g. bject { _accessLevel: 2, _id: "5d9fd4e87bc2409bd20a359f", _modelType: "user", admin: true, + * created: "2019-10-11T01:03:36.067000+00:00", email: "mmasoud2@outlook.com", + * emailVerified: false, firstName: "Mohamed", groupInvites: [], groups: [], … } + */ + + getUserInfo = () => { + return webix.storage.local.get(`${"user" + "-"}${getHostIndex()}`); + } - getUserInfo = () => { - return webix.storage.local.get(`${"user" + "-"}${getHostIndex()}`); - } + /** + * Get User id + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @returns {string} e.g. "5d9fd4e87bc2409bd20a359f" + */ - getUserId = () => { - let userInfo = getUserInfo(); - return userInfo ? userInfo._id : null; - } + getUserId = () => { + let userInfo = getUserInfo(); + return userInfo ? userInfo._id : null; + } - isLoggedIn = () => { - return getToken() && getUserInfo() ? true : false; - } + /** + * Check if login to DSA host server + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @returns {bool} + */ + + isLoggedIn = () => { + return getToken() && getUserInfo() ? true : false; + } + /** + * Remove host credentials + * + * @memberof HistoJS + * @since 1.0.0 + * @version 1.0.0 + * @param {number} hostIndex + * @param {string} hostApi + */ - removeHostCredentials = (hostIndex, hostApi) => { - webix.ajax().del(hostApi + `/user/authentication`) - .catch(parseError) - .then( result => {}); + removeHostCredentials = (hostIndex, hostApi) => { + webix.ajax().del(hostApi + `/user/authentication`) + .catch(parseError) + .then( result => {}); - webix.storage.local.remove(`${"user" + "-"}${hostIndex}`); - webix.storage.local.remove(`${"authToken" + "-"}${hostIndex}`); - } + webix.storage.local.remove(`${"user" + "-"}${hostIndex}`); + webix.storage.local.remove(`${"authToken" + "-"}${hostIndex}`); + } //----------------- Get-Set Host functions -----------------------------// diff --git a/test/histojs_test/designFunctions_test.js b/test/histojs_test/designFunctions_test.js index 920dd8f..b1cab1c 100644 --- a/test/histojs_test/designFunctions_test.js +++ b/test/histojs_test/designFunctions_test.js @@ -277,4 +277,28 @@ describe("Main Design Phase Functions", function () { }); }); + describe('#setTokenIntoUrl()', function () { + it('return token into URL', function () { + expect( setTokenIntoUrl("ZPqaug9dF8bG6LUmE0Xa7VT", "?") ).to.be.eql("?token=ZPqaug9dF8bG6LUmE0Xa7VT"); + }); + }); + + describe('#getToken()', function () { + it('return token', function () { + expect( getToken() ).to.be.oneOf(["string", null]); + }); + }); + + describe('#getUserId()', function () { + it('return user id on DSA', function () { + expect( getUserId() ).to.be.oneOf(["string", null]); + }); + }); + + describe('#isLoggedIn()', function () { + it('return if login to DSA host', function () { + expect( isLoggedIn() ).to.be.oneOf([true, false]); + }); + }); + });