6 * @param {object} params
9 function GraphSchedule(params) {
16 GraphSchedule.prototype = {
28 GraphSchedule.prototype.createUI = function createUI() {
29 var $tmp = $('<div></div>');
31 $tmp.html(this.template);
32 this.$flag = $tmp.find('.flag');
33 this.$graph = $tmp.find('.GraphSchedule');
34 this.ui = this.$graph;
36 this.addCurrentTimeBar();
42 GraphSchedule.prototype.center = function center() {
43 // set scroll position;
44 this.$graph[0].scrollLeft = 1000 * ((new Date().getHours() - 4) / 24);
47 GraphSchedule.prototype.refresh = function refresh() {
48 this.updateTimeRanges();
53 GraphSchedule.prototype.onTemplateLoad = function onTemplateLoad() {
56 GraphSchedule.prototype.init = function init(params) {
57 var $loader = $('<div></div>');
59 if (params && params.onSuccess) {
60 this.onTemplateLoad = params.onSuccess;
64 $loader.load('templates/GraphSchedule.tmpl', null, function (data) {
67 this.onTemplateLoad();
73 * @param {Array} times
76 GraphSchedule.prototype.pushTimeOfFlags = function pushTimeOfFlags(times) {
79 // clear previous times;
82 if (times instanceof Array) {
84 for (i = 0; i < count; i += 1) {
85 if (times[i] instanceof Date) {
86 this.flags.push({ time: times[i] });
88 throw {message: 'Bag argument at [' + i + '] element of Array. Expected {Date}'};
92 throw {message: 'Bad argument. Expected {Array} of {Date}'};
96 GraphSchedule.prototype.addCurrentTimeBar = function addCurrentTimeBar() {
97 // remove old time bar;
98 var $currentTimeBar = this.$graph.find('.currentTimeBar'),
99 currentTime = new Date(),
100 hours = currentTime.getHours();
102 if ($currentTimeBar.length === 0) {
104 $currentTimeBar = $('<div class="currentTimeBar"></div>');
111 this.$graph.find('.ranges .h' + hours).append($currentTimeBar);
112 $currentTimeBar.css('left', 100 * currentTime.getMinutes() / 60 + '%');
114 setTimeout(this.addCurrentTimeBar.bind(this), 5 * 60 * 1000);
117 GraphSchedule.prototype.addFlag = function addFlag(newFlag) {
118 var $flagClone, hours = newFlag.time.getHours();
122 $flagClone = this.$flag.clone();
123 this.$graph.find('.grid td:contains(' + hours + ')').append($flagClone);
124 $flagClone.css('left', 100 * newFlag.time.getMinutes() / 60 + '%');
127 GraphSchedule.prototype.showFlags = function showFlags() {
128 var i, len = this.flags.length;
132 // add all flags to view;
133 for (i = 0; i < len; i += 1) {
134 this.addFlag(this.flags[i]);
140 GraphSchedule.prototype.removeFlags = function removeFlags() {
141 this.$graph.find('.flag').remove();
144 GraphSchedule.prototype.setTimeRanges = function setTimeRanges(ranges) {
145 this.timeRanges = ranges;
148 GraphSchedule.prototype.setVisibleWeekend = function (bool) {
149 var row = this.ui.find('.rangesWeekend');
150 return bool ? row.show() : row.hide();
153 GraphSchedule.prototype.setVisibleWorkdays = function (bool) {
154 var row = this.ui.find('.ranges');
155 return bool ? row.show() : row.hide();
159 * Update time ranges on graph
160 * @param ranges {array} array of boolen, keys are hours, example: [false, false, false, true, true]
162 GraphSchedule.prototype.updateTimeRanges = function updateTimeRanges() {
165 this.$graph.find('.th').removeClass('th');
168 hours = this.timeRanges.workday;
170 for (i = 0; i < len; i += 1) {
172 this.$graph.find('.ranges .h' + ((i < 10) ? '0' + i : i)).addClass('th');
177 hours = this.timeRanges.weekend;
179 for (i = 0; i < len; i += 1) {
181 this.$graph.find('.rangesWeekend .h' + ((i < 10) ? '0' + i : i)).addClass('th');