-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathccm.er_trainer.js
291 lines (253 loc) · 11.4 KB
/
ccm.er_trainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* @overview ccmjs-based web component for training relations in an ER diagram
* @author André Kless <andre.kless@web.de> 2021-2022
* @license The MIT License (MIT)
* @version latest (6.0.0)
*/
( () => {
const component = {
name: 'er_trainer',
ccm: './libs/ccm/ccm.js',
config: {
// "anytime_finish": true,
"css": [ "ccm.load",
[ // serial
"./libs/bootstrap-5/css/bootstrap.css",
"./resources/styles.css"
],
{ "url": "./libs/bootstrap-5/css/bootstrap-fonts.css", "context": "head" }
],
// "data": { "store": [ "ccm.store" ] },
"default": {
"format": "svg",
"images": [ "e", "1", "c", "n", "cn", "r", "s" ],
"notation": "abrial",
"path": "./resources/img/"
},
"feedback": true,
"helper": [ "ccm.load", { "url": "./libs/ccm/helper.js", "type": "module" } ],
"html": [ "ccm.load", { "url": "./resources/templates.js", "type": "module" } ],
"lang": [ "ccm.start", "./libs/lang/ccm.lang.js", {
"active": "de",
"translations": {
"de": [ "ccm.load", { "url": "./resources/resources.js#de", "type": "module" } ],
"en": [ "ccm.load", { "url": "./resources/resources.js#en", "type": "module" } ]
}
} ],
"legend": true,
"modal": [ "ccm.start", "./libs/modal/ccm.modal.js", {
"backdrop_close": true,
"content": "",
"closed": true,
"buttons": ""
} ],
"notations": [ "ccm.load", { "url": "./resources/resources.js#notations", "type": "module" } ],
// "number": 5,
// "onchange": event => console.log( event ),
"onfinish": { "log": true, "restart": true },
// "onready": event => console.log( event ),
// "onstart": event => console.log( event ),
"phrases": [ "ccm.load", { "url": "./resources/resources.js#phrases", "type": "module" } ],
"retry": true,
"skip": true,
"show_solution": true,
"shuffle": true,
"text": [ "ccm.load", { "url": "./resources/resources.js#de", "type": "module" } ],
// "user": [ "ccm.start", "./libs/user/ccm.user.js" ],
"values": [ [ "1", "c", "n", "cn" ], [ "t", "p", "d", "n" ] ]
},
Instance: function () {
/**
* shortcut to help functions
* @type {Object.<string,Function>}
*/
let $;
/**
* app state data
* @type {Object}
*/
let data;
/**
* current selected notation
* @type {string}
*/
let notation;
/**
* current phrase number
* @type {number}
*/
let phrase_nr;
/**
* when the instance is created, when all dependencies have been resolved and before the dependent sub-instances are initialized and ready
* @returns {Promise<void>}
*/
this.init = async () => {
// set shortcut to help functions
$ = Object.assign( {}, this.ccm.helper, this.helper ); $.use( this.ccm );
// set title of modal dialog
this.modal.title = this.text.legend;
// uniform notations data
for ( const key in this.notations ) {
let notation = this.notations[ key ];
this.notations[ key ] = {
key: notation.key,
title: notation.title,
swap: !!notation.swap,
centered: !!notation.centered,
left: notation.left || this.default.left,
images: ( notation.images || this.default.images ).map( image => image.includes( '.' ) ? image : ( notation.path || this.default.path ) + notation.key + '/' + image + '.' + ( notation.format || this.default.format ) ),
comment: notation.comment
};
}
// uniform phrases data
if ( $.isObject( this.phrases ) ) this.phrases = Object.values( this.phrases ).map( phrase => { delete phrase.key; return phrase; } );
};
/**
* when all dependencies are solved after creation and before the app starts
* @returns {Promise<void>}
*/
this.ready = async () => {
if ( !this.number ) this.number = this.phrases.length; // use all phrases as default
this.onready && await this.onready( { instance: this } ); // trigger 'ready' event
};
/**
* starts the app
* @returns {Promise<void>}
*/
this.start = async () => {
// load app state data
data = await $.dataset( this.data );
const initial = {
correct: 0,
notation: notation || data.notation || this.default.notation,
sections: [],
total: this.number
};
data = data.sections && data.sections.length < data.phrases.length ? data: initial;
// no loaded phrases? => take required number of original phrases and shuffle them
if ( !data.phrases ) {
data.phrases = $.clone( this.phrases );
this.shuffle && $.shuffleArray( data.phrases );
data.phrases = data.phrases.slice( 0, this.number );
}
// render first phrase
phrase_nr = data.sections.length ? data.sections.length : 0; nextPhrase();
// render language selection and user login/logout
const aside = this.element.querySelector( 'aside' );
if ( aside ) {
aside && this.lang && !this.lang.getContext() && $.append( aside, this.lang.root );
aside && this.user && $.append( aside, this.user.root );
}
// set content of modal dialog for legend table
this.html.render( this.html.legend( this ), this.modal.element.querySelector( 'main' ) );
// trigger 'start' event
this.onstart && await this.onstart( { instance: this } );
};
/**
* returns current app state data
* @returns {Object}
*/
this.getValue = () => $.clone( data );
/**
* contains all event handlers
* @type {Object.<string,Function>}
*/
const events = {
/** when selected entry for displayed notation changes */
onNotation: ( value, show_solution ) => {
if ( data.phrases[ phrase_nr - 1 ].entities.length > 2 && this.notations[ value ].swap ) return;
data.notation = value;
render( show_solution );
this.onchange && this.onchange( { event: 'notation', instance: this } );
},
/** when 'legend' button is clicked */
onLegend: () => {
this.modal.open();
this.lang.translate( this.modal.element );
this.onchange && this.onchange( { event: 'legend', instance: this } );
},
/** when selected entry of a selector box changes */
onSelect: ( nr, value ) => {
setInput( nr, value );
render();
this.onchange && this.onchange( { event: 'input', instance: this, phrase: phrase_nr, nr: nr, value: value } );
},
/** when 'submit' button is clicked */
onSubmit: () => {
const section = data.sections[ phrase_nr - 1 ];
if ( section.correct !== undefined || section.input.includes( '' ) ) return;
section.correct = section.input.toString() === data.phrases[ phrase_nr - 1 ].solution.toString();
section.correct && data.correct++;
this.feedback && this.element.classList.add( section.correct ? 'correct' : 'failed' );
render();
this.onchange && this.onchange( { event: 'submit', instance: this, phrase: phrase_nr } );
!this.feedback && events.onNext();
},
/** when 'retry' button is clicked */
onRetry: () => {
const section = data.sections[ phrase_nr - 1 ];
if ( !this.retry || section.correct !== false ) return;
section.retry = section.retry ? section.retry + 1 : 1;
delete section.correct;
this.element.classList.remove( 'failed' );
render();
this.onchange && this.onchange( { event: 'retry', instance: this, phrase: phrase_nr } );
},
/** when 'solution' button is clicked */
onSolution: () => {
if ( !this.show_solution || data.sections[ phrase_nr - 1 ].correct !== false ) return;
render( true );
this.onchange && this.onchange( { event: 'solution', instance: this, phrase: phrase_nr } );
},
/** when 'next' button is clicked */
onNext: () => {
if ( !this.skip && data.sections[ phrase_nr - 1 ].correct === undefined || phrase_nr === this.number ) return;
reset();
nextPhrase();
this.onchange && this.onchange( { event: 'next', instance: this, phrase: phrase_nr } );
},
/** when 'finish' button is clicked */
onFinish: () => {
if ( !this.onfinish || !this.skip && data.sections[ phrase_nr - 1 ].correct === undefined || !this.anytime_finish && phrase_nr < this.number ) return;
reset();
this.onfinish && $.onFinish( this );
}
};
/** starts the next phrase */
const nextPhrase = () => {
phrase_nr++;
data.sections.push( { input: data.phrases[ phrase_nr - 1 ].solution.map( () => '' ) } );
// ternary relation in a swapped notation? => switch to not swapped notation (= Abrial)
if ( data.phrases[ phrase_nr - 1 ].entities.length > 2 && this.notations[ data.notation ].swap )
data.notation = Object.values( this.notations ).find( notation => !notation.swap ).key;
render();
};
/**
* renders current phrase
* @param {boolean} [show_solution] - reveal correct solution
*/
const render = show_solution => {
this.html.render( this.html.main( this, data, events, phrase_nr, show_solution ), this.element );
this.element.querySelectorAll( '[selected]' ).forEach( option => option.selected = true ); // workaround for lit-html bug
this.lang && this.lang.translate();
};
/** resets visual feedback and selected input values */
const reset = () => {
this.element.classList.remove( 'correct', 'failed' );
this.element.querySelectorAll( '[selected]' ).forEach( option => option.selected = false );
this.element.querySelectorAll( '[type="radio"]' ).forEach( radio => radio.checked = false );
};
/**
* updates selected value of a selector box in app state data
* @param {number} nr - selector box number (1: left, 2: middle, 3: right)
* @param {string} value - selected value
*/
const setInput = ( nr, value ) => {
const section = data.sections[ phrase_nr - 1 ];
if ( !section.input ) section.input = [];
section.input[ nr - 1 ] = value;
};
}
};
let b="ccm."+component.name+(component.version?"-"+component.version.join("."):"")+".js";if(window.ccm&&null===window.ccm.files[b])return window.ccm.files[b]=component;(b=window.ccm&&window.ccm.components[component.name])&&b.ccm&&(component.ccm=b.ccm);"string"===typeof component.ccm&&(component.ccm={url:component.ccm});let c=(component.ccm.url.match(/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/)||[""])[0];if(window.ccm&&window.ccm[c])window.ccm[c].component(component);else{var a=document.createElement("script");document.head.appendChild(a);component.ccm.integrity&&a.setAttribute("integrity",component.ccm.integrity);component.ccm.crossorigin&&a.setAttribute("crossorigin",component.ccm.crossorigin);a.onload=function(){(c="latest"?window.ccm:window.ccm[c]).component(component);document.head.removeChild(a)};a.src=component.ccm.url}
} )();