Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Changed buttons to icons and commented code
Browse files Browse the repository at this point in the history
  • Loading branch information
amininger committed Aug 29, 2015
1 parent 0db7ead commit 2d2f789
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 86 deletions.
84 changes: 52 additions & 32 deletions calendar_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,36 @@ var EventManager = function(){
}
};

// Take the data returned from the api and create the
// events to display in the popup
this.parseEvents = function(){
console.log("TASKS");
// Add each task
this.taskData.items.forEach(function(task){
this.events.push(new Task().fromObject(task));
}.bind(this));

console.log("EVENTS");
// Add each calendar event
// If a recurring event, only accept the first instance
event_ids = {};
this.calendarData.items.forEach(function(event){
if ("recurringEventId" in event){
console.log(event.recurringEventId);
if (event.recurringEventId in event_ids){
return;
}
event_ids[event.recurringEventId] = event.id;
}
if ('start' in event && 'end' in event){
this.events.push(new CalEvent().fromObject(event));
this.events.push(new CalendarEvent().fromObject(event));
}
}.bind(this));

this.sortEvents();
this.ready = true;
};

// Create the event from the given string
this.createEvent = function(stringRep){
console.log("EventManager::createEvent(" + stringRep + ")");
var e = null;
var colon = stringRep.indexOf(":");
if(colon == -1){
Expand All @@ -77,27 +81,34 @@ var EventManager = function(){
}
var prefix = stringRep.substr(0, colon);
console.log(prefix);
if(prefix.toLowerCase().startsWith("todo") || prefix.toLowerCase().startsWith("due")){

// Tasks must start with 'todo: ' or 'due: '
if(prefix.toLowerCase().startsWith("todo")
|| prefix.toLowerCase().startsWith("due")){
e = new Task().fromString(stringRep);
} else {
e = new CalEvent().fromString(stringRep);
e = new CalendarEvent().fromString(stringRep);
}

console.log(e.toString());
console.log("Created Event: " + e.toString());

this.events.push(e);
this.sortEvents();

if(e.getType() == "task"){
// Tell the google api to create the event
if(e.getType() === TASK){
api_postTask(this.authToken, e);
} else {
api_postCalendarEvent(this.authToken, e);
}
};

// Delete the event with the given id
this.deleteEvent = function(eventId){
console.log("EventManager::deleteEvent(" + eventId + ")");
var e = null;
var i = 0;
// Search for the event and delete it
for(i = 0; i < this.events.length; i++){
if(this.events[i].id == eventId){
e = this.events[i];
Expand All @@ -110,13 +121,15 @@ var EventManager = function(){
}
this.events.splice(i, 1);

if(e.getType() == "task"){
// Tell the google api to delete the event
if(e.getType() === TASK){
api_putTaskCompleted(this.authToken, e);
} else {
api_deleteCalendarEvent(this.authToken, e);
}
};

// Sort all the events by their timestamps
this.sortEvents = function(){
this.events.forEach(function(e){
e.calcSortKey();
Expand All @@ -127,11 +140,16 @@ var EventManager = function(){
};
};

// Task - defines a task in the default task list
// Can have a due date or not
var Task = function(){
this.id = null;
this.title = "";
this.dueDate = null;


// Create the task from a string representation
// Format: 'todo: <description>'
// Format: 'due MMM DD: <decription>'
this.fromString = function(str){
var colon = str.indexOf(":");
if (str.toLowerCase().startsWith("due")){
Expand All @@ -149,6 +167,7 @@ var Task = function(){
return this;
};

// Create the task from the object returned by google api
this.fromObject = function(obj){
this.id = obj.id;
this.title = obj.title;
Expand All @@ -163,6 +182,7 @@ var Task = function(){
return this;
};

// Generate a string representation of the task
this.toString = function(){
if(this.dueDate != null){
return this.dueDate.format("MMM D") + " " + getDOW(this.dueDate) + ": " + this.title;
Expand All @@ -171,34 +191,44 @@ var Task = function(){
}
};

// Return either TASK or CALENDAR_EVENT
this.getType = function(){
return "task";
return TASK;
}

// Calculate a string to use when sorting the events (timestamp)
this.calcSortKey = function(){
if(this.dueDate != null){
this.sortKey = this.dueDate.toISOString();
} else {
this.sortKey = defaultDateTime.toISOString();
this.sortKey = moment.tz("2000-01-01", "America/New_York").toISOString();
}
};

// Returns the object to use as the body of a HTTP POST used to create the task
this.getCreateBody = function(){
if(this.dueDate == null){
return { title: this.title };
} else {
return { title: this.title, due: toDate(this.dueDate) };
return { title: this.title, due: this.dueDate.format(fullDateFormat) };
}
};
};

var CalEvent = function(){
// CalendarEvent - defines an event on the primary calendar
// Can be a time range, a whole day, or multiple days
var CalendarEvent = function(){
this.id = null;
this.summary = "";

this.start = defaultDateTime;
this.end = defaultDateTime;
this.start = moment.tz(EST);
this.end = moment.tz(EST);

// Create the calendar event from a string representation
// Format: 'Sep 24: <description>' - Full day event
// Format: 'Jun 31-2: <description>' - Multi-day event
// Format: 'Apr 15 @16.15: <description>' - Hour long event at 4:15 PM
// Format: 'Jan 12 @9-15: <description>' - Event from 9AM to 3PM
this.fromString = function(str){
var prefix = str.toUpperCase().substring(0, str.indexOf(":"));
var dash = prefix.indexOf("-");
Expand Down Expand Up @@ -251,6 +281,7 @@ var CalEvent = function(){
return this;
};

// Create the calendar event from the object returned by the google api
this.fromObject = function(obj){
this.id = obj.id;
this.summary = obj.summary;
Expand All @@ -271,6 +302,7 @@ var CalEvent = function(){
return this;
};

// Generate a string representation of the event
this.toString = function(){
var str = this.start.format("MMM D");
str += " " + getDOW(this.start);
Expand All @@ -295,14 +327,17 @@ var CalEvent = function(){
return str;
};

// Returns either TASK or CALENDAR_EVENT
this.getType = function(){
return "event";
return CALENDAR_EVENT;
}

// Calculate a string to use when sorting the events (timestamp)
this.calcSortKey = function(){
this.sortKey = this.start.toISOString();
};

// Returns an object used in the body of an HTTP POST when creating
this.getCreateBody = function(){
if(this.start.hour() == 0){
return {
Expand All @@ -320,18 +355,3 @@ var CalEvent = function(){
};
};

function test(str){
var e = new CalEvent().fromString(str);
e.calcSortKey();
console.log(e.toString());
}

//test("Sep 1: stuff");
//test("Sep 1-4: stuff");
//test("Sep 30-4: stuff");
//test("Sep 30 @4: stuff");
//test("Sep 30 @4.15: stuff");
//test("Sep 30 @16-20: stuff");
//test("Sep 30 @16.30-20: stuff");
//test("Sep 30 @16-20.30: stuff");
//test("Sep 30 @16.15-20.30: stuff");
3 changes: 3 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ var REFRESH_DATA = "refresh-data";
var CREATE_EVENT = "create-event";
var DELETE_EVENT = "delete-event";

var TASK = "task";
var CALENDAR_EVENT = "calendar-event";

Binary file added create.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions google_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ function api_getTasks(token, callback){

xhr.onerror = function () {
console.log("google_api: HTTP GET ERROR [TASK]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP GET SUCCESS [TASK]:");
console.log(" response: ");
console.log(this.response);
callback(this.response);
};
Expand Down Expand Up @@ -53,13 +51,11 @@ function api_getCalendarEvents(token, callback){

xhr.onerror = function () {
console.log("google_api: HTTP GET ERROR [CAL]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP GET SUCCESS [CAL]");
console.log(" response: ");
console.log(this.response);

callback(this.response);
Expand All @@ -85,13 +81,11 @@ function api_postTask(token, task){

xhr.onerror = function () {
console.log("google_api: HTTP POST ERROR [TASK]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP POST SUCCESS [TASK]");
console.log(" response =");
console.log(this.response);

task.id = this.response.id;
Expand Down Expand Up @@ -120,13 +114,11 @@ function api_postCalendarEvent(token, calEvent){

xhr.onerror = function () {
console.log("google_api: HTTP POST ERROR [EVENT]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP POST SUCCESS [EVENT]");
console.log(" response =");
console.log(this.response);

calEvent.id = this.response.id;
Expand Down Expand Up @@ -156,13 +148,11 @@ function api_putTaskCompleted(token, task){

xhr.onerror = function () {
console.log("google_api: HTTP PUT ERROR [TASK]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP PUT SUCCESS [TASK]");
console.log(" response:");
console.log(this.response);
};

Expand All @@ -189,13 +179,11 @@ function api_deleteCalendarEvent(token, calEvent){

xhr.onerror = function () {
console.log("google_api: HTTP DELETE ERROR [EVENT]");
console.log(" err info:");
console.log(this);
};

xhr.onload = function() {
console.log("google_api: HTTP DELETE SUCCESS [EVENT]");
console.log(" response:");
console.log(this.response);
};

Expand Down
8 changes: 2 additions & 6 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
</button>
</div>
<div id="content-div" style="display: none">
<!-- Div Containing the Refresh Button -->
<div id="refresh-div">
<button id="refresh-button">Refresh</button>
</div>

<!-- Div Containing the Create Event Form -->
<div id="create-event-div">
<input type="image" id="refresh-button" src="sync.png" alt="Sync" />
<input type="text" id="create-event-text"></input>
<button id="create-event-button">Create Event</button>
<input type="image" id="create-event-button" src="create.png" alt="Create" />
</div>

<!-- Div Containing the Event List (Populated by javascript) -->
Expand Down
Loading

0 comments on commit 2d2f789

Please sign in to comment.