summaryrefslogtreecommitdiff
path: root/www/js/calendar.js
blob: 740a4dd81cb553cd597ab6b393d74062044a2c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 *  rhrdweb
 *
 *  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
 *
 *  This file is part of rhrdweb.
 *
 *  rhrdweb is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  rhrdweb is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with rhrdweb. If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var current_week_offset = 0;

function getLastMonday() {
  var d = clock.now()
  d.setUTCHours(12, 0, 0, 0);
  var dow = d.getUTCDay()
  if(dow == 0) {
    return new Date(d.valueOf() - (6*24*3600000));
  } else {
    return new Date(d.valueOf() - ((dow-1)*24*3600000));
  }
}

function addDeltaDays(d, days) {
  return new Date(d.valueOf() + (days*24*3600000))
}

function isToday(d) {
  var today = clock.now()
  today.setUTCHours(12, 0, 0, 0);
  return ((d - today) == 0)
}

function addWeekClass(row, week) {
  switch(week) {
    case 1:
      row.addClass('label-info');
      break;
    case 2:
      row.addClass('label-warning');
      break;
    case 3:
      row.addClass('label-success');
      break;
    case 4:
      row.addClass('label-danger');
      break;
    default:
      row.addClass('label-default');
  }
}

function calendar_redraw(weekstart) {
  var cal = $('#calendar');
  cal.find("tr:gt(0)").remove();

  var date = getLastMonday();
  date = addDeltaDays(date, weekstart * 7);
  for(var w = 0; w < 20; w++) {
    var week = get_rd_week(date.valueOf() + 7199999);
    var row = $('<tr>');
    addWeekClass(row, week);
    row.append($('<td>').addClass('week').text(week));
    row.append($('<td>').addClass('month').text(monthname_short[date.getUTCMonth()] + ' ' + date.getUTCFullYear()));
    for(var d = 0; d < 7; d++) {
      var col = $('<td>').text(date.getUTCDate());
      if(isToday(date)) {
        col.addClass('today');
      }
      row.append(col);
      if(d < 6) {
          date = addDeltaDays(date, 1);
      }
    }
    row.append($('<td>').addClass('month').text(monthname_short[date.getUTCMonth()] + ' ' + date.getUTCFullYear()));
    date = addDeltaDays(date, 1);
    $('#calendar').append(row);
  }
}

function calendar_init() {
  $('#btn-today').click(calendar_today);
  $('#btn-earlier').click(calendar_prev);
  $('#btn-later').click(calendar_next);
  $('#calendar').on('wheel', calendar_scroll);
  current_week_offset = -4;
  clock.addCallback(function() {
    calendar_redraw(current_week_offset);
  });
}

function calendar_today() {
  current_week_offset = -4;
  calendar_redraw(current_week_offset)
}

function calendar_prev() {
  current_week_offset--;
  calendar_redraw(current_week_offset)
}

function calendar_next() {
  current_week_offset++;
  calendar_redraw(current_week_offset)
}

function calendar_scroll(event) {
  console.log(event.originalEvent.deltaY);
  if(event.originalEvent.deltaY > 0) {
    calendar_next();
    return false
  }

  if(event.originalEvent.deltaY < 0) {
    calendar_prev();
    return false
  }
  return false;
}