-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
1258 lines (1158 loc) · 39.7 KB
/
app.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
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// function Book(
// title = 'Unknown',
// author = 'Unknown',
// totalPages = 0,
// completedPages = 0,
// completed = false
// ) {
// this.id = ++uniqueId;
// this.title = title;
// this.author = author;
// this.totalPages = totalPages;
// this.completedPages = completedPages;
// this.completed = completed;
// this.container = null;
// }
class Book {
constructor(
title = 'Unknown',
author = 'Unknown',
totalPages = 0,
completedPages = 0,
completed = false
) {
this.id = ++uniqueId;
this.title = title;
this.author = author;
this.totalPages = totalPages;
this.completedPages = completedPages;
this.completed = completed;
this.container = null;
}
}
function BookContainer(book) {
this.element = document.createElement('div');
this.element.classList.add('book-container');
this.element.dataset.id = book.id;
// create options element
this.options = document.createElement('div');
this.options.classList.add('options');
// create and add button element
this.buttonEdit = document.createElement('button');
this.buttonEdit.classList.add('button');
this.buttonEdit.classList.add('green-bg');
this.buttonEdit.textContent = 'Edit';
this.options.appendChild(this.buttonEdit);
// create and add button element
this.buttonRemove = document.createElement('button');
this.buttonRemove.classList.add('button');
this.buttonRemove.classList.add('red-bg');
this.buttonRemove.textContent = 'Remove';
this.options.appendChild(this.buttonRemove);
// add options element
this.element.appendChild(this.options);
// create content element ! undefined
this.content = document.createElement('div');
this.content.classList.add('content');
// create title h2 element
this.title = document.createElement('h2');
this.title.classList.add('title');
this.title.textContent = book.title;
this.content.appendChild(this.title);
// create author h3 element
this.author = document.createElement('h3');
this.author.classList.add('author');
this.author.textContent = book.author;
this.content.appendChild(this.author);
// add content element
this.element.appendChild(this.content);
// create pages-controller element
this.pagesController = document.createElement('div');
this.pagesController.classList.add('pages-controller');
// create minus button element
this.buttonPagesSubtract = document.createElement('button');
this.buttonPagesSubtract.classList.add('button');
this.buttonPagesSubtract.classList.add('minus-gb');
this.buttonPagesSubtract.textContent = '-';
this.pagesController.appendChild(this.buttonPagesSubtract);
// create complete button element
this.buttonPagesComplete = document.createElement('button');
this.buttonPagesComplete.classList.add('button');
this.buttonPagesComplete.classList.add('check-gb');
this.buttonPagesComplete.textContent = '✓';
this.pagesController.appendChild(this.buttonPagesComplete);
// create add button element
this.buttonPagesAdd = document.createElement('button');
this.buttonPagesAdd.classList.add('button');
this.buttonPagesAdd.classList.add('plus-gb');
this.buttonPagesAdd.textContent = '+';
this.pagesController.appendChild(this.buttonPagesAdd);
// add pages-controller element
this.element.appendChild(this.pagesController);
// create pages-count element
this.pagesCount = document.createElement('div');
this.pagesCount.classList.add('pages-count');
// create pages completed h2 element
this.completedPages = document.createElement('h2');
this.completedPages.classList.add('completed');
this.completedPages.textContent = book.completedPages;
this.pagesCount.appendChild(this.completedPages);
// create separator span element
this.separator = document.createElement('span');
this.separator.classList.add('separator');
this.separator.textContent = '|';
this.pagesCount.appendChild(this.separator);
// create pages total h2 element
this.totalPages = document.createElement('h2');
this.totalPages.classList.add('total');
this.totalPages.textContent = book.totalPages;
this.pagesCount.appendChild(this.totalPages);
// add pages-count element
this.element.appendChild(this.pagesCount);
// add container to book
book.container = this;
this.bookRemove = function() {
// remove book and container
let index = myLibrary.findIndex((b) => b.id === book.id);
myLibrary.splice(index, 1);
libraryContainer.removeChild(this.element);
saveAll();
};
// handlers
// call confirmation modal
const queryConfirmationHandler = (e) => {
confirmationSelectionStorageModal.confirm(this);
};
const bookEditHandler = (e) => {
editModal.clearAllInputs();
editModal.on();
editModal.editBook(book.id);
};
const bookIncCompletedPagesHandler = (e) => {
if (book.completedPages < book.totalPages) {
book.completedPages++;
if (book.completedPages === book.totalPages) {
book.completed = true;
}
toggleBookBgColor(book);
this.updateContainerFor(book);
saveAll();
}
};
const bookDecCompletedPagesHandler = (e) => {
if (book.completedPages > 0) {
book.completedPages--;
book.completed = false;
toggleBookBgColor(book);
this.updateContainerFor(book);
saveAll();
}
};
const bookCompleteHandler = (e) => {
book.completed = true;
toggleBookBgColor(book);
book.completedPages = book.totalPages;
this.updateContainerFor(book);
saveAll();
};
// add events
this.buttonPagesComplete.addEventListener('click', bookCompleteHandler);
this.buttonPagesSubtract.addEventListener(
'click',
bookDecCompletedPagesHandler
);
this.buttonPagesAdd.addEventListener('click', bookIncCompletedPagesHandler);
this.buttonRemove.addEventListener('click', queryConfirmationHandler);
this.buttonEdit.addEventListener('click', bookEditHandler);
}
BookContainer.prototype.updateContainerFor = function(book) {
if (book.container) {
book.container.title.textContent = book.title;
book.container.author.textContent = book.author;
book.container.totalPages.textContent = book.totalPages;
book.container.completedPages.textContent = book.completedPages;
// book.container something = book.completed;
}
};
function ModalBookFactory(modalType) {
if (modalType === 'add' || modalType === 'edit') {
// create wrapper element
this.wrapper = document.createElement('div');
this.wrapper.classList.add(`modal-${modalType}-wrapper`);
this.wrapper.classList.add('display-none');
// create modal-add element
this.element = document.createElement('div');
this.element.classList.add(`modal-${modalType}`);
// create modal-title element
this.modalTitle = document.createElement('div');
this.modalTitle.classList.add('modal-title');
// create title h2 element
this.title = document.createElement('h2');
if (modalType === 'add') {
this.title.textContent = 'Add New Book';
} else {
this.title.textContent = 'Edit Book';
}
// add title element
this.modalTitle.appendChild(this.title);
// add modal-title element
this.element.appendChild(this.modalTitle);
// create content element
this.content = document.createElement('div');
this.content.classList.add('content');
// create book title element
this.bookTitleElement = document.createElement('div');
this.bookTitleElement.classList.add('element');
// create input book-title
this.bookTitleInput = document.createElement('input');
this.bookTitleInput.classList.add('input');
this.bookTitleInput.setAttribute('type', 'text');
this.bookTitleInput.setAttribute('id', `book-${modalType}-title`);
this.bookTitleInput.setAttribute('required', '');
this.bookTitleInput.validated = false;
// add input
this.bookTitleElement.appendChild(this.bookTitleInput);
// create label
this.bookTitleLabel = document.createElement('label');
this.bookTitleLabel.classList.add('title');
this.bookTitleLabel.setAttribute('for', `book-${modalType}-title`);
this.bookTitleLabel.textContent = 'Title';
// add label
this.bookTitleElement.appendChild(this.bookTitleLabel);
// add book title element
this.content.appendChild(this.bookTitleElement);
// create book author element
this.bookAuthorElement = document.createElement('div');
this.bookAuthorElement.classList.add('element');
// create input book-author
this.bookAuthorInput = document.createElement('input');
this.bookAuthorInput.classList.add('input');
this.bookAuthorInput.setAttribute('type', 'text');
this.bookAuthorInput.setAttribute('id', `book-${modalType}-author`);
this.bookAuthorInput.setAttribute('required', '');
this.bookAuthorInput.validated = false;
// add input
this.bookAuthorElement.appendChild(this.bookAuthorInput);
// create label
this.bookAuthorLabel = document.createElement('label');
this.bookAuthorLabel.classList.add('title');
this.bookAuthorLabel.setAttribute('for', `book-${modalType}-author`);
this.bookAuthorLabel.textContent = 'Author';
// add label
this.bookAuthorElement.appendChild(this.bookAuthorLabel);
// add book author element
this.content.appendChild(this.bookAuthorElement);
// create book total pages element
this.bookTotalPagesElement = document.createElement('div');
this.bookTotalPagesElement.classList.add('element');
// create input book-total-pages
this.bookTotalPagesInput = document.createElement('input');
this.bookTotalPagesInput.classList.add('input');
this.bookTotalPagesInput.classList.add('number');
this.bookTotalPagesInput.setAttribute('type', 'text');
this.bookTotalPagesInput.setAttribute(
'id',
`book-${modalType}-total-pages`
);
this.bookTotalPagesInput.setAttribute('required', '');
this.bookTotalPagesInput.validated = false;
// add input
this.bookTotalPagesElement.appendChild(this.bookTotalPagesInput);
// create label
this.bookTotalPagesLabel = document.createElement('label');
this.bookTotalPagesLabel.classList.add('title');
this.bookTotalPagesLabel.setAttribute(
'for',
`book-${modalType}-total-pages`
);
this.bookTotalPagesLabel.textContent = 'Total pages';
// add label
this.bookTotalPagesElement.appendChild(this.bookTotalPagesLabel);
// add book author element
this.content.appendChild(this.bookTotalPagesElement);
// create book completed pages element
this.bookCompletedPagesElement = document.createElement('div');
this.bookCompletedPagesElement.classList.add('element');
// create check pages element
this.bookCompletedPagesCheckElement = document.createElement('div');
this.bookCompletedPagesCheckElement.classList.add('completed-check');
// create input book-completed-pages
this.bookCompletedPagesInput = document.createElement('input');
this.bookCompletedPagesInput.classList.add('input');
this.bookCompletedPagesInput.classList.add('number');
this.bookCompletedPagesInput.setAttribute('type', 'text');
this.bookCompletedPagesInput.setAttribute(
'id',
`book-${modalType}-completed-pages`
);
this.bookCompletedPagesInput.setAttribute('required', '');
this.bookCompletedPagesInput.validated = false;
// add input
this.bookCompletedPagesCheckElement.appendChild(
this.bookCompletedPagesInput
);
// create label
this.bookCompletedPagesLabel = document.createElement('label');
this.bookCompletedPagesLabel.classList.add('title');
this.bookCompletedPagesLabel.setAttribute(
'for',
`book-${modalType}-completed-pages`
);
this.bookCompletedPagesLabel.textContent = 'Completed pages';
// add label
this.bookCompletedPagesCheckElement.appendChild(
this.bookCompletedPagesLabel
);
// create check input
this.bookCompleted = document.createElement('input');
this.bookCompleted.classList.add('input');
this.bookCompleted.classList.add('check');
this.bookCompleted.setAttribute('type', 'checkbox');
// add check input
this.bookCompletedPagesCheckElement.appendChild(this.bookCompleted);
// add completed pages check element
this.bookCompletedPagesElement.appendChild(
this.bookCompletedPagesCheckElement
);
// add book author element
this.content.appendChild(this.bookCompletedPagesElement);
// add content element
this.element.appendChild(this.content);
// create options element
this.options = document.createElement('div');
this.options.classList.add('options');
// create cancel button
this.buttonCancel = document.createElement('button');
this.buttonCancel.classList.add('button');
this.buttonCancel.classList.add('red-bg');
this.buttonCancel.textContent = 'Cancel';
// add cancel button
this.options.appendChild(this.buttonCancel);
// create add button
if (modalType === 'add') {
this.buttonAdd = document.createElement('button');
this.buttonAdd.classList.add('button');
this.buttonAdd.classList.add('green-bg');
this.buttonAdd.textContent = 'Add';
// add button
this.options.appendChild(this.buttonAdd);
} else {
// create edit button
this.buttonEdit = document.createElement('button');
this.buttonEdit.classList.add('button');
this.buttonEdit.classList.add('green-bg');
this.buttonEdit.textContent = 'Edit';
// add button
this.options.appendChild(this.buttonEdit);
}
// add options element
this.element.appendChild(this.options);
// add element
this.wrapper.appendChild(this.element);
// events and functions
// disable modal
this.buttonCancel.addEventListener('click', () => this.off());
// add new book
if (modalType == 'add') {
this.buttonAdd.addEventListener('click', () => {
if (this.validate()) {
let newBook = new Book(
this.bookTitleInput.value,
this.bookAuthorInput.value,
+this.bookTotalPagesInput.value,
+this.bookCompletedPagesInput.value,
this.bookCompleted.checked
);
let newBookWrapper = new BookContainer(newBook);
// add new book to myLibrary
myLibrary.push(newBook);
libraryContainer.insertBefore(
newBookWrapper.element,
libraryContainer.lastElementChild
);
toggleBookBgColor(newBook);
saveAll();
this.clearAllInputs();
this.off();
}
});
} else {
// make new changes to book
this.buttonEdit.addEventListener('click', () => {
if (this.validate()) {
let book = myLibrary.find((book) => book.id == this.currentBookId);
book.title = this.bookTitleInput.value;
book.author = this.bookAuthorInput.value;
book.totalPages = +this.bookTotalPagesInput.value;
book.completedPages = +this.bookCompletedPagesInput.value;
book.completed = this.bookCompleted.checked;
toggleBookBgColor(book);
this.off();
book.container.updateContainerFor(book);
saveAll();
}
});
}
// renderBook
this.renderBook = function(book) {
let newBookWrapper = new BookContainer(book);
libraryContainer.insertBefore(
newBookWrapper.element,
libraryContainer.lastElementChild
);
};
// edit book
if (modalType === 'edit') {
this.editBook = function(bookId) {
// change current id
this.currentBookId = bookId;
let book = myLibrary.find((book) => book.id == bookId);
this.bookTitleInput.value = book.title;
this.bookAuthorInput.value = book.author;
this.bookTotalPagesInput.value = book.totalPages;
this.bookCompletedPagesInput.value = book.completedPages;
this.bookCompleted.checked = book.completed;
this.changeAllToValid();
if (book.totalPages !== book.completedPages) {
this.bookCompleted.checked = false;
}
};
}
// validation
// validation on title input
this.bookTitleInput.addEventListener('input', (e) => {
if (
!!this.bookTitleInput.value &&
this.bookTitleInput.value.length <= 50
) {
this.changeToValid(this.bookTitleInput, this.bookTitleLabel, 'Title');
} else {
this.changeToNotValid(
this.bookTitleInput,
this.bookTitleLabel,
'Title cannot be empty and length must be less than 50'
);
}
});
// validation on author input
this.bookAuthorInput.addEventListener('input', (e) => {
if (
!!this.bookAuthorInput.value &&
this.bookAuthorInput.value.length <= 30
) {
this.changeToValid(
this.bookAuthorInput,
this.bookAuthorLabel,
'Author'
);
} else {
this.changeToNotValid(
this.bookAuthorInput,
this.bookAuthorLabel,
'Author cannot be empty and length must be less than 30'
);
}
});
// handler for completed pages validation
const validCompletedPagesHandler = (e) => {
if (
this.bookTotalPagesInput.validated &&
!!this.bookCompletedPagesInput.value &&
/^\d+$/g.test(this.bookCompletedPagesInput.value) &&
+this.bookCompletedPagesInput.value >= 0 &&
+this.bookCompletedPagesInput.value <= +this.bookTotalPagesInput.value
) {
this.changeToValid(
this.bookCompletedPagesInput,
this.bookCompletedPagesLabel,
'Completed pages'
);
if (
+this.bookTotalPagesInput.value ===
+this.bookCompletedPagesInput.value
) {
this.bookCompleted.checked = true;
} else {
this.bookCompleted.checked = false;
}
} else {
this.changeToNotValid(
this.bookCompletedPagesInput,
this.bookCompletedPagesLabel,
'Completed pages number should be less than total pages number'
);
this.bookCompleted.checked = false;
}
};
// handler for checkbox completed pages
const validCompletedCheckBoxHandler = (e) => {
if (this.bookTotalPagesInput.validated && this.bookCompleted.checked) {
this.bookCompletedPagesInput.value = this.bookTotalPagesInput.value;
validCompletedPagesHandler();
}
if (
+this.bookTotalPagesInput.value === +this.bookCompletedPagesInput.value
) {
this.bookCompleted.checked = true;
}
if (!this.bookTotalPagesInput.validated) {
this.bookCompleted.checked = false;
}
};
// validation total pages
this.bookTotalPagesInput.addEventListener('input', (e) => {
if (
!!this.bookTotalPagesInput.value &&
/^\d+$/g.test(this.bookTotalPagesInput.value) &&
+this.bookTotalPagesInput.value > 0 &&
+this.bookTotalPagesInput.value <= 9999999
) {
this.changeToValid(
this.bookTotalPagesInput,
this.bookTotalPagesLabel,
'Total pages'
);
validCompletedPagesHandler();
} else {
this.changeToNotValid(
this.bookTotalPagesInput,
this.bookTotalPagesLabel,
'Total pages number should be more than 0 and less than 9999999'
);
}
});
// validation completed pages
this.bookCompletedPagesInput.addEventListener(
'input',
validCompletedPagesHandler
);
// validation completed check
this.bookCompleted.addEventListener('click', validCompletedCheckBoxHandler);
} // this is end of if btw
}
// create prototype functions for modals
ModalBookFactory.prototype.on = function() {
if (this.wrapper) this.wrapper.classList.remove('display-none');
};
ModalBookFactory.prototype.off = function() {
if (this.wrapper) this.wrapper.classList.add('display-none');
};
ModalBookFactory.prototype.insertInBody = function() {
if (this.wrapper) document.body.appendChild(this.wrapper);
};
ModalBookFactory.prototype.clearAllInputs = function() {
if (this.wrapper) {
this.clearInput(this.bookTitleInput, this.bookTitleLabel, 'Title');
this.clearInput(this.bookAuthorInput, this.bookAuthorLabel, 'Author');
this.clearInput(
this.bookTotalPagesInput,
this.bookTotalPagesLabel,
'Total pages'
);
this.clearInput(
this.bookCompletedPagesInput,
this.bookCompletedPagesLabel,
'Completed pages'
);
this.bookCompleted.checked = false;
}
};
ModalBookFactory.prototype.clearInput = function(input, label, defaultValue) {
input.value = '';
input.classList.remove('input-validated');
input.classList.remove('input-not-validated');
label.classList.remove('validation-message');
label.textContent = defaultValue;
input.validated = false;
};
ModalBookFactory.prototype.changeToValid = function(input, label, message) {
input.classList.remove('input-not-validated');
input.classList.add('input-validated');
label.classList.remove('validation-message');
label.textContent = message;
input.validated = true;
};
ModalBookFactory.prototype.changeToNotValid = function(input, label, message) {
input.classList.add('input-not-validated');
input.classList.remove('input-validated');
label.classList.add('validation-message');
label.textContent = message;
input.validated = false;
};
ModalBookFactory.prototype.validate = function() {
if (this.wrapper) {
// check for empty
if (!this.bookTitleInput.validated) {
this.changeToNotValid(
this.bookTitleInput,
this.bookTitleLabel,
'Title cannot be empty and length must be less than 50'
);
}
if (!this.bookAuthorInput.validated) {
this.changeToNotValid(
this.bookAuthorInput,
this.bookAuthorLabel,
'Author cannot be empty and length must be less than 30'
);
}
if (!this.bookTotalPagesInput.validated) {
this.changeToNotValid(
this.bookTotalPagesInput,
this.bookTotalPagesLabel,
'Total pages number should be more than 0 and less than 9999999'
);
}
if (!this.bookCompletedPagesInput.validated) {
this.changeToNotValid(
this.bookCompletedPagesInput,
this.bookCompletedPagesLabel,
'Completed pages number should be less than total pages number'
);
}
if (
this.bookTitleInput.validated &&
this.bookAuthorInput.validated &&
this.bookTotalPagesInput.validated &&
this.bookCompletedPagesInput.validated
) {
return true;
}
}
};
ModalBookFactory.prototype.changeAllToValid = function() {
this.changeToValid(this.bookTitleInput, this.bookTitleLabel, 'Title');
this.changeToValid(this.bookAuthorInput, this.bookAuthorLabel, 'Author');
this.changeToValid(
this.bookCompletedPagesInput,
this.bookCompletedPagesLabel,
'Completed pages'
);
this.changeToValid(
this.bookTotalPagesInput,
this.bookTotalPagesLabel,
'Total pages'
);
};
// modal query factory
function ModalQueryFactory(modalType) {
if (
modalType === 'storage-selection' ||
modalType === 'confirmation-selection-storage' ||
modalType === 'confirmation-delete-library'
) {
// create wrapper element
this.wrapper = document.createElement('div');
this.wrapper.classList.add(`modal-${modalType}-wrapper`);
this.wrapper.classList.add('display-none');
// create element
this.element = document.createElement('div');
this.element.classList.add(`modal-${modalType}`);
// create title element
this.modalTitle = document.createElement('div');
this.modalTitle.classList.add('modal-title');
// create title h2 element
this.title = document.createElement('h2');
if (modalType === 'storage-selection') {
this.title.textContent = 'Where to Save Your Data ?';
} else if (modalType === 'confirmation-selection-storage') {
this.title.textContent = 'Do you Want to Remove this Book ?';
} else if (modalType === 'confirmation-delete-library') {
this.title.textContent = 'Do you Want to Remove this Library ?';
}
// add title element
this.modalTitle.appendChild(this.title);
// add modal-title element
this.element.appendChild(this.modalTitle);
// create content element
this.content = document.createElement('div');
this.content.classList.add('content');
// create element for content
if (modalType === 'storage-selection') {
// create storage cloud element
this.storageCloudElement = document.createElement('div');
this.storageCloudElement.classList.add('element');
// create storage cloud wrapper
this.storageCloudWrapper = document.createElement('div');
this.storageCloudWrapper.classList.add('selection');
this.storageCloudWrapper.classList.add('cloud');
// create button cloud
this.storageCloudButton = document.createElement('button');
this.storageCloudButton.textContent = 'Cloud';
// add button cloud element
this.storageCloudWrapper.appendChild(this.storageCloudButton);
// add wrapper element
this.storageCloudElement.appendChild(this.storageCloudWrapper);
// create storage local element
this.storageLocalElement = document.createElement('div');
this.storageLocalElement.classList.add('element');
// create storage local wrapper
this.storageLocalWrapper = document.createElement('div');
this.storageLocalWrapper.classList.add('selection');
this.storageLocalWrapper.classList.add('local');
// create button local
this.storageLocalButton = document.createElement('button');
this.storageLocalButton.textContent = 'Local';
// add button local element
this.storageLocalWrapper.appendChild(this.storageLocalButton);
// add wrapper element
this.storageLocalElement.appendChild(this.storageLocalWrapper);
this.content.appendChild(this.storageCloudElement);
this.content.appendChild(this.storageLocalElement);
} else if (
modalType === 'confirmation-selection-storage' ||
modalType === 'confirmation-delete-library'
) {
// create no selection element
this.noSelectionElement = document.createElement('div');
this.noSelectionElement.classList.add('element');
// create no wrapper
this.noSelectionWrapper = document.createElement('div');
this.noSelectionWrapper.classList.add('selection');
// create button no
this.noSelectionButton = document.createElement('button');
this.noSelectionButton.textContent = 'No';
// add button no element
this.noSelectionWrapper.appendChild(this.noSelectionButton);
// add wrapper element
this.noSelectionElement.appendChild(this.noSelectionWrapper);
// create yes selection element
this.yesSelectionElement = document.createElement('div');
this.yesSelectionElement.classList.add('element');
// create yes wrapper
this.yesSelectionWrapper = document.createElement('div');
this.yesSelectionWrapper.classList.add('selection');
// create button yes
this.yesSelectionButton = document.createElement('button');
this.yesSelectionButton.textContent = 'Yes';
// add button yes element
this.yesSelectionWrapper.appendChild(this.yesSelectionButton);
// add wrapper element
this.yesSelectionElement.appendChild(this.yesSelectionWrapper);
this.content.appendChild(this.noSelectionElement);
this.content.appendChild(this.yesSelectionElement);
}
// add content element
this.element.appendChild(this.content);
// add element
this.wrapper.appendChild(this.element);
// events
if (modalType === 'confirmation-delete-library') {
// this.yesButtonHandler = null;
this.noButtonHandler = () => this.off();
this.noSelectionButton.addEventListener('click', this.noButtonHandler);
}
if (modalType === 'confirmation-selection-storage') {
// handlers for confirmation
const yesButtonHandler = () => {
if (this.bookContainerToRemove) {
this.bookContainerToRemove.bookRemove();
this.bookContainerToRemove = null;
}
this.off();
};
const noButtonHandler = () => {
this.bookContainerToRemove = null;
this.off();
};
this.yesSelectionButton.addEventListener('click', yesButtonHandler);
this.noSelectionButton.addEventListener('click', noButtonHandler);
// waiting for confirmation
this.confirm = (bookContainer) => {
this.bookContainerToRemove = bookContainer;
this.on();
};
}
if (modalType === 'storage-selection') {
// storage handlers
const localButtonHandler = () => {
isLocal = true;
initAuthState();
this.off();
};
const cloudButtonHandler = () => {
isLocal = false;
initAuthState();
this.off();
};
// events
this.storageLocalButton.addEventListener('click', localButtonHandler);
this.storageCloudButton.addEventListener('click', cloudButtonHandler);
}
}
}
ModalQueryFactory.prototype.on = ModalBookFactory.prototype.on;
ModalQueryFactory.prototype.off = ModalBookFactory.prototype.off;
ModalQueryFactory.prototype.insertInBody =
ModalBookFactory.prototype.insertInBody;
const initAuthState = function() {
const uiWrapper = document.querySelector('.modal-ui-wrapper');
const libContainer = document.querySelector('.library-container');
function displayUserInformation(user) {
document.querySelector('.user .user-name').textContent = user.displayName;
const userImg = document.querySelector('.user .user-photo');
userImg.setAttribute('src', user.photoURL);
userImg.classList.remove('display-none');
}
function disableUserPanelButtons() {
libraryPanel.querySelector('.data .switch-btn').style.display = 'none';
libraryPanel.querySelector('.data .delete-btn-cloud').style.display =
'none';
libraryPanel.querySelector('.element.rewrite').style.display = 'none';
}
function enableUserPanelButtons(isLocal) {
const btn = libraryPanel.querySelector('.data .switch-btn');
if (isLocal) {
btn.textContent = 'Switch to Cloud';
} else {
btn.textContent = 'Switch to Local';
}
btn.style.display = '';
libraryPanel.querySelector('.data .delete-btn-cloud').style.display = '';
libraryPanel.querySelector('.element.rewrite').style.display = '';
}
firebase.auth().onAuthStateChanged(function(user) {
// working on local
if (user && isLocal) {
changeButtonToSingOut();
displayUserInformation(user);
database = firebase.database();
loadAll();
// TODO add btns in panel
// TODO make loading modal
libContainer.classList.remove('display-none');
enableUserPanelButtons(true);
}
if (!user && isLocal) {
changeButtonToSingIn();
loadAll();
// TODO remove btns from panel
// TODO make loading modal
libContainer.classList.remove('display-none');
disableUserPanelButtons();
}
// working on cloud
if (user && !isLocal) {
changeButtonToSingOut();
displayUserInformation(user);
database = firebase.database();
loadAll();
libContainer.classList.remove('display-none');
enableUserPanelButtons();
}
if (!user && !isLocal) {
uiWrapper.style.display = '';
ui.start('#firebaseui-auth-container', uiConfig);
}
});
};
const singInHandler = function() {
const uiWrapper = document.querySelector('.modal-ui-wrapper');
uiWrapper.style.display = '';
uiConfig.callbacks.signInSuccessWithAuthResult = function(
authResult,
redUrl
) {
isLocal = false;
uiWrapper.style.display = 'none';
return false;
};
ui.start('#firebaseui-auth-container', uiConfig);
};
const singOutHandler = function() {
clearLibrary(myLibrary);
// TODO make loading modal
isLocal = true;
document.querySelector('.user .user-photo').classList.add('display-none');
document.querySelector('.user .user-name').textContent = '';
firebase.auth().signOut();
};
const changeButtonToSingIn = function() {
// change state of button
singInButton.textContent = 'Sign in';
singInButton.removeEventListener('click', singOutHandler);
singInButton.addEventListener('click', singInHandler);
};
const changeButtonToSingOut = function() {
// change state of button
singInButton.textContent = 'Sign Out';
singInButton.removeEventListener('click', singInHandler);
singInButton.addEventListener('click', singOutHandler);
};
// even vor sing in button
const singInButton = document.querySelector('.user .sing-in');
// ref main elements
const libraryContainer = document.querySelector('.library-main');
const libraryPanel = document.querySelector('.library-panel');
const buttonAddBook = document.querySelector('.add-button');
let addModal = new ModalBookFactory('add');
let editModal = new ModalBookFactory('edit');
let storageSelectionModal = new ModalQueryFactory('storage-selection');
let confirmationSelectionStorageModal = new ModalQueryFactory(
'confirmation-selection-storage'
);
let confirmationDeleteLibraryModal = new ModalQueryFactory(
'confirmation-delete-library'
);
// append to the end of the body
addModal.insertInBody();
editModal.insertInBody();
storageSelectionModal.insertInBody();
confirmationSelectionStorageModal.insertInBody();
confirmationDeleteLibraryModal.insertInBody();
// make remember this selection
// my library data
let myLibrary = [];
let uniqueId = null;
let database = null;
// is local storage data
let isLocal = null;
// load data from cloud or local
storageSelectionModal.on();
// add events
buttonAddBook.addEventListener('click', addBookHandler);
function addBookHandler(e) {
addModal.clearAllInputs();
addModal.on();
}