diff options
Diffstat (limited to 'js/GraphSchedule.js')
-rw-r--r-- | js/GraphSchedule.js | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/js/GraphSchedule.js b/js/GraphSchedule.js new file mode 100644 index 0000000..72df6f8 --- /dev/null +++ b/js/GraphSchedule.js @@ -0,0 +1,186 @@ +/*jslint devel: true*/ +/*global $ */ +/** + * Constructor + * + * @param {object} params + * @returns + */ +function GraphSchedule(params) { + "use strict"; + this.init(params); +} + +(function () { + "use strict"; + GraphSchedule.prototype = { + template: '', + ui: null, + $flag: null, + $graph: null, + timeRanges: { + workday: [], + weekend: [] + }, + flags: [] + }; + + GraphSchedule.prototype.createUI = function createUI() { + var $tmp = $('<div></div>'); + + $tmp.html(this.template); + this.$flag = $tmp.find('.flag'); + this.$graph = $tmp.find('.GraphSchedule'); + this.ui = this.$graph; + + this.addCurrentTimeBar(); + this.showFlags(); + + this.center(); + }; + + GraphSchedule.prototype.center = function center() { + // set scroll position; + this.$graph[0].scrollLeft = 1000 * ((new Date().getHours() - 4) / 24); + }; + + GraphSchedule.prototype.refresh = function refresh() { + this.updateTimeRanges(); + this.showFlags(); + this.center(); + }; + + GraphSchedule.prototype.onTemplateLoad = function onTemplateLoad() { + }; + + GraphSchedule.prototype.init = function init(params) { + var $loader = $('<div></div>'); + + if (params && params.onSuccess) { + this.onTemplateLoad = params.onSuccess; + } + + this.flags = []; + $loader.load('templates/GraphSchedule.tmpl', null, function (data) { + this.template = data; + this.createUI(); + this.onTemplateLoad(); + }.bind(this)); + }; + + /** + * + * @param {Array} times + * @returns + */ + GraphSchedule.prototype.pushTimeOfFlags = function pushTimeOfFlags(times) { + var i, count; + + // clear previous times; + this.flags = []; + + if (times instanceof Array) { + count = times.length; + for (i = 0; i < count; i += 1) { + if (times[i] instanceof Date) { + this.flags.push({ time: times[i] }); + } else { + throw {message: 'Bag argument at [' + i + '] element of Array. Expected {Date}'}; + } + } + } else { + throw {message: 'Bad argument. Expected {Array} of {Date}'}; + } + }; + + GraphSchedule.prototype.addCurrentTimeBar = function addCurrentTimeBar() { + // remove old time bar; + var $currentTimeBar = this.$graph.find('.currentTimeBar'), + currentTime = new Date(), + hours = currentTime.getHours(); + + if ($currentTimeBar.length === 0) { + // add new; + $currentTimeBar = $('<div class="currentTimeBar"></div>'); + } + + if (hours < 10) { + hours = '0' + hours; + } + + this.$graph.find('.ranges .h' + hours).append($currentTimeBar); + $currentTimeBar.css('left', 100 * currentTime.getMinutes() / 60 + '%'); + + setTimeout(this.addCurrentTimeBar.bind(this), 5 * 60 * 1000); + }; + + GraphSchedule.prototype.addFlag = function addFlag(newFlag) { + var $flagClone, hours = newFlag.time.getHours(); + if (hours < 10) { + hours = '0' + hours; + } + $flagClone = this.$flag.clone(); + this.$graph.find('.grid td:contains(' + hours + ')').append($flagClone); + $flagClone.css('left', 100 * newFlag.time.getMinutes() / 60 + '%'); + }; + + GraphSchedule.prototype.showFlags = function showFlags() { + var i, len = this.flags.length; + // remove old flags; + this.removeFlags(); + + // add all flags to view; + for (i = 0; i < len; i += 1) { + this.addFlag(this.flags[i]); + } + + this.center(); + }; + + GraphSchedule.prototype.removeFlags = function removeFlags() { + this.$graph.find('.flag').remove(); + }; + + GraphSchedule.prototype.setTimeRanges = function setTimeRanges(ranges) { + this.timeRanges = ranges; + }; + + GraphSchedule.prototype.setVisibleWeekend = function (bool) { + var row = this.ui.find('.rangesWeekend'); + return bool ? row.show() : row.hide(); + }; + + GraphSchedule.prototype.setVisibleWorkdays = function (bool) { + var row = this.ui.find('.ranges'); + return bool ? row.show() : row.hide(); + }; + + /** + * Update time ranges on graph + * @param ranges {array} array of boolen, keys are hours, example: [false, false, false, true, true] + */ + GraphSchedule.prototype.updateTimeRanges = function updateTimeRanges() { + var i, len, hours; + + this.$graph.find('.th').removeClass('th'); + + // workdays; + hours = this.timeRanges.workday; + len = hours.length; + for (i = 0; i < len; i += 1) { + if (hours[i]) { + this.$graph.find('.ranges .h' + ((i < 10) ? '0' + i : i)).addClass('th'); + } + } + + //weekends; + hours = this.timeRanges.weekend; + len = hours.length; + for (i = 0; i < len; i += 1) { + if (hours[i]) { + this.$graph.find('.rangesWeekend .h' + ((i < 10) ? '0' + i : i)).addClass('th'); + } + } + }; + +}()); |