-
Notifications
You must be signed in to change notification settings - Fork 0
/
examplestore.js
148 lines (110 loc) · 4.81 KB
/
examplestore.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
$(function () {
// --------------------
// -- DATABASE STUFF --
// --------------------
// Config data
let firebaseConfig = {
apiKey: 'AIzaSyCWpVaZvZ83dHxQKw3dRwem7SJmGs704gY',
authDomain: 'example-store-ac051.firebaseapp.com',
databaseURL: 'https://example-store-ac051.firebaseio.com',
projectId: 'example-store-ac051',
storageBucket: 'example-store-ac051.appspot.com',
messagingSenderId: '336729192315',
appId: '1:336729192315:web:5f8af3e53c07f194e0efde',
measurementId: 'G-FYCQVYFTNC'
};
function populateSubMenus(subcategories) {
/* ----- HTML STRUCTURE -----
.submenu #submenu-x -- div
.submenu__container -- div
.section #section-x -- div
.section__title -- h3
#section__list-x -- ul
.section__item -- li
a
*/
/* TODO: wrap this in a for loop that counts nav li elements
in order to remove hardcoded container identifier */
for (let i = 0; i < subcategories.length; i++) {
console.log(subcategories.length);
// generate container
let sectionContainerElement = document.createElement('div');
sectionContainerElement.className = 'section';
sectionContainerElement.id = `section-${i + 1}`;
/* puts hardcoded promo elements at END of list by
putting the container of our generated elements first,
requires array to be reversed before function call */
$("#submenu__container-1").prepend(sectionContainerElement);
// generate h3
let subcategoryTitle = subcategories[i].title;
let titleElement = document.createElement('h3');
titleElement.className = 'section__title';
titleElement.innerHTML = subcategoryTitle;
sectionContainerElement.append(titleElement);
// generate ul. ul is not styled
let ulElement = document.createElement('ul');
ulElement.id = `section__list-${i + 1}`;
sectionContainerElement.append(ulElement);
for (let x = 0; x < subcategories[i].items.length - 1; x++) {
// generate li, which is unstyled
let liElement = document.createElement('li');
// generate anchor
let anchor = document.createElement('a');
liElement.appendChild(anchor);
anchor.href = '#';
anchor.className = 'section__link';
anchor.innerHTML = subcategories[i].items[x];
$(`#section__list-${i + 1}`).append(liElement);
}
}
}
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
let ref = firebase.database().ref();
ref.once('value').then(function (snapshot) {
// array of individual items
let itemsList = [];
// array of all menu sections
let subcategories = [];
// STRUCTURE: sublist --> special & subcategory*num --> title & li*num
// get number of subcategories in sublist
for (let i = 1; i < snapshot.child('sublist-1').numChildren(); i++) {
// object for menu sections
let subcategory = {
title: null,
items: itemsList
}
// store title of subcategory
let subcategoryTitle = snapshot.child('sublist-1').child(`subcategory-${i}`).child('title').val();
subcategory.title = subcategoryTitle;
// clear list of items before storing this array in object
itemsList = [];
// get number of sub list items in subcategory
for (let k = 1; k < snapshot.child('sublist-1').child(`subcategory-${i}`).numChildren() + 1; k++) {
// store value of list item
let subcategoryItem = snapshot.child('sublist-1').child(`subcategory-${i}`).child(`li-${k}`).val();
itemsList.push(subcategoryItem);
}
// store list of items in subcategory object
subcategory.items = itemsList;
// store subcategory into array
subcategories.push(subcategory);
console.log(subcategories)
}
// generate html from array of subcategory objects
subcategories.reverse();
populateSubMenus(subcategories)
});
// ----------------------------
// -- CAROUSEL CONFIGURATION --
// ----------------------------
$('.carousel').slick({
dots: true,
arrows: true,
appendDots: $('.carousel__dots'),
infinite: true
// TODO: add scroll event to trigger fade in animation, implement lazy loading
})
// MENUS
})