can import and export courses w/ json file

This commit is contained in:
sghsri
2018-12-11 17:36:17 -06:00
parent b4f19a0c61
commit 0eb7c9b949
3 changed files with 176 additions and 46 deletions

View File

@@ -1,50 +1,45 @@
var courses;
// get the courses from storage
chrome.storage.sync.get('savedCourses', function (data) {
//find, build, and show the messages for the conflicts in the saved courses
chrome.runtime.sendMessage({
command: "checkConflicts"
}, function (response) {
var isConflicted = [];
if (response.isConflict) {
var between = response.between;
var text = "";
for (var i = 0; i < between.length; i++) {
text += "CONFLICT: " + getSimpleName(between[i][0].coursename, between[i][0].unique) + " and " + getSimpleName(between[i][1].coursename, between[i][1].unique);
isConflicted.push(between[i][0].unique);
isConflicted.push(between[i][1].unique);
if (i != between.length - 1) {
text += "<br>";
}
setCourseList();
// var modhtml = '<div class=modal id=myModal><div class=modal-content><span class=close>×</span><div class=card><div class=cardcontainer></div></div></div></div>';
// $("#html").prepend(modhtml);
function setCourseList(){
$("#courseList").empty()
chrome.storage.sync.get('savedCourses', function (data) {
updateConflicts();
courses = data.savedCourses
if (courses.length != 0) {
$("#empty").hide();
$("#courseList").show();
} else{
$("#empty").show();
}
// build and append the course list element
for (var i = 0; i < courses.length; i++) {
var color;
status = courses[i].status;
if (status.includes("open")) {
color = "#4CAF50";
} else if (status.includes("waitlisted")) {
color = "#FF9800"
} else if (status.includes("closed") || status.includes("cancelled")) {
color = "#FF5722";
}
$("#courseList").prepend("<p style='font-size:small; font-weight:bold; color:red; margin:5px 5px 5px 10px'>" + text + "</>");
var department = courses[i].coursename.substring(0, courses[i].coursename.search(/\d/) - 2);
var course_nbr = courses[i].coursename.substring(courses[i].coursename.search(/\d/), courses[i].coursename.indexOf(" ", courses[i].coursename.search(/\d/)));
var profname = prettifyName(courses[i].profname);
if (profname == "") {
profname = "Undecided Professor";
}
var listhtml = "<li id='" + i + "'style='padding: 0px 5px 5px 5px; overflow-y: auto;max-height:400px;'><div class='card'><div class='container' style='background:" + color + "''><h4 class='truncate' style='color:white;margin:5px; display:inline-block;font-size:large;'><b>" + department + " " + course_nbr + "<span style='font-size:medium'>" + " with </span><span style='font-size:medium'>" + profname + " (" + courses[i].unique + ")" + "</span></b></h4><p id='arrow' style='float:right;font-size:small;display:inline-block;margin-top:10px;color:white;'>&#9658;</p></div></div><div id='moreInfo' style='display: none;'><p style='font-weight:bold;padding:10px;margin:0px 5px 0px 15px;font-size:small;background-color:#FFCDD2;'>" + makeLine(i) + "</p><div id='infoButtons' style='border-radius:0px;'><button class='matbut' id='listRemove'style='float:right;background:#F44336; margin:5px;'>Remove</button><button class='matbut' id='register' style='float:right;background:#4CAF50; margin:5px;'>Register</button><button class='matbut' id='listMoreInfo' style='float:right;background:#2196F3; margin:5px;'>More Info</button></div></div></li>";
$("#courseList").append(listhtml);
}
});
courses = data.savedCourses;
if (courses.length != 0) {
$("#empty").hide();
}
// build and append the course list element
for (var i = 0; i < courses.length; i++) {
var color;
status = courses[i].status;
if (status.includes("open")) {
color = "#4CAF50";
} else if (status.includes("waitlisted")) {
color = "#FF9800"
} else if (status.includes("closed") || status.includes("cancelled")) {
color = "#FF5722";
}
var department = courses[i].coursename.substring(0, courses[i].coursename.search(/\d/) - 2);
var course_nbr = courses[i].coursename.substring(courses[i].coursename.search(/\d/), courses[i].coursename.indexOf(" ", courses[i].coursename.search(/\d/)));
var profname = prettifyName(courses[i].profname);
if (profname == "") {
profname = "Undecided Professor";
}
var listhtml = "<li id='" + i + "'style='padding: 0px 5px 5px 5px; overflow-y: auto;max-height:400px;'><div class='card'><div class='container' style='background:" + color + "''><h4 class='truncate' style='color:white;margin:5px; display:inline-block;font-size:large;'><b>" + department + " " + course_nbr + "<span style='font-size:medium'>" + " with </span><span style='font-size:medium'>" + profname + " (" + courses[i].unique + ")" + "</span></b></h4><p id='arrow' style='float:right;font-size:small;display:inline-block;margin-top:10px;color:white;'>&#9658;</p></div></div><div id='moreInfo' style='display: none;'><p style='font-weight:bold;padding:10px;margin:0px 5px 0px 15px;font-size:small;background-color:#FFCDD2;'>" + makeLine(i) + "</p><div id='infoButtons' style='border-radius:0px;'><button class='matbut' id='listRemove'style='float:right;background:#F44336; margin:5px;'>Remove</button><button class='matbut' id='register' style='float:right;background:#4CAF50; margin:5px;'>Register</button><button class='matbut' id='listMoreInfo' style='float:right;background:#2196F3; margin:5px;'>More Info</button></div></div></li>";
$("#courseList").append(listhtml);
}
});
}
/* prettify the name for the conflict messages*/
function getSimpleName(coursename, unique) {
@@ -84,7 +79,7 @@ function updateConflicts() {
/* Handle the button clicks */
$(document).ready(function () {
$("#courseList li").click(function () {
$("#courseList").on('click', 'li', function () {
$(this).find("#listMoreInfo").click(function () {
window.open(courses[$(this).closest("li").attr("id")].link);
});
@@ -153,6 +148,41 @@ $(document).ready(function () {
'url': 'https://registrar.utexas.edu/schedules'
});
});
$("#impexp").click(function(){
if($("#impexp>i").text() == 'close'){
$(".settings").find('#import').remove();
$(".settings").find('#export').remove();
$("#impexp>i").text('import_export');
} else{
$("#impexp>i").text('close');
$(".settings").prepend(`
</button><button title='Import' style="background-color:white;" class="settingsbut" id='import'>
<i style='color:#FF9800' class="material-icons">
arrow_upward
</i>
</button>
<button title='Export' style="background-color:white;" class="settingsbut" id='export'>
<i style='color:#FF9800' class="material-icons">
arrow_downward
</i>
</button>
`);
}
})
$(".settings").on('click', '#import', function () {
$("#importOrig").click();
});
$(".settings").on('click', '#export',function () {
chrome.storage.sync.get('savedCourses', function (data) {
var exportArray = JSON.stringify(data.savedCourses, null, 4);
var exportlink = document.createElement('a');
var url = window.URL.createObjectURL(new Blob([exportArray], {type: "octet/stream"}));
exportlink.setAttribute('href', url);
exportlink.setAttribute('download', 'my_courses.json' );
exportlink.click();
});
});
$("#open").click(function () {
chrome.tabs.create({
'url': "options.html"
@@ -165,6 +195,46 @@ $(document).ready(function () {
});
});
$("#importOrig").change(function(e){
var files = e.target.files;
var reader = new FileReader();
reader.onload = function(){
try{
var impCourses = JSON.parse(this.result);
console.log(impCourses);
if(impCourses && impCourses.length && (impCourses.length == 0 || validateObject(impCourses))){
chrome.storage.sync.set({
savedCourses: impCourses
});
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(tabs[i].id, {
command: "updateCourseList"
});
}
});
setCourseList(impCourses);
}
} catch(err){
}
importOrig.value = ''; //make sure to clear input value after every import
}
reader.readAsText(files[0]);
});
function validateObject(impCourses){
for(var i = 0; i<impCourses.length; i++){
var course = impCourses[i];
var isValid = course["coursename"] && course["datetimearr"] && course["link"] && course["profname"] && course["status"] && course["unique"];
console.log(isValid);
if(!isValid){
return false;
}
}
return true;
}
/* convert from the dtarr and maek the time lines*/
function makeLine(index) {
var datetimearr = courses[index].datetimearr;
@@ -239,6 +309,7 @@ function clear() {
});
}
});
$("#courseList").empty()
console.log("cleared");
$("#courseList").fadeOut(300, function () {
$("#empty").fadeIn(200);