From 6e60e149c887a4878493e89d7508982636f5dc18 Mon Sep 17 00:00:00 2001 From: Sriram Hariharan Date: Sat, 10 Aug 2019 15:19:56 -0500 Subject: [PATCH] more refactoring --- js/Template.js | 53 ++++++++++++ js/courseCatalog.js | 197 ++++++++++++++++++++++---------------------- js/qol.js | 11 --- js/templates.js | 43 ---------- js/utplanner.js | 4 +- manifest.json | 4 +- 6 files changed, 157 insertions(+), 155 deletions(-) create mode 100644 js/Template.js delete mode 100644 js/qol.js delete mode 100644 js/templates.js diff --git a/js/Template.js b/js/Template.js new file mode 100644 index 00000000..f96f0cab --- /dev/null +++ b/js/Template.js @@ -0,0 +1,53 @@ +class Template { + static mainModal() { + return `` + } + static catalogLoading() { + return `
+
+
+

Loading Courses

+

Failed to Load Courses

+
+ +
` + } + static extensionButton() { + return `` + } +} \ No newline at end of file diff --git a/js/courseCatalog.js b/js/courseCatalog.js index 44d27b15..9be8abea 100644 --- a/js/courseCatalog.js +++ b/js/courseCatalog.js @@ -1,53 +1,58 @@ -var next = $("#next_nav_link"); -var semesterCode = new URL(window.location.href).pathname.split('/')[4]; -var doneLoading = true; - +console.log(`UT Registration Plus is running on this page: ${window.location.href}`); var curr_course = {} +var semesterCode = new URL(window.location.href).pathname.split('/')[4]; +var doneLoading = true; +updateListConflictHighlighting(); + +var next = $("#next_nav_link"); +if (next) { + chrome.storage.sync.get('loadAll', function (data) { + if (data.loadAll) + $('[title*="next listing"]').remove(); + }); +} + //This extension may be super lit, but you know what's even more lit? //Matthew Tran's twitter and insta: @MATTHEWTRANN and @matthew.trann -console.log('UT Registration Plus is running on this page.'); - $(window).scroll(function () { - if ($(document).height() <= $(window).scrollTop() + $(window).height() + 150) { + if ($(document).height() <= $(window).scrollTop() + $(window).height() + 150) loadNextPages(); - } }); -if (next) { - chrome.storage.sync.get('loadAll', function (data) { - if (data.loadAll) { - $('[title*="next listing"]').remove(); + +if (document.querySelector('#fos_fl')) { + let params = (new URL(document.location)).searchParams; + let dep = params.get("fos_fl"); + let level = params.get("level"); + if (dep && level) { + if (dep.length == 3 && (level == 'U' || level == 'L' || level == 'G')) { + document.querySelector('#fos_fl').value = dep; + document.querySelector('#level').value = level; } - }); + } } //make heading and modal if (!$("#kw_results_table").length) { - $("table thead th:last-child").after('Plus'); - $("table").after(`
-
-
-

Loading Courses

-

Failed to Load Courses

