Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/views/settings-view.js
1036 views
1
/*globals define*/
2
define([
3
'config/settings',
4
'text!./../../templates/settings-view.html'
5
], function( Settings, settingsTemplate ) {
6
'use strict';
7
8
function checkboxListener( event ) {
9
var id = event.target.id;
10
Settings[ id ] = event.target.checked;
11
}
12
13
function toggleSettings() {
14
var settingsEl = document.querySelector( '.settings' );
15
if ( settingsEl.style.display === '' ) {
16
settingsEl.style.display = 'inline';
17
} else {
18
settingsEl.style.display = '';
19
}
20
}
21
22
function SettingsView( el ) {
23
this.el = document.querySelector( el );
24
if ( !this.el ) {
25
this.el = document.createElement( 'div' );
26
this.el.id = 'settings-view';
27
this.el.classList.add( 'settings-view' );
28
}
29
30
this.template = settingsTemplate;
31
this.initialize();
32
33
this.toggleBtn = null;
34
}
35
36
SettingsView.prototype.initialize = function() {
37
this.el.innerHTML = this.template;
38
39
Settings.keys.map(function( key ) {
40
var element = this.el.querySelector( '#' + key );
41
element.addEventListener( 'click', checkboxListener );
42
element.checked = Settings[ key ];
43
}.bind( this ));
44
45
this.toggleBtn = this.el.querySelector( '.toggle-settings-btn' );
46
this.toggleBtn.addEventListener( 'click', toggleSettings );
47
};
48
49
SettingsView.prototype.remove = function() {
50
Settings.keys.map(function( key ) {
51
var element = this.el.querySelector( '#' + key );
52
element.removeEventListener( 'click', checkboxListener );
53
}.bind( this ));
54
this.toggleBtn.removeEventListener( 'click', toggleSettings );
55
56
this.el.innerHTML = '';
57
58
if ( this.el.parentElement ) {
59
this.el.parentElement.removeChild( this.el );
60
}
61
};
62
63
return SettingsView;
64
});
65
66