-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicTrailPageCode.js
251 lines (213 loc) · 8.14 KB
/
DynamicTrailPageCode.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
//-------------Imports-------------//
import {local} from 'wix-storage';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixUsers from 'wix-users';
import wixCRM from 'wix-crm';
//-------------Global Variables-------------//
// Current item.
let hike;
// Current user.
let user = wixUsers.currentUser;
$w.onReady(async function () {
//-------------Page Setup-------------//
hike = $w('#dynamicDataset').getCurrentItem();
// Check if the current item is in the hiking list and act accordingly.
checkWishlist();
// Set the action that occurs when the login message is clicked to be the loginMessageClick() function.
$w('#loginMessage').onClick(loginMessageClick);
if (wixWindow.rendering.env === "browser") {
// CREATES 3 RANDOMISED HIKES TO BE DISPLAYED
if(local.getItem('dynamicPageNames')) {
const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');
const dynamicPageNames = local.getItem('dynamicPageNames').split(',');
const dynamicPageImages = local.getItem('dynamicPageImages').split(',');
const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
const currentPageIndex = dynamicPageURLs.indexOf(currentPage);
const len = dynamicPageURLs.length-1;
const x = Math.floor((Math.random() * len) + 2);
if (x===currentPageIndex){
if (x < len - 2){
$w("#text73").text=dynamicPageNames[x+1];
$w("#text93").text=dynamicPageNames[x+2];
$w("#text94").text=dynamicPageNames[x+3];
$w("#image14").src=dynamicPageImages[x+1];
$w("#image15").src=dynamicPageImages[x+2];
$w("#image16").src=dynamicPageImages[x+3];
$w("#button12").link=dynamicPageURLs[x+1];
$w("#button13").link=dynamicPageURLs[x+2];
$w("#button14").link=dynamicPageURLs[x+3];
}
else{
$w("#text73").text=dynamicPageNames[x-1];
$w("#text93").text=dynamicPageNames[x-2];
$w("#text94").text=dynamicPageNames[x-3];
$w("#image14").src=dynamicPageImages[x-1];
$w("#image15").src=dynamicPageImages[x-2];
$w("#image16").src=dynamicPageImages[x-3];
$w("#button12").link=dynamicPageURLs[x-1];
$w("#button13").link=dynamicPageURLs[x-2];
$w("#button14").link=dynamicPageURLs[x-3];
}
}
else{
if (x < len - 1){
$w("#text73").text=dynamicPageNames[x];
$w("#text93").text=dynamicPageNames[x+1];
$w("#text94").text=dynamicPageNames[x+2];
$w("#image14").src=dynamicPageImages[x];
$w("#image15").src=dynamicPageImages[x+1];
$w("#image16").src=dynamicPageImages[x+2];
$w("#button12").link=dynamicPageURLs[x];
$w("#button13").link=dynamicPageURLs[x+1];
$w("#button14").link=dynamicPageURLs[x+2];
}
else{
$w("#text73").text=dynamicPageNames[x];
$w("#text93").text=dynamicPageNames[x-1];
$w("#text94").text=dynamicPageNames[x-2];
$w("#image14").src=dynamicPageImages[x];
$w("#image15").src=dynamicPageImages[x-1];
$w("#image16").src=dynamicPageImages[x-2];
$w("#button12").link=dynamicPageURLs[x];
$w("#button13").link=dynamicPageURLs[x-1];
$w("#button14").link=dynamicPageURLs[x-2];
}
}
}
}
// UPDATES MAP AND WEATHER WIDGET FOR EACH ITEM BY EDITING THE HTML
const siteURL = $w('#dynamicDataset').getCurrentItem().webappUrl;
const windySrc = $w('#dynamicDataset').getCurrentItem().windysrc;
$w("#html2").src = windySrc;
$w("#html1").src = siteURL;
});
// OPENS A LIGHTBOX WHEN ELEVATION GRAOH BUTTON IS CLICKED, DISPLAYING GRAPH FOR THAT ROW IN THE DATASET
export function button11_click(event, $w) {
const graphdata = $w('#dynamicDataset').getCurrentItem();
wixWindow.openLightbox('Elevation Graph', graphdata);
}
//~~~~~~~~~~~~~~~~~~~~~~ HIKING LIST STUFF ~~~~~~~~~~~~~~~~~~~~`
// Check if the current item is in the hiking list and act accordingly.
async function checkWishlist() {
// If the current user is logged in:
if (wixUsers.currentUser.loggedIn) {
// Query the collection to find if the item was already added to the user's hiking list.
let wishListResult = await wixData.query("myhikes")
.eq("hike", hike._id)
.eq("userId", user.id)
.find();
// If the item was already added to the user's hiking list:
if(wishListResult.items.length > 0)
// Show the "inWishList" image with a fade effect.
$w('#inWishList').show('fade', {duration: 100});
// If the product was not yet added to the user's wishlist:
else
// Show the "notInWishList" image with a fade effect.
$w('#notInWishList').show('fade', {duration: 100});
}
// If the current user is not logged in:
else {
// Show the "notInWishList" image with a fade effect.
$w('#notInWishList').show('fade', {duration: 100});
}
}
//-------------Event Handlers-------------//
// Set the action that occurs when the "inWishList" image is clicked.
export function inWishList_click(event, $w) {
// If the current user is logged in:
if (user.loggedIn)
// Remove the current product from the wishlist.
removeFromWishlist();
}
// Set the action that occurs when the "notInWishList" image is clicked.
export function notInWishList_click(event, $w) {
// If the current user is logged in:
if (user.loggedIn)
// Add the current product to the wishlist.
addToWishlist()
// If the current user is not logged in:
else
// Show the login message.
$w('#loginMessage').show();
}
// Set the action that occurs when the login message is clicked.
async function loginMessageClick() {
// Set the login options.
let options = {"mode": "login"};
// Hide the login message.
$w('#loginMessage').hide();
// Prompt the user to login using the options created above.
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("MemberProfile")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
// add the item to the collection
wixData.insert("MemberProfile", toInsert)
.catch( (err) => {
console.log(err);
} );
wixCRM.emailContact('testEmail', wixUsers.currentUser.id);
}
//update buttons accordingly
$w("#button15").label = "LOGOUT";
$w("#button16").show();
wixLocation.to(wixLocation.url);
} )
.catch( (err) => {
console.log(err);
} );
}
//-------------Hiking list Functionality-------------//
// Add the current item to the current user's hiking list and update the page accordingly.
async function addToWishlist() {
// Create the hiking list item relating the current item to the current user.
let wishListItem = {
hike: hike._id,
userId: user.id
};
// Hide the "notInWishList" image with a fade effect.
$w('#notInWishList').hide('fade', {duration: 100});
// Show the "inWishList" image with a fade effect.
$w('#inWishList').show('fade', {duration: 100});
// Insert the item created above into the collection.
let result = await wixData.insert("myhikes", wishListItem);
}
// Remove the current item to the current user's hiking list and update the page accordingly.
async function removeFromWishlist() {
// Query the collection to find the hiking list item corresponding to the current item and current user.
let wishListResult = await wixData.query("myhikes")
.eq("hike", hike._id)
.eq("userId", user.id)
.find();
// If a wishlist item was found:
if (wishListResult.length > 0) {
// Show the "notInWishList" image with a fade effect.
$w('#notInWishList').show('fade', {duration: 100});
// Hide the "inWishList" image with a fade effect.
$w('#inWishList').hide('fade', {duration: 100});
// Remove the wishlist item from the collection.
await wixData.remove("myhikes", wishListResult.items[0]._id)
}
}