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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
/**
* Retrieve the border color property name depending on direction.
* @param {String} direction - Either `top`, `right`, `left` or `bottom`.
* @returns {String} The CSS property name.
*/
function getBorderColorProperty(direction) {
let borderColorProperty;
switch (direction) {
case "top":
borderColorProperty = "borderTopColor";
break;
case "right":
borderColorProperty = "borderRightColor";
break;
case "bottom":
borderColorProperty = "borderBottomColor";
break;
case "left":
borderColorProperty = "borderLeftColor";
break;
default:
borderColorProperty = "borderColor";
break;
}
return borderColorProperty;
}
/**
* Retrieve the border style property name depending on direction.
* @param {String} direction - Either `top`, `right`, `bottom` or `left`.
* @returns {String} The CSS property name.
*/
function getBorderStyleProperty(direction) {
let borderStyleProperty;
switch (direction) {
case "top":
borderStyleProperty = "borderTopStyle";
break;
case "right":
borderStyleProperty = "borderRightStyle";
break;
case "bottom":
borderStyleProperty = "borderBottomStyle";
break;
case "left":
borderStyleProperty = "borderLeftStyle";
break;
default:
borderStyleProperty = "borderStyle";
break;
}
return borderStyleProperty;
}
/**
* Retrieve the border width property name depending on direction.
* @param {String} direction - Either `top`, `right`, `left` or `bottom`.
* @returns {String} The CSS property name.
*/
function getBorderWidthProperty(direction) {
let borderWidthProperty;
switch (direction) {
case "top":
borderWidthProperty = "borderTopWidth";
break;
case "right":
borderWidthProperty = "borderRightWidth";
break;
case "bottom":
borderWidthProperty = "borderBottomWidth";
break;
case "left":
borderWidthProperty = "borderLeftWidth";
break;
default:
borderWidthProperty = "borderWidth";
break;
}
return borderWidthProperty;
}
/**
* Apply the custom border to an element.
* @param {HTMLElement} el - Apply border to this element.
* @param {String} property - Either `color`, `style` or `width`.
* @param {String} value - The value to apply.
* @param {String} [direction] - Either `top`, `right`, `bottom` or `left`.
*/
function setBorder(el, property, value, direction = null) {
let borderProperty;
switch (property) {
case "color":
borderProperty = getBorderColorProperty(direction);
break;
case "style":
borderProperty = getBorderStyleProperty(direction);
break;
case "width":
borderProperty = getBorderWidthProperty(direction);
default:
break;
}
el.style[borderProperty] = value;
}
/**
* Apply the custom border radius to an element.
* @param {HTMLElement} el - Apply border radius to this element.
* @param {String} firstRadius - The first radius value.
* @param {String} [secondRadius] - The second radius value.
* @param {String} [x] - The horizontal direction: either `right` or `left`.
* @param {String} [y] - The vertical direction: either `top` or `bottom`.
*/
function setBorderRadius(el, firstRadius, secondRadius, x = null, y = null) {
const direction = `${x}-${y}`;
const value = `${firstRadius}${secondRadius ? ` / ${secondRadius}` : ""}`;
let borderRadiusProperty;
switch (direction) {
case "left-top":
borderRadiusProperty = "borderTopLeftRadius";
break;
case "right-top":
borderRadiusProperty = "borderTopRightRadius";
break;
case "left-bottom":
borderRadiusProperty = "borderBottomLeftRadius";
break;
case "right-bottom":
borderRadiusProperty = "borderBottomRightRadius";
break;
default:
borderRadiusProperty = "borderRadius";
break;
}
el.style[borderRadiusProperty] = value;
}
/**
* Display the corresponding border settings.
* @param {String} string - Either `common` or `individual`.
*/
function toggleBorderSettingsDisplay(string) {
const allBordersFieldset = document.getElementById("fieldset-borders");
const topBorderFieldset = document.getElementById("fieldset-border-top");
const rightBorderFieldset = document.getElementById("fieldset-border-right");
const bottomBorderFieldset = document.getElementById(
"fieldset-border-bottom"
);
const leftBorderFieldset = document.getElementById("fieldset-border-left");
if (string === "common") {
allBordersFieldset.style.display = "";
topBorderFieldset.style.display = "none";
rightBorderFieldset.style.display = "none";
bottomBorderFieldset.style.display = "none";
leftBorderFieldset.style.display = "none";
} else {
allBordersFieldset.style.display = "none";
topBorderFieldset.style.display = "";
rightBorderFieldset.style.display = "";
bottomBorderFieldset.style.display = "";
leftBorderFieldset.style.display = "";
}
}
/**
* Display the corresponding border-radius settings.
* @param {String} string - Either `common` or `individual`.
*/
function toggleBorderRadiusSettingsDisplay(string) {
const allBordersRadiusFieldset = document.getElementById(
"fieldset-borders-radius"
);
const topLeftBorderRadiusFieldset = document.getElementById(
"fieldset-border-top-left-radius"
);
const topRightBorderRadiusFieldset = document.getElementById(
"fieldset-border-top-right-radius"
);
const bottomLeftBorderRadiusFieldset = document.getElementById(
"fieldset-border-bottom-left-radius"
);
const bottomRightBorderRadiusFieldset = document.getElementById(
"fieldset-border-bottom-right-radius"
);
if (string === "common") {
allBordersRadiusFieldset.style.display = "";
topLeftBorderRadiusFieldset.style.display = "none";
topRightBorderRadiusFieldset.style.display = "none";
bottomLeftBorderRadiusFieldset.style.display = "none";
bottomRightBorderRadiusFieldset.style.display = "none";
} else {
allBordersRadiusFieldset.style.display = "none";
topLeftBorderRadiusFieldset.style.display = "";
topRightBorderRadiusFieldset.style.display = "";
bottomLeftBorderRadiusFieldset.style.display = "";
bottomRightBorderRadiusFieldset.style.display = "";
}
}
/**
* Print the generated code into the given element.
* @param {HTMLElement} el - The element where to print generated code.
*/
function printCode(el) {
const code = document.querySelector(".result__code");
let codeOutput = `
.box {\n`;
for (const property of el.style) {
codeOutput += `\t${property}: ${el.style[property]};\n`;
}
codeOutput += "}";
code.textContent = codeOutput;
}
/**
* Check which type of settings is checked.
* @param {String} radioValue - The input radio value.
* @returns {Boolean} True if is individual; false if is common.
*/
function isIndividualSettings(radioValue) {
return radioValue === "true" ? true : false;
}
/**
* Set all borders to a given element.
* @param {HTMLElement} el - Apply border to this element.
*/
function setCommonBorder(el) {
const allBordersColorInput = document.getElementById("borders-color");
const allBordersStyleSelect = document.getElementById("borders-style");
const allBordersUnitSelect = document.getElementById("borders-unit");
const allBordersWidthInput = document.getElementById("borders-width");
setBorder(el, "color", allBordersColorInput.value);
setBorder(el, "style", allBordersStyleSelect.value);
setBorder(
el,
"width",
`${allBordersWidthInput.value}${allBordersUnitSelect.value}`
);
allBordersColorInput.addEventListener("input", () => {
setBorder(el, "color", allBordersColorInput.value);
printCode(el);
});
allBordersStyleSelect.addEventListener("input", () => {
setBorder(el, "style", allBordersStyleSelect.value);
printCode(el);
});
allBordersUnitSelect.addEventListener("input", () => {
setBorder(
el,
"width",
`${allBordersWidthInput.value}${allBordersUnitSelect.value}`
);
printCode(el);
});
allBordersWidthInput.addEventListener("input", () => {
setBorder(
el,
"width",
`${allBordersWidthInput.value}${allBordersUnitSelect.value}`
);
printCode(el);
});
}
/**
* Set the top border to the given element.
* @param {HTMLElement} el - Apply the top border to this element.
*/
function setTopBorder(el) {
const topBorderColorInput = document.getElementById("border-top-color");
const topBorderStyleSelect = document.getElementById("border-top-style");
const topBorderUnitSelect = document.getElementById("border-top-unit");
const topBorderWidthInput = document.getElementById("border-top-width");
setBorder(el, "color", topBorderColorInput.value, "top");
setBorder(el, "style", topBorderStyleSelect.value, "top");
setBorder(
el,
"width",
`${topBorderWidthInput.value}${topBorderUnitSelect.value}`,
"top"
);
topBorderColorInput.addEventListener("input", () => {
setBorder(el, "color", topBorderColorInput.value, "top");
printCode(el);
});
topBorderStyleSelect.addEventListener("input", () => {
setBorder(el, "style", topBorderStyleSelect.value, "top");
printCode(el);
});
topBorderUnitSelect.addEventListener("input", () => {
setBorder(
el,
"width",
`${topBorderWidthInput.value}${topBorderUnitSelect.value}`,
"top"
);
printCode(el);
});
topBorderWidthInput.addEventListener("input", () => {
setBorder(
el,
"width",
`${topBorderWidthInput.value}${topBorderUnitSelect.value}`,
"top"
);
printCode(el);
});
}
/**
* Set the right border to the given element.
* @param {HTMLElement} el - Apply the right border to this element.
*/
function setRightBorder(el) {
const rightBorderWidthInput = document.getElementById("border-right-width");
const rightBorderUnitSelect = document.getElementById("border-right-unit");
const rightBorderStyleSelect = document.getElementById("border-right-style");
const rightBorderColorInput = document.getElementById("border-right-color");
setBorder(el, "color", rightBorderColorInput.value, "right");
setBorder(el, "style", rightBorderStyleSelect.value, "right");
setBorder(
el,
"width",
`${rightBorderWidthInput.value}${rightBorderUnitSelect.value}`,
"right"
);
rightBorderColorInput.addEventListener("input", () => {
setBorder(el, "color", rightBorderColorInput.value, "right");
printCode(el);
});
rightBorderStyleSelect.addEventListener("input", () => {
setBorder(el, "style", rightBorderStyleSelect.value, "right");
printCode(el);
});
rightBorderUnitSelect.addEventListener("input", () => {
setBorder(
el,
"width",
`${rightBorderWidthInput.value}${rightBorderUnitSelect.value}`,
"right"
);
printCode(el);
});
rightBorderWidthInput.addEventListener("input", () => {
setBorder(
el,
"width",
`${rightBorderWidthInput.value}${rightBorderUnitSelect.value}`,
"right"
);
printCode(el);
});
}
/**
* Set the bottom border to the given element.
* @param {HTMLElement} el - Apply the bottom border to this element.
*/
function setBottomBorder(el) {
const bottomBorderWidthInput = document.getElementById("border-bottom-width");
const bottomBorderUnitSelect = document.getElementById("border-bottom-unit");
const bottomBorderStyleSelect = document.getElementById(
"border-bottom-style"
);
const bottomBorderColorInput = document.getElementById("border-bottom-color");
setBorder(el, "color", bottomBorderColorInput.value, "bottom");
setBorder(el, "style", bottomBorderStyleSelect.value, "bottom");
setBorder(
el,
"width",
`${bottomBorderWidthInput.value}${bottomBorderUnitSelect.value}`,
"bottom"
);
bottomBorderColorInput.addEventListener("input", () => {
setBorder(el, "color", bottomBorderColorInput.value, "bottom");
printCode(el);
});
bottomBorderStyleSelect.addEventListener("input", () => {
setBorder(el, "style", bottomBorderStyleSelect.value, "bottom");
printCode(el);
});
bottomBorderUnitSelect.addEventListener("input", () => {
setBorder(
el,
"width",
`${bottomBorderWidthInput.value}${bottomBorderUnitSelect.value}`,
"bottom"
);
printCode(el);
});
bottomBorderWidthInput.addEventListener("input", () => {
setBorder(
el,
"width",
`${bottomBorderWidthInput.value}${bottomBorderUnitSelect.value}`,
"bottom"
);
printCode(el);
});
}
/**
* Set the left border to the given element.
* @param {HTMLElement} el - Apply the left border to this element.
*/
function setLeftBorder(el) {
const leftBorderWidthInput = document.getElementById("border-left-width");
const leftBorderUnitSelect = document.getElementById("border-left-unit");
const leftBorderStyleSelect = document.getElementById("border-left-style");
const leftBorderColorInput = document.getElementById("border-left-color");
setBorder(el, "color", leftBorderColorInput.value, "left");
setBorder(el, "style", leftBorderStyleSelect.value, "left");
setBorder(
el,
"width",
`${leftBorderWidthInput.value}${leftBorderUnitSelect.value}`,
"left"
);
leftBorderColorInput.addEventListener("input", () => {
setBorder(el, "color", leftBorderColorInput.value, "left");
printCode(el);
});
leftBorderStyleSelect.addEventListener("input", () => {
setBorder(el, "style", leftBorderStyleSelect.value, "left");
printCode(el);
});
leftBorderUnitSelect.addEventListener("input", () => {
setBorder(
el,
"width",
`${leftBorderWidthInput.value}${leftBorderUnitSelect.value}`,
"left"
);
printCode(el);
});
leftBorderWidthInput.addEventListener("input", () => {
setBorder(
el,
"width",
`${leftBorderWidthInput.value}${leftBorderUnitSelect.value}`,
"left"
);
printCode(el);
});
}
/**
* Set all borders radius to the given element.
* @param {HTMLElement} el - Apply the border radius to this element.
*/
function setCommonBorderRadius(el) {
const borderCommonFirstRadius = document.getElementById(
"borders-first-radius"
);
const borderCommonFirstRadiusUnit = document.getElementById(
"borders-first-radius-unit"
);
const borderCommonSecondRadius = document.getElementById(
"borders-second-radius"
);
const borderCommonSecondRadiusUnit = document.getElementById(
"borders-second-radius-unit"
);
let firstRadius = `${borderCommonFirstRadius.value}${borderCommonFirstRadiusUnit.value}`;
let secondRadius = borderCommonSecondRadius.value
? `${borderCommonSecondRadius.value}${borderCommonSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius);
borderCommonFirstRadius.addEventListener("input", () => {
firstRadius = `${borderCommonFirstRadius.value}${borderCommonFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius);
printCode(el);
});
borderCommonFirstRadiusUnit.addEventListener("input", () => {
firstRadius = `${borderCommonFirstRadius.value}${borderCommonFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius);
printCode(el);
});
borderCommonSecondRadius.addEventListener("input", () => {
secondRadius = borderCommonSecondRadius.value
? `${borderCommonSecondRadius.value}${borderCommonSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius);
printCode(el);
});
borderCommonSecondRadiusUnit.addEventListener("input", () => {
secondRadius = borderCommonSecondRadius.value
? `${borderCommonSecondRadius.value}${borderCommonSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius);
printCode(el);
});
}
/**
* Set the top left border-radius to the given element.
* @param {HTMLElement} el - Apply the top left border-radius to this element.
*/
function setTopLeftBorderRadius(el) {
const borderTopLeftFirstRadius = document.getElementById(
"border-top-left-first-radius"
);
const borderTopLeftFirstRadiusUnit = document.getElementById(
"border-top-left-first-radius-unit"
);
const borderTopLeftSecondRadius = document.getElementById(
"border-top-left-second-radius"
);
const borderTopLeftSecondRadiusUnit = document.getElementById(
"border-top-left-second-radius-unit"
);
let firstRadius = `${borderTopLeftFirstRadius.value}${borderTopLeftFirstRadiusUnit.value}`;
let secondRadius = borderTopLeftSecondRadius.value
? `${borderTopLeftSecondRadius.value}${borderTopLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "top");
borderTopLeftFirstRadius.addEventListener("input", () => {
firstRadius = `${borderTopLeftFirstRadius.value}${borderTopLeftFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "left", "top");
printCode(el);
});
borderTopLeftFirstRadiusUnit.addEventListener("input", () => {
firstRadius = `${borderTopLeftFirstRadius.value}${borderTopLeftFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "left", "top");
printCode(el);
});
borderTopLeftSecondRadius.addEventListener("input", () => {
secondRadius = borderTopLeftSecondRadius.value
? `${borderTopLeftSecondRadius.value}${borderTopLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "top");
printCode(el);
});
borderTopLeftSecondRadiusUnit.addEventListener("input", () => {
secondRadius = borderTopLeftSecondRadius.value
? `${borderTopLeftSecondRadius.value}${borderTopLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "top");
printCode(el);
});
}
/**
* Set the top right border-radius to the given element.
* @param {HTMLElement} el - Apply the top right border-radius to this element.
*/
function setTopRightBorderRadius(el) {
const borderTopRightFirstRadius = document.getElementById(
"border-top-right-first-radius"
);
const borderTopRightFirstRadiusUnit = document.getElementById(
"border-top-right-first-radius-unit"
);
const borderTopRightSecondRadius = document.getElementById(
"border-top-right-second-radius"
);
const borderTopRightSecondRadiusUnit = document.getElementById(
"border-top-right-second-radius-unit"
);
const firstRadius = `${borderTopRightFirstRadius.value}${borderTopRightFirstRadiusUnit.value}`;
const secondRadius = borderTopRightSecondRadius.value
? `${borderTopRightSecondRadius.value}${borderTopRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "top");
borderTopRightFirstRadius.addEventListener("input", () => {
firstRadius = `${borderTopRightFirstRadius.value}${borderTopRightFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "right", "top");
printCode(el);
});
borderTopRightFirstRadiusUnit.addEventListener("input", () => {
firstRadius = `${borderTopRightFirstRadius.value}${borderTopRightFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "right", "top");
printCode(el);
});
borderTopRightSecondRadius.addEventListener("input", () => {
secondRadius = borderTopRightSecondRadius.value
? `${borderTopRightSecondRadius.value}${borderTopRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "top");
printCode(el);
});
borderTopRightSecondRadiusUnit.addEventListener("input", () => {
secondRadius = borderTopRightSecondRadius.value
? `${borderTopRightSecondRadius.value}${borderTopRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "top");
printCode(el);
});
}
/**
* Set the bottom left border-radius to the given element.
* @param {HTMLElement} el - Apply bottom left border-radius to this element.
*/
function setBottomLeftBorderRadius(el) {
const borderBottomLeftFirstRadius = document.getElementById(
"border-bottom-left-first-radius"
);
const borderBottomLeftFirstRadiusUnit = document.getElementById(
"border-bottom-left-first-radius-unit"
);
const borderBottomLeftSecondRadius = document.getElementById(
"border-bottom-left-second-radius"
);
const borderBottomLeftSecondRadiusUnit = document.getElementById(
"border-bottom-left-second-radius-unit"
);
const firstRadius = `${borderBottomLeftFirstRadius.value}${borderBottomLeftFirstRadiusUnit.value}`;
const secondRadius = borderBottomLeftSecondRadius.value
? `${borderBottomLeftSecondRadius.value}${borderBottomLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "bottom");
borderBottomLeftFirstRadius.addEventListener("input", () => {
firstRadius = `${borderBottomLeftFirstRadius.value}${borderBottomLeftFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "left", "bottom");
printCode(el);
});
borderBottomLeftFirstRadiusUnit.addEventListener("input", () => {
firstRadius = `${borderBottomLeftFirstRadius.value}${borderBottomLeftFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "left", "bottom");
printCode(el);
});
borderBottomLeftSecondRadius.addEventListener("input", () => {
secondRadius = borderBottomLeftSecondRadius.value
? `${borderBottomLeftSecondRadius.value}${borderBottomLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "bottom");
printCode(el);
});
borderBottomLeftSecondRadiusUnit.addEventListener("input", () => {
secondRadius = borderBottomLeftSecondRadius.value
? `${borderBottomLeftSecondRadius.value}${borderBottomLeftSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "left", "bottom");
printCode(el);
});
}
/**
* Set the bottom right border-radius to the given element.
* @param {HTMLElement} el - Apply bottom right border-radius to this element.
*/
function setBottomRightBorderRadius(el) {
const borderBottomRightFirstRadius = document.getElementById(
"border-bottom-right-first-radius"
);
const borderBottomRightFirstRadiusUnit = document.getElementById(
"border-bottom-right-first-radius-unit"
);
const borderBottomRightSecondRadius = document.getElementById(
"border-bottom-right-second-radius"
);
const borderBottomRightSecondRadiusUnit = document.getElementById(
"border-bottom-right-second-radius-unit"
);
const firstRadius = `${borderBottomRightFirstRadius.value}${borderBottomRightFirstRadiusUnit.value}`;
const secondRadius = borderBottomRightSecondRadius.value
? `${borderBottomRightSecondRadius.value}${borderBottomRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "bottom");
borderBottomRightFirstRadius.addEventListener("input", () => {
firstRadius = `${borderBottomRightFirstRadius.value}${borderBottomRightFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "right", "bottom");
printCode(el);
});
borderBottomRightFirstRadiusUnit.addEventListener("input", () => {
firstRadius = `${borderBottomRightFirstRadius.value}${borderBottomRightFirstRadiusUnit.value}`;
setBorderRadius(el, firstRadius, secondRadius, "right", "bottom");
printCode(el);
});
borderBottomRightSecondRadius.addEventListener("input", () => {
secondRadius = borderBottomRightSecondRadius.value
? `${borderBottomRightSecondRadius.value}${borderBottomRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "bottom");
printCode(el);
});
borderBottomRightSecondRadiusUnit.addEventListener("input", () => {
secondRadius = borderBottomRightSecondRadius.value
? `${borderBottomRightSecondRadius.value}${borderBottomRightSecondRadiusUnit.value}`
: null;
setBorderRadius(el, firstRadius, secondRadius, "right", "bottom");
printCode(el);
});
}
/**
* Display a message inside the given element.
* @param {HTMLElement} el - The element where to print the message.
* @param {String} msg - The message to display.
* @param {Number} [duration] - The message duration.
*/
function printMessage(el, msg, duration = 1000) {
const backupContent = el.textContent;
el.textContent = msg;
setTimeout(() => (el.textContent = backupContent), duration);
}
/**
* Copy code to the clipboard.
*/
function copyCode() {
const code = document.querySelector(".result__code");
navigator.clipboard.writeText(code.textContent);
}
/**
* Listen the button copy to clipboard.
*/
function listenCopyCodeBtn() {
const btn = document.getElementById("copy-code");
btn.addEventListener("click", () => {
copyCode();
printMessage(btn, "Copied to clipboard!");
});
}
/**
* Initialize borders settings and borders.
* @param {String} radioValue - The input radio value.
* @param {HTMLElement} el - The element where to apply borders.
*/
function initBorders(radioValue, el) {
if (isIndividualSettings(radioValue)) {
toggleBorderSettingsDisplay("individual");
setTopBorder(el);
setRightBorder(el);
setBottomBorder(el);
setLeftBorder(el);
} else {
toggleBorderSettingsDisplay("common");
setCommonBorder(el);
}
}
/**
* Initialize border-radius settings and border-radius.
* @param {String} radioValue - The input radio value.
* @param {HTMLElement} el - The element where to apply border-radius.
*/
function initBordersRadius(radioValue, el) {
if (isIndividualSettings(radioValue)) {
toggleBorderRadiusSettingsDisplay("individual");
setTopLeftBorderRadius(el);
setTopRightBorderRadius(el);
setBottomLeftBorderRadius(el);
setBottomRightBorderRadius(el);
} else {
toggleBorderRadiusSettingsDisplay("common");
setCommonBorderRadius(el);
}
}
/**
* Initialize the app.
*/
function init() {
const box = document.querySelector(".box");
const borderPropertyRadio = document.querySelectorAll(
'input[name="border-property"]'
);
const borderRadiusPropertyRadio = document.querySelectorAll(
'input[name="border-radius-property"]'
);
for (const radio of borderPropertyRadio) {
if (radio.checked) initBorders(radio.value, box);
radio.addEventListener("change", () => initBorders(radio.value, box));
}
for (const radio of borderRadiusPropertyRadio) {
if (radio.checked) initBordersRadius(radio.value, box);
radio.addEventListener("change", () => initBordersRadius(radio.value, box));
}
printCode(box);
listenCopyCodeBtn();
}
init();
|