Repository navigation
Expand file tree
/
Copy pathwidget.js
More file actions
370 lines (280 loc) · 9.68 KB
/
Copy pathwidget.js
File metadata and controls
370 lines (280 loc) · 9.68 KB
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
// A widget I created that makes suggestions based on user choices. Works with a remote API for machine learning and another to obtain the information for the choices. This file was injected into the site post-launch, so it was all in one file with the design
// Could only use JS(with jQuery)/HTML/CSS
// Was my first widget so it was a learning process
var widgetHtml = 'somehtml & inline css';
var modalHtml = 'somehtml'
$(document).ready(function(){
var ItemsArr = [];
var current;
var num = 0;
var liked = [];
var currentToken;
var started = false;
var userID = generateGUID();
var userIP;
// Got the IP to have a makeshift way of determing if the user was new or old for ad targeting and machine learning
getUserIP();
$("#widget").append(widgetHtml);
$("body").append(modalHtml);
function getUserIP() {
$.ajax({
type:'GET',
url: 'id'
}).done(function(res){
userIP = res.Result;
});
}
// User clicks YES on a Item in the modal
$(document).on('click',"#yesModalButton", function(e){
e.preventDefault();
liked.push(ItemsArr[num]);
num++;
remoteApiTracking(6, ItemsArr[num]);
// On mobile, the buttons change on press then return to normal state after a short timeout.
if (isMobile()) {
$('#yesModalButton').css("background-color", "#9fc14b");
$('#yesModalButton').css("color", "#ffffff");
setTimeout(function(){
$('#yesModalButton').css("background-color", "#ffffff");
$('#yesModalButton').css("color", "#9fc14b");
}, 100)
}
if (num < 5) {
nextItemInfo(num);
}
else {
getItemSuggestions();
}
});
// User clicks NO on a Item in the modal
$(document).on('click',"#noModalButton", function(e){
e.preventDefault();
num++;
remoteApiTracking(7, ItemsArr[num]);
if (isMobile()) {
$('#noModalButton').css("background-color", "black");
$('#noModalButton').css("color", "#ffffff");
setTimeout(function(){
$('#noModalButton').css("background-color", "#ffffff");
$('#noModalButton').css("color", "black");
}, 100)
}
if (num < 5) {
nextItemInfo(num);
}
else {
getItemSuggestions();
}
});
// User clicks on the "START" button in the main widget which prompts the opening of the modal
$("#startwidgetButton").on("click", function(e) {
document.getElementById("openwidgetModal").className += " visible";
if (isMobile()) {
$('.startAgain').empty();
$('.startAgain').append('<img src="imglink">');
}
// Reset the modal after closing and reopening.
if (started === true) {
}
else {
started = true;
$.ajax({
type:'GET',
url: ''+userIP,
crossDomain: true,
headers: {
}
}).done(function(a){
currentToken = a.AccessKey
$.ajax({
type:'GET',
url: '',
crossDomain: true,
headers: {
"Token": currentToken
}
}).done(function(data){
ItemsArr = data.Result
nextItemInfo(0);
});
});
}
});
$(document).on('click',".startAgain", function(e){
e.preventDefault();
startAgain();
})
// Start a new experience. Is called on click of "startAgain" button or closing and reopening the modal
function startAgain() {
liked = [];
num = 0;
$(".widgetFinalTip306x124").hide();
$(".desktopModalMain").empty();
$.ajax({
type:'GET',
url: '',
crossDomain: true,
headers: {
"Token": currentToken
}
}).done(function(data){
ItemsArr = data.Result
$(".desktopModalMain").append('text');
num = 0;
$(".widgetModalHeader").text("text");
nextItemInfo(0);
});
$(".widgetModalButtons").show();
}
// Loads the Item information in the modal after starting or clicking yes/no
function nextItemInfo (num) {
// Sometimes the image can be a little slow to load and the user might click the button again too quickly. Having two removes of the image prevents two images appearing on screen after the AJAX call below completes.
// Similarly, calling endSpinner at the beginning before calling loadSpinner ensures there won't be two spinners if the user clicks too quickly.
endSpinner();
$('#widgetModalImage').remove();
$('#widgetModalImage').remove();
// Circle active is the progress bar changing as the user continues through.
$('.circle'+num).addClass('circle-active');
if (num > 0) {
$('.circle'+(num - 1)).removeClass('circle-active');
}
$(".widgetModalText").empty();
loadSpinner('.widgetSelect', true);
$.ajax({
type: 'GET',
url: '' + ItemsArr[num].toString(),
crossDomain: true
}).done(function(data){
var imgs = data.Items[0].image
var imageUrl = "" + imgs.imagePath
imageUrl += imgs.fullSize;
endSpinner();
$('.widgetSelect').prepend('<img id="widgetModalImage" src="">');
$("#widgetModalImage").attr("src", imageUrl);
$(".widgetModalText").append(data.Items[0].title)
});
}
// After clicking through the 5 suggestions, loads the suggestions from the API and changes the view in the modal
function getItemSuggestions () {
if (liked.length > 0) {
$.ajax({
type: 'POST',
url: '',
crossDomain: true,
headers: {
"Token": currentToken
},
data: {
"ids": liked
}
}).done(function(data) {
if (data.Result.length > 0) {
$(".desktopModalMain").empty();
$(".widgetModalButtons").hide();
$(".desktopModalMain").append("<div class='finalResultsDiv'></div>");
$(".widgetModalHeader").text("text");
for (i=0; i<5; i++) {
finalScreenItemInfo(data.Result[i], i.toString());
};
setTimeout(function () {
$(".widgetFinalTip306x124").show();
}, 100);
if (isMobile()) {
$("#closewidgetMobileButton").css("display", "block");
$("#mobilePresentedModal").css("visibility", "hidden");
}
}
else {
$(".desktopModalMain").empty();
$(".widgetModalHeader").text("sorry!");
$(".desktopModalMain").append("html");
}
})
}
else {
$(".desktopModalMain").empty();
$(".widgetModalHeader").text("sorry!");
$(".desktopModalMain").append("html");
}
}
// Loads an individual Item's info for the final screen and adds it to the list
function finalScreenItemInfo (ItemId, id) {
$.ajax({
type: 'GET',
url: '' + ItemId.toString(),
crossDomain: true
}).done(function(data){
var imgs = data.Items[0].image
var thumbnailSizeUrl = "link" + imgs.imagePath + imgs.fullSize;
var linkUrl = "link" + data.Items[0].url;
$(".finalResultsDiv").append("<div id= 'resultDiv' class='resultDiv"+id+"'></div>");
$(".resultDiv"+id).append("<a class='finalResultsImg' href="+ linkUrl +" target='_blank'><div class='finalResultsImgDiv'></div></a>");
var imgDivParent = $(".resultDiv"+id + " > a");
imgDivParent.children().css("background-image","url("+thumbnailSizeUrl+")");
imgDivParent.children().css("background-position","center")
$(".resultDiv"+id).append("<div class='resultDivText'><p>"+ data.Items[0].title +"</p><div class='sourceText'>"+ data.Items[0].source +"</div></div>");
})
}
// Determines if it is a mobile device via touch events
// Means some tablets counts as mobile but for our purposes that is fine
function isMobile() {
try{ document.createEvent("TouchEvent"); return true; }
catch(e){ return false; }
}
// Loading spinner if image doesn't load immediately
function loadSpinner(element, prepend) {
var loaderHtml = '<div id="floatingCirclesG"><div class="f_circleG" id="frotateG_01"></div><div class="f_circleG" id="frotateG_02"></div><div class="f_circleG" id="frotateG_03"></div><div class="f_circleG" id="frotateG_04"></div><div class="f_circleG" id="frotateG_05"></div><div class="f_circleG" id="frotateG_06"></div><div class="f_circleG" id="frotateG_07"></div><div class="f_circleG" id="frotateG_08"></div></div>';
if (prepend) {
$(element).prepend(loaderHtml);
}
else {
$(element).append(loaderHtml);
}
};
function endSpinner() {
$('#floatingCirclesG').remove();
}
// not a true GUID but almost impossible for this not to be unique
function generateGUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
// Sends tracking info to API to allow for improvement of suggestions algorithm
function remoteApiTracking(actionNum, ItemID) {
$.ajax({
type: 'POST',
url: '',
crossDomain: true,
headers: {
"Token": currentToken
},
// Action types nums are as follows:
data: {
"action" : {
"ActionType": actionNum,
"ItemID":ItemID,
"UserID": userID,
"DevicePlatform":"Web"
}
}
}).done(function(data){
});
}
// Gives the close button a function for tracking purposes
$(document).on('click',".close", function(e){
$("#openwidgetModal").removeClass("visible");
closeButton();
});
$(document).on('click',"#closewidgetMobileButton", function(e){
$("#openwidgetModal").removeClass("visible");
closeButton();
});
function closeButton() {
startAgain();
}
});