-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
3336 lines (2588 loc) · 97.4 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
/*
* @license MIT
* cinetech (muvisho)
* Copyright (c) 2023 Abraham Ukachi. The Muvisho Project Contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @name: App
* @type: script
* @author: Abraham Ukachi <abraham.ukachi@laplateforme.io>
*
* Example usage:
* 1+|> var muvishoApp = new App(DEFAULT_LANGUAGE, LIGHT_THEME);
* -|>
* -|> muvishoApp.setTitle('Movies & TV Shows Online Free - Muvisho');
* -|>
* -|> muvishoApp.run();
*
*
* 2+|> // Open a dialog in the main part in 0.5 seconds
* -|>
* -|> muvishoApp.openDialog({
* -|> id: 'deleteAccount',
* -|> title: 'Delete Account',
* -|> message: 'Are you sure?',
* -|> confirmBtnText: 'Yes',
* -|> cancelBtnText: 'No',
* -|> onConfirm: () => console.log(`confirm button clicked`),
* -|> onCancel: () => console.log(`cancel button clicked`),
* -|> noDivider: false,
* -|> isCancelable: true
* -|> }, 0.5, MAIN_PART);
* -|>
*
* 2+|> // Close a dialog from the main part
* -|>
* -|> muvishoApp.closeDialog('deleteAccount', 0.5, MAIN_PART);
* -|>
*
*
* 3+|> // Open a menu in the aside part in 0.3 seconds
* -|>
* -|> muvishoApp.openMenu(this, {
* -|> id: 'default',
* -|> title: '',
* -|> items: [
* -|> {
* -|> id: 'favInfo',
* -|> icon: 'about_outline',
* -|> text: 'Details',
* -|> link: '/',
* -|> onClick: () => console.log(`favInfo item clicked...`)
* -|> },
* -|> {
* -|> id: 'removeFav',
* -|> icon: 'delete_outline',
* -|> text: 'Remove from favorites',
* -|> link: '/delete/favorite/[:id]',
* -|> onClick: () => console.log(`removeFav item clicked...`)
* -|> }
* -|> ],
* -|> noDivider: false,
* -|> isCancelable: true
* -|> }, 0.5, ASIDE_PART);
* -|>
*
*
* 2+|> // Close a menu from the main part
* -|>
* -|> muvishoApp.closeMenu('default', 0.5, MAIN_PART);
* -|>
*/
import { html, Engine } from './Engine.js'; // <- we just need stuff from our custom engine to get started #LOL !!! :)
import { eventMixin } from './helpers/mixins/event-mixin.js';
import { installStorageWatcher } from './helpers/LiveStorage.js';
import { installRouter, getPageRoute, getViewRoute, getSearchParams } from './helpers/router.js';
import { installMediaQueryWatcher } from './helpers/mediawatcher.js';
import I18n from './helpers/i18n.js'; // <- i18n helper
import Request from './helpers/request.js'; // <- request helper
"use strict";
// ^^^^^^^^^ This keeps us on our toes, as it forces us to use all pre-defined variables, among other things 😅
// Defining some constant variables...
// app name
export const APP_NAME = "muvisho";
// app version
export const APP_VERSION = "0.0.1";
// author
export const AUTHOR = "Abraham Ukachi";
// base directory
export const BASE_DIR = "/cinetech/" // "muvisho-js/"; (for production)
// assets directory
export const ASSETS_DIR = `${BASE_DIR}root/public/assets`;
// theme directory
export const THEME_DIR = `${ASSETS_DIR}/theme`;
// styles directory
export const STYLES_DIR = `${ASSETS_DIR}/stylesheets`;
// animations directory
export const ANIM_DIR = `${ASSETS_DIR}/animations`;
// source directory
export const SOURCE_DIR = `${BASE_DIR}root/public/src`;
// screens directory
export const SCREENS_DIR = `${SOURCE_DIR}/screens`;
// pages directory
export const PAGES_DIR = `${SOURCE_DIR}/pages`;
// views directory
export const VIEWS_DIR = `${SOURCE_DIR}/views`;
// screens
export const SPLASH_SCREEN = 'splash';
export const WELCOME_SCREEN = 'welcome';
// pages
export const HOME_PAGE = 'home';
export const SEARCH_PAGE = 'search'; // <- or explore 🤔
export const EXPLORE_PAGE = 'explore'; // <- or search 😜
export const MOVIES_PAGE = 'movies';
export const SERIES_PAGE = 'series';
export const FAVORITES_PAGE = 'favorites';
export const DETAILS_PAGE = 'details';
export const ACCOUNT_PAGE = 'account';
export const PROFILE_PAGE = 'profile';
export const SETTINGS_PAGE = 'settings';
export const HELP_PAGE = 'help';
// export const ARTICLES_PAGE = 'articles';
// export const SAVES_PAGE = 'saves';
// export const PROFILE_PAGE = 'profile';
// export const ADMIN_PAGE = 'admin';
// export const SETTINGS_PAGE = 'settings';
// views
export const VIEW_DEFAULT = 'default';
export const VIEW_LOGIN = 'login';
export const VIEW_REGISTER = 'register';
export const VIEW_INFO = 'info';
export const VIEW_IDENTITY = 'identity';
export const VIEW_EMAIL = 'email';
export const VIEW_PASSWORD = 'password';
export const VIEW_LANGUAGE = 'language';
export const VIEW_THEME = 'theme';
export const VIEW_CONTACT = 'contact';
export const VIEW_ABOUT = 'about';
export const VIEW_MOVIE = 'movie';
export const VIEW_SHOW = 'show';
// a list of all assets that have been loaded
export const loadedAssetsList = [];
// app children levels
export const APP_SCREENS = 1;
export const APP_PAGES = 2;
export const APP_DIALOGS = 3;
export const APP_MENUS = 4;
export const APP_TOASTS = 5;
// page types
export const MAIN_PAGE_TYPE = 'main';
export const ASIDE_PAGE_TYPE = 'aside';
// themes
export const CLASSIC_THEME = 'classic';
export const LIGHT_THEME = 'light';
export const DARK_THEME = 'dark';
// toast types
export const DEFAULT_TOAST_TYPE = 'default';
export const SUCCESS_TOAST_TYPE = 'success';
export const ERROR_TOAST_TYPE = 'error';
// default toast timeout
export const DEFAULT_TOAST_TIMEOUT = 5000; // <- 5 seconds
// ++++++ Constants 4rm Maxaboom +++++++
// toast types
export const ERROR_TOAST = 'et';
export const SUCCESS_TOAST = 'st';
export const GOOD_TOAST = '1t';
export const BAD_TOAST = '2t';
export const NORMAL_TOAST = '0t';
export const DEFAULT_TOAST = NORMAL_TOAST; // <- default toast type is normal
// default toast timeout
// export const DEFAULT_TOAST_TIMEOUT = 5; // <- default toast timeout is 5 seconds
export const DEFAULT_MENU_TIMEOUT = 0.5;
export const DEFAULT_BACKDROP_TIMEOUT = 0.5;
// parts
export const MAIN_PART = '0p';
export const ASIDE_PART = '1p';
export const FULL_PART = '2p';
export const DEFAULT_PART = FULL_PART; // <- default part is full
// +++++ End of Constants 4rm Maxaboom +++++++
// TODO: Turn the App into a custom element by extending `HTMLElement`
// Create a `App` class
export class App extends Engine {
// some app specific constants
// static get CLASSIC_THEME() { return 'classic' }
// static get LIGHT_THEME() { return 'light' }
// static get DARK_THEME() { return 'dark' }
/**
* Properties
*
* @type { Object }
*/
static get properties() {
return {
id: { type: String },
name: { type: String },
title: { type: String },
lang: { type: String },
theme: { type: String },
updated: { type: Boolean },
labelsHidden: { type: Boolean },
_navbarOrientation: { type: String },
_pageLoading: { type: Boolean }
};
}
/**
* Theme
*
* @type { Array }
*/
static get theme() {
return [ 'typography', 'color', 'styles' ];
}
/**
* Styles
*
* @type { Array }
*/
static get styles() {
return [
// 'splash-screen'
];
}
/**
* Animations
*
* @type { Array }
*/
static get animations() {
return [
'fade-in', 'fade-out',
'pop-in',
'slide-from-left', 'slide-from-down', 'slide-down',
'slide-from-up', 'slide-up'
];
}
/**
* Screens
*
* @type { Object }
*/
static get screens() {
return {
// splash: { name: 'splash-screen' }
// welcome: { name: 'welcome-screen' }
};
}
/**
* Pages
*
* @type { Array[Object] }
*/
static get pages() {
return [
{ name: HOME_PAGE , views: [VIEW_DEFAULT, VIEW_LOGIN, VIEW_REGISTER] },
{ name: SEARCH_PAGE, views: [VIEW_DEFAULT] },
{ name: MOVIES_PAGE, views: [VIEW_DEFAULT] },
{ name: SERIES_PAGE, views: [VIEW_DEFAULT] },
{ name: FAVORITES_PAGE, views: [VIEW_DEFAULT] },
{ name: DETAILS_PAGE, views: [VIEW_DEFAULT, VIEW_MOVIE, VIEW_SHOW] },
{ name: ACCOUNT_PAGE, views: [VIEW_DEFAULT] },
{ name: SETTINGS_PAGE, views: [VIEW_DEFAULT, VIEW_LANGUAGE, VIEW_THEME] },
{ name: PROFILE_PAGE, views: [VIEW_DEFAULT, VIEW_IDENTITY, VIEW_EMAIL, VIEW_PASSWORD] },
{ name: HELP_PAGE, views: [VIEW_DEFAULT, VIEW_CONTACT, VIEW_ABOUT] }
];
}
/**
* Supported Themes
*
* @type { Array[Object] }
*/
static get supportedThemes() {
return [
{ id: 'classic', name: 'Classic' },
{ id: 'light', name: 'Light' },
{ id: 'dark', name: 'Dark' }
];
}
/**
* Supported Languages
*
* @type { Array[Object] }
*/
static get supportedLanguages() {
return [
{ id: 'en', name: 'English' },
{ id: 'fr', name: 'French' },
{ id: 'ru', name: 'Russian' },
{ id: 'es', name: 'Spanish' }
];
}
/**
* Supported Pages
*
* @type { Array[Object] }
*/
static get supportedPages() {
return [
{id: 'home', type: MAIN_PAGE_TYPE, name: 'Home'},
{id: 'search', type: ASIDE_PAGE_TYPE, name: 'Search'},
{id: 'movies', type: MAIN_PAGE_TYPE, name: 'Movies'},
{id: 'series', type: MAIN_PAGE_TYPE, name: 'Series'},
{id: 'favorites', type: MAIN_PAGE_TYPE, name: 'Favorites'},
{id: 'details', type: ASIDE_PAGE_TYPE, name: 'Details'},
{id: 'account', type: MAIN_PAGE_TYPE, name: 'Account'},
{id: 'profile', type: ASIDE_PAGE_TYPE, name: 'Profile'},
{id: 'settings', type: MAIN_PAGE_TYPE, name: 'Settings'},
{id: 'help', type: ASIDE_PAGE_TYPE, name: 'Help'}
];
}
/**
* Config of the app
*/
static get config() {
return {
baseUrl: '/cinetech/'
};
}
// Define some public properties
// Define some private properties
#toasting = false;
#_currentLayout = null;
/**
* Constructor of the App
* NOTE: This constructor will be executed automatically when a new object (eg. muvishoApp) is created.
*
* @param { String } lang - The default language of the App
* @param { String } theme - The default theme of the App
*/
constructor(lang = 'en', theme = 'dark') {
// call the `Engine` constructor with `App` as it's controller
super(App);
// set default attributes
this.lang = lang;
this.theme = theme;
// set both current screen and page to null
// (WE ARE IN "BOOTING MODE"... So, no screens; no pages)
this.currentScreen = null;
this.currentPage = null;
this.currentView = null;
this.currentParams = null;
// create a new `I18n` instance with `lang` as the default language
this.i18n = new I18n(lang);
// create an object of the `Request` class
this.request = new Request(lang);
// when the data is loaded,
// call the `onReady` method with the loaded data as parameter
this.i18n.dataLoaded = (data) => this._onI18nDataLoaded(data);
// list of primary pages
this.primaryPages = [ HOME_PAGE, SEARCH_PAGE, MOVIES_PAGE, SERIES_PAGE, FAVORITES_PAGE, ACCOUNT_PAGE, PROFILE_PAGE ];
// show / log a welcome message
this.#showWelcomeMessage();
// create the app
this.#create();
// DEBUG [4dbsmaster]: tell me about it ;)
// console.log(`[constructor]: #_props.init =>`, this.#_props.init);
}
/**
* Method that is called from the Engine's constructor
* @override from `Engine`
*/
init() {
// Initialize public properties
this.id = 'app';
this.name = APP_NAME;
this.title = 'Muvisho';
this.updated = false;
this.labelsHidden = false; // <- by default both side and nav labels should be visible or shown ;)
// Initialize private properties
this._navbarOrientation = 'horizontal';
this._pageLoading = false;
// ====== TESTING PROPERTIES ==========
/*
setTimeout(() => {
this.loading = false;
this.title = 'Articles';
}, 2000);
*/
// ====================================
}
/**
* Renders the app's template
* IMPORTANT: This is where the html content of the app is defined.
*
* TODO: Return a `HTMLTemplate` instead
*
* @returns { String }
*/
render() {
return html`
<!-- App Container -->
<div id="appContainer" class="theme ${this.theme}" lang="${this.lang}" fit>
<!-- Screens -->
<div id="screens" fit hidden></div>
<!-- End of Screens -->
<!-- Pages -->
<div id="pages" class="flex-layout horizontal" fit hidden></div>
<!-- End of Pages -->
<!-- Backdrop -->
<div id="backdrop" fit hidden></div>
<!-- Dialogs -->
<div id="dialogs" fit hidden></div>
<!-- Menus -->
<div id="menus" fit hidden></div>
<!-- Toasts -->
<div id="toasts" class="fade-in" fit hidden></div>
<!-- Progress Bar -->
<div id="progressBar" class="progress-bar" hidden>
<span class="progress-bar-value"></span>
</div>
</div>
<!-- End of App Container -->
<!-- NOTE: Style Links will be injected here -->
`;
}
/**
* First time this app gets updated
* @override from `Engine`
*/
firstUpdated() {
// install a router
installRouter(this, (location, event) => this._handleNavigation(location, event));
// install a storage watcher from 'LiveStorage'
installStorageWatcher(this, ['lang', 'theme'], (changedStorageItems) => this._handleChangedStorageItems(changedStorageItems));
// install a media-query watcher with a `460px` breakpoint
installMediaQueryWatcher(this, 460,
(firstNarrowQuery) => this._handleNarrowLayout(firstNarrowQuery),
(firstWideQuery) => this._handleWideLayout(firstWideQuery)
);
// if the current values of `lang` and `theme` in our live storage
if (this.liveStorage.isNullItems('lang', 'theme')) {
this.liveStorage.setItems({lang: this.lang, theme: this.theme});
}
// add event listeners here
// this.host.addEventListener('click', (ev) => console.log(`clicking host ev.currentTarget =>`, ev.currentTarget));
// TODO: Install the starter helper & media-query watcher
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[40m\x1b[33m[firstUpdated](1): App have been updated for the first time\x1b[0m`);
console.log(`\x1b[40m\x1b[33m[firstUpdated](2): this.containerEl => ${eval(this.containerEl)}\x1b[0m`);
}
/**
* Handler that is called whenever a property changes
*
* @param { Array[Object] } changedProperties
* @override
*/
propertiesUpdated(changedProperties) {
changedProperties.forEach((prop) => {
if (prop.name === 'updated' && prop.value === true) {
// call the first updated method
this.firstUpdated();
}
if (prop.name === 'title') {
this.setTitle(prop.value);
}
// if the `labelsHidden` property has changed...
if (prop.name === 'labelsHidden') {
// ...handle it ;)
this._handleLabelsHiddenChange(prop.value, prop.oldValue);
}
if (prop.name === '_pageLoading') {
this._handlePageLoadingChange(prop.value, prop.oldValue);
}
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[33m[changedProperties]:
1. prop.name => ${prop.name}
2. prop.value => ${prop.value}
3. prop.oldValue => ${prop.oldValue}
\x1b[0m`);
});
}
/**
* Handler that is called whenever a property gets reset to its initial value
*
* @param { String } prop - The property's name
* @param { String|Number|Boolean|Array } value - The value of the property after reset
* @param { String|Number|Boolean|Array } oldValue - The value of the property before reset
*
* @override from `Engine`
*/
propertyResetHandler(prop, value, oldValue) {
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[37m[propertyResetHandler] prop => ${prop} & value => ${value} & oldValue => ${oldValue}\x1b[0m`);
}
/* >> PUBLIC METHODS << */
/**
* Method used to open a menu using the given `params`
*
* @param { Object } params - The params object
*
* @param { String } params.id - The id of the menu
* @param { String } params.title - The title of the menu
* @param { Array } params.items - The items of the menu (e.g [{icon: '', text: 'delete', link: '/delete', onClick: () => console.log()}, {...}, ...]
* @param { Boolean } params.noDivider - Whether to hide the divider between the buttons
*
* @param { Number } timeout - How long it will take to open the menu
* @param { String } part - Which part of the app the menu should be display.
* @param { Object } controller - a class object (e.g. `this` or `muvishoApp.detailsPage`)
*
* @returns { Promise } - A promise that will be resolved when the menu is opened
*/
openMenu(params, timeout = 0.5, part = DEFAULT_PART, controller) {
return new Promise ((resolve, reject) => {
// get the menus element of the given `part` as `dialogsEl`
let menusEl = this.getCurrentMenusElement(part);
// initialize the `menuId` variable
let menuId = params.id || 'menu';
// reject the promise, if there's already a menu with this `menuId`
// in the specified `part`
if (this.getMenuById(menuId, part)) {
return reject(`The menu with id "${menuId}" is already open, close it and try again!`);
}
// Now, rendering the menu...
// get the menu's html template with the given `params` as `menuHTMLTemplate`
let menuHTMLTemplate = this._getMenuHTMLTemplate(params);
// insert 'beforend' the `menuHTMLTemplate` to `menusEl`
menusEl.insertAdjacentHTML('beforeend', menuHTMLTemplate);
// get the menu element using `menuId`
let menuEl = this.getMenuById(menuId, part);
// install menu event listeners on this `menuEl`
this.#installMenuEventListeners(menuEl, params, controller);
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[35m[openMenu]: menuId => ${menuId} & menusEl => \x1b[0m`, menusEl);
// show the backdrop of the given `part`
this.showBackdropOf(part, params.isCancelable ?? false);
// show or unhide the `menusEl`
menusEl.hidden = false;
// show or unhide the `menuEl`
menuEl.hidden = false;
// remove the `fade-out` class from `menusEl`
menusEl.classList.remove('fade-out');
// add the `fade-in` class to `menusEl`
menusEl.classList.add('fade-in');
// remove the `slide-down` class from `menuEl`
menuEl.classList.remove('slide-down');
// add the `slide-from-down` class to `menuEl`
menuEl.classList.add('slide-from-down');
// cancel any active timers
clearTimeout(this._closeMenuTimer);
clearTimeout(this._openMenuTimer);
// resolve the promise after `duration` seconds
this._openMenuTimer = setTimeout(() => {
// TODO ? Do something before resolving the promise
// add a `opened` property to `menuEl`
menuEl.setAttribute('opened', '');
// resolve the promise
resolve(menuEl);
}, timeout * 1000);
});
}
/**
* Method used to close the menu with the given `menuId`
*
* @param { String } menuId - The id of the menu to close
* @param { Number } duration - The duration of the animation (in seconds)
* @param { String } part - The part of the app where the menu will be hidden (eg. MAIN_PART, ASIDE_PART, FULL_PART)
*
* @returns { Promise } - A promise that will be resolved when the menu is closed
*/
closeMenu(menuId = 'menu', duration = 0.5, part = DEFAULT_PART) {
return new Promise((resolve, reject) => {
// get the menus element of the given `part` as `menusEl`
let menusEl = this.getCurrentMenusElement(part);
// get the menu element with the given `menuId` as `menuEl`
let menuEl = this.getMenuById(menuId, part);
// if the menu element doesn't exist, reject the promise
if (!menuEl) { return reject(`Menu with id "${menuId}" doesn't exist`) }
// hide the backdrop of the given `part`
this.hideBackdropOf(part);
// remove the `fade-in` class from `menusEl`
menusEl.classList.remove('fade-in');
// add the `fade-out` class to `menusEl`
menusEl.classList.add('fade-out');
// remove slide-from-down class from `menuEl`
menuEl.classList.remove('slide-from-down');
// add the `slide-down` class to `menuEl`
menuEl.classList.add('slide-down');
// cancel any active timers
clearTimeout(this._closeMenuTimer);
clearTimeout(this._openMenuTimer);
// resolve the promise after `duration` seconds
this._closeMenuTimer = setTimeout(() => {
// TODO ? Do something before resolving the promise
// remove the `opened` property from `menuEl`
menuEl.removeAttribute('opened');
// hide the `menuEl`
menuEl.hidden = true;
// hide the `menusEl`
menusEl.hidden = true;
// remove the `menuEl` from `menusEl`
menuEl.remove();
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[34m[closeMenu](_closeMenuTimer): menuEl ==> \x1b[0m`, menuEl);
// resolve the promise
resolve();
}, duration * 1000);
});
}
/**
* Method used to open a dialog using the given `params`
*
* @param { Object } params - The params object
*
* @param { String } params.id - The id of the dialog
* @param { String } params.title - The title of the dialog
* @param { String } params.message - The message of the dialog
* @param { String } params.confirmBtnText - The text of the confirm button
* @param { String } params.cancelBtnText - The text of the cancel button
* @param { Boolean } params.noDivider - Whether to hide the divider between the buttons
* @param { Function } params.onConfirm - The function to call when the confirm button is clicked
* @param { Function } params.onCancel - The function to call when the cancel button is clicked
*
* @returns { Promise } - A promise that will be resolved when the dialog is opened
*/
openDialog(params, timeout = 0.5, part = DEFAULT_PART) {
return new Promise ((resolve, reject) => {
// get the dialogs element of the given `part` as `dialogsEl`
let dialogsEl = this.getCurrentDialogsElement(part);
// initialize the `dialogId` variable
let dialogId = params.id || 'dialog';
// reject the promise, if there's already a dialog with this `dialogId`
// in the specified `part`
if (this.getDialogById(dialogId, part)) {
return reject(`The dialog with id "${dialogId}" is already open, close it and try again!`);
}
// Now, rendering the dialog...
// get the dialog html template with the given `params` as `dialogHTMLTemplate`
let dialogHTMLTemplate = this._getDialogHTMLTemplate(params);
// insert 'beforend' the `dialogHTMLTemplate` to `dialogsEl`
dialogsEl.insertAdjacentHTML('beforeend', dialogHTMLTemplate);
// get the dialog element using `dialogId`
let dialogEl = this.getDialogById(dialogId, part);
// get the confirm button element as `confirmBtnEl`
let confirmBtnEl = dialogEl.querySelector('.confirm-btn');
// get the cancel button element as `cancelBtnEl`
let cancelBtnEl = dialogEl.querySelector('.cancel-btn');
// attach the `onConfirm` and `onCancel` functions to the buttons
confirmBtnEl.onclick = params.onConfirm ?? (() => this.closeDialog(dialogId, timeout, part));
cancelBtnEl.onclick = params.onCancel ?? (() => this.closeDialog(dialogId, timeout, part));
// if the dialog element doesn't exist, reject the promise
// if (!dialogEl) { reject(`Dialog with id "${dialogId}" doesn't exist`); }
// show the backdrop of the given `part`
this.showBackdropOf(part, params.isCancelable ?? false);
// show or unhide the `dialogsEl`
dialogsEl.hidden = false;
// show or unhide the `dialogEl`
dialogEl.hidden = false;
// remove the `fade-out` class from `dialogsEl`
dialogsEl.classList.remove('fade-out');
// add the `fade-in` class to `dialogsEl`
dialogsEl.classList.add('fade-in');
// remove the `slide-up` class from `dialogEl`
dialogEl.classList.remove('slide-up');
// add the `slide-from-up` class to `dialogEl`
dialogEl.classList.add('slide-from-up');
// cancel any active timers
clearTimeout(this._closeDialogTimer);
clearTimeout(this._openDialogTimer);
// resolve the promise after `duration` seconds
this._openDialogTmer = setTimeout(() => {
// TODO ? Do something before resolving the promise
// add a `opened` property to `dialogEl`
dialogEl.setAttribute('opened', '');
// resolve the promise
resolve(dialogEl);
}, timeout * 1000);
});
}
/**
* Method used to close the dialog with the given `dialogId`
*
* @param { String } dialogId - The id of the dialog to close
* @param { Number } duration - The duration of the animation (in seconds)
* @param { String } part - The part of the app where the menu will be hidden (eg. MAIN_PART, ASIDE_PART, FULL_PART)
*
* @returns { Promise } - A promise that will be resolved when the dialog is closed
*/
closeDialog(dialogId = 'dialog', duration = 0.5, part = DEFAULT_PART) {
return new Promise((resolve, reject) => {
// get the dialogs element of the given `part` as `menusEl`
let dialogsEl = this.getCurrentDialogsElement(part);
// get the dialog element with the given `dialogId` as `dialogEl`
let dialogEl = this.getDialogById(dialogId, part);
// if the dialog element doesn't exist, reject the promise
if (!dialogEl) { return reject(`Dialog with id "${dialogId}" doesn't exist`) }
// hide the backdrop of the given `part`
this.hideBackdropOf(part);
// remove the `fade-in` class from `dialogsEl`
dialogsEl.classList.remove('fade-in');
// add the `fade-out` class to `dialogsEl`
dialogsEl.classList.add('fade-out');
// remove slide-from-up class from `dialogEl`
dialogEl.classList.remove('slide-from-up');
// add the `slide-up` class to `dialogEl`
dialogEl.classList.add('slide-up');
// cancel any active timers
clearTimeout(this._closeDialogTimer);
clearTimeout(this._openDialogTimer);
// resolve the promise after `duration` seconds
this._closeDialogTimer = setTimeout(() => {
// TODO ? Do something before resolving the promise
// remove the `opened` property from `dialogEl`
dialogEl.removeAttribute('opened');
// deactivate the dialog element`
dialogEl.removeAttribute('active');
// hide the `dialogEl`
dialogEl.hidden = true;
// hide the `dialogsEl`
dialogsEl.hidden = true;
// remove the `fade-out` class from `dialogsEl`
dialogsEl.classList.remove('fade-out');
// remove the `dialogEl` from `dialogsEl`
dialogEl.remove();
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[34m[closeDialog](_closeDialogTimer): dialogEl ==> \x1b[0m`, dialogEl);
// resolve the promise
resolve();
}, duration * 1000);
});
}
/**
* Shows or unhides the backdrop of the app (or a specific part of the app)
*
* @param { String } part - The part of the app to show the backdrop of
* @param { Boolean } isCancelable - If TRUE, the backdrop will be cancelable
*/
showBackdropOf(part = DEFAULT_PART, isCancelable = false) {
// get the correct backdrop element
let backdropEl = part === MAIN_PART ? this.mainBackdropEl : (part === ASIDE_PART ? this.asideBackdropEl : this.backdropEl);
// set the cancelable attribute of the backdrop element
backdropEl.setAttribute('cancelable', isCancelable);
// set the `hidden` attribute to false
backdropEl.hidden = false;
}
/**
* Hides the backdrop of the app (or a specific part of the app)
*
* @param { String } part - The part of the app to show the backdrop of
* @param { Number } duration - The duration (in milliseconds) of the hiding animation
*/
hideBackdropOf(part = DEFAULT_PART, duration = 300) {
// get the correct backdrop element
let backdropEl = part === MAIN_PART ? this.mainBackdropEl : (part === ASIDE_PART ? this.asideBackdropEl : this.backdropEl);
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[34m[hideBackdropOf]: backdropEl => \x1b[0m`, backdropEl);
// add the `fade-out` class to the backdrop element
backdropEl.classList.add('fade-out');
// hide the backdrop element after 300 milliseconds
this.hideBackdropTimer = setTimeout(() => {
// set the `hidden` attribute to true
backdropEl.hidden = true;
// remove the `fade-out` class from the backdrop element
backdropEl.classList.remove('fade-out');
}, duration);
}
/**
* Toggles the default backdrop of the app
*/
toggleBackdrop() {
// if the backdrop is hidden, then show it
if (this.backdropEl.hidden) this.showBackdropOf(DEFAULT_PART);
// if the backdrop is not hidden, then hide it
else this.hideBackdropOf(DEFAULT_PART);
}
/**
* Shows all the labels in both side and nav labels
* NOTE: This method sets the `labelsHidden` property to FALSE
*/
showLabels() {
this.labelsHidden = false;
}