-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss_classchanger.js
More file actions
429 lines (323 loc) · 12.6 KB
/
Copy pathcss_classchanger.js
File metadata and controls
429 lines (323 loc) · 12.6 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// CLASS of an pCars Driver
function CSSClassChanger(aCSSDefinition){
this.CurrentState = "";
this.aCSSClasses = new Array(); //Array of all handled CSS classes
this.sLastDriverSelectionClass = 'initial_value';
// structure:
// aCSSClasses['hideallsvgs']
// aCSSClasses['CSSTOP3VEHICLES']
// ...
this.aCSSDef = {};
this.aCSSDef = aCSSDefinition; // copy CSS definitions into local varaibale
//return this;
}
// usage:
// add class:
// var object_TestCSSClass = setStyle(
// '.TestCSSClass{ fill: grey; } \n',
// object_TestCSSClass );
// remove class:
// if(object_TestCSSClass) setStyle( '', object_TestCSSClass );
//
function setStyle(cssText) {
var sheet = document.createElement('style');
sheet.type = 'text/css';
/* Optional */ window.customSheet = sheet;
(document.head || document.getElementsByTagName('head')[0]).appendChild(sheet);
return (setStyle = function(cssText, node) {
if(!node || node.parentNode !== sheet)
return sheet.appendChild(document.createTextNode(cssText));
node.nodeValue = cssText;
return node;
})(cssText);
};
// a function to clear all css classes that were managed by this object
function ClearAllCssClases(){
for (var key in this.aCSSClasses){
//delete css class from html DOM structure
if(this.aCSSClasses[key]) setStyle( '', this.aCSSClasses[key] );
//delete from internale list
delete this.aCSSClasses[key];
}
return 1;
}
// a function to a specifc css class
function ClearSpecificCssClases( sCssRegisterationKey ){
if ( this.aCSSClasses[sCssRegisterationKey]){
//delete css class from html DOM structure
if(this.aCSSClasses[sCssRegisterationKey]) setStyle( '', this.aCSSClasses[sCssRegisterationKey] );
//delete from internale list
delete this.aCSSClasses[sCssRegisterationKey];
}
return 1;
}
//
function cleanDriverLabelStyles(){
setStyle( '', this.aCSSClasses['styledriverlabel'] );
delete this.aCSSClasses['styledriverlabel'];
}
//cut driverlabe to display only race position
function setDriverLabelStyle(mode, options){
switch (mode){
case "cutdriverlabeldark":
this.cleanDriverLabelStyles();
var CSSStyleHandle = setStyle(
'.driverlabel{\n' +
'/*max-width: 29px;*/\n' +
'max-width: 5ch; \n' +
'overflow: hidden; \n' +
'text-overflow: clip; \n' +
'white-space: nowrap; \n' +
'}\n',
CSSStyleHandle );
//remember object handle
this.aCSSClasses['styledriverlabel'] = (CSSStyleHandle);
break;
case "normallabelsdark":
this.cleanDriverLabelStyles();
var CSSStyleHandle = setStyle(
'.driverlabel{\n' +
'fill: black;\n' +
'}\n',
CSSStyleHandle );
//remember object handle
this.aCSSClasses['styledriverlabel'] = (CSSStyleHandle);
/*
setStyle( '', this.aCSSClasses['styledriverlabel'] );
delete this.aCSSClasses['styledriverlabel'];
*/
break;
case "normallabelslight":
this.cleanDriverLabelStyles();
// 'text-shadow: 1px 1px grey; \n' +
//'text-decoration-color: white \n' +
var CSSStyleHandle = setStyle(
'.driverlabel{\n' +
'overflow: hidden; \n' +
'text-overflow: clip; \n' +
'white-space: nowrap; \n' +
'fill: lightgrey; \n' +
'}\n',
CSSStyleHandle );
//remember object handle
this.aCSSClasses['styledriverlabel'] = (CSSStyleHandle);
break;
case "cutlabelslight":
this.cleanDriverLabelStyles();
var CSSStyleHandle = setStyle(
'.driverlabel{\n' +
'max-width: 5ch; \n' +
'fill: lightgrey; \n' +
'}\n',
CSSStyleHandle );
//remember object handle
this.aCSSClasses['styledriverlabel'] = (CSSStyleHandle);
break;
case "update_opacity_drivers":
//delete style before create a new one
if ( this.aCSSClasses['opacity_drivers'] ){
setStyle( '', this.aCSSClasses['opacity_drivers'] );
delete this.aCSSClasses['opacity_drivers'];
}
//set new style
var CSSStyleHandle = setStyle(
'.CSS_OPACITY_DRIVER{\n' +
' opacity: '+ options['opacity_driver'] +';\n' +
'}\n',
CSSStyleHandle );
//remember object handle
this.aCSSClasses['opacity_drivers'] = (CSSStyleHandle);
break;
}
}
// hide svg objects during change of race session
function HideAllSvg() {
/*
//clear all other individual CSS definitions
this.ClearAllCssClases();
//remember object handle
this.aCSSClasses['hideallsvgs'] = setStyle( 'svg.driverlabel{ display: none; } \n');
*/
//20170321 - use this instead of CSS classes because "delete this.aCSSClasses['hideallsvgs'];" did not work. Marker are still hidden
$(".stations").hide();
return 1;
}
//unhide svg ojects
function UnHideAllSvg(){
//set an empty style to unhide markers
/*
if ( this.aCSSClasses['hideallsvgs'] ){ // only call it if a hide class exists
this.aCSSClasses['hideallsvgs'] = setStyle( 'svg.driverlabel{ display: block; } \n');
delete this.aCSSClasses['hideallsvgs'];
}
*/
//Workaround: because unhide not working correctly as workaround we use ClearAllCssClases()
//this.ClearAllCssClases();
//20170321 - use this instead of CSS classes because "delete this.aCSSClasses['hideallsvgs'];" did not work. Marker are still hidden
$(".stations").show();
return 1;
}
//set opacity level for filtered out driver objects from drivers table
function HideSpecificDrivers( ObjectsRemainDisplayed ){
//TODO: maybe find a better way to map key and value. array.map() function??
var assarray = [];
for (var i = 0; i < ObjectsRemainDisplayed.length; i++){
assarray[""+ ObjectsRemainDisplayed[i]] = i;
}
/*
//Jquery variant
var regex = /^(\d\W-\W)(.*)/;
$( ".driverlabel" ).each(function( index ){
if(log >= 3){console.log('------- HideSpecificDrivers: TEXT: ', $(this).children("text")[0].innerHTML );}
//extract player name to match with array from drivertable
var driverlabel = $(this).children("text")[0].innerHTML;
if( new RegExp(regex).test(driverlabel) ){
if( assarray[ ""+ driverlabel.match(regex)[2] ] != undefined ){
$(this).removeClass('CSS_OPACITY_DRIVER');
if(log >= 3){console.log('------- HideSpecificDrivers: Remove Class to this: ', $(this) );}
}else{
if(log >= 3){console.log('------- HideSpecificDrivers: ADD Class ', $(this) );}
if(log >= 3){console.log('------- HideSpecificDrivers: ADD Class current CLASSES: ', $(this).attr('class') );}
$(this).addClass('CSS_OPACITY_DRIVER');
if(log >= 3){console.log('------- HideSpecificDrivers: ADD Class AFTER ERROR: ', $(this).error() );}
if(log >= 3){console.log('------- HideSpecificDrivers: ADD Class AFTER current CLASSES: ', $(this).attr('class') );}
}
//alert( driverlabel.match(regex)[2] );
}
});
*/
//D3 variant => has Problems while add/remove classes
d3.selectAll(".driverlabel")
.each(function(d, i) {
//if(log >= 3){console.log('------- HideSpecificDrivers: this D3: ', d );}
//decision if hide or unhide an object
if ( assarray[""+ d.Key] != undefined ){
d3.select(this)
.classed("CSS_OPACITY_DRIVER", false); // this D3 function works like an RemoveClass()
}else{
d3.select(this)
.classed("CSS_OPACITY_DRIVER", true); // this D3 function works like an addClass()
}
});
}
//color the first tree vehicles
function ColorTop3vehicles(){
//clear all other individual CSS definitions
this.ClearAllCssClases();
var CSScls;
this.aCSSClasses['CSSTOP3VEHICLES'] = setStyle(this.aCSSDef['CSSTOP3VEHICLES'],CSScls);
return 1;
}
//
function ColorDynClasses(mode){
// remember object handle
this.aCSSClasses['CSSDYNAMICS'] = setStyle( 'circle.CSS_Vehicle_GT4 {fill: gold; stroke-width: 3px \n}' );
return 1;
}
//color same vehicles names
function ColorSameVName( aVNames ){
//clear all other individual CSS definitions
this.ClearAllCssClases();
var cnt = 0;
for (var key in aVNames){
//generate new CSS styl and remember it
this.aCSSClasses['CSName'+cnt] = setStyle('circle.CSS_VehicleName_' + key + CSSDEFINITIONS['CSSCOLORSELECTION'][cnt]);
cnt++;
}
return 1;
}
//all cars from same class get same color
function ColorSameClass( aVCls ){
//clear all other individual CSS definitions
this.ClearAllCssClases();
var cnt = 0;
for (var key in aVCls){
//generate new CSS styl and remember it
this.aCSSClasses['CSClass'+cnt] = setStyle('circle.CSS_VehicleClass_' + key + CSSDEFINITIONS['CSSCOLORSELECTION'][cnt] );
cnt++;
}
return 1;
}
//all cars for human drivers
function ColorHumanDrivers(){
//clear all other individual CSS definitions
this.ClearAllCssClases();
// style Human
var cnt = 0;
this.aCSSClasses['CSClass'+cnt] = setStyle('circle.CSS_IsHumanPlayer' + CSSDEFINITIONS['CSSCOLORSELECTION'][cnt] );
// style AI
cnt++;
this.aCSSClasses['CSClass'+cnt] = setStyle(
'circle.CSS_IsAiPlayer{\n' +
' opacity: 0.35;\n' +
'}\n'
);
return 1;
}
//all cars for AI drivers
function ColorAiDrivers(){
//clear all other individual CSS definitions
this.ClearAllCssClases();
// style AI
var cnt = 0;
this.aCSSClasses['CSClass'+cnt] = setStyle('circle.CSS_IsAiPlayer' + CSSDEFINITIONS['CSSCOLORSELECTION'][cnt] );
// style Humans
cnt++;
this.aCSSClasses['CSClass'+cnt] = setStyle(
'circle.CSS_IsHumanPlayer{\n' +
' opacity: 0.35;\n' +
'}\n'
);
return 1;
}
/* color the selected row/driver from driver table
* param {int}
*
* return {string} returns true if all is fine, null in case of errors/missing parameters
*/
function ColorSelectedVehicle(oRowData, bCancleColoration ){
var sCssClassRegistrationName = 'CSSDRIVERSELECTION';
// set default value if parameter is missing
if (!bCancleColoration){
bCancleColoration = false;
}
// remove CSS class attributes and decolorized marker
if (bCancleColoration && this.aCSSClasses[sCssClassRegistrationName]){
this.ClearSpecificCssClases(sCssClassRegistrationName);
this.sLastDriverSelectionClass = 'initial_value';
return true;
}
// check if driver name is defined
if (oRowData.drivername === undefined){
return null;
}
var sDriverName = oRowData.drivername+'';
//build CSS class name of a specific driver
var sClassName = "circle.CSS_DriverName_" + new PCARSdriver()._normalizeString(sDriverName);
// set new css class attributes
if ( this.sLastDriverSelectionClass != sClassName ){
//injection of css attributes
this.aCSSClasses[sCssClassRegistrationName] = setStyle( sClassName + CSSDRIVERSELECTION );
this.sLastDriverSelectionClass = sClassName;
}
/*
//console.log("SICECKHA ColorSelectedVehicle getelement():", document.querySelectorAll(sClassName) );
//console.log("SICECKHA ColorSelectedVehicle getelement():", document.getElementsByClassName(sClassName); );
*/
return true;
}
CSSClassChanger.prototype.setStyle=setStyle;
CSSClassChanger.prototype.HideAllSvg=HideAllSvg;
CSSClassChanger.prototype.UnHideAllSvg=UnHideAllSvg;
CSSClassChanger.prototype.HideSpecificDrivers=HideSpecificDrivers;
CSSClassChanger.prototype.ClearAllCssClases=ClearAllCssClases;
CSSClassChanger.prototype.ClearSpecificCssClases=ClearSpecificCssClases;
CSSClassChanger.prototype.ColorTop3vehicles=ColorTop3vehicles;
CSSClassChanger.prototype.setDriverLabelStyle=setDriverLabelStyle;
CSSClassChanger.prototype.cleanDriverLabelStyles=cleanDriverLabelStyles;
CSSClassChanger.prototype.ColorDynClasses=ColorDynClasses;
CSSClassChanger.prototype.ColorSameVName=ColorSameVName;
CSSClassChanger.prototype.ColorSameClass=ColorSameClass;
CSSClassChanger.prototype.ColorHumanDrivers=ColorHumanDrivers;
CSSClassChanger.prototype.ColorAiDrivers=ColorAiDrivers;
CSSClassChanger.prototype.ColorSelectedVehicle=ColorSelectedVehicle;