Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

languages: interface, server implementation #105

Merged
merged 6 commits into from Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions dist/wikibase.termbox.main.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -20,6 +20,8 @@
"http-status-codes": "^1.3.0",
"module-alias": "^2.1.0",
"mwbot": "^1.0.10",
"rtl-detect": "^1.0.2",
"ts-deferred": "^1.0.4",
"validate.js": "^0.12.0",
"vue": "^2.5.17",
"vue-class-component": "^6.0.0",
Expand Down
14 changes: 13 additions & 1 deletion src/client-entry.ts
Expand Up @@ -4,15 +4,27 @@ import buildApp from '@/common/buildApp';
import TermboxRequest from '@/common/TermboxRequest';
import { factory } from '@/common/TermboxFactory';
import UlsLanguageTranslationRepository from '@/client/data-access/UlsLanguageTranslationRepository';
import UlsLanguageRepository from '@/client/data-access/UlsLanguageRepository';
import EntityRepository from '@/client/data-access/EntityRepository';
import MwWindow from '@/client/mediawiki/MwWindow';
import { Hooks } from '@/client/mediawiki/Hooks';

Vue.config.productionTip = false;
const contentLanguages = new ( window as MwWindow ).wb.WikibaseContentLanguages();

factory.setLanguageTranslationRepository(
new UlsLanguageTranslationRepository( ( window as MwWindow ).wb.getLanguageNameByCode ),
new UlsLanguageTranslationRepository(
contentLanguages,
),
);

factory.setLanguageRepository(
new UlsLanguageRepository(
contentLanguages,
( window as MwWindow ).$.uls.data,
),
);

factory.setEntityRepository( new EntityRepository(
( window as MwWindow ).mw.hook( Hooks.entityLoaded ),
) );
Expand Down
41 changes: 41 additions & 0 deletions src/client/data-access/UlsLanguageRepository.ts
@@ -0,0 +1,41 @@
import LanguageRepository from '@/common/data-access/LanguageRepository.ts';
import LanguageCollection from '@/datamodel/LanguageCollection';
import Language from '@/datamodel/Language.ts';
import { WikibaseContentLanguages, UlsData } from '@/client/mediawiki/MwWindow';

export default class UlsLanguageRepository implements LanguageRepository {

private contentLanguages: WikibaseContentLanguages;
private directionalities: UlsData;

constructor( contentLanguages: WikibaseContentLanguages, directionalities: UlsData ) {
this.contentLanguages = contentLanguages;
this.directionalities = directionalities;
}

public getLanguages(): Promise<LanguageCollection> {
return Promise.resolve( this.getLanguageCollection() );
}

private getLanguageCollection(): LanguageCollection {
const codes = this.getLanguageCodes();
const collection: LanguageCollection = {};
codes.forEach( ( code ) => {
collection[ code ] = this.getDirectionalityByKey( code );
} );
return collection;
}

private getLanguageCodes(): string[] {
return Object.keys(
this.contentLanguages.getAllPairs(),
);
}

private getDirectionalityByKey( languageCode: string ): Language {
return {
code: languageCode,
directionality: this.directionalities.getDir( languageCode ),
};
}
}
19 changes: 9 additions & 10 deletions src/client/data-access/UlsLanguageTranslationRepository.ts
@@ -1,24 +1,23 @@
import LanguageTranslationRepository from '@/common/data-access/LanguageTranslationRepository';
import LanguageTranslations from '@/datamodel/LanguageTranslations';

export type LanguageNameInUserLangCallback = ( languageCode: string ) => string;
import LanguageTranslations, { StringTMap } from '@/datamodel/LanguageTranslations';
import { WikibaseContentLanguages } from '@/client/mediawiki/MwWindow';

export default class UlsLanguageTranslationRepository implements LanguageTranslationRepository {

private readonly getLanguageNameInUserLang: LanguageNameInUserLangCallback;

public constructor( getLanguageNameByCode: LanguageNameInUserLangCallback ) {
this.getLanguageNameInUserLang = getLanguageNameByCode;
private contentLanguages: WikibaseContentLanguages;
constructor( contentLanguages: WikibaseContentLanguages ) {
this.contentLanguages = contentLanguages;
}

public getLanguagesInLanguage( inLanguage: string ): Promise<LanguageTranslations> {
return new Promise<LanguageTranslations>( ( resolve ) => {
resolve( {
[inLanguage]: {
[inLanguage]: this.getLanguageNameInUserLang( inLanguage ),
},
[inLanguage]: this.getLanguagesNames(),
} as LanguageTranslations );
} );
}

private getLanguagesNames(): StringTMap<string> {
return this.contentLanguages.getAllPairs();
}
}
21 changes: 20 additions & 1 deletion src/client/mediawiki/MwWindow.ts
Expand Up @@ -10,11 +10,30 @@ interface MediaWiki {
config: MwConfig;
}

export interface WikibaseContentLanguages {
getAllPairs: () => any;
}

interface Wikibase {
getLanguageNameByCode( languageCode: string ): string;
WikibaseContentLanguages: {
new(): WikibaseContentLanguages;
};
}

export interface UlsData {
getDir: ( languageCode: string ) => string;
}

interface JQUls {
data: UlsData;
}

interface JQuery {
uls: JQUls;
}

export default interface MwWindow extends Window {
mw: MediaWiki;
wb: Wikibase;
$: JQuery;
}
14 changes: 14 additions & 0 deletions src/common/TermboxFactory.ts
@@ -1,8 +1,10 @@
import LanguageTranslationRepository from '@/common/data-access/LanguageTranslationRepository';
import EntityRepository from '@/common/data-access/EntityRepository';
import LanguageRepository from '@/common/data-access/LanguageRepository';

export default class TermboxFactory {
private languageTranslationRepository?: LanguageTranslationRepository;
private languageRepository?: LanguageRepository;
private entityRepository?: EntityRepository;

public setLanguageTranslationRepository( lookup: LanguageTranslationRepository ) {
Expand All @@ -17,6 +19,18 @@ export default class TermboxFactory {
}
}

public setLanguageRepository( languageRepository: LanguageRepository ) {
this.languageRepository = languageRepository;
}

public getLanguageRepository() {
if ( this.languageRepository ) {
return this.languageRepository;
} else {
throw new Error( 'languageRepository is undefined' );
}
}

public setEntityRepository( lookup: EntityRepository ) {
this.entityRepository = lookup;
}
Expand Down
5 changes: 5 additions & 0 deletions src/common/data-access/LanguageRepository.ts
@@ -0,0 +1,5 @@
import LanguageCollection from '@/datamodel/LanguageCollection';

export default interface LanguageRepository {
getLanguages(): Promise<LanguageCollection>;
}