1485 lines
57 KiB
JavaScript
Raw Normal View History

2016-09-04 20:10:58 -04:00
/* jshint esversion: 6 */
2016-08-12 12:28:55 -04:00
import {
Template
} from 'meteor/templating';
2016-08-09 01:33:14 -04:00
import './main.html';
var load = true;
2016-09-02 00:15:29 -04:00
var calWorkOpen = null;
var calWorkDate = null;
2017-04-15 23:39:49 -04:00
var dragging = false;
var clicked = false;
var workChanger = false;
2017-05-02 20:51:30 -04:00
var disconnect = false;
2017-04-15 23:39:49 -04:00
2016-08-20 23:37:04 -04:00
// Reactive variables.
2016-10-03 23:13:05 -04:00
Session.set("user", {}); // Stores user preferences.
Session.set("calendarEvents", []); // Stores calendar classes.
Session.set("myClasses", []); // Stores user classes.
2017-04-18 04:28:05 -04:00
Session.set("myWork", []); // Stores user related work.
Session.set("filterWork", []); // Stores work that is filtered out.
2016-10-03 23:13:05 -04:00
Session.set("requests", false); // Status of requests.
Session.set("sidebarMode", ""); // Status of sidebars.
2016-09-04 20:13:31 -04:00
Session.set("newWork", null); // If user creating new work.
2016-10-23 22:11:27 -04:00
Session.set("currentWork",null); // Current stored work.
2016-09-04 20:13:31 -04:00
Session.set("classDisp", []); // Stores current filter for classes.
2016-09-15 08:30:24 -04:00
Session.set("typeFilter", []); // Stores type filters for classes.
2016-10-03 23:13:05 -04:00
Session.set("typeFilterHover", null); // Stores current hovered type filter.
2016-09-15 10:25:10 -04:00
Session.set("classDispHover", null); // Stores current hovered class filter.
2016-10-16 02:15:47 -04:00
Session.set("restrictText", {}); // Stores text for comment character restriction.
2016-11-08 20:19:22 -05:00
Session.set("confirmText", ""); // Stores text for confirmations.
2016-08-09 01:33:14 -04:00
// On render actions
2016-09-16 16:48:39 -04:00
2017-05-02 20:51:30 -04:00
Meteor.autorun(function () { // Disconnect checking
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) return;
if (Meteor.status().status !== "connected" && Meteor.status().status !== "connecting" && !disconnect) {
disconnect = true;
var div = document.createElement("div");
div.id = "disconnect";
2017-09-16 22:50:25 -04:00
div.style.color = "#FFF";
var h = document.createElement("h3");
h.appendChild(document.createTextNode("Uh Oh. We can't reach you right now!"));
var h2 = document.createElement("h4");
h2.appendChild(document.createTextNode("Please check your connection, or reload the page!"));
var h5 = document.createElement("h5");
h5.appendChild(document.createTextNode("Reload!"));
h5.onclick = function() {
location.reload();
}
div.appendChild(h);
div.appendChild(h2);
div.appendChild(h5);
document.getElementsByTagName("body")[0].appendChild(div);
$("#disconnect").velocity("fadeIn", 150);
2017-05-02 20:51:30 -04:00
} else if(Meteor.status().status === "connected" && disconnect) {
disconnect = false;
$("#disconnect").velocity("fadeOut", 150, function() {
document.getElementsByTagName("body")[0].removeChild(document.getElementById("disconnect"));
sAlert.success("You're back!", {
effect: 'stackslide',
position: 'bottom-right',
timeout: 1500
});
});
2017-05-05 02:12:45 -04:00
} else if(Meteor.status().status === "connecting" && document.getElementById("disconnect") !== null) {
if(document.getElementById("disconnectLoad") !== null) return;
var h1 = document.createElement("h4");
h1.appendChild(document.createTextNode("Reconnecting..."));
h1.id = "disconnectLoad";
document.getElementById("disconnect").appendChild(h1);
$("#disconnectLoad").velocity("fadeIn", 150);
2017-05-02 20:51:30 -04:00
}
});
2016-09-09 02:17:32 -04:00
Template.login.rendered = function() {
Accounts._loginButtonsSession.set('dropdownVisible', true);
};
2017-09-16 22:50:25 -04:00
function randInt(min,max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2016-11-02 19:18:36 -04:00
Template.main.created = function() {
Session.set("mode", Session.get("user").preferences.mode);
Session.set("myWork", []);
Session.set("filterWork", []);
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var classes = ["Biology", "Math", "Literature", "History"];
var names = ["Worksheet", "Problems", "Notes"];
var counter = 0;
for(var i = 0; i < classes.length; i++) {
for(var j = 0; j < randInt(1,3); j++) {
var nameInt = randInt(0,2);
var item = {
_id: counter.toString(),
class: classes[i],
dueDate: moment().endOf('day').add(randInt(1,5), "day").subtract(12, "hours").toDate(),
type: "normal",
name: names[nameInt] + ((nameInt === 0) ? " #"+randInt(1,8) : (nameInt === 1) ? " #"+randInt(1,9) + "-" + randInt(20,34) : " pg. "+randInt(90,120)+"-"+randInt(130,140)),
confirmationLength: randInt(1,10),
reportLength: randInt(0,6),
done: []
};
updateWork(counter.toString(), item, "added");
counter++;
}
}
updateWork(counter.toString(), {
_id: counter.toString(),
class: "Math",
dueDate: moment().endOf('day').add(randInt(1,5), "day").subtract(12, "hours").toDate(),
type: "test",
name: "Math Test 3.1",
confirmationLength: randInt(1,10),
reportLength: randInt(0,6),
done: []
}, "added");
updateWork((counter+1).toString(), {
_id: (counter+1).toString(),
class: "Literature",
dueDate: moment().endOf('day').add(randInt(1,5), "day").subtract(12, "hours").toDate(),
type: "project",
name: "Poem Commentary",
confirmationLength: randInt(1,10),
reportLength: randInt(0,6),
done: []
}, "added");
updateWork((counter+2).toString(), {
_id: (counter+2).toString(),
class: "Biology",
dueDate: moment().endOf('day').add(randInt(1,5), "day").subtract(12, "hours").toDate(),
type: "quiz",
name: "Cell Respiration",
confirmationLength: randInt(1,10),
reportLength: randInt(0,6),
done: []
}, "added");
updateWork((counter+3).toString(), {
_id: (counter+3).toString(),
class: "Personal",
dueDate: moment().endOf('day').add(randInt(1,5), "day").subtract(12, "hours").toDate(),
type: "other",
name: "Clean Room",
confirmationLength: 1,
reportLength: 0,
done: []
}, "added");
var array = [];
var classes = ["Personal", "History", "Math", "Biology", "Literature"];
var prefix = ["Mr.", "Dr.", "Ms.", "Mrs."];
for(var i = 0; i < classes.length; i++) {
array.push({
box: " owned",
hour: (i === 0) ? "" : "A"+(i).toString(),
mine: false,
name: classes[i],
status: true,
category: (i === 0) ? "Personal" : "class",
selected: "rgba(0,0,0,0)",
subscribers: randInt(7,20),
_id: classes[i],
teacher: prefix[randInt(0,3)] + " Bot",
teachershort: "Bot"
});
}
Session.set("myClasses", array);
filterWork();
}
2016-11-07 21:22:51 -05:00
Session.set("classInfo", null);
$(document).on('keyup', (e) => {
if(event.keyCode === 27 && $(".overlay").css("display") !== "none") {
$(".overlay").velocity("fadeOut", 150);
Session.set("currentWork", null);
}
});
2017-04-18 04:28:05 -04:00
work.find().observeChanges({
added: function (id, fields) {
updateWork(id, fields, "added");
filterWork();
2017-04-18 04:28:05 -04:00
},
changed: function (id, fields) {
updateWork(id, fields, "changed");
filterWork();
2017-04-18 04:28:05 -04:00
},
removed: function (id) {
updateWork(id, null, "remove");
}
});
2017-02-09 10:26:22 -05:00
/*if (Notification.permission !== "granted") {
Notification.requestPermission().then(function(result) {
});
}*/
2016-11-02 19:18:36 -04:00
}
2016-09-09 02:17:32 -04:00
Template.main.rendered = function() {
Accounts._loginButtonsSession.set('dropdownVisible', true);
2016-09-16 18:08:56 -04:00
setTimeout(startDragula, 300);
2016-10-23 22:11:27 -04:00
$("#menuContainer").toggle();
2017-02-03 03:43:21 -05:00
$("#doneUsers").slimScroll({
height: '34vh',
touchScrollStep: 90
});
2016-11-02 19:18:36 -04:00
document.getElementsByTagName("body")[0].style.color = Session.get("user").preferences.theme.textColor;
2017-09-16 22:50:25 -04:00
2016-09-09 02:17:32 -04:00
};
Template.classesMode.rendered = function() {
$(".workHolder").slimScroll({
width: '100%',
height: '',
touchScrollStep: 90
});
2017-02-09 10:34:59 -05:00
$(".mainClass .slimScrollBar").css("display", "none");
2017-04-15 22:28:43 -04:00
var area = $("#classesMode");
var clickX = 0;
2017-05-01 23:23:16 -04:00
var reducer = 0.5;
2017-04-15 22:28:43 -04:00
area.on({
'mousemove': function(e) {
if(clicked && !dragging) {
2017-05-01 23:23:16 -04:00
area.scrollLeft(area.scrollLeft() + (clickX - reducer*e.pageX));
clickX = reducer*e.pageX;
}
2017-04-15 22:28:43 -04:00
},
'mousedown': function(e) {
clicked = true;
2017-05-01 23:23:16 -04:00
clickX = reducer*e.pageX;
2017-04-15 22:28:43 -04:00
},
'mouseup': function() {
clicked = false;
2017-04-13 10:59:20 -04:00
}
});
2017-03-01 12:14:46 -05:00
2017-04-15 22:28:43 -04:00
/*if(dragging) {
var onLeft = e.pageX <= .1*screen.width;
var onRight = e.pageX >= (window.innerWidth-.1*screen.width);
console.log(onLeft);
if(onLeft) {
area.scrollLeft(area.scrollLeft() - 5);
} else if(onRight) {
area.scrollLeft(area.scrollLeft() + 5);
}
}*/
};
// Global Helpers
2017-04-13 01:52:34 -04:00
Template.registerHelper('version', () => {
return version;
});
2016-09-12 11:12:06 -04:00
Template.registerHelper('screen', (multiplier, fraction) => {
2016-10-03 23:13:05 -04:00
if (typeof multiplier !== "string") return screen.width.toString() + "px";
if (typeof fraction !== "string") return (screen.width * parseFloat(multiplier)).toString() + "px";
return ((screen.width) * parseFloat(multiplier) / parseFloat(fraction)).toString() + "px";
2016-09-12 11:12:06 -04:00
});
2016-08-29 21:02:02 -04:00
Template.registerHelper('divColor', (div) => { // Reactive color changing based on preferences. Colors stored in themeColors.
if(Session.get("user") === null) return;
2017-01-15 23:59:10 -05:00
return (Object.keys(Session.get("user")).length === 0) ? themeColors["lux"][div] : Session.get("user").preferences.theme[div];
2016-08-22 23:44:48 -04:00
});
2016-08-28 23:25:43 -04:00
Template.registerHelper('overlayDim', (part) => { // Gets size of the overlay container.
var dim = [window.innerWidth * 0.25, window.innerHeight * 0.2];
2016-08-12 12:28:55 -04:00
var width = "width:" + dim[0].toString() + "px;";
var height = "height:" + dim[1].toString() + "px;";
var margin = "margin-left:" + (-dim[0] / 2).toString() + "px;";
2016-09-29 23:39:34 -04:00
var bg = "background-color:" + Session.get("user").preferences.theme.header + ";";
2016-08-12 12:28:55 -04:00
return width + height + margin + bg;
});
2016-08-30 18:49:33 -04:00
Template.registerHelper('myClasses', () => { // Gets all classes and respective works.
2017-04-19 01:45:46 -04:00
var myClasses = Session.get("user").classes;
2017-09-16 22:50:25 -04:00
if(!Session.get("demo")) getClasses(myClasses);
return Session.get("myClasses");
2016-08-14 07:52:27 -04:00
});
2016-08-30 18:49:33 -04:00
Template.registerHelper('pref', (val) => { // Obtains all user preferences.
2017-04-08 00:07:00 -04:00
try {
2017-04-13 01:10:16 -04:00
if(val === "school") return Session.get("user").school;
2017-04-08 00:07:00 -04:00
var preferences = Session.get("user").preferences;
return options[val].filter(function(entry) {
return (val === 'theme') ? _.isEqual(preferences[val], themeColors[entry.val]) : preferences[val] === entry.val;
})[0].alias;
} catch(err) {}
2016-08-28 14:36:26 -04:00
});
2016-10-16 02:15:47 -04:00
Template.registerHelper('restrict', (input) => { // Returns characters left for comment length.
var restrict = Session.get("restrictText");
$(".resText").removeClass("noneLeft");
if(Object.keys(restrict).length === 0) return "";
if(restrict[restrict.selected][0] === "0") $(".resText").addClass("noneLeft");
return (restrict.selected === input) ? Session.get("restrictText")[input] : "";
2016-09-07 21:26:26 -04:00
});
2016-09-05 14:51:40 -04:00
2016-10-16 02:15:47 -04:00
Template.registerHelper('selectOptions', (val) => {
2016-10-16 19:55:19 -04:00
if(val === "grade") {
var grade = [];
for(var i = 0; i < 5; i++) {
2016-10-28 23:59:02 -04:00
var year = (new Date()).getFullYear() + i;
2016-10-16 19:55:19 -04:00
grade.push( { "val": year, "alias": year.toString() } );
}
grade.push( { "val": 0, "alias": "Faculty" } );
2016-10-16 19:55:19 -04:00
return grade;
} else if(val === "school") {
var school = [];
var schoolList = schools.find().fetch();
for(var i = 0; i < schoolList.length; i++) {
school.push( { "val": schoolList[i].name, "alias": schoolList[i].name } );
}
return school;
} else {
return options[val];
}
2016-10-16 02:15:47 -04:00
});
2016-10-16 02:15:47 -04:00
Template.registerHelper('work', (value) => {// Returns the specified work value.
var thisWork = Session.get("currentWork");
2017-04-25 02:46:06 -04:00
if(Session.equals("currentWork", null)) return;
if(Session.get("newWork")) {
switch(value) {
case "class":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return thisWork["class"];
2017-04-25 02:46:06 -04:00
var id = thisWork["class"];
2017-05-02 20:51:30 -04:00
if(!id) return;
2017-04-25 02:46:06 -04:00
return (id === Meteor.userId()) ? "Personal" : classes.findOne({_id: id}).name;
case "dueDate":
return getReadableDate(thisWork.dueDate);
case "type":
return options.type.filter(function(names) {
return names.val === thisWork.type;
})[0].alias;
default:
return;
}
2016-10-16 02:15:47 -04:00
}
2017-04-25 02:46:06 -04:00
var input = thisWork;
try {
switch (value) {
case "typeColor":
return input.typeColor = workColors[input.type];
case "name":
return input.name;
case "class":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return thisWork["class"];
2017-04-25 02:46:06 -04:00
var id = input["class"];
return (id === Meteor.userId()) ? "Personal" : classes.findOne({_id: id}).name;
case "dueDate":
return getReadableDate(input.dueDate);
case "description":
return input.description;
case "type":
return input.type[0].toUpperCase() + input.type.slice(1);
case "comments":
var comments = input.comments;
var resort = [];
if (Session.get("newWork")) return []; // Don't display comments if user is creating work.
for (var k = 0; k < comments.length; k++) {
var re = comments.length - k - 1;
resort[re] = {
"comment": comments[k].comment,
"date": null,
"user": null,
"avatar": null,
"email": null
};
var user = Meteor.users.findOne({
_id: comments[k].user
});
resort[re].user = user.profile.name;
resort[re].date = moment(comments[k].date).fromNow();
resort[re].avatar = user.services.google.picture;
resort[re].email = user.services.google.email;
}
return resort;
case "done":
if (Session.get("newWork")) return [];
for (var i = 0; i < input.done.length; i++) { // Display users who marked as done.
var user = Meteor.users.findOne({
_id: input.done[i]
});
input.done[i] = {
"user": user.profile.name,
"avatar": user.services.google.picture,
"email": user.services.google.email
};
}
return input.done;
case "doneCol":
if (Session.get("newWork")) return "";
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
if (input.done.length === 0) return "";
} else {
if (!_.contains(input.done, Meteor.userId())) return "";
}
2017-04-25 02:46:06 -04:00
return "#27A127";
case "doneText":
if (Session.get("newWork")) return "";
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
if(input.done.length === 0) return "Mark done";
} else {
if (!_.contains(input.done, Meteor.userId())) return "Mark done";
}
2017-04-25 02:46:06 -04:00
return "Done!";
case "doneIcon":
if (Session.get("newWork")) return "";
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
if(input.done.length === 0) return "fa-square-o";
} else {
if (!_.contains(input.done, Meteor.userId())) return "fa-square-o";
}
2017-04-25 02:46:06 -04:00
return "fa-check-square-o";
case "userConfirm":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return "#27A127";
2017-04-25 02:46:06 -04:00
if (!_.contains(input.confirmations, Meteor.userId())) return (Meteor.Device.isPhone() || Meteor.Device.isTablet()) ? "rgb(101,101,101)" : "";
return "#27A127";
case "confirmations":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return input.confirmationLength;
2017-04-25 02:46:06 -04:00
return input.confirmations.length;
case "userReport":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return "";
2017-04-25 02:46:06 -04:00
if (!_.contains(input.reports, Meteor.userId())) return (Meteor.Device.isPhone() || Meteor.Device.isTablet()) ? "rgb(101,101,101)" : "";
return "#FF1A1A";
case "reports":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return input.reportLength;
2017-04-25 02:46:06 -04:00
return input.reports.length;
case "email":
return Meteor.users.findOne({
_id: input.creator
}).services.google.email;
case "avatar":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return "Logos/ColoredLogo.png";
2017-04-25 02:46:06 -04:00
return Meteor.users.findOne({
_id: input.creator
}).services.google.picture;
case "creator":
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return "A. Robot";
2017-04-25 02:46:06 -04:00
return Meteor.users.findOne({
_id: input.creator
}).profile.name;
}
} catch(err){}
2016-10-16 19:55:19 -04:00
});
2016-11-08 20:19:22 -05:00
Template.registerHelper('confirmText', () => {
return Session.get("confirmText");
})
// Main template helpers and events
2016-08-09 01:33:14 -04:00
Template.main.helpers({
2016-10-16 02:15:47 -04:00
/*themeName() {
2016-10-19 21:20:49 -04:00
var vals = _.values(themeColors);
var curtheme = Session.get("user").preferences.theme;
for (var i = 0; i < vals.length; i++) {
if (_.isEqual(vals[i], curtheme)) {
var name = _.keys(themeColors)[i];
return name.charAt(0).toUpperCase() + name.slice(1);
2016-10-28 23:59:02 -04:00
}
2016-10-19 21:20:49 -04:00
}
return "Custom";
},*/
2016-08-30 18:49:33 -04:00
schoolName() { // Finds the name of the user's school.
2016-10-03 23:13:05 -04:00
if (Session.get("user").school === undefined || Session.get("user").school === null) return;
return " - " + Session.get("user").school;
2016-08-12 12:28:55 -04:00
},
2016-09-07 15:33:13 -04:00
avatar() { // Returns avatar.
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return "Logos/ColoredLogo.png";
2017-04-18 04:28:05 -04:00
try {
return Meteor.user().services.google.picture;
} catch(err) {}
2016-09-07 15:33:13 -04:00
},
username() { // Returns user name.
return Session.get("user").name;
},
bgSrc() { // Returns background.
return "MDBackgrounds/" + "MD"+Session.get("user").preferences.theme.background;
2016-08-12 12:28:55 -04:00
},
2016-10-23 22:11:27 -04:00
iconStatus(icon) {
var sidebar = Session.get("sidebarMode");
return (sidebar === icon) ? Session.get("user").preferences.theme.iconHighlight + ";background-color:rgba(0,0,0,0.2)" : "";
},
sidebarStatus(sidebar) {
return sidebar === Session.get("sidebarMode");
2016-08-12 12:28:55 -04:00
},
2016-09-11 23:22:50 -04:00
requestStatus() {
if (Session.get("requests")) return "0px";
return openValues.requests;
},
2016-11-03 23:12:57 -04:00
currMode(mode) { // Status of display mode.
return Session.equals("mode", mode);
},
currSettingMode(mode) {
return Session.equals("settingMode", mode) && Session.equals("sidebarMode", "option");
2016-08-12 12:28:55 -04:00
},
2016-08-30 18:49:33 -04:00
calendarOptions() { // Settings for the calendar, including work displaying.
2016-08-12 12:28:55 -04:00
return {
2016-08-18 23:59:38 -04:00
id: "fullcalendar",
2016-08-15 20:55:21 -04:00
height: window.innerHeight * 0.8,
2016-08-12 12:28:55 -04:00
buttonText: {
today: 'Today',
month: 'Month',
week: 'Week',
day: 'Day'
},
2016-09-02 00:15:29 -04:00
eventLimit: 3,
events: Session.get("calendarEvents"),
2016-08-30 18:49:33 -04:00
eventDrop: function(event, delta, revertFunc) { // When user drops from click-dragging.
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var current = Session.get("myWork").filter(function(obj) {
return obj._id === event.id;
})[0];
} else {
var current = work.findOne({
_id: event.id
});
}
var date = event.start.format().split("-");
2016-09-04 20:13:31 -04:00
current.dueDate = new Date(date[0], parseInt(date[1]) - 1, date[2], 11, 59, 59);
2016-10-28 23:59:02 -04:00
if(Date.parse(new Date()) > Date.parse(current.dueDate)) {
2016-10-23 22:11:27 -04:00
revertFunc();
return;
2016-10-28 23:59:02 -04:00
}
2016-08-29 21:02:02 -04:00
serverData = current;
sendData("editWork");
2016-08-18 23:59:38 -04:00
},
2016-08-30 18:49:33 -04:00
eventClick: function(event, jsEvent, view) { // On-click for work.
2016-09-04 20:13:31 -04:00
Session.set("newWork", false);
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
Session.set("currentWork", Session.get("myWork").filter(function(obj) {
return obj._id === event.id;
})[0]);
} else {
Session.set("currentWork", work.findOne({_id: event.id}));
}
$(".overlay").velocity("fadeIn", 150);
2017-02-09 09:01:18 -05:00
$("#comment").slimScroll({
width: '100%',
height: '20vh',
touchScrollStep: 90
});
},
2016-10-03 23:13:05 -04:00
eventMouseover: function(event, jsEvent, view) {
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var className = Session.get("myWork").filter(function(obj) {
return obj._id === event.id;
})[0].class;
} else {
var classid = work.findOne({_id: event.id})["class"];
var className = (classid === Meteor.userId()) ? "Personal" : classes.findOne({_id: classid}).name;
}
2017-04-25 22:00:56 -04:00
var span = this.children[0].children[0];
$(span).velocity("stop");
$(span).velocity({opacity:0}, 50, function() {
span.childNodes[0].nodeValue = className;
$(span).velocity({opacity:1}, 50);
});
2016-09-11 18:47:50 -04:00
this.style.boxShadow = "inset 0 0 0 99999px rgba(255,255,255,0.2)";
},
2016-10-03 23:13:05 -04:00
eventMouseout: function(event, jsEvent, view) {
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var workName = Session.get("myWork").filter(function(obj) {
return obj._id === event.id;
})[0].name;
} else {
var workName = work.findOne({_id: event.id}).name;
}
2017-04-25 22:00:56 -04:00
var span = this.children[0].children[0];
$(span).velocity("stop");
$(span).velocity({opacity:0}, 50, function() {
span.childNodes[0].nodeValue = workName;
$(span).velocity({opacity:1}, 50);
});
this.style.boxShadow = "";
},
2016-08-30 18:49:33 -04:00
dayClick: function(date, jsEvent, view) { // On-click for each day.
if (jsEvent.target.className.includes("fc-past")) return;
2017-04-15 22:28:43 -04:00
var realDate = date;
2017-04-25 02:46:06 -04:00
date.startOf("day");
2017-05-02 20:51:30 -04:00
date.hour(12);
2017-04-15 22:28:43 -04:00
realDate = realDate._d;
2017-04-25 03:07:56 -04:00
Session.set("currentWork", {dueDate: realDate, type: "normal"});
if(!Session.equals("sidebarMode", "create")) toggleToSidebar("create");
2016-08-19 22:04:32 -04:00
}
2016-08-12 12:28:55 -04:00
};
},
calCenter() { // Centers the calendar
2016-08-12 12:28:55 -04:00
var width = window.innerWidth * 0.85;
2016-08-23 00:11:57 -04:00
return "width:" + width.toString() + "px;margin-left:" + (0.5 * window.innerWidth - 0.5 * width).toString() + "px;";
},
2016-08-30 18:49:33 -04:00
highlight() { // Calendar highlight/scale option.
var hoverHighlight = Session.get("classDispHover");
2016-09-15 10:25:10 -04:00
var typeHighlight = Session.get("typeFilterHover");
2016-10-03 23:13:05 -04:00
if (Session.equals("mode", "classes")) {
$(".workCard").toggleClass("scaled", false);
2016-09-11 18:47:50 -04:00
try {
2016-10-03 23:13:05 -04:00
$(".workCard[classid=\'" + hoverHighlight + "\']").toggleClass("scaled", true);
$(".workCard[type=\'" + typeHighlight + "\']").toggleClass("scaled", true);
} catch (err) {}
2016-09-11 18:47:50 -04:00
} else {
2016-10-03 23:13:05 -04:00
$(".workevent").toggleClass("scaled", false);
2016-09-10 02:26:11 -04:00
try {
2016-10-03 23:13:05 -04:00
$("." + hoverHighlight).toggleClass("scaled", true);
$("." + typeHighlight).toggleClass("scaled", true);
} catch (err) {}
}
return;
},
2016-08-30 18:49:33 -04:00
newWork() { // If user is creating a new work.
2016-08-18 23:59:38 -04:00
return Session.get("newWork");
},
2016-08-30 18:49:33 -04:00
inRole() { // Checks correct permissions.
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) return true;
2016-10-16 02:15:47 -04:00
if(Session.equals("currentWork",null)) return;
2017-02-26 19:27:50 -05:00
try {
var thisWork = work.findOne({
_id: Session.get("currentWork")._id
2016-09-04 20:13:31 -04:00
});
2017-02-26 19:27:50 -05:00
if (Session.get("newWork")) {
return true;
} else {
if (thisWork === undefined) return;
var currClass = classes.findOne({
_id: thisWork["class"]
});
if (Meteor.userId() === thisWork.creator ||
Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
currClass.moderators.indexOf(Meteor.userId()) !== -1 ||
2017-05-02 20:51:30 -04:00
currClass.banned.indexOf(Meteor.userId()) !== -1 ||
currClass.admin === Meteor.userId()
2017-02-26 19:27:50 -05:00
) return true;
}
} catch(err) {}
2016-09-02 00:15:29 -04:00
},
2016-09-07 15:33:13 -04:00
admin() {
return Roles.userIsInRole(Meteor.userId(), ['admin', 'superadmin']);
2016-09-04 20:13:31 -04:00
}
2016-08-09 01:33:14 -04:00
});
Template.main.events({
2016-08-30 18:49:33 -04:00
'click' (event) { // Closes respective divs when clicking outside of them. Order matters.
2016-08-12 12:28:55 -04:00
var e = event.target.className;
2016-10-12 01:45:36 -04:00
if(modifyingInput !== null && event.target !== document.getElementById(modifyingInput)) {
if (!(e.includes("optionHolder") || e.includes("optionText"))) {
2016-10-12 01:45:36 -04:00
if(document.getElementById(modifyingInput).className.includes("dropdown")) {
toggleOptionMenu(false, modifyingInput);
2016-10-28 23:59:02 -04:00
} else {
2016-10-12 01:45:36 -04:00
closeInput(modifyingInput);
}
modifyingInput = null;
}
}
2016-08-28 18:41:35 -04:00
2016-10-23 22:11:27 -04:00
if (!e.includes("fa-cog") && // Sidebar closing.
2016-11-03 23:12:57 -04:00
!e.includes("fa-bars") &&
!e.includes("fa-question") &&
!document.getElementById("menuContainer").contains(event.target) &&
!document.getElementById("menuBar").contains(event.target)) {
if(!(e.includes("fc-day") && !e.includes("fc-past")) &&
!Session.equals("sidebarMode", "option")) {
toggleToSidebar(false);
}
2016-08-12 12:28:55 -04:00
}
2016-09-04 20:13:31 -04:00
if (e === "overlay") { // Overlay closing.
$(".overlay").velocity("fadeOut", 150);
2016-09-04 20:13:31 -04:00
if (!Session.get("newWork")) {
document.getElementById("workComment").value = "";
2017-02-09 10:26:22 -05:00
var res = Session.get("restrictText");
res[Object.keys(res)[0]] = "";
Session.set("restrictText", res);
Session.set("currentWork", null);
}
2016-08-12 23:10:27 -04:00
}
2017-04-27 02:43:14 -04:00
if (document.getElementById("userDropdown").style.display === "block" &&
!document.getElementById("userDropdown").contains(event.target))
$("#userDropdown").velocity("fadeOut", 150);
2016-08-12 23:10:27 -04:00
},
2016-08-30 18:49:33 -04:00
// MAIN MENU BUTTONS
2016-10-23 22:11:27 -04:00
'click .fa-bars' (event) { // Click menu button.
toggleToSidebar("menu");
2016-08-30 18:49:33 -04:00
},
2016-10-23 22:11:27 -04:00
'click .fa-cog' (event) { // Click settings button.
toggleToSidebar("option");
},
2016-11-01 23:07:09 -04:00
'click .fa-question' (event) { // Click requests button.
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
sAlert.error("Not available in demo!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
toggleToSidebar("requests");
2016-10-23 22:11:27 -04:00
},
2016-08-30 18:49:33 -04:00
'click .creWork' (event) { // Cick add work button.
2016-09-04 20:10:58 -04:00
var attr;
2017-04-25 02:46:06 -04:00
attr = event.target.getAttribute("classid");
2016-08-12 23:10:27 -04:00
Session.set("newWork", true);
2017-04-25 02:46:06 -04:00
Session.set("currentWork",{class: attr, dueDate: moment().startOf('day').add(1,"days")._d, type:"normal"});
$(".overlay").velocity("fadeIn", 150);
2017-02-09 09:01:18 -05:00
$("#comment").slimScroll({
width: '100%',
height: '20vh',
touchScrollStep: 90
});
2017-02-09 10:32:27 -05:00
$("#workComments .slimScrollBar").css("display", "none");
2016-11-08 20:19:22 -05:00
},
'click .fa-check-circle-o' () { // Confirmation Button
sendData(confirm);
$("#confirmOverlay").velocity("fadeOut", 150);
2016-11-08 20:19:22 -05:00
if(confirm === "changeAdmin") {
$("#changeAdminWrapper").velocity("fadeOut", 150);
2016-11-08 20:19:22 -05:00
} else if(confirm === "deleteClass") {
Session.set("classInfo", null);
}
serverData = null;
confirm = null;
2016-11-08 20:19:22 -05:00
},
'click .fa-times-circle-o' () { // Deny Button
$("#confirmOverlay").velocity("fadeOut", 150);
2016-11-08 20:19:22 -05:00
serverData = null;
confirm = null;
2016-09-04 20:10:58 -04:00
},
2016-09-07 15:33:13 -04:00
'click #dropdown' (event) {
2016-10-03 23:13:05 -04:00
if (document.getElementById("userDropdown").style.display === "block") return;
$("#userDropdown").velocity("fadeIn", 150);
2016-09-07 15:33:13 -04:00
},
2016-08-30 18:49:33 -04:00
'click .workCard' (event) { // Display work information on work card click.
2017-04-27 03:04:14 -04:00
if(event.target.className.indexOf("fa") !== -1) return;
2016-10-09 22:57:04 -04:00
var workid = event.target.getAttribute("workid");
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var thisWork = Session.get("myWork").filter(function(obj) {
return obj._id === workid;
})[0];
} else {
var thisWork = work.findOne({
_id: workid
});
}
2016-09-04 20:13:31 -04:00
2016-10-09 22:57:04 -04:00
Session.set("newWork", false);
Session.set("currentWork", thisWork);
$(".overlay").velocity("fadeIn", 150);
2017-02-09 09:01:18 -05:00
$("#comment").slimScroll({
width: '100%',
height: '20vh',
touchScrollStep: 90
});
2017-02-09 10:32:27 -05:00
$("#workComments .slimScrollBar").css("display", "none");
2016-08-12 23:10:27 -04:00
},
2016-09-08 16:51:22 -04:00
'click #requestSubmit' () {
var area = document.getElementById("requestArea");
2016-10-03 23:13:05 -04:00
if (area.value === "") return;
2016-09-08 16:51:22 -04:00
var array = {};
array.content = area.value;
array.info = {
2016-09-09 08:20:51 -04:00
"users": Meteor.users.find().fetch(),
2016-09-08 16:51:22 -04:00
"userInfo": Meteor.user(),
"userClasses": Session.get("myClasses")
2016-09-08 16:51:22 -04:00
};
2016-10-03 23:13:05 -04:00
Meteor.call("createRequest", array, function(err, result) {
area.value = "";
Session.set("restrictText", {});
$("#requestSubmit span:first-child").fadeOut(200, function() {
$("#requestSubmit span:nth-child(2)").velocity("fadeIn", 200);
})
2016-10-03 23:13:05 -04:00
setTimeout(function() {
$("#requestSubmit span:nth-child(2)").fadeOut(200, function() {
$("#requestSubmit span:first-child").velocity("fadeIn", 200);
})
}, 1250);
2016-09-11 18:47:50 -04:00
});
2016-09-08 16:51:22 -04:00
},
'click #exportDiv' (event) {
2016-10-23 17:50:05 -04:00
var events = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN";
var userClasses = Session.get("myClasses");
2016-10-23 17:50:05 -04:00
var timestamp = new Date().toJSON().replace(/-|:|\./gi, "");
for (var i = 0; i < userClasses.length; i++) {
var works = userClasses[i].thisClassWork;
for (var j = 0; j < works.length; j++) {
var work = works[j];
var workclass = classes.findOne({
_id: work.class
});
if (work.description == defaultWork.description) work.description = "";
if (work.dueDate == defaultWork.dueDate) continue;
if (work.name == defaultWork.name) work.name = "";
if (workclass === undefined) workclass = {
name: "Personal"
};
2016-10-23 17:50:05 -04:00
if (work.description === undefined) {
work.description = "";
} else {
work.description = " - " + work.description;
}
var duedate = work.dueDate.toJSON().slice(0,10).replace(/-/gi,"");
2016-10-23 17:50:05 -04:00
events += "\nBEGIN:VEVENT" +
"\nUID:" + timestamp + work._id + "@hourglass.tk" +
"\nDTSTAMP:" + timestamp +
"\nDTSTART:" + duedate +
"\nDTEND:" + duedate +
"\nSUMMARY:" + work.name + work.description +
"\nCATEGORIES:" + workclass.name +
"\nEND:VEVENT";
}
2016-10-23 17:50:05 -04:00
events += "\nEND:VCALENDAR";
}
2016-10-23 17:50:05 -04:00
var eventBlob = new Blob([events], {
type: "data:text/ics;charset=utf-8"
});
2016-10-23 17:50:05 -04:00
saveAs(eventBlob, "hourglass.ics");
2016-10-12 01:45:36 -04:00
},
// HANDLING INPUT CHANGING
2016-10-12 01:45:36 -04:00
'focus .clickModify' (event) {
2017-02-09 10:26:22 -05:00
toggleOptionMenu(false, modifyingInput);
if(modifyingInput !== null) {
if(!$("#"+modifyingInput)[0].className.includes("dropdown")) closeInput(modifyingInput);
2016-10-28 23:59:02 -04:00
}
modifyingInput = event.target.id;
if(!$("#"+modifyingInput)[0].className.includes("dropdown")) {
event.target.select();
2016-10-28 23:59:02 -04:00
event.target.style.cursor = "text";
2017-02-03 03:43:21 -05:00
event.target.style.backgroundColor = "rgba(0,0,0,0.1)";
}
2016-08-12 23:10:27 -04:00
},
2017-02-09 10:26:22 -05:00
'keydown #wName' (event) {
if(event.keyCode === 13) {
closeInput(modifyingInput);
event.target.blur();
}
},
'focus #workComment' () {
toggleOptionMenu(false, modifyingInput);
modifyingInput = null;
},
2016-10-09 22:57:04 -04:00
'keydown .dropdown' (event) {
var first = $("#"+modifyingInput).next().children("p:first-child");
2016-10-28 23:59:02 -04:00
var last = $("#"+modifyingInput).next().children("p:last-child");
var next = $(".selectedOption").next();
var prev = $(".selectedOption").prev();
var lastSel = $(".selectedOption");
if (event.keyCode === 38) {
2016-10-18 16:27:42 -04:00
event.preventDefault();
if (lastSel === undefined) {
last.addClass("selectedOption");
} else {
if (prev.length === 0) {
last.addClass("selectedOption");
2016-10-28 23:59:02 -04:00
lastSel.removeClass("selectedOption");
} else {
prev.addClass("selectedOption");
lastSel.removeClass("selectedOption");
}
}
} else if (event.keyCode === 40) {
2016-10-18 16:27:42 -04:00
event.preventDefault();
if (lastSel === undefined) {
first.addClass("selectedOption");
last.removeClass("selectedOption");
} else {
if (next.length === 0) {
first.addClass("selectedOption");
lastSel.removeClass("selectedOption");
} else {
next.addClass("selectedOption");
lastSel.removeClass("selectedOption");
}
2016-10-28 23:59:02 -04:00
}
} else if (event.keyCode === 13) {
lastSel[0].click();
$("#"+modifyingInput)[0].focus();
}
2016-10-09 22:57:04 -04:00
},
'click .dropdown, focus .dropdown' (event) {
if(clickDisabled) return;
clickDisabled = true;
if(event.target.id === optionOpen[0] && optionOpen[1]) {
toggleOptionMenu(false, event.target.id);
} else {
toggleOptionMenu(true, event.target.id);
}
setTimeout(function(){clickDisabled = false;},130); // Prevents spamming and handles extra click events.
2016-10-09 22:57:04 -04:00
},
'click .optionText' (event) { // Click each preferences setting.
var option = event.target.childNodes[0].nodeValue;
if(modifyingInput[0] === 'w') {
2017-04-25 02:46:06 -04:00
document.getElementById(modifyingInput).children[0].childNodes[0].nodeValue = option;
2016-10-09 22:57:04 -04:00
var newSetting = Session.get("currentWork");
2017-04-25 02:46:06 -04:00
newSetting.type = options.type.filter(function(names) {
return names.alias === option;
})[0].val;
2016-10-09 22:57:04 -04:00
Session.set("currentWork", newSetting);
2017-04-25 02:46:06 -04:00
closeInput();
toggleOptionMenu(false, modifyingInput);
2017-04-25 02:46:06 -04:00
modifyingInput = null;
2016-11-08 20:19:22 -05:00
} else if(modifyingInput.slice(0,3) === "cre") {
document.getElementById(modifyingInput).value = option;
2016-08-22 23:44:48 -04:00
} else {
2016-10-09 22:57:04 -04:00
var newSetting = Session.get("user");
newSetting.preferences[modifyingInput] = (function() {
var value = options[modifyingInput].filter(function(entry) {
return option === entry.alias;
})[0].val;
return (modifyingInput === 'theme') ? themeColors[value] : value;
})();
Session.set("user", newSetting);
serverData = Session.get("user");
2016-10-28 23:47:26 -04:00
sendData("editProfile");
2016-08-22 23:44:48 -04:00
}
2016-10-09 22:57:04 -04:00
toggleOptionMenu(false, modifyingInput);
$(".selectedOption").removeClass("selectedOption");
},
2016-09-05 14:51:40 -04:00
'input .restrict' (event) {
var restrict = event.target.maxLength;
var chars = restrict - event.target.value.length;
2016-10-16 02:15:47 -04:00
var newSetting = Session.get("restrictText");
newSetting[event.target.id] = (chars === restrict) ? "" : (chars.toString() + ((chars === 1) ? " character " : " characters ") + "left");
newSetting.selected = event.target.id;
Session.set("restrictText", newSetting);
2016-09-05 14:51:40 -04:00
},
2016-10-12 01:45:36 -04:00
'focus #wDueDate' () { // Open date picker.
$('#wDueDate').datepicker({
2016-08-30 18:49:33 -04:00
format: 'DD, MM d, yyyy',
2016-10-12 01:45:36 -04:00
clickInput: true,
2016-08-30 18:49:33 -04:00
startDate: 'd',
todayHighlight: true,
todayBtn: true,
autoclose: true
2016-08-30 18:49:33 -04:00
});
},
// WORK OVERLAY BUTTONS
'click #commentSubmit' (event) { // Click to submit a comment.
2017-09-16 22:59:59 -04:00
if (Session.get("demo")) {
sAlert.error("Not available in demo!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
2016-10-16 02:15:47 -04:00
workId = Session.get("currentWork")._id;
var input = document.getElementById('workComment');
comment = input.value;
input.value = "";
2016-08-22 16:52:04 -04:00
if (comment !== "") {
document.getElementById('workComment').value = "";
2016-10-16 02:15:47 -04:00
serverData = [comment, workId];
sendData("addComment");
2016-08-22 16:52:04 -04:00
}
},
2016-08-30 18:49:33 -04:00
'click #workSubmit' () { // Click submit work to create a work.
2017-04-25 02:46:06 -04:00
var thisWork = Session.get("currentWork");
var no = [];
if(thisWork.name === "") no.push("name");
if(thisWork.dueDate === "") no.push("due date");
if(thisWork.type === "") no.push("type");
if(no.length === 0) {
serverData = Session.get("currentWork");
sendData("createWork");
$(".overlay").velocity("fadeOut", 150);
Session.set("currentWork", null);
2017-04-25 02:46:06 -04:00
} else {
var message = no.reduce(function(a, b) {
return (b === no[no.length - 1]) ? a + ((no.length === 2) ? " and " : ", and ") + b : a + ", " + b;
});
sAlert.error("You are missing the " + message + "!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
},
'click #workDelete' () {
2016-10-16 02:15:47 -04:00
serverData = Session.get("currentWork")._id;
sendData("deleteWork");
$(".overlay").velocity("fadeOut", 150);
Session.set("currentWork", null);
},
2016-08-30 18:49:33 -04:00
'click #markDone' () { // Click done button.
serverData = [Session.get("currentWork")._id, "done"];
2016-08-30 18:49:33 -04:00
sendData("toggleWork");
},
2016-08-30 18:49:33 -04:00
'click #markConfirm' () { // Click confirm button.
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
sAlert.error("Not available in demo!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
serverData = [Session.get("currentWork")._id, "confirmations"];
2016-08-30 18:49:33 -04:00
sendData("toggleWork");
},
'click #markReport' () { // Click report button.
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
sAlert.error("Not available in demo!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
serverData = [Session.get("currentWork")._id, "reports"];
2016-08-30 18:49:33 -04:00
sendData("toggleWork");
2017-04-13 01:10:16 -04:00
},
'click .cWorkBottom .fa-thumbs-up' (event) {
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) return;
2017-04-13 01:10:16 -04:00
serverData = [event.target.parentNode.parentNode.parentNode.parentNode.getAttribute("workid"), "confirmations"]
2017-04-18 04:28:05 -04:00
sendData("toggleWork");
2017-04-13 01:10:16 -04:00
},
'click .cWorkBottom .fa-exclamation-triangle' (event) {
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) return;
2017-04-13 01:10:16 -04:00
serverData = [event.target.parentNode.parentNode.parentNode.parentNode.getAttribute("workid"), "reports"]
2017-04-18 04:28:05 -04:00
sendData("toggleWork");
2017-04-15 22:28:43 -04:00
},
'click #signout' () {
$(".noScroll").velocity("fadeOut", 50);
Session.set('sidebarMode','');
2017-09-16 22:50:25 -04:00
if (Session.get("demo")) {
Session.set("demo", false);
return;
}
2017-04-15 22:28:43 -04:00
document.getElementById('login-buttons-logout').click();
}
2016-08-09 01:33:14 -04:00
});
2017-04-18 04:28:05 -04:00
Template.classesMode.helpers({
thisClassWork() {
var id = this._id;
return Session.get("myWork").filter(function(work) {
return work.classid === id;
});
}
});
2016-10-16 02:15:47 -04:00
// Other Functions
2017-01-15 23:59:10 -05:00
toggleOptionMenu = function(toggle, menu) {
if(toggle) {
$(".selectedOption").removeClass("selectedOption");
2017-05-07 01:45:37 -04:00
$("#" + menu).next()
.css('opacity', 0)
.slideDown(300)
.animate(
{ opacity: 1 },
2017-05-07 01:45:37 -04:00
{ queue: false, duration: 100 }
);
optionOpen = [menu, toggle];
2016-10-23 22:11:27 -04:00
} else {
2017-05-07 01:45:37 -04:00
$("#" + menu).next().slideUp(100, function() {
$(this).css("opacity", 0);
});
optionOpen = [null, toggle];
2016-10-23 22:11:27 -04:00
}
}
sendData = function(funcName) { // Call Meteor function, and do actions after function is completed depending on function.
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var allWork = Session.get("myWork").concat(Session.get("filterWork"));
if(funcName === "editProfile") {
Session.set("user", serverData);
} else if(funcName === "editWork") {
updateWork(serverData._id, serverData, "changed");
} else if(funcName === "createWork") {
serverData._id = allWork.length;
serverData.done = [];
serverData.confirmationLength = 1;
serverData.reportLength = 0;
updateWork(serverData._id, serverData, "added");
} else if(funcName === "toggleWork") {
var toggle = allWork.filter(function(obj) {
return serverData[0] === obj._id;
})[0];
if(toggle.done.length === 0) {
toggle.done = ["yes"];
} else {
toggle.done = [];
}
updateWork(serverData[0], toggle, "changed");
} else if(funcName === "deleteWork") {
updateWork(serverData, null, "remove")
2016-10-19 01:24:14 -04:00
}
2017-09-16 22:50:25 -04:00
filterWork();
} else {
if(funcName === "editWork" || funcName === "createWork") {
for(var key in serverData) {
if(serverData[key] === true) serverData[key] = "";
}
}
if(funcName === "editWork") workChanger = true;
if(funcName === "editProfile") filterWork();
Meteor.call(funcName, serverData, function(error, result) {
if(funcName === "createWork") Session.set("currentWork", null);
serverData = null;
if (error !== undefined) {
console.log(funcName);
console.log(error);
sAlert.error(error.error[1] || error.message, {
effect: 'stackslide',
position: 'top'
});
} else {
if(funcName === "createClass") {
var inputs = document.getElementsByClassName("creInput");
for(var i = 0; i < inputs.length; i++) {
inputs[i].value = "";
}
toggleToMode("manageClass");
2016-11-08 20:19:22 -05:00
}
}
2017-09-16 22:50:25 -04:00
});
}
document.getElementsByTagName("body")[0].style.color = Session.get("user").preferences.theme.textColor;
2016-08-12 12:28:55 -04:00
}
2016-08-12 23:10:27 -04:00
2016-10-09 22:57:04 -04:00
function closeInput() { // Close a changeable input and change it back to span.
2017-04-25 02:46:06 -04:00
var thisWork = Session.get("currentWork");
var inputs = $(".clickModify");
2016-10-16 02:15:47 -04:00
Session.set("restrictText", {});
2017-04-25 02:46:06 -04:00
if(modifyingInput !== "wType") {
$("#"+modifyingInput).css('cursor','pointer');
$("#"+modifyingInput).css('background-color', 'rgba(0,0,0,0)');
2016-10-28 23:59:02 -04:00
}
2016-08-12 23:10:27 -04:00
2017-04-25 02:46:06 -04:00
thisWork.name = inputs[0].value;
thisWork.dueDate = toDate(inputs[1].value);
thisWork.description = inputs[2].value;
thisWork.type = options.type.filter(function(names) {
return names.alias === inputs[3].children[0].innerHTML;
})[0].val;
Session.set("currentWork", thisWork);
2016-08-22 23:44:48 -04:00
2017-04-25 02:46:06 -04:00
if(!Session.get("newWork")) {
var no = [];
if(thisWork.name === "") no.push("name");
if(thisWork.dueDate === "") no.push("due date");
if(thisWork.type === "") no.push("type");
if(no.length === 0) {
serverData = Session.get("currentWork");
sendData("editWork");
} else {
2017-04-25 02:46:06 -04:00
var message = no.reduce(function(a, b) {
return (b === no[no.length - 1]) ? a + ((no.length === 2) ? " and " : ", and ") + b : a + ", " + b;
});
sAlert.error("You are missing " + message + "!", {
effect: 'stackslide',
position: 'top',
timeout: 2000
});
return;
}
}
}
2016-09-04 20:13:31 -04:00
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
2017-04-08 00:07:00 -04:00
getReadableDate = function(date) { // Get readable date from Date constructor.
2016-09-04 20:13:31 -04:00
return days[date.getDay()] + ", " + months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
2016-08-13 20:22:54 -04:00
}
2017-04-08 00:07:00 -04:00
toDate = function(date) { // Turns formatted date back to Date constructor.
2016-09-04 20:13:31 -04:00
date = date.substring(date.search(",") + 2, date.length);
month = months.indexOf(date.substring(0, date.search(" ")));
day = date.substring(date.search(" ") + 1, date.search(","));
year = date.substring(date.search(",") + 2, date.length);
2017-05-02 20:51:30 -04:00
return new Date(year, month, day, 12,0,0);
2016-08-14 07:52:27 -04:00
}
2016-10-16 02:15:47 -04:00
2017-01-17 00:18:26 -05:00
checkComplete = function(required, inputs) {
var values = {};
var no = [];
for (var i = 0; i < inputs.length; i++) {
var val = inputs[i].value;
var where = inputs[i].getAttribute("form");
if (val === "" && _.contains(required, where)) {
no.push(where);
}
values[where] = val;
}
if (no.length > 0) { // Check missing fields.
return [false,no.reduce(function(a, b) {
return (b === no[no.length - 1]) ? a + ((no.length === 2) ? " and " : ", and ") + b : a + ", " + b;
}), values];
} else {
return [true,"", values];
}
}
startDragula = function() {
2016-10-16 02:15:47 -04:00
dragula([document.querySelector('#classesMode'), document.querySelector('#nonexistant')], {
moves: function(el, container, handle) {
2017-04-13 10:59:20 -04:00
return _.intersection(["classInfo"], handle.classList).length > 0;
2016-10-16 02:15:47 -04:00
}
})
2017-04-15 22:28:43 -04:00
.on('drag', function() {
dragging = true;
})
.on('out', function(el) {
var els = document.getElementsByClassName("classWrapper");
if($(els[0]).hasClass("gu-transit")) return;
dragging = false;
2017-04-15 23:39:49 -04:00
clicked = false;
2017-04-15 22:28:43 -04:00
var final = [];
for (var i = 0; i < els.length; i++) {
var classid = els[i].getElementsByClassName("creWork")[0].getAttribute("classid");
final.push(classid);
}
Meteor.call("reorderClasses", final);
});
2016-11-18 20:51:04 -05:00
};
getClasses = function(myClasses) {
var array = [];
var classDisp = Session.get("classDisp");
for(var i = 0; i < myClasses.length; i++) {
var classObj = {};
if(myClasses[i] === Meteor.userId()) {
classObj.name = "Personal";
classObj.box = " owned";
classObj.mine = false; // Actual value is reversed.
classObj.subscribers = 1;
2017-04-25 10:43:42 -04:00
classObj.status = true;
classObj.admin = Meteor.userId();
classObj._id = Meteor.userId();
} else {
classObj = classes.findOne({_id: myClasses[i]});
if(classObj === undefined) return;
var isAdmin = classObj.admin === Meteor.userId();
classObj.box = (isAdmin) ? " owned" : "";
classObj.mine = (isAdmin) ? false : true; // Actual value is reversed
classObj.subscribers = classObj.subscribers.length;
2017-04-27 02:10:46 -04:00
classObj.teachershort = (classObj.teacher === undefined) ? "" : classObj.teacher.split(" ").slice(1);
}
classObj.selected = ((classDisp.indexOf(myClasses[i]) !== -1)) ? Session.get("user").preferences.theme.modeHighlight : "rgba(0,0,0,0)"; // Filter selected.
array.push(classObj);
}
Session.set("myClasses", array);
}
updateWork = function(id, fields, type) {
2017-04-25 22:09:03 -04:00
if(type === "added") {
var allWork = Session.get("myWork").concat(Session.get("filterWork"));
if(allWork.filter(function(work) {
return work._id === id;
}).length !== 0) return;
}
if(type === "remove" && Session.get("myWork").concat(Session.get("filterWork")).length === 0) return;
2017-04-25 22:09:03 -04:00
if(type === "remove" && Session.get("myWork").concat(Session.get("filterWork")).filter(function(work) { // Removed work and exists in user data.
return work._id === id;
}).length !== 0) {
Session.set("myWork", Session.get("myWork").filter(function(work) {
return work._id !== id;
}));
2017-04-25 22:09:03 -04:00
Session.set("filterWork", Session.get("filterWork").filter(function(work) {
return work._id !== id;
}));
return;
}
var workObj;
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
workObj = fields;
} else {
if(type === "added") {
workObj = Object.assign({}, fields, {_id: id});
} else if(type === "changed") {
workObj = work.findOne({_id: id});
}
}
2017-09-16 22:50:25 -04:00
if(!Session.get("demo")) {
if(Meteor.users.findOne({_id: workObj.creator}) === undefined) return;
if(Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) && !_.contains(Session.get("user").classes, workObj["class"])) return;
}
workObj.classid = workObj.class;
workObj.shortname = (workObj.name.length <= 20) ? workObj.name : workObj.name.substring(0,20) + "...";
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
workObj.className = workObj.class;
} else {
workObj.className = (workObj.classid === Meteor.userId()) ? "Personal" : classes.findOne({_id: workObj.classid}).name;
}
workObj.dateWord = (!(Meteor.Device.isPhone() || Meteor.Device.isTablet())) ?
moment(workObj.dueDate).calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] dddd',
sameElse: 'MMMM Do'
}) :
moment(workObj.dueDate).calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] ddd[.]',
sameElse: 'MMMM Do'
});
workObj.shortdesc = (workObj.description === undefined) ? "" : (workObj.description.length <= 30 ) ? workObj.description : workObj.description.substring(0,30) + "...";
if (workObj.dateWord === "Today") { // Font weight based on date proximity.
workObj.cardDate = "600";
} else if (workObj.dateWord === "Tomorrow") {
workObj.cardDate = "400";
}
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
workObj.confirmed = "#27A127";
workObj.reported = "";
} else {
workObj.confirmationLength = workObj.confirmations.length; // Counts the number of confirmations and reports for a particular work.
workObj.reportLength = workObj.reports.length;
workObj.confirmed = (_.contains(workObj.confirmations, Meteor.userId())) ? "#27A127" : "";
workObj.reported = (_.contains(workObj.reports, Meteor.userId())) ? "#FF1A1A" : "";
}
workObj.typeColor = workColors[workObj.type];
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
workObj.creatorname = "A. Robot";
} else {
workObj.creatorname = Meteor.users.findOne({
_id: workObj.creator
}).profile.name;
}
var normalColor = Session.get("user").preferences.theme.text;
// Ratio color handling
/*var conf = workObj.confirmations.length;
var repo = workObj.reports.length;
var ratio = conf / repo;
if (Math.abs(conf - repo)) {
if ((conf + repo) <= 1) {
2017-04-13 01:10:16 -04:00
thisWork[j].doneRatio = normalColor;
} else {
thisWork[j].doneRatio = "#F9F906";
}
} else if (ratio >= 2) {
thisWork[j].doneRatio = "#33DD33";
} else if (ratio <= 0.9) {
thisWork[j].doneRatio = "#FF1A1A";
}*/
workObj.doneRatio = normalColor;
var myWork;
if(type === "added") {
myWork = Session.get("myWork");
myWork.push(workObj);
} else if(type === "changed") {
myWork = Session.get("myWork").filter(function(work) {
return work._id !== id;
});
myWork.push(workObj);
2017-04-25 03:34:59 -04:00
if(!Session.equals("currentWork", null) && Session.get("currentWork")._id === id) { //Notification beta, probably rework.
Session.set("currentWork", workObj);
2017-04-25 03:34:59 -04:00
/*if($(".overlay").css("display") !== "none") { // If currently viewing work.
var message = Object.keys(fields)[0].replace("dueDate", "due date");
if(!workChanger && message !== "comments" && message !== "done" && message !== "confirmations") sAlert.success("The " + message + " of this work was updated by someone else!", {
effect: 'stackslide',
position: 'top',
timeout: 1500
});
workChanger = false;
2017-04-25 03:34:59 -04:00
}*/
}
}
Session.set("myWork", myWork);
}
filterWork = function() {
var classDisp = Session.get("classDisp");
var typeFilter = Session.get("typeFilter");
var hideTime = Session.get("user").preferences.timeHide;
var allWork = Session.get("filterWork").concat(Session.get("myWork"));
var hideWork = [];
var displayWork = [];
_.each(allWork, function(workObj) {
var notInClassFilter = classDisp.length !== 0 && !_.contains(classDisp, workObj.classid);
var notInTypeFilter = typeFilter.length !== 0 && !_.contains(typeFilter, workObj.type);
var pastHideDate = hideTime !== 0 && (moment().subtract(hideTime, 'days'))._d > (moment(workObj.dueDate))._d;
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var markedDone = Session.get("user").preferences.done && workObj.done.length !== 0;
} else {
var markedDone = Session.get("user").preferences.done && !(Meteor.Device.isPhone() || Meteor.Device.isTablet()) && _.contains(workObj.done, Meteor.userId());
}
2017-04-25 03:07:56 -04:00
var reported = false; // Temp, working on reporting.
//var reported = (workObj.reportLength / (workObj.reportLength + workObj.confirmationLength)) > 0.7; // Over 70% are reports
if(notInClassFilter || notInTypeFilter || pastHideDate || markedDone || reported) {
hideWork.push(workObj);
} else {
displayWork.push(workObj);
}
});
2017-09-16 22:50:25 -04:00
Session.set("myWork", displayWork.sort(function(a,b) { // Display work.
2017-05-02 20:51:30 -04:00
if(Date.parse(a.dueDate) === Date.parse(b.dueDate)) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return (x < y) ? -1 : (x > y) ? 1 : 0;
}
return Date.parse(a.dueDate) - Date.parse(b.dueDate);
}));
Session.set("filterWork", hideWork); // Not displayed
calendarEvents();
$("#fullcalendar").fullCalendar("removeEvents");
$("#fullcalendar").fullCalendar("addEventSource", Session.get("calendarEvents"));
}
2017-04-18 04:28:05 -04:00
function calendarEvents() {
var events = [];
2017-04-18 04:28:05 -04:00
var myWork = Session.get("myWork");
for(var i = 0; i < myWork.length; i++) {
var work = myWork[i];
2017-09-16 22:50:25 -04:00
if(Session.get("demo")) {
var currClass = Session.get("myClasses").filter(function(obj) {
return obj.name === work.class;
})[0];
} else {
var currClass = classes.findOne({ _id: work.classid});
}
2017-04-18 04:28:05 -04:00
var inRole = false;
2017-09-16 22:50:25 -04:00
if (Session.get("demo") ||
work.class === Meteor.userId() ||
2017-04-18 04:28:05 -04:00
Meteor.userId() === work.creator ||
Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
currClass.moderators.indexOf(Meteor.userId()) !== -1 ||
2017-05-02 20:51:30 -04:00
currClass.banned.indexOf(Meteor.userId()) !== -1 ||
currClass.admin === Meteor.userId()
2017-04-18 04:28:05 -04:00
) inRole = true;
events.push({
id: work._id,
start: work.dueDate.toISOString().slice(0, 10),
2017-04-18 04:28:05 -04:00
title: work.name,
backgroundColor: workColors[work.type],
borderColor: "#444",
startEditable: inRole,
className: work.type + " workevent " + work.class
});
}
Session.set("calendarEvents", events);
}
2017-02-09 10:26:22 -05:00
function notifyMe() {
}