-
- -
`); - $("#container").prepend(mainModal()); + $("table").after(Template.catalogLoading()); + $("#container").prepend(Template.mainModal()); $("#myModal").prepend("
save course popup...
"); - //go through all the rows in the list + + // now add to the table + $("table thead th:last-child").after('Plus'); $('table').find('tr').each(function () { if (!($(this).find('td').hasClass("course_header")) && $(this).has('th').length == 0) { - //if a course row, then add the extension button - $(this).append(``); + $(this).append(Template.extensionButton()); } }); } -//update the conflicts -update(0); + + + /*Handle the button clicks*/ $("body").on('click', '#distButton', function () { var row = $(this).closest('tr'); @@ -99,27 +104,6 @@ $("#retry").click(function () { }); - - -$(document).keydown(function (e) { - /*Close Modal when hit escape*/ - if (e.keyCode == 27) { - close(); - } else if (e.keyCode == 13 && $('#myModal').is(':visible')) { - saveCourse(); - } -}); - -/*Listen for update mssage coming from popup*/ -chrome.runtime.onMessage.addListener( - function (request, sender, sendResponse) { - if (request.command == "updateCourseList") { - update(0); - } - } -); - - function sepNameParts(name) { numIndex = name.search(/\d/); department = name.substring(0, numIndex).trim(); @@ -234,18 +218,8 @@ function saveCourse() { }); } - -function toggleSnackbar() { - setTimeout(function () { - $("#snackbar").attr("class", "show"); - }, 200); - setTimeout(function () { - $("#snackbar").attr("class", ""); - }, 3000); -} - /* Update the course list to show if the row contains a course that conflicts with the saved course is one of the saved courses */ -function update(start) { +function updateListConflictHighlighting(start = 0) { chrome.storage.sync.get('courseConflictHighlight', function (data) { let canHighlight = data.courseConflictHighlight; $('table').find('tr').each(function (i) { @@ -494,11 +468,60 @@ function getDescription(course_info) { }); } +function loadNextPages() { + chrome.storage.sync.get('loadAll', function (data) { + if (data.loadAll) { + let link = next.prop('href'); + if (doneLoading && next && link) { + toggleLoadingPage(true); + $.get(link, function (response) { + if (response) { + var nextpage = $('
').html(response).contents(); + var current = $('tbody'); + var oldlength = $('tbody tr').length; + var last = current.find('.course_header>h2:last').text(); + next = nextpage.find("#next_nav_link"); + toggleLoadingPage(false); + var newrows = []; + nextpage.find('tbody>tr').each(function () { + let hasCourseHead = $(this).find('td').hasClass("course_header"); + if (!(hasCourseHead && $(this).has('th').length == 0)) + $(this).append(Template.extensionButton()); + if (!(hasCourseHead && last == $(this).find('td').text())) + newrows.push($(this)); + }); + current.append(newrows); + updateListConflictHighlighting(oldlength + 1) + } + }).fail(function () { + toggleLoadingPage(false); + $("#retrylabel").css('display', 'inline-block'); + $('#retry').css('display', 'inline-block'); + }); + } + } + }); +} + + + +/*Listen for update mssage coming from popup*/ +chrome.runtime.onMessage.addListener( + function (request, sender, sendResponse) { + if (request.command == "updateCourseList") { + updateListConflictHighlighting(0); + } + } +); + + function toggleLoadingPage(loading) { if (loading) { + doneLoading = false; $('#loader').css('display', 'inline-block'); $("#nextlabel").css('display', 'inline-block'); } else { + doneLoading = true; $('#loader').hide(); $("#nextlabel").hide(); } @@ -522,45 +545,25 @@ function toggleDescriptionLoading(loading) { } } -function loadNextPages() { - chrome.storage.sync.get('loadAll', function (data) { - if (data.loadAll) { - let link = next.prop('href'); - if (doneLoading && next && link) { - toggleLoadingPage(true); - doneLoading = false; - $.get(link, function (response) { - if (response) { - var nextpage = $('
').html(response).contents(); - var current = $('tbody'); - var oldlength = $('tbody tr').length; - var last = current.find('.course_header>h2:last').text(); - next = nextpage.find("#next_nav_link"); - doneLoading = true; - $("#nextlabel").hide(); - $('#loader').hide(); - var newrows = []; - nextpage.find('tbody>tr').each(function () { - let hasCourseHead = $(this).find('td').hasClass("course_header"); - if (!(hasCourseHead && $(this).has('th').length == 0)) - $(this).append(extensionButton()); - if (!(hasCourseHead && last == $(this).find('td').text())) - newrows.push($(this)); - }); - current.append(newrows); - update(oldlength + 1) - } - }).fail(function () { - doneLoading = true; - toggleLoadingPage(false); - $("#retrylabel").css('display', 'inline-block'); - $('#retry').css('display', 'inline-block'); - }); - } - } - }); +function toggleSnackbar() { + setTimeout(function () { + $("#snackbar").attr("class", "show"); + }, 200); + setTimeout(function () { + $("#snackbar").attr("class", ""); + }, 3000); } +$(document).keydown(function (e) { + /*Close Modal when hit escape*/ + if (e.keyCode == 27) { + close(); + } else if (e.keyCode == 13 && $('#myModal').is(':visible')) { + saveCourse(); + } +}); + + function allowClosing() { $('.close').click(function () { close(); diff --git a/js/qol.js b/js/qol.js deleted file mode 100644 index 08a5b642..00000000 --- a/js/qol.js +++ /dev/null @@ -1,11 +0,0 @@ -if (document.querySelector('#fos_fl')) { - let params = (new URL(document.location)).searchParams; - let dep = params.get("fos_fl"); - let level = params.get("level"); - if (dep && level) { - if (dep.length == 3 && (level == 'U' || level == 'L' || level == 'G')) { - document.querySelector('#fos_fl').value = dep; - document.querySelector('#level').value = level; - } - } -} \ No newline at end of file diff --git a/js/templates.js b/js/templates.js deleted file mode 100644 index c8ba8b37..00000000 --- a/js/templates.js +++ /dev/null @@ -1,43 +0,0 @@ -function extensionButton() { - return `` -} - - -function mainModal() { - return `` -} \ No newline at end of file diff --git a/js/utplanner.js b/js/utplanner.js index 2592e3a7..b3c54023 100644 --- a/js/utplanner.js +++ b/js/utplanner.js @@ -2,12 +2,12 @@ if ($('html').hasClass('gr__utexas_collegescheduler_com')) { $.initialize("table.section-detail-grid", function () { $(this).find('thead>tr').append(' Plustr').each(function () { - $(this).append(extensionButton()); + $(this).append(Template.extensionButton()); }) }); } curr_course = {} -$("body").prepend(mainModal()); +$("body").prepend(Template.mainModal()); $("body").on('click', '#distButton', function () { var row = $(this).closest('tr'); diff --git a/manifest.json b/manifest.json index 1592909a..a7ce7f04 100644 --- a/manifest.json +++ b/manifest.json @@ -17,13 +17,13 @@ "content_scripts": [{ "css": ["css/styles.css"], "js": ["js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/config.js", "js/util.js", - "js/qol.js", "js/templates.js", "js/courseCatalog.js" + "js/Template.js", "js/courseCatalog.js" ], "matches": ["https://utdirect.utexas.edu/apps/registrar/course_schedule/*"] }, { "css": ["css/styles.css"], "js": ["js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/config.js", "js/util.js", - "js/templates.js", "js/utPlanner.js" + "js/Template.js", "js/utPlanner.js" ], "matches": ["https://utexas.collegescheduler.com/*"] }, {