-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
570 lines (512 loc) · 20 KB
/
index.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports)
: typeof define === 'function' && define.amd
? define([ 'exports' ], factory)
: factory((global.gsheets = global.gsheets || {}))
})(this, function (exports) {
'use strict'
// Array.prototype.find polyfill – needed for IE11
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}
/**
* The available languages
* @typedef {'de' | 'fr' | 'it' | 'ro'} LanguageCode
*/
/** Colors are defined as hex codes
* @typedef {string} Color - A color as a hex code e.g. #123456
*/
/** Party IDs are numbers from 1 to about 50
* @typedef {number} PartyId - The ID of a party as visible in the spreadsheet
*/
/**
* The Party definition object
* @typedef {Object} PartyDefinition
* @property {string} abbr - The commonly used abbrevation of the party
* @property {string} name - The official name of the party
* @property {Color} color - The hex color for use on larger color patches
* @property {Color} blackOrWhite - The hex color for the text displayed on patches of that color
* @property {Color} fontColor - A tweaked version of the hex color for use as a text color on light backgrounds
* @property {Color} fontColorOnBlack - A tweaked version of the hex color for use as a text color on dark backgrounds
*/
/** @type {LanguageCode[]} */
var available_languages = [ 'de', 'fr', 'it', 'ro' ]
/**
* Fallback object in cases where the party can't be found for some reason
* @type {PartyDefinition}
*/
var fallBackColor = {
abbr: '?',
name: 'Not found',
color: '#9D9D9D',
blackOrWhite: '#000',
fontColor: '#757575',
fontColorOnBlack: '#757575'
}
// here the data from the google spreadsheet is loaded
var colors = require('./definitions.json')
/**
* Get a function that returns a party definition for an abbreviation in the given language
* @param {LanguageCode} lang language code that the abbreviations will be in when called
* @return {Function} getParty
* @throws If the langauge that is requested is not valid, an Error is thrown
*/
function getPartyFunctionForLanguage (lang) {
lang = lang.toLowerCase()
return function getParty (abbr) {
abbr = abbr.toUpperCase()
if (available_languages.indexOf(lang) < 0) {
throw new Error(
'the requested language ' +
lang +
' is not available. Chose either "de", "fr", "it" or "ro"'
)
} else {
// This method should be polyfilled, see above
// eslint-disable-next-line es5/no-es6-methods
var party = colors.data.find(function(party){return party['abbr_' + lang] === abbr})
if (!party) {
console.error(
'Error in getParty: party ' +
abbr +
' not found in lang ' +
lang +
', returning fallback color'
)
return fallBackColor
}
return {
abbr: party['abbr_' + lang],
name: party['name_' + lang],
color: party.color,
blackOrWhite: party.on_color,
fontColor: party.on_white,
fontColorOnBlack: party.on_black
}
}
}
}
/**
* Get the complete party definition by party id and language
* @param {PartyId} id ID of the party in question
* @param {LanguageCode} [lang='de'] Language that the return value is
* @return {PartyDefinition}
*/
function getPartyById (id, lang) {
// Default value for language
lang = lang || 'de'
if (available_languages.indexOf(lang) < 0) {
throw new Error(
'the requested language ' +
lang +
' is not available. Chose either "de", "fr", "it" or "ro"')
} else {
var availableIds = colors.data.map(function (d) { return d.id })
if (availableIds.indexOf(id) < 0) {
console.error(
'the requested party id ' +
id +
' is not available, returning fallback color')
return fallBackColor
}
// eslint-disable-next-line es5/no-es6-methods
var party = colors.data.find(function (d) { return d.id === id })
return {
abbr: party['abbr_' + lang],
name: party['name_' + lang],
color: party.color,
blackOrWhite: party.on_color,
fontColor: party.on_white,
fontColorOnBlack: party.on_black,
}
}
}
/**
* Get the complete party definition by abbreviation
* @param {LanguageCode} lang Language that the abbreviation is in
* @param {string} abbr Abbreviation of the party in question
* @return {PartyDefinition}
* @throws If the langauge that is requested is not valid, an Error is thrown
*/
function getParty (lang, abbr) {
return getPartyFunctionForLanguage(lang)(abbr)
}
// create one function per language for export
var getPartyDE = getPartyFunctionForLanguage('de')
var getPartyFR = getPartyFunctionForLanguage('fr')
var getPartyIT = getPartyFunctionForLanguage('it')
var getPartyRO = getPartyFunctionForLanguage('ro')
/**
* Get party abbreviation by id and language
* @param {PartyId} id ID of the party in question
* @param {LanguageCode} lang The language the returend abbreviation will be in
* @return {string}
*/
function getPartyAbbrById (id, lang) {
var party = getPartyById(id, lang)
if (party) return party.abbr
}
/**
* Get party name by id and language
* @param {PartyId} id ID of the party in question
* @param {LanguageCode} lang The language the returend abbreviation will be in
* @return {string} Name of the party in the lang specified
*/
function getPartyNameById (id, lang) {
var party = getPartyById(id, lang)
if (party) return party.name
}
/**
* Get party name by language and abbreviation
* @param {LanguageCode} lang The language of the abbreviation and the party name
* @param {string} abbr Abbreviation of the party in the lang specified
* @return {string} Name of the party in the lang specified
*/
function getPartyName (lang, abbr) {
var party = getPartyFunctionForLanguage(lang)(abbr)
if (party) return party.name
}
/**
* Get party name by abbreviation
* @param {string} abbr Abbreviation of the party in German
* @return {string} Name of the party in German
*/
function getPartyNameDE (abbr) {
var party = getPartyDE(abbr)
if (party) return party.name
}
/**
* Get party name by abbreviation
* @param {string} abbr Abbreviation of the party in French
* @return {string} Name of the party in French
*/
function getPartyNameFR (abbr) {
var party = getPartyFR(abbr)
if (party) return party.name
}
/**
* Get party name by abbreviation
* @param {string} abbr Abbreviation of the party in Italian
* @return {string} Name of the party in Italian
*/
function getPartyNameIT (abbr) {
var party = getPartyIT(abbr)
if (party) return party.name
}
/**
* Get party name by abbreviation
* @param {string} abbr Abbreviation of the party in Rhaeto-Romanic
* @return {string} Name of the party in Rhaeto-Romanic
*/
function getPartyNameRO (abbr) {
var party = getPartyRO(abbr)
if (party) return party.name
}
/**
* Get party name by language and abbreviation
* @param {LanguageCode} lang The language of the abbreviation
* @param {string} abbr Abbreviation of the party in the lang specified
* @return {string} Primary party color as hex
*/
function getPartyColor (lang, abbr) {
var party = getPartyFunctionForLanguage(lang)(abbr)
if (party) return party.color
}
/**
* Get primary party color by id
* @param {PartyId} id ID of the party in question
* @return {string} Primary party color as hex
*/
function getPartyColorById (id) {
var party = getPartyById(id)
if (party) return party.color
}
/**
* Get primary party color by abbreviation
* @param {string} abbr Abbreviation of the party in German
* @return {Color} Primary party color as hex
*/
function getPartyColorDE (abbr) {
var party = getPartyDE(abbr)
if (party) return party.color
}
/**
* Get primary party color by abbreviation
* @param {string} abbr Abbreviation of the party in French
* @return {Color} Primary party color as hex
*/
function getPartyColorFR (abbr) {
var party = getPartyFR(abbr)
if (party) return party.color
}
/**
* Get primary party color by abbreviation
* @param {string} abbr Abbreviation of the party in Italian
* @return {Color} Primary party color as hex
*/
function getPartyColorIT (abbr) {
var party = getPartyIT(abbr)
if (party) return party.color
}
/**
* Get primary party color by abbreviation
* @param {string} abbr Abbreviation of the party in Rhaeto-Romanic
* @return {Color} Primary party color as hex
*/
function getPartyColorRO (abbr) {
var party = getPartyRO(abbr)
if (party) return party.color
}
/**
* Get black or white for text on a primary colored background by language and abbreviation
* @param {LanguageCode} lang The language of the abbreviation
* @param {string} abbr Abbreviation of the party in the lang specified
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhite (lang, abbr) {
var party = getPartyFunctionForLanguage(lang)(abbr)
if (party) return party.blackOrWhite
}
/**
* Get black or white for text on a primary colored background by id
* @param {PartyId} id ID of the party in question
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhiteById (id) {
var party = getPartyById(id)
if (party) return party.blackOrWhite
}
/**
* Get black or white for text on a primary colored background by abbreviation
* @param {string} abbr Abbreviation of the party in German
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhiteDE (abbr) {
var party = getPartyDE(abbr)
if (party) return party.blackOrWhite
}
/**
* Get black or white for text on a primary colored background by abbreviation
* @param {string} abbr Abbreviation of the party in French
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhiteFR (abbr) {
var party = getPartyFR(abbr)
if (party) return party.blackOrWhite
}
/**
* Get black or white for text on a primary colored background by abbreviation
* @param {string} abbr Abbreviation of the party in Italian
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhiteIT (abbr) {
var party = getPartyIT(abbr)
if (party) return party.blackOrWhite
}
/**
* Get black or white for text on a primary colored background by abbreviation
* @param {string} abbr Abbreviation of the party in Rhaeto-Romanic
* @return {Color} Either #FFF or #000
*/
function getBlackOrWhiteRO (abbr) {
var party = getPartyRO(abbr)
if (party) return party.blackOrWhite
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {LanguageCode} lang The language of the abbreviation
* @param {string} abbr Abbreviation of the party in the lang specified
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColor (lang, abbr) {
var party = getPartyFunctionForLanguage(lang)(abbr)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {PartyId} id ID of the party in question
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorById (id) {
var party = getPartyById(id)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in German
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorDE (abbr) {
var party = getPartyDE(abbr)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in French
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorFR (abbr) {
var party = getPartyFR(abbr)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in Italian
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorIT (abbr) {
var party = getPartyIT(abbr)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on white by langauge and abbreviation. This color is often darker than
* the primary party color so it can be used on a white background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in Rhaeto-Roman
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorRO (abbr) {
var party = getPartyRO(abbr)
if (party) return party.fontColor
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {LanguageCode} lang The language of the abbreviation
* @param {string} abbr Abbreviation of the party in the lang specified
* @return {Color} Accessibility friendly, dark party color as hex
*/
function getPartyFontColorOnBlack (lang, abbr) {
var party = getPartyFunctionForLanguage(lang)(abbr)
if (party) return party.fontColorOnBlack
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {PartyId} id ID of the party in question
* @return {Color} Accessibility friendly, bright party color as hex
*/
function getPartyFontColorOnBlackById (id) {
var party = getPartyById(id)
if (party) return party.fontColorOnBlack
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in German
* @return {Color} Accessibility friendly, bright party color as hex
*/
function getPartyFontColorOnBlackDE (abbr) {
var party = getPartyDE(abbr)
if (party) return party.fontColorOnBlack
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in French
* @return {Color} Accessibility friendly, bright party color as hex
*/
function getPartyFontColorOnBlackFR (abbr) {
var party = getPartyFR(abbr)
if (party) return party.fontColorOnBlack
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in Italian
* @return {Color} Accessibility friendly, bright party color as hex
*/
function getPartyFontColorOnBlackIT (abbr) {
var party = getPartyIT(abbr)
if (party) return party.fontColorOnBlack
}
/**
* Get the party color for use as text color on black by langauge and abbreviation. This color is often brighter than
* the primary party color so it can be used on a black background and maintain a contrast ratio of at least 1:4.5
* @param {string} abbr Abbreviation of the party in Rhaeto-Roman
* @return {Color} Accessibility friendly, bright party color as hex
*/
function getPartyFontColorOnBlackRO (abbr) {
var party = getPartyRO(abbr)
if (party) return party.fontColorOnBlack
}
exports.getPartyById = getPartyById
exports.getParty = getParty
exports.getPartyDE = getPartyDE
exports.getPartyFR = getPartyFR
exports.getPartyIT = getPartyIT
exports.getPartyRO = getPartyRO
exports.getPartyAbbrById = getPartyAbbrById
exports.getPartyName = getPartyName
exports.getPartyNameById = getPartyNameById
exports.getPartyNameDE = getPartyNameDE
exports.getPartyNameFR = getPartyNameFR
exports.getPartyNameIT = getPartyNameIT
exports.getPartyNameRO = getPartyNameRO
exports.getPartyColor = getPartyColor
exports.getPartyColorById = getPartyColorById
exports.getPartyColorDE = getPartyColorDE
exports.getPartyColorFR = getPartyColorFR
exports.getPartyColorIT = getPartyColorIT
exports.getPartyColorRO = getPartyColorRO
exports.getBlackOrWhite = getBlackOrWhite
exports.getBlackOrWhiteById = getBlackOrWhiteById
exports.getBlackOrWhiteDE = getBlackOrWhiteDE
exports.getBlackOrWhiteFR = getBlackOrWhiteFR
exports.getBlackOrWhiteIT = getBlackOrWhiteIT
exports.getBlackOrWhiteRO = getBlackOrWhiteRO
exports.getPartyFontColor = getPartyFontColor
exports.getPartyFontColorById = getPartyFontColorById
exports.getPartyFontColorDE = getPartyFontColorDE
exports.getPartyFontColorFR = getPartyFontColorFR
exports.getPartyFontColorIT = getPartyFontColorIT
exports.getPartyFontColorRO = getPartyFontColorRO
exports.getPartyFontColorOnBlack = getPartyFontColorOnBlack
exports.getPartyFontColorOnBlackById = getPartyFontColorOnBlackById
exports.getPartyFontColorOnBlackDE = getPartyFontColorOnBlackDE
exports.getPartyFontColorOnBlackFR = getPartyFontColorOnBlackFR
exports.getPartyFontColorOnBlackIT = getPartyFontColorOnBlackIT
exports.getPartyFontColorOnBlackRO = getPartyFontColorOnBlackRO
Object.defineProperty(exports, '__esModule', { value: true })
})