Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sortGame_OnClick() {
- sortMode = 1;
- sortEntries();
- }
- function sortSystem_OnClick() {
- sortMode = 2;
- sortEntries();
- }
- function sortRating_OnClick() {
- sortMode = 3;
- sortEntries();
- }
- var systemList = [
- 'Nintendo NES', // 0
- 'Nintendo SNES', // 1
- 'Nintendo 64', // 2
- 'Nintendo GameCube', // 3
- 'Nintendo Wii', // 4
- 'Nintendo Wii U', // 5
- 'Nintendo Game Boy', // 6
- 'Nintendo Game Boy Color', // 7
- 'Nintendo Game Boy Advance',// 8
- 'Nintendo DS', // 9
- 'Nintendo 3DS', // 10
- 'Sega Master System', // 11
- 'Sega Genesis', // 12
- 'Sega CD', // 13
- 'Sega 32x', // 14
- 'Sega Saturn', // 15
- 'Sega Dreamcast', // 16
- 'Sega Game Gear', // 17
- 'Playstation 1', // 18
- 'Playstation 2 ', // 19
- 'Playstation 3', // 20
- 'Playstation 4', // 21
- 'Playstation Portable', // 22
- 'Playstation Vita', // 23
- 'Xbox', // 24
- 'Xbox 360', // 25
- 'Xbox One', // 26
- 'PC', // 27
- 'Other' // 28
- ];
- var ratingMaList = [
- 'A',
- 'B',
- 'C',
- 'D',
- 'E',
- 'F'
- ];
- var ratingMiList = [
- '+',
- '',
- '-'
- ];
- var entries = [];
- var lastSortMode = sortMode = 0;
- var sortListA = [
- '',
- 'game',
- 'system',
- 'ratingmi'
- ]
- var sortListB = [
- '',
- '',
- '',
- 'ratingma'
- ]
- var elementList = [
- '',
- '#sortGameA',
- '#sortSystemA',
- '#sortRatingA'
- ]
- function sortEntries() {
- var sortA = sortListA[sortMode];
- var sortB = sortListB[sortMode];
- if (typeof entries[0][sortA] == 'string') {
- if (lastSortMode != sortMode) {
- entries.sort(function(b, a){
- if(a[sortA] > b[sortA]) return -1;
- if(a[sortA] < b[sortA]) return 1;
- return 0;
- });
- } else {
- entries.sort(function(a, b){
- if(a[sortA] > b[sortA]) return -1;
- if(a[sortA] < b[sortA]) return 1;
- return 0;
- });
- }
- } else {
- if (lastSortMode != sortMode) {
- if (sortB != '') {
- entries.sort(function(a, b) {
- if (a[sortB] == b[sortB]) {
- return (a[sortA] < b[sortA]) ? -1 : (a[sortA] > b[sortA]) ? 1 : 0;
- } else {
- return (a[sortB] < b[sortB]) ? -1 : 1;
- }
- });
- } else {
- entries.sort(function(a, b){
- return a[sortA]-b[sortA];
- });
- }
- } else {
- if (sortB != '') {
- entries.sort(function(b, a) {
- if (a[sortB] == b[sortB]) {
- return (a[sortA] < b[sortA]) ? -1 : (a[sortA] > b[sortA]) ? 1 : 0;
- } else {
- return (a[sortB] < b[sortB]) ? -1 : 1;
- }
- });
- } else {
- entries.sort(function(b, a){
- return a[sortA]-b[sortA];
- });
- }
- }
- }
- headerRefresh();
- if (lastSortMode == sortMode) { sortMode = 0; }
- lastSortMode = sortMode;
- entriesRefresh();
- }
- function headerRefresh() {
- for (var i=0;i<elementList.length;i++) {
- $(elementList[i]).css('visibility','hidden');
- }
- $(elementList[sortMode]).css('visibility','visible');
- if (sortMode == lastSortMode) {
- $(elementList[sortMode]).text('▲');
- } else {
- $(elementList[sortMode]).text('▼');
- }
- }
- $(document).ready(function() {
- for (var i=0;i<systemList.length;i++) {
- $('#systemSelect').append('<option value="' + i + '">' + systemList[i] + '</option>');
- }
- for (var i=0;i<ratingMaList.length;i++) {
- $('#ratingMaSelect').append('<option value="' + i + '">' + ratingMaList[i] + '</option>');
- }
- for (var i=0;i<ratingMiList.length;i++) {
- $('#ratingMiSelect').append('<option value="' + i + '">' + ratingMiList[i] + '</option>');
- }
- $("input[type=checkbox][value=all]").change(function(){checkboxAll()});
- $('#buttonEdit').prop('disabled',true);
- newFileInput();
- headerRefresh();
- });
- function newFileInput() {
- $('#fileinputcont').html('<input id="fileinput" type="file" accept=".json">')
- $('#fileinput').change(function(e) {
- var file = $('#fileinput')[0].files[0];
- var reader = new FileReader();
- reader.onload = function(e) {
- if (confirm("Are you sure you want to overwrite the current table?")) {
- entries = JSON.parse(reader.result);
- entriesRefresh();
- lastSortMode = sortMode = 0;
- headerRefresh();
- }
- }
- reader.readAsText(file);
- newFileInput();
- });
- }
- function entriesAdd() {
- entries.push({
- game: $('#gameSelect').val().toString(),
- system: parseInt($('#systemSelect').val()),
- ratingma: parseInt($('#ratingMaSelect').val()),
- ratingmi: parseInt($('#ratingMiSelect').val())
- });
- entriesRefresh();
- checkboxAll();
- lastSortMode = sortMode = 0;
- headerRefresh();
- }
- function entriesRefresh() {
- $('#entries').html('');
- for (var i=0;i<entries.length;i++) {
- $('#entries').append('<tr class="entry" id="tr' + i + '"><td style="width:4%"><input type="checkbox" value="' + i + '"></td><td style="width:32%">' + entries[i].game + '</td><td style="width:32%">' + systemList[entries[i].system] + '</td><td style="width:32%">' + ratingMaList[entries[i].ratingma] + ratingMiList[entries[i].ratingmi] + '</td></tr>');
- $("input[type=checkbox][value=" + i + "]").change(function(){checkboxAny()});
- }
- }
- function saveAsJSON() {
- // http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a-text-file-in-html5-using-javascrip/
- var textFileAsBlob = new Blob([JSON.stringify(entries)], {type:'text/plain'});
- var fileNameToSaveAs = "gamelist.json";
- var downloadLink = document.createElement("a");
- downloadLink.download = fileNameToSaveAs;
- downloadLink.innerHTML = "Download File";
- if (window.webkitURL != null)
- {
- // Chrome allows the link to be clicked
- // without actually adding it to the DOM.
- downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
- }
- else
- {
- // Firefox requires the link to be added to the DOM
- // before it can be clicked.
- downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
- downloadLink.onclick = destroyClickedElement;
- downloadLink.style.display = "none";
- document.body.appendChild(downloadLink);
- }
- downloadLink.click();
- }
- function loadClick() {
- $('#loadfile').trigger('click')
- };
- function deleteSelected() {
- for (var i=entries.length;i>=0;i--) {
- if ($("input[type=checkbox][value=" + i + "]").prop("checked")) {
- entries.splice(i,1);
- }
- }
- entriesRefresh();
- lastSortMode = sortMode = 0;
- headerRefresh();
- }
- function editButton_onClick() {
- for (var i=0;i<entries.length;i++) {
- if ($("input[type=checkbox][value=" + i + "]").prop("checked")) {
- $('#gameSelect').val(entries[i].game);
- $('#systemSelect').val(entries[i].system);
- $('#ratingMaSelect').val(entries[i].ratingma);
- $('#ratingMiSelect').val(entries[i].ratingmi);
- entries.splice(i,1);
- entriesRefresh();
- checkboxAny();
- }
- }
- }
- function checkboxAll() {
- for (var i=0;i<entries.length;i++) {
- $("input[type=checkbox][value=" + i + "]").prop("checked",$("input[type=checkbox][value=all]").prop("checked"));
- }
- checkboxAny();
- }
- function checkboxAny() {
- var checkedAmount = 0;
- for (var i=0;i<entries.length;i++) {
- checkedAmount += $("input[type=checkbox][value=" + i + "]").prop("checked");
- }
- $('#buttonEdit').prop('disabled',(checkedAmount == 0) || (checkedAmount > 1));
- if (entries.length != 0) {
- $("input[type=checkbox][value=all]").prop("checked",checkedAmount == entries.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement