forked from ClockworkMod/clockworkmod.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jscript.js
745 lines (665 loc) · 26.1 KB
/
jscript.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
// Developer >> Rom List >> Rom Details
//TODO: escape!
$(document).ready(function()
{
var devCookie = null;
var sortTypeCookie = null;
var sortOrderCookie = null;
//get device cookie
if(readCookie('deviceCookie'))
{
devCookie = readCookie('deviceCookie');
}
//get sort cookie
if(readCookie('sortCookie'))
{
sortTypeCookie = readCookie('sortCookie').split('_')[0];
sortOrderCookie = readCookie('sortCookie').split('_')[1];
}
var hash = window.location.hash;
var mainHash = null;
var devHash = null;
var romHash = null;
mainHash = hash.split('/')[0];
if (hash.split('/')[1])
devHash = hash.split('/')[1];
if (hash.split('/')[2])
romHash = hash.split(mainHash+"/"+devHash+"/")[1];
//reacts when the hash changes
window.addEventListener("hashchange", changeTab, false);
$("#devTab").addClass("selected");
// Arrays for holding info on devices, developers and ratings
var devices = null;
var developers = null;
var devRats = null;
var masterList = [];
//Grab device info
var uri = "http://gh-pages.clockworkmod.com/ROMManagerManifest/devices.js";
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(uri),
function(data)
{
devices = data.devices.sort(function(a, b) {
var A = a.name.toLowerCase(),
B = b.name.toLowerCase();
return sorthelper(0, A, B);
});
fillPage();
},
"jsonp"
);
//Get developers
manUri = 'http://gh-pages.clockworkmod.com/ROMManagerManifest/manifests.js';
$.get(
"http://jsonp.deployfu.com/clean/" + encodeURIComponent(manUri),
function(data)
{
developers = data.manifests.sort(function(a, b) {
var A = a.developer.toLowerCase(),
B = b.developer.toLowerCase();
return sorthelper(0, A, B);
});
fillPage();
},
"jsonp"
);
//Get dev's ratings
var ratUri = "http://rommanager.deployfu.com/v2/ratings";
$.get(
"http://jsonp.deployfu.com/clean/" + encodeURIComponent(ratUri),
function(data)
{
devRats = data.result;
fillPage();
},
"jsonp"
);
//============================================================================
//===========================<functions>======================================
//============================================================================
// Attempt to fill page with content
function fillPage()
{
if (devices == null || developers == null || devRats == null)
return;
//Fill masterList
for (dev in developers)
{
var devID = developers[dev].id;
if (devRats[devID])
{
var ratingInfo = devRats[devID];
var rating = 0;
if (ratingInfo)
{
if (ratingInfo.ratingCount)
rating = ratingInfo.totalRating / ratingInfo.ratingCount;
masterList.push({
"devName": developers[dev].developer,
"id": devID,
"icon": null,
"summary": developers[dev].summary,
"rating": rating,
"downloads": ratingInfo.downloadCount + ratingInfo.anonymousDownloadCount,
"utcMod": ratingInfo.lastModified
});
}
}
else
{
masterList.push({
"devName": developers[dev].developer,
"id": devID,
"icon": null,
"summary": developers[dev].summary,
"rating": 0,
"downloads": 0,
"utcMod": null
});
}
for (index in masterList)
{
for (dev in developers)
{
if (developers[dev].id == masterList[index].id)
masterList[index].icon = developers[dev].icon;
}
}
}
//Figure out what to do based on hash input
hashControl(mainHash, devHash, romHash);
// Sub header removes extra tabs
$('#removeXtraTabs').click(function(event){
removeXtraTabs();
})
//Button and dropdown logic
buttonMenuLogic();
}
//==============================hashParse()===================================
//Use to direct immediate links to the site
//If a nonsense link: just go through the normal processs
//If legit hashtag: go further.
function hashControl(main, dev, rom)
{
//load developers
fillDevTableBy("name", 0);
if(!(main=="#devInfo")&&!(main=="#romInfo"))
{
window.location.hash = "#developers";
return;
}
//load roms
var devExists=false;
for(i in developers)
{
if(developers[i].id == dev)
{
fillDevInfo(dev);
devExists = true;
if(rom)
{
var done = false;
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(developers[i].manifest),
function(data){
//load rom
for(j in data.roms)
{
if((rom == data.roms[j].modversion)||(rom == encodeURIComponent(data.roms[j].url))||(rom == data.roms[j].url))
{
fillRomInfo(dev, j);
done=true;
break;
}
}
if(done)
return;
else
{
window.location.hash=main+"/"+dev;
alert('Invalid rom.');
return;
}
},"jsonp");
}
}
}
if(!devExists)
{
alert('Invalid developer.');
window.location.hash="#developer";
}
}
//================================fillDevTableby()============================
function fillDevTableBy(what, updown)
{
if (what == "name")
sortbyName(updown);
else if (what == "rating")
sortbyRating(updown);
else if (what == "downloads")
sortbyDownloads(updown);
else if (what == "date")
sortbyUTC(updown);
giantString = '<table id="devTable" width="100%">'
var devFilter = '-'
if(document.getElementById('filter').value != '-')
devFilter = document.getElementById('filter').value;
for (i in masterList)
{
var devId = escape(masterList[i].id);
var devOk = false;
for (j in developers)
{
// Check if developer id works
if(developers[j].id == devId)
{
// Check if works with the device chosen
if ((developers[j].roms[devFilter])||(devFilter=="-")||(developers[j].roms['all']))
{
devOk = true;
break;
}
}
}
if(devOk)
{
giantString += '<tr id="devRow' + i + '"><td><a class="DEV" id ="devdev' + devId + '" href="#romInfo"><img class="devIcon" height=100 width=100 src =';
//Add the icon
if (masterList[i].icon)
giantString += masterList[i].icon + '>';
else
giantString += '"https://github.com/ClockworkMod/ajaxThing/raw/gh-pages/no_icon.png">';
// Add dev's name and description
giantString += masterList[i].devName + '</a><br>' + masterList[i].summary + '<br>';
// Get the rating
if (devRats[String(devId)])
{
// Add to list
theDev = devRats[String(devId)];
totalDL = theDev.anonymousDownloadCount + theDev.downloadCount;
//Issue with this?
if (masterList[i].utcMod) {
lastMod = new Date((masterList[i].utcMod) * 1000);
var month = lastMod.getMonth() + 1;
var day = lastMod.getDate();
var year = lastMod.getFullYear();
lastMod = month + "/" + day + "/" + year;
}
else
lastMod = "Never Modified";
//Add the rating
if (theDev.ratingCount)
{
var rating = theDev.totalRating / theDev.ratingCount;
giantString += '<div class = "jRating" data = "' + parseInt(4 * rating) + '"></div><span style="padding-left:30px"></span>';
}
else
giantString += '<div class="filler">0</div>Not Rated<span style="padding-left:30px"></span>';
// Add the number of downloads and the last modified date
giantString += '(' +totalDL + ' Downloads)<span style="padding-left:30px"></span> ' + lastMod + '</td></tr>';
}
else
{
giantString += '<div class="filler">0</div>Not Rated<span style="padding-left:30px"></span>(0 Downloads) <span style="padding-left:30px"></span>Never Modified</td></tr>';
}
}
}
$("#devTable").remove();
$("#devListing").append(giantString + "</table>");
callJrating();
// Clicking developer name should create new tab for his roms,
// hide the developer tab, and show the new tab
$("a.DEV").click(function(event)
{
event.preventDefault();
var devId = this.id.split("devdev")[1];
//Fill in developer tab data
fillDevInfo(devId);
});
$("#devTab").click(function(event)
{
$("#devInfo").remove();
$("#romListItemTab").remove();
$("#romInfoListItem").remove();
$("#romListTab").remove();
$("#romInfo").remove();
$("#romInfoTab").remove();
$("div.tabContent").addClass("hide");
$("a.tabItem").removeClass("selected");
$("div.developers").removeClass("hide");
$("#devTab").addClass("selected");
});
}
//============================fillDevInfo()===================================
function fillDevInfo(devId)
{
// Change address bar hash
window.location.hash = "devInfo/" + devId;
var devIndex = 0;
for (i in developers)
{
if(developers[i].id==devId)
{
devIndex = i;
break;
}
}
$("#devInfo").remove();
$("#romListItemTab").remove();
$("#romInfoListItem").remove();
$("#romListTab").remove();
$("#romInfo").remove();
$("#romInfoTab").remove();
$("div.developers").addClass("hide");
$(".tabItem").removeClass("selected");
$('#tabs').append('<li id="romListItemTab"><a id = "romListTab" class = "tabItem selected">' + developers[devIndex].developer + '</a></li>');
$('.newTab').append('<div class = "tabContent romList" id = "devInfo"></div>');
// Controls for clicking the rom list tab
$("#romListTab").click(function(event)
{
$("div.tabContent").addClass("hide");
$("a.tabItem").removeClass("selected");
$("div.romList").removeClass("hide");
$("#romListTab").addClass("selected");
});
//Get icon and summary
if (developers[devIndex].icon)
$("#devInfo").append('<img height = 100 width = 100 src = "' + developers[devIndex].icon + '"><p>' + developers[devIndex].summary + '</p><table id="romListTable" width="100%"></table>');
else
$("#devInfo").append('<img height = 100 width = 100 src = "https://github.com/ClockworkMod/ajaxThing/raw/gh-pages/no_icon.png"><p>' + developers[devIndex].summary + '</p><table id="romListTable" width="100%"></table>');
// List the roms
// Clicking a rom will create a new tab with ratings and a download option
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(developers[devIndex].manifest),
function(data)
{
var giantRomList = "";
if (data.roms && data.roms[0])
{
urlThing = "http://rommanager.deployfu.com/v2/ratings/";
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(urlThing)+devId,
function(rData)
{
$.each(data.roms,
function(i, val)
{
var devOptions = document.getElementById('filter').value;
var romRating = "Not Rated";
var romDownloads = 0;
if (val.visible == null)
{
if ((devOptions == '-') || (val.device == devOptions) || val.device == 'all')
{
giantRomList += '<tr><td><a class = "ROM" id = "' + devId + "___" + i + '" href="#romInfo">' + val.name + '</a><br>' + val.summary;
if (rData.result[val.modversion])
{
romRating = rData.result[val.modversion].rating;
romDownloads = rData.result[val.modversion].downloads;
if(rData.result[val.modversion].ratingcount)
giantRomList += '<br><div class="jRating" data="'+ parseInt(romRating*4)+'"></div>';
else
giantRomList += '<br>Not Rated';
giantRomList += '<span style="padding-left:30px"></span>(' + romDownloads;
}
else
{
giantRomList += '<br>Not Rated<span style="padding-left:30px"></span>(0';
}
giantRomList += ' Downloads)</td></tr>';
}
}
});
$("#romListTable").append(giantRomList);
callJrating();
$("a.ROM").click(function(event)
{
event.preventDefault();
var romIndex = parseInt(this.id.split("___")[1]);
fillRomInfo(devId, romIndex);
});
},"jsonp");
}
else
{
giantRomList += '<tr><td>No roms available.</td></tr>';
$("#romListTable").append(giantRomList);
}
},"jsonp");
}
//=============================fillRomInfo()==================================
function fillRomInfo(devID, romIndex)
{
$("#romInfo").remove();
$("#romInfoTab").remove();
$("#romInfoListItem").remove();
$("div.romList").addClass("hide");
$(".tabItem").removeClass("selected");
var devIndex = 0;
var romName = null;
var modV = null;
for (i in developers)
{
if (devID == developers[i].id)
devIndex = i;
}
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(developers[devIndex].manifest),
function(data)
{
romName = String(data.roms[romIndex].name);
modV = String(data.roms[romIndex].modversion);
//Create the tab
$('#tabs').append('<li id="romInfoListItem"><a id = "romInfoTab" class = "tabItem selected">' + romName + '</a></li>');
// Controls for clicking the rom list tab
$("#romInfoTab").click(function(event)
{
$("div.tabContent").addClass("hide");
$("a.tabItem").removeClass("selected");
$("div.romInfo").removeClass("hide");
$("#romInfoTab").addClass("selected");
});
if(modV)
window.location.hash = "romInfo/" + developers[devIndex].id + "/" + modV;
else
window.location.hash = "romInfo/" + developers[devIndex].id + "/" + encodeURIComponent(data.roms[romIndex].url);
// Tab content starts here
$('.newTab').append('<div class = "tabContent romInfo" id = "romInfo"></div>');
var romInfoString = '<center><a href="' + data.roms[romIndex].url + '">Download ROM Here</a><br><br>';
if (data.roms[romIndex].screenshots)
{
var i = 0;
while (data.roms[romIndex].screenshots[i])
{
romInfoString += '<img height=300 width=200 src=' + data.roms[romIndex].screenshots[i] + '> ';
if ((i > 0) && (i % 2))
romInfoString += '<br>';
i++;
}
}
romInfoString += '</center>';
// Rating & number of downloads
var romRatUri = "http://rommanager.deployfu.com/v2/ratings/";
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(romRatUri + developers[devIndex].id),
function(xdata)
{
var ratImage = null;
var romID = null
var rating = null;
if (xdata.result[romName])
romID = romName;
else if (xdata.result[modV])
romID = modV;
else if (xdata.result[modV.toUpperCase()])
romID = modV.toUpperCase();
if (romID)
{
rating = xdata.result[romID].rating.toFixed(1);
if (xdata.result[romName])
romInfoString += '<div class = "jRating" data = "' + parseInt(4 * rating) + '"></div><br>' + xdata.result[romName].downloads + ' Downloads<br><br><h2 class="blogger">Comments:</h2>';
else if (xdata.result[modV])
romInfoString += '<div class = "jRating" data = "' + parseInt(4 * rating) + '"></div><br>' + xdata.result[modV].downloads + ' Downloads<br><br><h2 class="blogger">Comments:</h2>';
else
romInfoString += '<div class = "jRating" data = "' + parseInt(4 * rating) + '"></div><br>' + xdata.result[modV.toUpperCase()].downloads + ' Downloads<br><h2 class="blogger">Comments:</h2>';
$("#romInfo").append(romInfoString);
// Comments
var commentUri = "http://rommanager.deployfu.com/ratings/";
$.get("http://jsonp.deployfu.com/clean/" + encodeURIComponent(commentUri + developers[devIndex].id + '/' + modV),
function(ydata)
{
$.each(ydata.result.comments,
function(j, com) {
var rating = com.rating;
$("#romInfo").append('<hr><strong>' + com.nickname + '</strong><div class = "jRating comments" data = "' + parseInt(4 * rating) + '"></div><br><i>' + com.comment + '</i><br>');
callJrating();
});
},
"jsonp"
);
}
else
{
$("#romInfo").append("<center>Sorry, no information can be found about this rom.</center>");
}
},
"jsonp"
);
},
"jsonp"
);
}
//==============================readCookie()==================================
function readCookie(name)
{
name = name.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
var regExp = new RegExp('(?:^|;)\\s?' + name + '=(.*?)(?:;|$)','i'),
match = document.cookie.match(regExp);
return match && unescape(match[1]);
}
//=============================callJrating()==================================
function callJrating()
{
$('.jRating').jRating({
step: false,
// no step
length: 5,
// show 5 stars at the init
isDisabled: true,
decimalLength: 1,
// show small stars instead of big default stars
bigStarsPath: 'stars_gray.png'
});
}
//============================buttonsMenuLogic()===============================
//Put in logic for dropdown menus and buttons
function buttonMenuLogic()
{
// Fill drop down list
$.each(devices,
function(i, val)
{
$("select.filter").append('<option value = "' + val.key + '">' + val.name + '</option>');
});
if(devCookie)
{
document.getElementById('filter').value = devCookie;
$.each(developers,
function(i, val)
{
var usesDevice = false;
// Check to see if developer supports device
$.each(val.roms,
function(j, rList)
{
// Add class to hide developers that don't support the device
if (j == devCookie)
usesDevice = true;
});
if (!usesDevice)
$("#devRow" + i).addClass("hideDev");
});
}
if(sortTypeCookie && sortOrderCookie)
{
document.getElementById('sorty').value = sortTypeCookie;
document.getElementById('updown').value = sortOrderCookie;
fillDevTableBy(sortTypeCookie, parseInt(sortOrderCookie));
}
// Clicking items in drop down will narrow down developer list
$("select.filter").change(function(event)
{
fillDevTableBy(sortTypeCookie, sortOrderCookie);
var listVal = String(document.getElementById('filter').value);
document.cookie = 'deviceCookie='+ listVal;
if (listVal != "-")
{
$.each(developers,
function(i, val)
{
var usesDevice = false;
// Check to see if developer supports device
$.each(val.roms,
function(j, rList)
{
// Add class to hide developers that don't support the device
if (j == listVal)
usesDevice = true;
});
if (!usesDevice)
$("#devRow" + i).addClass("hideDev");
});
}
});
$("input.allButton").click(function(event) {
document.cookie = 'deviceCookie=-';
document.cookie = 'sortCookie=name_0';
document.getElementById('filter').value = '-';
document.getElementById('sorty').value = "name";
document.getElementById('updown').value = 0;
fillDevTableBy('name', 0);
});
// New Dropdown bar needs to filter table contents
// Clicking items in drop down will narrow down developer list
$("input.sortButton").click(function(event) {
var listVal = String(document.getElementById('sorty').value);
var updown = parseInt(document.getElementById('updown').value);
document.cookie = 'sortCookie='+listVal+"_"+updown;
fillDevTableBy(listVal, updown);
});
}
//==============================changeTab()===================================
function changeTab()
{
var hash = window.location.hash.split("/")[0];
if (hash == "#romInfo")
{
$(".tabContent").addClass("hide");
$(".tabItem").removeClass("selected");
$("div.romInfo").removeClass("hide");
$("#romInfoListItem").addClass("selected");
}
else if (hash == "#developers")
{
$(".tabContent").addClass("hide");
$(".tabItem").removeClass("selected");
$("#devListing").removeClass("hide");
$("#devTab").addClass("selected");
}
}
//===============================removeXtraTabs()=============================
function removeXtraTabs()
{
$('#devInfo').remove();
$('#romListItemTab').remove();
$('#romInfo').remove();
$('#romInfoListItem').remove();
}
//===============================Sorting Functions============================
function sorthelper(updown, A, B)
{
if (updown)
{
if (A < B)
return 1
if (A > B)
return - 1
}
else
{
if (A < B)
return - 1
if (A > B)
return 1
}
//default return value (no sorting)
return 0
}
function sortbyName(updown)
{
masterList = masterList.sort(function(a, b) {
var A = a.devName.toLowerCase(),
B = b.devName.toLowerCase();
return sorthelper(updown, A, B);
});
}
function sortbyRating(updown)
{
masterList = masterList.sort(function(a, b) {
var A = a.rating,
B = b.rating;
return sorthelper(updown, A, B);
});
}
function sortbyDownloads(updown)
{
masterList = masterList.sort(function(a, b) {
var A = a.downloads,
B = b.downloads;
return sorthelper(updown, A, B);
});
}
function sortbyUTC(updown)
{
masterList = masterList.sort(function(a, b) {
var A = a.utcMod,
B = b.utcMod;
return sorthelper(updown, A, B);
});
}
});