This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
712 lines (640 loc) · 24.3 KB
/
script.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
$(function () {
// Some useful strings to reduce the size of the script.
var usd = 'usd';
var black = 'black';
var blue = 'blue';
var red = 'red';
// var silver = 'silver';
var midnight = 'midnight';
var white = 'white';
// var obsidian = 'obsidian';
var aero = 'aero';
var sport = 'sport';
var long = 'long';
var standard = 'standard';
// Setting default variables.
var currency = usd;
var current_lang = 'en_US';
var import_tax = 1;
var vat = 1;
var incentive = 0;
var color = black;
var wheels = aero;
var premium = true;
var white_interior = false;
var battery = long;
var autopilot = false;
// var self_driving = false;
var dual = true;
var performance = false;
var performance_upgrade = false;
var usd_to_eur = 0.881475;
var change_margin = 1;
var prices = {
usd: {
'base': 35000,
'wheels': {
'aero': 0,
'sport': 1500,
},
'paint': {
black: 0,
blue: 1500,
red: 2500,
silver: 1500,
midnight: 1500,
white: 2000,
obsidian: 1500
},
'battery': {
standard: 0,
mid: 6000,
long: 9000
},
'premium': 5000,
'white_interior': 1500,
'autopilot': 5000,
// 'self_driving': 3000,
'dual': 4000,
'performance': 6000,
'performance_upgrade': 5000,
'dest_doc': 1200
},
};
prices.eur = {
'base': Math.round(prices.usd.base * usd_to_eur * change_margin),
'wheels': {
aero: 0,
sport: 1600 / 1.2,
},
'paint': {
black: 0,
blue: 1600 / 1.2,
red: 2600 / 1.2,
silver: 1600 / 1.2,
midnight: 1600 / 1.2,
white: 2000 / 1.2,
obsidian: 1600 / 1.2
},
'battery': {
standard: 0,
mid: Math.round(prices.usd.battery.mid * usd_to_eur * change_margin),
long: Math.round(prices.usd.battery.long * usd_to_eur * change_margin)
},
'premium': Math.round(prices.usd.premium * usd_to_eur * change_margin),
'white_interior': Math.round(prices.usd.white * usd_to_eur * change_margin),
'autopilot': 5300 / 1.2,
// 'self_driving': Math.round(prices.usd.self_driving * usd_to_eur * change_margin),
'dual': Math.round(prices.usd.dual * usd_to_eur * change_margin),
'performance': Math.round(prices.usd.performance * usd_to_eur * change_margin),
'performance_upgrade': Math.round(prices.usd.performance_upgrade * usd_to_eur * change_margin),
'dest_doc': Math.round(prices.usd.dest_doc * usd_to_eur * change_margin)
};
var lang = {
'en_US': {
'thou_sep': ',',
'yes': 'Yes',
'no': 'No',
'title': 'Tesla Model 3 calculator',
'h1': 'Configure your Tesla Model 3',
'included': 'Included',
'wheels': {
aero: 'Aero',
sport: 'Sport',
},
'paint': {
black: 'Solid Black',
blue: 'Deep Blue Metallic',
red: 'Red Multi-Coat',
// silver: 'Silver Metallic',
midnight: 'Midnight Silver Metallic',
white: 'Pearl White Multi-Coat',
// obsidian: 'Obsidian Black Metallic'
},
'battery': {
standard: 'Standard (220 miles)',
mid: 'Mid range (260 miles)',
long: 'Long Range (310 miles)'
},
'data': {
'color': 'Color',
'wheels': 'Wheels',
'premium': 'Premium Upgrades',
'white_interior': 'White Interior',
'battery': 'Battery',
'autopilot': 'Enhanced Autopilot',
// 'self_driving': 'Full Self-Driving',
'dual': 'All Wheel Drive',
'performance': 'Performance',
'performance_upgrade': 'Performance Upgrade',
},
'form': {
'dest_doc': 'Destination & Doc Fee',
'import': 'Import Tax',
'total_no_vat': 'Total (no VAT)',
'vat': 'VAT',
'incentive': 'Direct incentive',
'total_vat': "Total"
},
'notes': {
'title': 'Note',
// 'self_driving': 'Full Self-Driving requires Enhanced Autopilot',
'price': 'Tentative price, not officially released by Tesla',
'performance': 'Performance requires all wheel drive'
}
},
'es_ES': {
'thou_sep': '.',
'yes': 'Sí',
'no': 'No',
'title': 'Calculadora del Tesla Model 3',
'h1': 'Configura tu Tesla Model 3',
'included': 'Incluido',
'wheels': {
aero: 'Aero',
sport: 'Sport',
},
'paint': {
black: 'Negro sólido',
blue: 'Azul oscuro metálico',
red: 'Rojo multi-capa',
// silver: 'Plateado metálico',
midnight: 'Plateado metálico medianoche',
white: 'Blanco perlado multi-capa',
// obsidian: 'Negro obsidiana metálico'
},
'battery': {
standard: 'Estándar (354 km)',
mid: 'Alcance medio (418 km)',
long: 'Largo alcance (544 km)'
},
'data': {
'color': 'Color',
'wheels': 'Ruedas',
'premium': 'Mejoras premium',
'white_interior': 'Interior blanco',
'battery': 'Batería',
'autopilot': 'Piloto automático mejorado',
// 'self_driving': 'Conducción autónoma',
'dual': 'Tracción integral',
'performance': 'Máximo rendimiento',
'performance_upgrade': 'Mejoras para máximo rendimiento',
},
'form': {
'dest_doc': 'Tasa de destinación y documentación',
'import': 'Tasas de importación',
'total_no_vat': 'Total (sin IVA)',
'vat': 'IVA',
'incentive': 'Ayudas directas',
'total_vat': "Total"
},
'notes': {
'title': 'Nota',
// 'self_driving': 'La conducción autónoma require el piloto automático mejorado',
'price': 'Precio tentativo, no publicado por Tesla',
'performance': 'El modo máximo rendimiento requiere tracción integral'
}
}
};
var number_format = function (x, t) {
return x.toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, t);
};
var currency_format = function (amount) {
return (currency == usd ? '$' : '') +
number_format(amount, lang[current_lang].thou_sep) +
(currency == 'eur' ? '€' : '');
};
var add_class = 'add';
var remove_class = 'remove';
var selected_class = 'selected';
$('.color .button').click(function () {
if ($(this).hasClass(black)) {
color = black;
} else if ($(this).hasClass(blue)) {
color = blue;
} else if ($(this).hasClass(red)) {
color = red;
// } else if ($(this).hasClass(silver)) {
// color = silver;
} else if ($(this).hasClass(midnight)) {
color = midnight;
} else if ($(this).hasClass(white)) {
color = white;
// } else if ($(this).hasClass(obsidian)) {
// color = obsidian;
}
update_image();
$('.color .button.selected').removeClass(selected_class);
$(this).addClass(selected_class);
update_data();
update_total();
});
var performance_upgrade_modify_selector = ".data .performance_upgrade .modify";
$('.wheels .button').click(function () {
if ($(this).hasClass(aero)) {
wheels = aero;
if (performance_upgrade) {
$(performance_upgrade_modify_selector).click();
}
} else if ($(this).hasClass(sport)) {
wheels = sport;
if (performance && !performance_upgrade) {
$(performance_upgrade_modify_selector).click();
}
}
update_image();
$('.wheels .button.selected').removeClass(selected_class);
$(this).addClass(selected_class);
update_data();
update_total();
});
var white_interior_modify_selector = '.data .white_interior .modify';
$('.data .premium .modify').click(function () {
if (premium) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
}
premium = !premium;
update_data();
update_total();
});
var performance_modify_selector = '.data .performance .modify';
$(white_interior_modify_selector).click(function () {
if (white_interior) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
if (!performance) {
$(performance_modify_selector).click();
}
}
white_interior = !white_interior;
update_data();
update_total();
});
var battery_modify_selector = '.data .battery .modify';
$(battery_modify_selector).click(function () {
if (battery === standard) {
battery = long;
$(this).removeClass(add_class);
$(this).addClass(remove_class);
} else {
battery = standard;
$(this).removeClass(remove_class);
$(this).addClass(add_class);
if (performance) {
$(performance_modify_selector).click();
}
}
update_data();
update_total();
});
var autopilot_modify_selector = '.data .autopilot .modify';
// var self_driving_modify_selector = '.data .self_driving .modify';
$(autopilot_modify_selector).click(function () {
if (autopilot) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
// if (self_driving) {
// $(self_driving_modify_selector).click();
// }
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
}
autopilot = !autopilot;
update_data();
update_total();
});
// $(self_driving_modify_selector).click(function () {
// if (!self_driving) {
// $(this).removeClass(add_class);
// $(this).addClass(remove_class);
// if (!autopilot) {
// $(autopilot_modify_selector).click();
// }
// } else {
// $(this).removeClass(remove_class);
// $(this).addClass(add_class);
// }
// self_driving = !self_driving;
// update_data();
// update_total();
// });
var dual_modify_selector = '.data .dual .modify';
$(dual_modify_selector).click(function () {
if (dual) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
if (performance) {
$(performance_modify_selector).click();
}
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
}
dual = !dual;
update_data();
update_total();
});
$(performance_modify_selector).click(function () {
if (performance) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
if (white_interior) {
$(white_interior_modify_selector).click();
}
if (performance_upgrade) {
$(performance_upgrade_modify_selector).click();
}
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
if (!dual) {
$(dual_modify_selector).click();
}
if (battery === standard) {
$(battery_modify_selector).click();
}
}
performance = !performance;
update_data();
update_total();
});
$(performance_upgrade_modify_selector).click(function () {
if (performance_upgrade) {
$(this).removeClass(remove_class);
$(this).addClass(add_class);
performance_upgrade = false;
if (wheels === sport) {
$('.wheels .button.aero').click();
}
} else {
$(this).removeClass(add_class);
$(this).addClass(remove_class);
performance_upgrade = true;
if (wheels === aero) {
$('.wheels .button.sport').click();
}
if (!performance) {
$(performance_modify_selector).click();
}
}
update_data();
update_total();
});
// Currency and language change:
var currency_selector = 'nav.currency';
var open_class = 'open';
$('nav.currency li').click(function () {
if ($(currency_selector).hasClass(open_class)) {
$(this).addClass(selected_class);
$(currency_selector).removeClass(open_class);
currency = $(this).data('currency');
update_currency();
update_data();
update_total();
} else {
$(currency_selector).addClass(open_class);
$(this).removeClass(selected_class);
}
});
var lang_selector = 'nav.lang';
$('nav.lang li').click(function () {
if ($(lang_selector).hasClass(open_class)) {
$(this).addClass(selected_class);
$(lang_selector).removeClass(open_class);
current_lang = $(this).data('lang');
update_currency();
update_language();
update_data();
update_total();
} else {
$(lang_selector).addClass(open_class);
$(this).removeClass(selected_class);
}
});
$('#import').change(function () {
import_tax = 1 + $(this).val() / 100;
update_total();
});
$('#vat').change(function () {
vat = 1 + $(this).val() / 100;
update_total();
});
$('#incentive').change(function () {
incentive = $(this).val();
update_total();
});
var update_currency = function () {
var color_price = color === black ?
lang[current_lang].included :
'+' + currency_format(prices[currency].paint[color]) + (currency === usd ? '' : '**');
$('.data .color .price').text(color_price);
var wheels_price = wheels === aero ?
lang[current_lang].included :
'+' + currency_format(prices[currency].wheels[wheels]) + (currency === usd ? '' : '**');
$('.data .wheels .price').text(wheels_price);
$('.data .premium .price').text('+' + currency_format(prices[currency].premium) + (currency === usd ? '' : '**'));
$('.data .white_interior .price').text('+' + currency_format(prices[currency].white_interior));
var battery_price = battery === standard ?
lang[current_lang].included :
'+' + currency_format(prices[currency].battery[battery]) + (currency === usd ? '' : '**');
$('.data .battery .price').text(battery_price);
$('.data .autopilot .price').text('+' + currency_format(prices[currency].autopilot) + (currency === usd ? '' : '**'));
// $('.data .self_driving .price').text('+' + currency_format(prices[currency].self_driving) + (currency === usd ? '' : '**'));
$('.data .dual .price').text('+' + currency_format(prices[currency].premium));
$('.data .performance .price').text('+' + currency_format(prices[currency].performance));
$('.data .performance_upgrade .price').text('+' + currency_format(prices[currency].performance_upgrade));
if (currency == 'eur') {
$('.currency_symbol.before').hide();
$('.currency_symbol.after').show();
} else if (currency == 'usd') {
$('.currency_symbol.after').hide();
$('.currency_symbol.before').show();
}
$('.price .dest_doc .price').text(currency_format(prices[currency].dest_doc))
};
var update_language = function () {
$('title').text(lang[current_lang].title);
$('h1').text(lang[current_lang].h1);
// Data section
$('.color .title').text(lang[current_lang].data.color);
$('.wheels .title').text(lang[current_lang].data.wheels);
$('.premium .title').text(lang[current_lang].data.premium);
$('.white_interior .title').text(lang[current_lang].data.white_interior);
$('.battery .title').text(lang[current_lang].data.battery);
$('.autopilot .title').text(lang[current_lang].data.autopilot);
// $('.self_driving .title').text(lang[current_lang].data.self_driving);
$('.dual .title').text(lang[current_lang].data.dual);
$('.performance .title').text(lang[current_lang].data.performance);
$('.performance_upgrade .title').text(lang[current_lang].data.performance_upgrade);
// Form
$('.dest_doc .title').text(lang[current_lang].form.dest_doc);
$('label[for="import"]').text(lang[current_lang].form.import);
$('.total-no-vat .title').text(lang[current_lang].form.total_no_vat);
$('label[for="vat"]').text(lang[current_lang].form.vat);
$('label[for="incentive"]').text(lang[current_lang].form.incentive);
$('.total-vat .title').text(lang[current_lang].form.total_vat);
// Notes
$('.note .title').text(lang[current_lang].notes.title);
// $('.note.self-driving .text').text(lang[current_lang].notes.self_driving);
$('.note.price .text').text(lang[current_lang].notes.price);
$('.note.performance .text').text(lang[current_lang].notes.performance);
};
var update_image = function () {
$('img.car').attr('src', 'img/' + color + '_' + wheels + '.png');
};
var update_data = function () {
$('.data .color .text').text(lang[current_lang].paint[color]);
var color_price = color == black ?
lang[current_lang].included :
'+' + currency_format(prices[currency].paint[color]) + (currency == usd ? '' : '**');
$('.data .color .price').text(color_price);
var wheels_price = wheels == aero ?
lang[current_lang].included :
'+' + currency_format(prices[currency].wheels[wheels]) + (currency == usd ? '' : '**');
$('.data .wheels .price').text(wheels_price);
$('.data .wheels .text').text(lang[current_lang].wheels[wheels]);
var premium_text = premium ? lang[current_lang].yes : lang[current_lang].no;
$('.data .premium .text').text(premium_text);
var white_interior_text = white_interior ? lang[current_lang].yes : lang[current_lang].no;
$('.data .white_interior .text').text(white_interior_text);
var battery_text = lang[current_lang].battery[battery];
$('.data .battery .text').text(battery_text);
var autopilot_text = autopilot ? lang[current_lang].yes : lang[current_lang].no;
$('.data .autopilot .text').text(autopilot_text);
// var self_driving_text = self_driving ? lang[current_lang].yes : lang[current_lang].no;
// $('.data .self_driving .text').text(self_driving_text);
var dual_text = dual ? lang[current_lang].yes : lang[current_lang].no;
$('.data .dual .text').text(dual_text);
var performance_text = performance ? lang[current_lang].yes : lang[current_lang].no;
$('.data .performance .text').text(performance_text);
var performance_upgrade_text = performance_upgrade ? lang[current_lang].yes : lang[current_lang].no;
$('.data .performance_upgrade .text').text(performance_upgrade_text);
};
var update_total = function () {
var price = prices[currency].base;
if (premium) {
price += prices[currency].premium;
}
if (white_interior) {
price += prices[currency].white_interior;
}
if (autopilot) {
price += prices[currency].autopilot;
}
// if (self_driving) {
// price += prices[currency].self_driving;
// }
if (dual) {
price += prices[currency].dual;
}
if (performance) {
price += prices[currency].performance;
}
if (performance_upgrade) {
price += prices[currency].performance_upgrade;
}
if (!performance_upgrade) {
price += prices[currency].wheels[wheels];
}
price += prices[currency].paint[color];
price += prices[currency].battery[battery];
price += prices[currency].dest_doc;
var import_price = price * (import_tax - 1);
$('.price .import span').text(currency_format(import_price));
price = price + import_price;
$('.price h3.total-no-vat span.price').text(currency_format(price));
var vat_price = price * (vat - 1);
$('.price .vat span.price').text(currency_format(vat_price));
var total_price = price * vat - incentive;
$('.price h2 span.price').text(currency_format(total_price));
};
var setupValue = function (key, value) {
key = key.trim();
value = value.trim();
switch (key) {
case "currency":
if (typeof prices[value] !== 'undefined') {
currency = value;
}
break;
case "current_lang":
if (typeof lang[value] !== 'undefined') {
current_lang = value;
}
break;
case "import_tax":
value = Number.parseFloat(value);
if (value >= 0) {
import_tax = value;
}
break;
case "vat":
value = Number.parseFloat(value);
if (value >= 0) {
vat = value;
}
break;
case "incentive":
value = Number.parseFloat(value);
if (value >= 0) {
incentive = value;
}
break;
case "color":
if (typeof prices[current_lang].paint[value] !== 'undefined') {
color = value;
}
break;
case "wheels":
if (typeof prices[current_lang].wheels[value] !== 'undefined') {
wheels = value;
}
break;
case "premium":
// TODO parse boolean
break;
case "white_interior":
// TODO parse boolean
break;
case "battery":
if (typeof prices[current_lang].battery[value] !== 'undefined') {
color = value;
}
break;
case "autopilot":
// TODO parse boolean
break;
// case "self_driving":
// // TODO parse boolean
// break;
case "dual":
// TODO parse boolean
break;
case "performance":
// TODO parse boolean
break;
}
};
// Check if any parameters were provided and update UI.
var queryString = document.documentURI.split('?')
if (typeof queryString[1] !== 'undefined') {
queryString = queryString[1].split('&');
for (i in queryString) {
var queryParam = queryString[i].split('=');
setupValue(queryParam[0], queryParam[1]);
}
update_image();
update_currency();
update_language();
update_data();
update_total();
}
});