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

Last change on this file since 35433 was 35433, checked in by cstephen, 3 years ago

Check for duplicates before transcribing them

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