-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model.js
107 lines (91 loc) · 2.46 KB
/
Model.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
// Model.js - Model (MVC)
// (c) Marco Vieth, 2019
// https://benchmarko.github.io/CPCBasic/
"use strict";
var Utils;
if (typeof require !== "undefined") {
Utils = require("./Utils.js"); // eslint-disable-line global-require
}
function Model(config, initialConfig) {
this.init(config, initialConfig);
}
Model.prototype = {
init: function (config, initialConfig) {
this.config = config || {}; // store only a reference
this.initialConfig = initialConfig || {};
this.databases = {};
this.examples = {}; // loaded examples per database
},
getProperty: function (sProperty) {
return this.config[sProperty];
},
setProperty: function (sProperty, sValue) {
this.config[sProperty] = sValue;
},
getAllProperties: function () {
return this.config;
},
getAllInitialProperties: function () {
return this.initialConfig;
},
getChangedProperties: function () {
var oCurrent = this.config,
oInitial = this.initialConfig,
oChanged = {},
sName;
for (sName in oCurrent) {
if (oCurrent.hasOwnProperty(sName)) {
if (oCurrent[sName] !== oInitial[sName]) {
oChanged[sName] = oCurrent[sName];
}
}
}
return oChanged;
},
addDatabases: function (oDb) {
var sPar, oEntry;
for (sPar in oDb) {
if (oDb.hasOwnProperty(sPar)) {
oEntry = oDb[sPar];
this.databases[sPar] = oEntry;
this.examples[sPar] = {};
}
}
return this;
},
getAllDatabases: function () {
return this.databases;
},
getDatabase: function () {
var sDatabase = this.getProperty("database");
return this.databases[sDatabase];
},
getAllExamples: function () {
var selectedDatabase = this.getProperty("database");
return this.examples[selectedDatabase];
},
getExample: function (sKey) {
var selectedDatabase = this.getProperty("database");
return this.examples[selectedDatabase][sKey];
},
setExample: function (oExample) {
var selectedDatabase = this.getProperty("database"),
sKey = oExample.key;
if (!this.examples[selectedDatabase][sKey]) {
if (Utils.debug > 1) {
Utils.console.debug("setExample: creating new example:", sKey);
}
}
this.examples[selectedDatabase][sKey] = oExample;
},
removeExample: function (sKey) {
var selectedDatabase = this.getProperty("database");
if (!this.examples[selectedDatabase][sKey]) {
Utils.console.warn("removeExample: example does not exist: " + sKey);
}
delete this.examples[selectedDatabase][sKey];
}
};
if (typeof module !== "undefined" && module.exports) {
module.exports = Model;
}