Skip to content

Instantly share code, notes, and snippets.

@enricopolanski
Created March 2, 2018 10:13
Show Gist options
  • Select an option

  • Save enricopolanski/0c5256998793f2151e3bba9722feb95f to your computer and use it in GitHub Desktop.

Select an option

Save enricopolanski/0c5256998793f2151e3bba9722feb95f to your computer and use it in GitHub Desktop.
'use strict';
function Init() {
// Get current week's Monday.
this.startingWeek = (function startingWeek() {
const date = new Date();
// If it's Sunday, start from the Sunday before
if (date.getDay() === 0) {
date.setDate(date.getDate() - 7);
}
// then set the date to Monday
date.setDate((date.getDate() - date.getDay()) + 1);
return date;
}());
this.days = {};
this.addDay = function addDay(date) {
this.days[date] = [];
};
/* Each studyBlock object contains a key, value representation of
topic and state, where state is either "todo", for non completed
study blocks, or "done" for completed study blocks. */
this.addStudyBlock = function addStudyBlock(date, topic) {
if (!Object.prototype.hasOwnProperty.call(this, topic)) {
return false;
}
const studyBlock = {};
studyBlock[String(topic)] = 'todo';
this.days[date].push(studyBlock);
return true;
};
// We only refer to the index as each day is an Array of studyBlock objects.
this.checkStudyBlock = function checkStudyBlock(date, studyBlockIndex) {
const topic = Object.keys(this.days[date][studyBlockIndex])[0];
this.days[date][studyBlockIndex][topic] = 'done';
};
this.removeStudyBlock = function removeStudyBlock(date, studyBlock) {
return this.days[date].splice(studyBlock, 1);
};
this.topics = {};
this.addTopic = function addTopic(topic) {
if (Object.prototype.hasOwnProperty.call(this.topics, topic)) {
return false;
}
this.topics[topic] = {
'goals': [],
'sources': {},
'comments': {},
'blockscount': 0,
};
return true;
};
this.removeTopic = function removeTopic(topic) {
if (this.topics[topic]) {
return delete this.topics[topic];
}
return false;
};
// to do: swap even non consecutive topics.
this.swapStudyBlocks = function swapStudyBlocks(date, studyBlockIndex, direction) {
let dir = direction;
const dateArray = this.days[date];
if (dateArray.length === 1) {
return date;
}
if (dir === 'up' && studyBlockIndex !== 0) {
dir = parseInt(studyBlockIndex, 10) - 1;
} else {
dir = parseInt(studyBlockIndex, 10) + 1;
}
return dateArray.splice(dir, 1, dateArray.splice(studyBlockIndex, 1, dateArray[dir])[0]);
};
this.addGoal = function addGoal(topic, goalText) {
this.topics[topic].goals.push({
'description': goalText,
'completed': false,
});
};
this.addSource = function addSource(topic, source, link) {
if (!Object.prototype.hasOwnProperty.call(this, topic)) {
return false;
}
this.topics[topic].sources[source] = link;
return true;
};
this.removeSource = function removeSource(topic, source) {
delete this.topics[topic].sources[source];
};
this.addCount = function addCount(topic) {
this.topics[topic].blockscount += 1;
};
this.addComment = function addComment(topic, title, comment) {
if (!Object.prototype.hasOwnProperty.call(this, topic)) {
return false;
}
this.topics[topic].comments[title] = comment;
return true;
};
this.removeComment = function removeComment(topic, title) {
delete this.topics[topic].comments[title];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment