-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.js
201 lines (175 loc) · 5.33 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
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
// Model.js - Model
//
/* globals */
"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 (properties: database, key, script, title)
this.initVariables();
},
getProperty: function (sProperty) {
return this.config[sProperty];
},
setProperty: function (sProperty, sValue) {
this.config[sProperty] = sValue;
return this;
},
getAllProperties: function () {
return this.config;
},
getAllInitialProperties: function () {
return this.initialConfig;
},
getVariable: function (sVar) {
return this.variables[sVar];
},
setVariable: function (sVar, sValue) {
this.variables[sVar] = sValue;
return this;
},
changeVariable: function (sPar, sValue) {
var oVariables = this.variables,
nValueAsNumber;
nValueAsNumber = parseFloat(sValue);
sValue = (String(nValueAsNumber) === sValue) ? nValueAsNumber : sValue; // use number, if it is a number
if (sValue !== oVariables[sPar]) { // change needed?
if (oVariables.gcfOriginal[sPar] === undefined) {
oVariables.gcfOriginal[sPar] = oVariables[sPar]; // save original value, if not done
}
this.setVariable(sPar, sValue);
return true; // changed
}
return false;
},
getAllVariables: function () {
return this.variables;
},
initVariables: function () {
this.variables = {
gcfOriginal: {}
};
return this;
},
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];
},
fnGetFilterCategories: function () {
var mFilterCategory, sFilterCategory, aFilterCategory, i;
sFilterCategory = this.getProperty("filterCategory");
if (sFilterCategory !== "") { // split: empty string returns array with empty string
aFilterCategory = sFilterCategory.split(",");
mFilterCategory = {};
for (i = 0; i < aFilterCategory.length; i += 1) {
mFilterCategory[aFilterCategory[i]] = true;
}
}
return mFilterCategory; // for empty list return undefined
},
fnGetExampleTitle: function (oExample) {
var sComment, iIndex, sTitle;
sComment = oExample.position.getComment(); // category and title
iIndex = sComment.indexOf("!");
sTitle = (iIndex >= 0) ? sComment.substring(iIndex + 1) : "";
return sTitle;
},
fnGetExampleCategory: function (oExample) {
var sComment, iIndex, sCategory;
sComment = oExample.position.getComment(); // category and title
iIndex = sComment.indexOf("!");
sCategory = (iIndex >= 0) ? sComment.substring(0, iIndex) : "";
return sCategory;
},
fnGetAllExampleCategories: function () {
var oItems = {},
oAllExamples = this.getAllExamples(),
oExample, sKey, sCategory;
// Get all categories from example titles
for (sKey in oAllExamples) {
if (oAllExamples.hasOwnProperty(sKey)) {
oExample = oAllExamples[sKey];
sCategory = this.fnGetExampleCategory(oExample);
oItems[sCategory] = true;
}
}
return oItems;
},
getFilteredExamples: function () {
var aItems = [],
oAllExamples = this.getAllExamples(),
mFilterCategories = this.fnGetFilterCategories(),
sFilterId = this.getProperty("filterId").toLowerCase(),
sFilterTitle = this.getProperty("filterTitle").toLowerCase(),
sKey, oExample, sTitle, sCategory;
for (sKey in oAllExamples) {
if (oAllExamples.hasOwnProperty(sKey)) {
oExample = oAllExamples[sKey];
sTitle = this.fnGetExampleTitle(oExample);
sCategory = this.fnGetExampleCategory(oExample);
if ((!mFilterCategories || mFilterCategories[sCategory])
&& (sFilterId === "" || sKey.toLowerCase().indexOf(sFilterId) >= 0)
&& (sFilterTitle === "" || sTitle.toLowerCase().indexOf(sFilterTitle) >= 0)) { // filter
aItems.push(oExample);
} else if (Utils.debug > 1) {
Utils.console.debug("getFilteredExamples: item " + sKey + " filtered");
}
}
}
return aItems;
},
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) {
Utils.console.debug("setExample: creating new example: " + sKey);
}
}
this.examples[selectedDatabase][sKey] = oExample;
return this;
},
deleteExample: function (sKey) {
var selectedDatabase = this.getProperty("database");
if (!this.examples[selectedDatabase][sKey]) {
Utils.console.warn("deleteExample: example does not exist: " + sKey);
}
delete this.examples[selectedDatabase][sKey];
return this;
}
};
if (typeof module !== "undefined" && module.exports) {
module.exports = Model;
}
// end