diff --git a/README.md b/README.md index 6b865d9..b699042 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ Okay: [**demo**](http://timetablejs.grible.co). Also, have a look at the website ## Installation Install with bower, or alternatively download the [ZIP](https://github.com/Grible/timetable.js/archive/master.zip): +``` +npm install timetable.js +``` +_or_ ``` bower install timetable ``` diff --git a/app/scripts/plugin.js b/app/scripts/plugin.js index d784dc4..7b1e871 100644 --- a/app/scripts/plugin.js +++ b/app/scripts/plugin.js @@ -3,245 +3,245 @@ 'use strict'; var Timetable = function() { - this.scope = { - hourStart: 9, - hourEnd: 17 - }; - this.locations = []; - this.events = []; + this.scope = { + hourStart: 9, + hourEnd: 17 + }; + this.locations = []; + this.events = []; }; Timetable.Renderer = function(tt) { - if (!(tt instanceof Timetable)) { - throw new Error('Initialize renderer using a Timetable'); - } - this.timetable = tt; + if (!(tt instanceof Timetable)) { + throw new Error('Initialize renderer using a Timetable'); + } + this.timetable = tt; }; (function() { - function isValidHourRange(start, end) { - return isValidHour(start) && isValidHour(end); - } - function isValidHour(number) { - return isInt(number) && isInHourRange(number); - } - function isInt(number) { - return number === parseInt(number, 10); - } - function isInHourRange(number) { - return number >= 0 && number < 24; - } - function locationExistsIn(loc, locs) { - return locs.indexOf(loc) !== -1; - } - function isValidTimeRange(start, end) { - var correctTypes = start instanceof Date && end instanceof Date; - var correctOrder = start < end; - return correctTypes && correctOrder; - } - function getDurationHours(startHour, endHour) { - return endHour >= startHour ? endHour - startHour : 24 + endHour - startHour; - } - - Timetable.prototype = { - setScope: function(start, end) { - if (isValidHourRange(start, end)) { - this.scope.hourStart = start; - this.scope.hourEnd = end; - } else { - throw new RangeError('Timetable scope should consist of (start, end) in whole hours from 0 to 23'); - } - - return this; - }, - addLocations: function(newLocations) { - function hasProperFormat() { - return newLocations instanceof Array; - } - - var existingLocations = this.locations; - - if (hasProperFormat()) { - newLocations.forEach(function(loc) { - if (!locationExistsIn(loc, existingLocations)) { - existingLocations.push(loc); - } else { - throw new Error('Location already exists'); - } - }); - } else { - throw new Error('Tried to add locations in wrong format'); - } - - return this; - }, - addEvent: function(name, location, start, end, options) { - if (!locationExistsIn(location, this.locations)) { - throw new Error('Unknown location'); - } - if (!isValidTimeRange(start, end)) { - throw new Error('Invalid time range: ' + JSON.stringify([start, end])); - } - - var optionsHasValidType = Object.prototype.toString.call(options) === '[object Object]'; - - this.events.push({ - name: name, - location: location, - startDate: start, - endDate: end, - options: optionsHasValidType ? options : undefined - }); - - return this; - } - }; - - function emptyNode(node) { - while (node.firstChild) { - node.removeChild(node.firstChild); - } - } - - function prettyFormatHour(hour) { - var prefix = hour < 10 ? '0' : ''; - return prefix + hour + ':00'; - } - - Timetable.Renderer.prototype = { - draw: function(selector) { + function isValidHourRange(start, end) { + return isValidHour(start) && isValidHour(end); + } + function isValidHour(number) { + return isInt(number) && isInHourRange(number); + } + function isInt(number) { + return number === parseInt(number, 10); + } + function isInHourRange(number) { + return number >= 0 && number < 24; + } + function locationExistsIn(loc, locs) { + return locs.indexOf(loc) !== -1; + } + function isValidTimeRange(start, end) { + var correctTypes = start instanceof Date && end instanceof Date; + var correctOrder = start < end; + return correctTypes && correctOrder; + } + function getDurationHours(startHour, endHour) { + return endHour >= startHour ? endHour - startHour : 24 + endHour - startHour; + } + + Timetable.prototype = { + setScope: function(start, end) { + if (isValidHourRange(start, end)) { + this.scope.hourStart = start; + this.scope.hourEnd = end; + } else { + throw new RangeError('Timetable scope should consist of (start, end) in whole hours from 0 to 23'); + } + + return this; + }, + addLocations: function(newLocations) { + function hasProperFormat() { + return newLocations instanceof Array; + } + + var existingLocations = this.locations; + + if (hasProperFormat()) { + newLocations.forEach(function(loc) { + if (!locationExistsIn(loc, existingLocations)) { + existingLocations.push(loc); + } else { + throw new Error('Location already exists'); + } + }); + } else { + throw new Error('Tried to add locations in wrong format'); + } + + return this; + }, + addEvent: function(name, location, start, end, options) { + if (!locationExistsIn(location, this.locations)) { + throw new Error('Unknown location'); + } + if (!isValidTimeRange(start, end)) { + throw new Error('Invalid time range: ' + JSON.stringify([start, end])); + } + + var optionsHasValidType = Object.prototype.toString.call(options) === '[object Object]'; + + this.events.push({ + name: name, + location: location, + startDate: start, + endDate: end, + options: optionsHasValidType ? options : undefined + }); + + return this; + } + }; + + function emptyNode(node) { + while (node.firstChild) { + node.removeChild(node.firstChild); + } + } + + function prettyFormatHour(hour) { + var prefix = hour < 10 ? '0' : ''; + return prefix + hour + ':00'; + } + + Timetable.Renderer.prototype = { + draw: function(selector) { var timetable = this.timetable; - function checkContainerPrecondition(container) { - if (container === null) { - throw new Error('Timetable container not found'); - } - } - function appendTimetableAside(container) { - var asideNode = container.appendChild(document.createElement('aside')); - var asideULNode = asideNode.appendChild(document.createElement('ul')); - appendRowHeaders(asideULNode); - } - function appendRowHeaders(ulNode) { - for (var k=0; k=0&&e<24}function r(e,t){return t.indexOf(e)!==-1}function i(e,t){var n=e instanceof Date&&t instanceof Date,o=e=e?t-e:24+t-e}function c(e){for(;e.firstChild;)e.removeChild(e.firstChild)}function s(e){var t=e<10?"0":"";return t+e+":00"}Timetable.prototype={setScope:function(t,n){if(!e(t,n))throw new RangeError("Timetable scope should consist of (start, end) in whole hours from 0 to 23");return this.scope.hourStart=t,this.scope.hourEnd=n,this},addLocations:function(e){function t(){return e instanceof Array}var n=this.locations;if(!t())throw new Error("Tried to add locations in wrong format");return e.forEach(function(e){if(r(e,n))throw new Error("Location already exists");n.push(e)}),this},addEvent:function(e,t,n,o,a){if(!r(t,this.locations))throw new Error("Unknown location");if(!i(n,o))throw new Error("Invalid time range: "+JSON.stringify([n,o]));var c="[object Object]"===Object.prototype.toString.call(a);return this.events.push({name:e,location:t,startDate:n,endDate:o,options:c?a:void 0}),this}},Timetable.Renderer.prototype={draw:function(e){function t(e){if(null===e)throw new Error("Timetable container not found")}function n(e){var t=e.appendChild(document.createElement("aside")),n=t.appendChild(document.createElement("ul"));o(n)}function o(e){for(var t=0;t=0&&e<24}function i(e,t){return t.indexOf(e)!==-1}function a(e,t){var n=e instanceof Date&&t instanceof Date,o=e=e?t-e:24+t-e}function s(e){for(;e.firstChild;)e.removeChild(e.firstChild)}function l(e){var t=e<10?"0":"";return t+e+":00"}e.prototype={setScope:function(e,n){if(!t(e,n))throw new RangeError("Timetable scope should consist of (start, end) in whole hours from 0 to 23");return this.scope.hourStart=e,this.scope.hourEnd=n,this},addLocations:function(e){function t(){return e instanceof Array}var n=this.locations;if(!t())throw new Error("Tried to add locations in wrong format");return e.forEach(function(e){if(i(e,n))throw new Error("Location already exists");n.push(e)}),this},addEvent:function(e,t,n,o,r){if(!i(t,this.locations))throw new Error("Unknown location");if(!a(n,o))throw new Error("Invalid time range: "+JSON.stringify([n,o]));var c="[object Object]"===Object.prototype.toString.call(r);return this.events.push({name:e,location:t,startDate:n,endDate:o,options:c?r:void 0}),this}},e.Renderer.prototype={draw:function(e){function t(e){if(null===e)throw new Error("Timetable container not found")}function n(e){var t=e.appendChild(document.createElement("aside")),n=t.appendChild(document.createElement("ul"));o(n)}function o(e){for(var t=0;t