Skip to content

Commit

Permalink
eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethbruskiewicz committed Nov 7, 2023
1 parent 366f22d commit 455a109
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 129 deletions.
3 changes: 1 addition & 2 deletions lib/DataHarmonizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ class DataHarmonizer {
// Bit of a hackey way to RESTORE classes to secondary headers. They are
// removed by Handsontable when re-rendering main table.
afterGetColHeader: function (column, TH, headerlev) {
//LocalizedHeaderRenderer(this, TH, undefined, column, this.colToProp(column), TH.innerText, null);
if (headerlev == 1) {
// Enables double-click listener for column help
$(TH).addClass('secondary-header-cell');
Expand Down Expand Up @@ -644,7 +643,7 @@ class DataHarmonizer {
if (source !== 'loadData') {
// Not triggered by the initial load
for (let i = 0; i < changes.length; i++) {
let [row, prop, oldValue, newValue] = changes[i];
let [row, prop] = changes[i];
const col = self.hot.propToCol(prop);
const td = self.hot.getCell(row, col);
// Assuming the new value is a translation key:
Expand Down
6 changes: 2 additions & 4 deletions lib/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import i18next from 'i18next';
import template from '@/lib/toolbar.html';
import '@/lib//toolbar.css';

// TODO: this is odd! package.json is a developer file. why should a UI component care about it?
// NOTE: this is odd! package.json is a developer file. why should a UI component care about it?
import pkg from '@/package.json';
const VERSION = pkg.version;

Expand Down Expand Up @@ -494,10 +494,8 @@ class Toolbar {
return this.menu[schemaName].exportFormats;
}

// NOTE: Stub-like, either remove or make robust
async _defaultGetLanguages(schemaName) {
async _defaultGetLanguages() {
return {
// TODO: default value propagation to which functions?
default: {
langcode: 'default',
nativeName: 'Default',
Expand Down
63 changes: 34 additions & 29 deletions lib/renderers/i18n-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Handsontable from 'handsontable';
import i18next from 'i18next';

export function combineRenderer(baseRenderer, customRenderer) {
return function (instance, td, row, col, prop, value, cellProperties) {
return function () //instance, td, row, col, prop, value, cellProperties
{
// First, use the base renderer (autocomplete in this case)
baseRenderer.apply(this, arguments);

Expand All @@ -12,6 +13,7 @@ export function combineRenderer(baseRenderer, customRenderer) {
}

// localization renderer for Handsontable
/* eslint-disable */
export function i18nContentRenderer(
instance,
td,
Expand All @@ -24,37 +26,40 @@ export function i18nContentRenderer(
const localizedValue = i18next.t(value);
td.innerHTML = localizedValue;
}

function i18nHeaderRenderer(
instance,
td,
row,
col,
prop,
value,
cellProperties
) {
const field = fields[col];
if (field && field.alias && field.alias) {
td.setAttribute('data-i18n', field.alias);
$(td).localize();
}
}
/* eslint-enable */

export const LocalizedAutoCompleteRenderer = combineRenderer(
Handsontable.renderers.AutocompleteRenderer,
i18nContentRenderer
);
export const LocalizedHeaderRenderer = combineRenderer(
Handsontable.renderers.TextRenderer,
i18nHeaderRenderer
);

export function getLocalizedColumnLabel(colIndex) {
const field = fields[colIndex];
if (field && field.metadata && field.metadata.label) {
return field.metadata.label;
}
// Return default header for this column index if no custom label is found
return Handsontable.helper.spreadsheetColumnLabel(colIndex);
}
// TODO/DEPRECATED
// function i18nHeaderRenderer(
// instance,
// td,
// row,
// col,
// prop,
// value,
// cellProperties
// ) {
// // TODO
// const field = fields[col];
// if (field && field.alias && field.alias) {
// td.setAttribute('data-i18n', field.alias);
// $(td).localize();
// }
// }
// export const LocalizedHeaderRenderer = combineRenderer(
// Handsontable.renderers.TextRenderer,
// i18nHeaderRenderer
// );

// export function getLocalizedColumnLabel(colIndex) {
// const field = fields[colIndex];
// if (field && field.metadata && field.metadata.label) {
// return field.metadata.label;
// }
// // Return default header for this column index if no custom label is found
// return Handsontable.helper.spreadsheetColumnLabel(colIndex);
// }
2 changes: 1 addition & 1 deletion lib/utils/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const urlToClickableAnchor = (string) => {
const escapedURL = formatEscapeHTML(url);
// Create a global regular expression to match all occurrences of the URL
const regex = new RegExp(
escapedURL.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),
escapedURL.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'),
'g'
);
// Replace the URL with an anchor tag in the string
Expand Down
73 changes: 40 additions & 33 deletions lib/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const rerender = () => {
$('table').localize();
};

export function initI18n(langChangeCallback = (lang) => {}) {
// langChangeCallback takes "lang" as argument
export function initI18n(langChangeCallback = () => {}) {
// use plugins and options as needed, for options, detail see
// https://www.i18next.com
i18next
Expand All @@ -45,7 +46,7 @@ export function initI18n(langChangeCallback = (lang) => {}) {
escapeValue: false,
},
},
(err, t) => {
(err) => {
if (err) return console.error(err);
// for options see
// https://github.com/i18next/jquery-i18next#initialize-the-plugin
Expand All @@ -57,10 +58,8 @@ export function initI18n(langChangeCallback = (lang) => {}) {
);

i18next.on('languageChanged', langChangeCallback);
} // TODO
function translateFromLocalizedTemplate() {
throw Error('Not Yet Implemented');
}

function transformLangFirstSpec(obj) {
const flatRepresentation = flattenObject(obj);

Expand Down Expand Up @@ -129,33 +128,39 @@ function transformStructFirstSpec(obj) {

return result;
}
const source = {
fr: {
nav: {
header: {
greeting: 'bonjour',
},
},
},
en: {
nav: {
header: {
greeting: 'hello',
},
},
},
};
const transformed = transformLangFirstSpec(source);
const initialObject = {
nav: {
header: {
greeting: {
en: 'hello',
fr: 'bonjour',
},
},
},
};

// TODO: test
// transformLangFirstSpec(source);

// An example object input:
// const inObj = {
// fr: {
// nav: {
// header: {
// greeting: 'bonjour',
// },
// },
// },
// en: {
// nav: {
// header: {
// greeting: 'hello',
// },
// },
// },
// };

// An example object output:
// const outObj = {
// nav: {
// header: {
// greeting: {
// en: 'hello',
// fr: 'bonjour',
// },
// },
// },
// };

// expect the structure-first style

Expand Down Expand Up @@ -204,6 +209,8 @@ export const resources = {
// ... other languages
};

i18next.add;
Object.entries(resources).forEach(([lang, { translation }]) => {
i18next.addResources(lang, 'translation', translation);
});

export { transformLangFirstSpec, transformStructFirstSpec };
4 changes: 2 additions & 2 deletions lib/utils/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function deepMerge(defaultObj, localizedObj) {

for (let key in localizedObj) {
if (
result.hasOwnProperty(key) &&
Object.prototype.hasOwnProperty.call(result, key) &&
typeof result[key] === 'object' &&
!Array.isArray(result[key])
) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export function flattenObject(obj) {

function flatten(currPath, currObj) {
for (let key in currObj) {
if (currObj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(currObj, key)) {
const newPath = currPath ? `${currPath}.${key}` : key;

if (typeof currObj[key] === 'object' && !Array.isArray(currObj[key])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class TemplateProxy {
*
* @returns {Proxy} - Returns a Proxy instance that serves localized or default data.
*/
// TODO

static async create(template_name, locale) {
const instance = new TemplateProxy();
await instance.init(template_name, locale);
Expand Down
113 changes: 57 additions & 56 deletions tests/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,47 @@ template.schema.prefixes.linkml.prefix_prefix == 'linkml'
template.schema.default.prefixes.linkml.prefix_prefix == 'linkml'
*/

// describe('TemplateProxy', () => {
// let proxy;

// beforeEach(async () => {
// // Mock the actual buildTemplate with our version
// // initiate localized
// proxy = await Template.create('test', 'de');
// });

// test('should return localized property if it exists', () => {
// expect(proxy.schema.name).toBe('AMBR_de');
// expect(proxy.schema.name).toBe(proxy.localized.schema.name);
// });

// test('should return default property if localized version doesn’t exist', () => {
// expect(proxy.schema.description).toBe('default_description');
// });

// test('should switch to a new locale and return appropriate data', () => {
// proxy.updateLocale('fr');
// expect(proxy.schema.name).toBe('AMBR_fr');
// expect(proxy.schema.name).toBe(proxy.localized.schema.name);
// expect(proxy.schema.description).toBe('french_description');
// });

// test('should return to the default locale when updating empty', () => {
// proxy.updateLocale();
// expect(proxy.schema.name).toBe('AMBR');
// expect(proxy.schema.name).toBe(proxy.default.schema.name);
// expect(proxy.schema.description).toBe(proxy.default.schema.description);
// });

// test('should throw error for unsupported locale', () => {
// expect(() => proxy.updateLocale('es')).toThrow(
// 'Locale es is not supported by the template.'
// );
// });

// // Additional tests can be based on other methods and functionalities of the TemplateProxy class
// });
// TODO
describe('TemplateProxy', () => {
let proxy;

beforeEach(async () => {
// Mock the actual buildTemplate with our version
// initiate localized
proxy = await Template.create('test', 'de');
});

test('should return localized property if it exists', () => {
expect(proxy.schema.name).toBe('AMBR_de');
expect(proxy.schema.name).toBe(proxy.localized.schema.name);
});

test('should return default property if localized version doesn’t exist', () => {
expect(proxy.schema.description).toBe('default_description');
});

test('should switch to a new locale and return appropriate data', () => {
proxy.updateLocale('fr');
expect(proxy.schema.name).toBe('AMBR_fr');
expect(proxy.schema.name).toBe(proxy.localized.schema.name);
expect(proxy.schema.description).toBe('french_description');
});

test('should return to the default locale when updating empty', () => {
proxy.updateLocale();
expect(proxy.schema.name).toBe('AMBR');
expect(proxy.schema.name).toBe(proxy.default.schema.name);
expect(proxy.schema.description).toBe(proxy.default.schema.description);
});

test('should throw error for unsupported locale', () => {
expect(() => proxy.updateLocale('es')).toThrow(
'Locale es is not supported by the template.'
);
});

// Additional tests can be based on other methods and functionalities of the TemplateProxy class
});

describe('deepMerge function', () => {
it('should merge two objects deeply', () => {
Expand Down Expand Up @@ -135,22 +136,22 @@ describe('Template utilities', () => {
});

// TODO
// describe('accessTemplate', () => {
// it('should return the correct template if it exists', async () => {
// const mockTemplate = { name: 'template1' };
// jest.mock('@/web/templates/manifest.json', () => ({
// children: [mockTemplate],
// }));

// const result = await accessTemplate('test');
// expect(result[0]).toBe('test');
// });

// it('should return null if the template does not exist', async () => {
// const result = await accessTemplate('non-existent-template');
// expect(result).toBeNull();
// });
// });
describe('accessTemplate', () => {
it('should return the correct template if it exists', async () => {
const mockTemplate = { name: 'template1' };
jest.mock('@/web/templates/manifest.json', () => ({
children: [mockTemplate],
}));

const result = await accessTemplate('test');
expect(result[0]).toBe('test');
});

it('should return null if the template does not exist', async () => {
const result = await accessTemplate('non-existent-template');
expect(result).toBeNull();
});
});

describe('buildTemplate', () => {
it('should correctly build the template', async () => {
Expand Down
Loading

0 comments on commit 455a109

Please sign in to comment.