Merge pull request #12 from vincentinttsh/fix_js

Fix issue (#9)
This commit is contained in:
snsd0805 2021-01-23 23:21:41 +08:00 committed by GitHub
commit d2f34d8a69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 79 deletions

View File

@ -1,5 +1,5 @@
var mainWindow = { var mainWindow = {
data: function(){ data: function () {
return { return {
'courses': [], 'courses': [],
'selectCourses': [], 'selectCourses': [],
@ -12,24 +12,24 @@ var mainWindow = {
}, },
created() { created() {
var main = this var main = this
window.fbAsyncInit = function() { window.fbAsyncInit = function () {
FB.init({ FB.init({
appId : '', appId: '',
cookie : true, cookie: true,
xfbml : true, xfbml: true,
version : 'v9.0' version: 'v9.0'
}); });
FB.AppEvents.logPageView(); FB.AppEvents.logPageView();
main.getCourseTable() main.getCourseTable()
}; };
(function(d, s, id){ (function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0]; var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;} if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id; js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js"; js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs); fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk')); }(document, 'script', 'facebook-jssdk'));
}, },
mounted() { mounted() {
@ -37,62 +37,74 @@ var mainWindow = {
axios axios
.get("./output.json") .get("./output.json")
.then(response => (main.courses = response.data)) .then(response => (main.courses = response.data))
.then(function(){ .then(function () {
for(var course of main.courses){ for (var course of main.courses) {
// console.log(course.name) // console.log(course.name)
if(main.departments.indexOf(course.department)==-1){ if (main.departments.indexOf(course.department) == -1) {
main.departments.push(course.department) main.departments.push(course.department)
} }
} }
}) })
.then(function(){ .then(function () {
main.departments.sort() main.departments.sort()
main.selectDepartment = main.departments[15] main.selectDepartment = main.departments[15]
}) })
// Collapse Navbar
var navbarCollapse = function () {
if ($("#mainNav").offset().top > 100) {
$("#mainNav").addClass("navbar-shrink");
} else {
$("#mainNav").removeClass("navbar-shrink");
}
};
// Collapse now if page is not at top
navbarCollapse();
// Collapse the navbar when page is scrolled
$(window).scroll(navbarCollapse);
}, },
methods: { methods: {
'login': function(){ 'login': function () {
var main = this var main = this
FB.login(function(){ FB.login(function () {
main.getCourseTable() main.getCourseTable()
}) })
}, },
'logout': function(){ 'logout': function () {
var main = this var main = this
FB.logout(function(response) { FB.logout(function (response) {
main.user = "" main.user = ""
main.token = "" main.token = ""
}); });
}, },
'getCourseTable': function(){ 'getCourseTable': function () {
var main = this var main = this
FB.getLoginStatus(function(response) { FB.getLoginStatus(function (response) {
main.statusChangeCallback(response); main.statusChangeCallback(response);
}); });
}, },
'statusChangeCallback': function(response){ 'statusChangeCallback': function (response) {
if(response.status == "connected"){ if (response.status == "connected") {
this.token = response.authResponse.accessToken this.token = response.authResponse.accessToken
var main = this var main = this
FB.api('/me', function(response){main.user = response}) FB.api('/me', function (response) { main.user = response })
fetch('https://api.snsd0805.com/courseTable?token='+this.token) fetch('https://api.snsd0805.com/courseTable?token=' + this.token)
.then(function(response){ .then(function (response) {
return response.json() return response.json()
}).then(function(jsonData){ }).then(function (jsonData) {
console.log(jsonData) console.log(jsonData)
main.selectCourses = JSON.parse(jsonData['data']) main.selectCourses = JSON.parse(jsonData['data'])
}) })
.catch(function(err){ .catch(function (err) {
alert("錯誤: "+err) alert("錯誤: " + err)
}) })
} }
}, },
'saveCourseTable': function(){ 'saveCourseTable': function () {
var main = this var main = this
if(this.token!=""){ if (this.token != "") {
fetch('https://api.snsd0805.com/courseTable',{ fetch('https://api.snsd0805.com/courseTable', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -102,49 +114,49 @@ var mainWindow = {
'data': main.selectCourses 'data': main.selectCourses
}) })
}) })
.then(function(response){ .then(function (response) {
return response.json() return response.json()
}) })
.then(function(response){ .then(function (response) {
if(response.status=="saved"){ if (response.status == "saved") {
alert("已儲存") alert("已儲存")
}else{ } else {
alert("錯誤") alert("錯誤")
} }
}) })
.catch(function(err){ .catch(function (err) {
alert("錯誤: "+err) alert("錯誤: " + err)
}) })
}else{ } else {
this.login() this.login()
} }
}, },
'getTime': function(timeString){ 'getTime': function (timeString) {
ans = [] ans = []
number = "" number = ""
for(var i of timeString){ for (var i of timeString) {
if(i>="0" && i<="9"){ if (i >= "0" && i <= "9") {
number = i number = i
}else if(i>="a" && i<="z"){ } else if (i >= "a" && i <= "z") {
ans.push(number+i) ans.push(number + i)
} }
else{ else {
ans.push(timeString) ans.push(timeString)
break break
} }
} }
return ans return ans
}, },
'select': function(department){ 'select': function (department) {
this.selectDepartment = department this.selectDepartment = department
}, },
'founded': function(courseName){ 'founded': function (courseName) {
this.foundName = courseName this.foundName = courseName
}, },
'addCourse': function(course){ 'addCourse': function (course) {
var time = this.getTime(course.time) var time = this.getTime(course.time)
for(var t of time){ for (var t of time) {
this.selectCourses.push({ this.selectCourses.push({
'time': t, 'time': t,
'name': course.name, 'name': course.name,
@ -152,20 +164,20 @@ var mainWindow = {
}) })
} }
}, },
'removeCourse': function(course){ 'removeCourse': function (course) {
console.log("remove "+course) console.log("remove " + course)
for(var i=this.selectCourses.length-1;i>=0;i--){ for (var i = this.selectCourses.length - 1; i >= 0; i--) {
if(this.selectCourses[i].name == course){ if (this.selectCourses[i].name == course) {
this.selectCourses.splice(i, 1) this.selectCourses.splice(i, 1)
} }
} }
}, },
'saveTemp': function(course){ 'saveTemp': function (course) {
if(course==null){ if (course == null) {
}else{ } else {
this.tempCourse = [] this.tempCourse = []
var time = this.getTime(course.time) var time = this.getTime(course.time)
for(var t of time){ for (var t of time) {
this.selectCourses.push({ this.selectCourses.push({
'time': t, 'time': t,
'name': course.name, 'name': course.name,
@ -174,23 +186,23 @@ var mainWindow = {
} }
} }
}, },
'deleteTemp': function(course){ 'deleteTemp': function (course) {
for(var i=this.selectCourses.length-1;i>=0;i--){ for (var i = this.selectCourses.length - 1; i >= 0; i--) {
if(this.selectCourses[i].name == course.name && this.selectCourses[i].temp == true){ if (this.selectCourses[i].name == course.name && this.selectCourses[i].temp == true) {
this.selectCourses.splice(i, 1) this.selectCourses.splice(i, 1)
} }
} }
}, },
'generatePic': function(){ 'generatePic': function () {
html2canvas(document.getElementById('course-table-div')).then(function(canvas) { html2canvas(document.getElementById('course-table-div')).then(function (canvas) {
var a = document.createElement('a'); var a = document.createElement('a');
a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream"); a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
a.download = '課表.jpg'; a.download = '課表.jpg';
a.click(); a.click();
}); });
}, },
'share': function(){ 'share': function () {
if(this.user!="") if (this.user != "")
$('#share').modal('show'); $('#share').modal('show');
else else
this.login() this.login()

View File

@ -41,18 +41,7 @@
offset: 80 offset: 80
}); });
// Collapse Navbar
var navbarCollapse = function() {
if ($("#mainNav").offset().top > 100) {
$("#mainNav").addClass("navbar-shrink");
} else {
$("#mainNav").removeClass("navbar-shrink");
}
};
// Collapse now if page is not at top
navbarCollapse();
// Collapse the navbar when page is scrolled
$(window).scroll(navbarCollapse);
// Floating label headings for the contact form // Floating label headings for the contact form
$(function() { $(function() {