Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/cap.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ $("*").click(function() {
function cal_cap() {

let mods_taken_raw = JSON.parse(localStorage.getItem('persist:planner'));
let all_mods_info_raw = JSON.parse(localStorage.getItem('persist:moduleBank'));
let modcodes_array = get_modcodes_array(mods_taken_raw);
// console.log(modcodes_array);

Expand All @@ -47,10 +46,10 @@ function cal_cap() {

try {
let mods_taken_grade = JSON.parse(localStorage.getItem('YSL:data'));
let all_mods_info_raw = JSON.parse(localStorage.getItem('persist:moduleBank'));
let mods_taken_info_cap = get_mods_taken_info(modcodes_array, all_mods_info_raw, mods_taken_grade);

// console.log(mods_taken_info_cap);

let cap = calculate_cap(mods_taken_info_cap);
return cap;
} catch(err) {
Expand Down Expand Up @@ -93,10 +92,32 @@ function update_YSL_data(modcodes_array) {
console.log("Error in updating YSL data! have modules:" + modcodes_array + "but data not present in YSL data");
} else {
localStorage.setItem('YSL:data', JSON.stringify(new_mods_taken_grade));
SULeft();
}
}


// same function as the one in grade.js
function SULeft() {
const ysl_data = JSON.parse(localStorage.getItem('YSL:data'));
if (!ysl_data) {
localStorage.setItem('YSL:SUleft', total_SU);
return total_SU;
}
const modules = JSON.parse(JSON.parse(localStorage.getItem('persist:moduleBank'))['modules']);
// console.log("su_left: "+count);
// console.log(ysl_data);
// const su_left = total_SU - Object.keys(ysl_data).reduce((acc, cur) => acc + ysl_data[cur]['su'] ? modules[cur]['moduleCredit'] : 0, 0);
let su_left = total_SU;
for(let mod_code in ysl_data) {
if (ysl_data[mod_code]['su']) {
su_left -= modules[mod_code]['moduleCredit'];
}
}
localStorage.setItem('YSL:SUleft', su_left);
// console.log('......' + su_left);
// count++;
return su_left;
}

function get_modcodes_array(mods_taken_raw) {
let res = [];
Expand All @@ -117,12 +138,19 @@ function get_mods_taken_info(modcodes_array, all_mods_info_raw, mods_taken_grade
for (let i = 0; i < modcodes_array.length; i++) {
let mod_code = modcodes_array[i];
let mc, grade, su;
if (all_mods_info.hasOwnProperty(mod_code)) {
// if (all_mods_info.hasOwnProperty(mod_code)) {
// mc = parseInt(all_mods_info[mod_code]['moduleCredit']);
// } else {
// console.log('Error: CAP calculator, no MC info for' + mod_code);
// mc = 0;
// }
if (all_mods_info[mod_code]) {
mc = parseInt(all_mods_info[mod_code]['moduleCredit']);
} else {
console.log('Error: CAP calculator, no MC info for' + mod_code);
mc = 0;
}

if (mods_taken_grade.hasOwnProperty(mod_code)) {
grade = mods_taken_grade[mod_code]['grade'];
if (grade === "") {
Expand Down Expand Up @@ -217,9 +245,9 @@ function calculate_cap(courses) {

// the followings functions involve the SUs
function get_su(){
if (localStorage.getItem('YSL:SUleft') === null) {
if (localStorage.getItem('YSL:SUleft') === null ) {
return "32";
}
}
return ""+localStorage.getItem('YSL:SUleft');
}

Expand Down
32 changes: 31 additions & 1 deletion src/grade.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
$(document).ready(function () {
add_editModules_listener();
add_ModuleCards_listener();
// add_Remove_listener();

add_Click_listener();

});

function add_editModules_listener() {
Expand Down Expand Up @@ -39,6 +42,19 @@ function add_Click_listener() {
});
}

function add_Remove_listener() {
$("body").click(function () {
$("button:contains('Remove')").click(
checkSU_when_remove
);
// console.log("removed one mod, su rebalanced");
});
}
function checkSU_when_remove() {
setTimeout(function() {
SULeft();
}, 50);
}
function toggleSU() {
console.log($(this).find("strong").text());
const mod_code = $(this).find("strong").text();
Expand Down Expand Up @@ -196,14 +212,23 @@ function updateEditModal() {

var total_SU = 32;

// var count = 0;
function SULeft() {
const ysl_data = JSON.parse(localStorage.getItem('YSL:data'));
if (!ysl_data) {
localStorage.setItem('YSL:SUleft', total_SU);
return total_SU;
}
const modules = JSON.parse(JSON.parse(localStorage.getItem('persist:moduleBank'))['modules']);
const su_left = total_SU - Object.keys(ysl_data).reduce((acc, cur) => acc + ysl_data[cur]['su'] ? modules[cur]['moduleCredit'] : 0, 0);
// console.log("su_left: "+count);
// console.log(ysl_data);
// const su_left = total_SU - Object.keys(ysl_data).reduce((acc, cur) => acc + ysl_data[cur]['su'] ? modules[cur]['moduleCredit'] : 0, 0);
let su_left = total_SU;
for(let mod_code in ysl_data) {
if (ysl_data[mod_code]['su']) {
su_left -= modules[mod_code]['moduleCredit'];
}
}
localStorage.setItem('YSL:SUleft', su_left);
return su_left;
}
Expand Down Expand Up @@ -241,7 +266,12 @@ function addSUSelector(mod, grade) {
}

function showDropdownList() {
// setTimeout(function () {
// // console.log('new modal!!!');
// updateEditModal();
// }, 0);
setTimeout(function () {
updateEditModal();
}, 0);

}