source: main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/main.js@ 35714

Last change on this file since 35714 was 35714, checked in by cstephen, 2 years ago

Add macroniser scaffolding.
Update translations.

File size: 6.3 KB
Line 
1import { createApp } from "vue";
2import { createStore } from "vuex"
3import AudioPlayback from "./js/AudioPlaybackModule";
4import App from "./App.vue";
5import ToggleButton from "./components/ToggleButton.vue"
6
7export class TranscriptionViewModel {
8 /**
9 * Initialises a new instance of the {@link TranscriptionViewModel} class.
10 *
11 * @param {TranscriptionModel} transcription
12 * @param {File} file The file from which the transcription was generated.
13 */
14 constructor(transcription, file) {
15 /** @type {String} The UUID of this transcription. */
16 this.id = TranscriptionViewModel.getId(file); // Should be a fairly reliable indicator of uniqueness.
17
18 /** @type {String} The transcription. */
19 this.transcription = transcription.transcription;
20
21 /** @type {String} The name of the file from which the transcription was generated. */
22 this.fileName = transcription.file_name;
23
24 /** @type {TranscriptionMetadata[]} The transcription metadata. */
25 this.metadata = transcription.metadata;
26
27 /** @type {File} The file from which the transcription was generated. */
28 this.file = file;
29 }
30
31 static getId(file) {
32 return file.name + file.size + file.type;
33 }
34}
35
36export class PlaybackState {
37 /**
38 * Initialises a new instance of the {@link PlaybackState} class.
39 * @param {String} id The ID of the transcription for which audio is being played.
40 * @param {Boolean} isPlaying A value indicating if audio is currently being played.
41 * @param {Number} currentTime The current time in the audio playback.
42 * @param {Number} length The length of the audio track.
43 */
44 constructor(id = "", isPlaying = false, length = 0) {
45 /** @type {String} The ID of the transcription for which audio is currently being played. */
46 this.id = id;
47
48 /** @type {Boolean} Gets a value indicating if audio is currently being played back. */
49 this.isPlaying = isPlaying;
50
51 /** @type {Map<String, Number} The current playback time of each transcription's audio. */
52 this.playbackTimes = new Map();
53
54 /** @type {Map<String, Number} The length of each transcription's audio. */
55 this.playbackLengths = new Map();
56 }
57}
58
59export class TranscriptionExistsError extends Error {
60 constructor(message = "", ...args) {
61 super(message, ...args);
62 }
63}
64
65const store = createStore({
66 state() {
67 return {
68 /** @type {Map<String, String>} */
69 translations: new Map(),
70 /** @type {Map<String, TranscriptionViewModel>} */
71 rawTranscriptions: new Map(),
72 playbackState: new PlaybackState()
73 }
74 },
75 mutations: {
76 /**
77 * Adds a new transcription to the store.
78 * @param {*} state The state of the store.
79 * @param {TranscriptionViewModel} transcription The transcription's view model object.
80 */
81 rawTranscriptionAdd(state, transcription) {
82 if (state.rawTranscriptions.has(transcription.id)) {
83 throw new TranscriptionExistsError("Cannot add the transcription because it already exists.");
84 }
85 state.rawTranscriptions.set(transcription.id, transcription);
86 },
87
88 /**
89 * Removes a transcription from the store.
90 * @param {*} state The state of the store.
91 * @param {String} id The ID of the transcription.
92 */
93 rawTranscriptionRemove(state, id) {
94 state.rawTranscriptions.delete(id);
95 },
96
97 playbackStateSetID(state, id) {
98 state.playbackState.id = id;
99 },
100 playbackStateSetIsPlaying(state, isPlaying) {
101 state.playbackState.isPlaying = isPlaying;
102 },
103 playbackStateSetTime(state, object) {
104 state.playbackState.playbackTimes.set(object.id, object.time);
105 },
106 playbackStateSetLength(state, object) {
107 state.playbackState.playbackLengths.set(object.id, object.time);
108 },
109
110 setTranslations(state, translations) {
111 state.translations = translations;
112 }
113 },
114 getters: {
115 hasTranscriptionOfFile: (state) => (file) => {
116 const id = TranscriptionViewModel.getId(file);
117 return state.rawTranscriptions.has(id);
118 },
119 transcriptionPlaybackTime: (state) => (id) => {
120 return state.playbackState.playbackTimes.has(id) ? state.playbackState.playbackTimes.get(id) : 0;
121 },
122 transcriptionPlaybackLength: (state) => (id) => {
123 return state.playbackState.playbackLengths.has(id) ? state.playbackState.playbackLengths.get(id) : 0;
124 }
125 }
126});
127
128AudioPlayback.initialise(store);
129
130const app = createApp(App);
131
132app.use(store)
133 .component("ToggleButton", ToggleButton)
134 .directive("width", {
135 mounted(el, binding) {
136 binding.value(el.offsetWidth);
137
138 window.addEventListener("resize", function() {
139 binding.value(el.offsetWidth);
140 });
141 }
142 })
143 .mount("#app");
144
145/* === Get interface translations === */
146
147/** @type {Map<String, String>} */
148const translations = new Map();
149
150/* We might be running under Greenstone, so pull the tranlsations from there if so */
151/* eslint-disable no-undef */
152if (typeof gs !== "undefined" && gs.text && gs.text.atea) {
153 for (const key in gs.text.atea) {
154 translations.set(key, gs.text.atea[key]);
155 }
156
157 store.commit("setTranslations", translations)
158}
159/* eslint-enable no-undef */
160else {
161 fetch("resources/interface_atea.properties")
162 .then(
163 async response => {
164 const responseText = await response.text();
165 const responseTranslations = responseText.split("\n");
166
167 for (const translation of responseTranslations) {
168 const keyvalue = translation.split("=");
169
170 if (keyvalue.length !== 2) {
171 continue;
172 }
173
174 const namespace = keyvalue[0].split(".");
175 if (namespace.length !== 3 || namespace[1] !== "asr") {
176 continue;
177 }
178
179 translations.set(namespace[namespace.length - 1], keyvalue[1]);
180 }
181
182 store.commit("setTranslations", translations)
183 }
184 );
185}
Note: See TracBrowser for help on using the repository browser.