Loading ...
');
279 | HTML_TPL.shipBox = $('').load('https://sophie-la-li.github.io/sc-fleet-info/VERSION', function (response, status) {
943 | if (response.trim() === VERSION) return;
944 | updateLink.find('.label').text('v' + VERSION + ' - Update available!');
945 | });
946 | }
947 |
948 | return fleetList;
949 | };
950 |
951 | // GROUPING AND SORTING ##########################################################################
952 |
953 | const sortObjectByKeys = function(object)
954 | {
955 | return Object.keys(object).sort().reduce((a, c) => (a[c] = object[c], a), {});
956 | };
957 |
958 | const sortObjectArrayByProperty = function(array, propertyName)
959 | {
960 | return array.sort((a,b) => (a[propertyName] > b[propertyName]) ? 1 : ((b[propertyName] > a[propertyName]) ? -1 : 0));
961 | };
962 |
963 | const sortDomArrayByContents = function(array, className)
964 | {
965 | array.sort(function(a,b){
966 | let aText = $(a).find(className).text();
967 | let bText = $(b).find(className).text();
968 | let aSortBack = $(a).hasClass('skSortBack');
969 | let bSortBack = $(b).hasClass('skSortBack');
970 | if (aSortBack && !bSortBack) return 1;
971 | if (!aSortBack && bSortBack) return -1;
972 | if (aText > bText) return 1;
973 | if (aText < bText) return -1;
974 | return 0;
975 | });
976 | return array;
977 | };
978 |
979 | const groupAndSortBoxes = function(list, groupOn, sortBy)
980 | {
981 | list.find('.skGroupHeaderBox').remove();
982 | const FALLBACK_GROUP = 'ZZZ';
983 | let grouped = {};
984 |
985 | $.each(list.find('.skShipBox'), function(i, box) {
986 | let group = (groupOn === null ? '' : $(box).find(groupOn).text());
987 | if (group.length === 0) group = FALLBACK_GROUP;
988 | if (grouped[group] === undefined) grouped[group] = [];
989 | grouped[group].push(box);
990 | });
991 |
992 | let groupCount = Object.keys(grouped).length;
993 | if (groupCount > 1) grouped = sortObjectByKeys(grouped);
994 |
995 | $.each(grouped, function(group, boxes) {
996 | if (groupCount > 1) {
997 | let header = HTML_TPL.groupHeaderBox.clone();
998 | if (group === FALLBACK_GROUP) group = 'Other';
999 | header.text(group);
1000 | list.append(header);
1001 | }
1002 |
1003 | boxes = sortDomArrayByContents(boxes, sortBy);
1004 |
1005 | $.each(boxes, function(i, box) {
1006 | list.append(box);
1007 | });
1008 | });
1009 | }
1010 |
1011 | // LOAD DATA AND TRIGGER RENDER ##################################################################
1012 |
1013 | let dataLoading = false;
1014 | let dataLoaded = false;
1015 | const loadData = function(page, callback)
1016 | {
1017 | if (dataLoaded) return callback();
1018 | dataLoading = true;
1019 |
1020 | const url = '/en/account/pledges?pagesize=50&page=' + page;
1021 | const $page = $('
');
1022 | $page.load(url + ' .page-wrapper', function (response, status) {
1023 | if ($('.list-items .empy-list', this).length > 0) {
1024 | dataLoaded = true;
1025 | dataLoading = false;
1026 | return callback();
1027 | }
1028 | processPledges(this);
1029 | loadData(page+1, callback);
1030 | });
1031 | };
1032 |
1033 | const renderEquipmentPage = function()
1034 | {
1035 | if (dataLoading) return;
1036 | $('div.sidenav ul li').removeClass('active');
1037 | $('.showEquipmentButton').addClass('active');
1038 | let fleetList = preparePage("MY EQUIPMENT");
1039 | loadData(1, function(){
1040 | logDebugInfo();
1041 | renderEquipment(fleetList);
1042 | addPageFormSelect('Group by', AVAILABLE_EQUIPMENT_GROUPS, SFI_DEFAULT_EQUIPMENT_GROUP, function() {
1043 | let groupOn = (this.value === 'null' ? null : this.value);
1044 | groupAndSortBoxes(fleetList, groupOn, '.skModelBox');
1045 | });
1046 | });
1047 | };
1048 |
1049 | const renderFleetPage = function()
1050 | {
1051 | if (dataLoading) return;
1052 | $('div.sidenav ul li').removeClass('active');
1053 | $('.showFleetsButton').addClass('active');
1054 | let fleetList = preparePage("MY FLEET");
1055 | loadData(1, function(){
1056 | logDebugInfo();
1057 | renderFleet(fleetList);
1058 | addPageFormSelect('Group by', AVAILABLE_FLEET_GROUPS, SFI_DEFAULT_FLEET_GROUP, function() {
1059 | let groupOn = (this.value === 'null' ? null : this.value);
1060 | groupAndSortBoxes(fleetList, groupOn, '.skModelBox');
1061 | });
1062 | addPageFormCheckbox('Show unassigned paints card', function() {
1063 | $('.skUnassignedPaints').toggle();
1064 | }, SFI_DEFAULT_SHOW_UNASSIGNED_PAINTS_CARD);
1065 | if (SFI_DEFAULT_SHOW_UNASSIGNED_PAINTS_CARD == false) {
1066 | $('.skUnassignedPaints').toggle();
1067 | }
1068 | });
1069 | };
1070 |
1071 | let debugInfoLogged = false;
1072 | const logDebugInfo = function()
1073 | {
1074 | if (debugInfoLogged) return;
1075 | console.log(skins);
1076 | console.log(upgrades);
1077 | console.log(ships);
1078 | console.log(equipment);
1079 | debugInfoLogged = true;
1080 | }
1081 |
1082 | // INJECT NAVIGATION AND STYLESHEET ##############################################################
1083 |
1084 | let nav = $('div.sidenav ul li');
1085 | if (nav.length > 0) {
1086 | let newNav = $(nav[0]).clone();
1087 | newNav.removeClass('active');
1088 | newNav.addClass('showEquipmentButton');
1089 | newNav.find('.bg').text('MY EQUIPMENT');
1090 | newNav.find('a').attr('href', 'javascript:void(0)');
1091 | newNav.find('a').click(renderEquipmentPage);
1092 | $('div.sidenav ul').prepend(newNav);
1093 |
1094 | let newNavB = $(nav[0]).clone();
1095 | newNavB.removeClass('active');
1096 | newNavB.addClass('showFleetsButton');
1097 | newNavB.find('.bg').text('MY FLEET');
1098 | newNavB.find('a').attr('href', 'javascript:void(0)');
1099 | newNavB.find('a').click(renderFleetPage);
1100 | $('div.sidenav ul').prepend(newNavB);
1101 | }
1102 |
1103 | // CIG seams to store the pagesize in session.
1104 | // Since we overwrite it with 1 in pledge deeplinks,
1105 | // we have to fix it in all other links to the pledges list.
1106 | $("a").each((index, element) => {
1107 | let href = $(element).attr('href');
1108 | if (href != undefined && href.search(/^\/en\/account\/pledges/i) != -1) {
1109 | href = href.replace(/(\&|)pagesize\=\d+(\&|)/, "");
1110 | let delim = '?';
1111 | if (href.search(/\?/) != -1) delim = '&';
1112 | $(element).attr('href', href + delim + 'pagesize=' + SFI_PLEDGE_LIST_PAGE_SIZE);
1113 | }
1114 | });
1115 |
1116 | $('head').append('');
1117 | });
1118 |
--------------------------------------------------------------------------------