forked from zedesk/zdk-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zdk-calendar.htm
182 lines (169 loc) · 5.23 KB
/
zdk-calendar.htm
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<link rel="import" href="../polymer/polymer.html">
<script src="../momentjs/min/moment-with-locales.min.js"></script>
<polymer-element name="zdk-calendar" attributes="date start stop i18n data">
<template>
<link rel="stylesheet" href="zdk-calendar.css">
<div id="calendar">
<div class="row head">
<div on-click="{{prevMonth}}"><img src="left.png"></div>
<div on-click="{{today}}"><img src="point.png"></div>
<div on-click="{{nextMonth}}"><img src="right.png"></div>
<div id="date"></div>
</div>
</div>
</template>
<style type="text/css">
::shadow .td_action, ::shadow .th_actions {
-webkit-flex: 2 !important;
}
::shadow a.a_edit, ::shadow a.a_report {
padding: 0 22px;
color: #33f;
}
::shadow .th_actions {
font-weight: bold;
}
</style>
<script>
Polymer('zdk-calendar', {
day: null,
i18n: null,
data: {},
ready: function() {
this.i18n = this.i18n?this.i18n:navigator.language || navigator.userLanguage;
this.day = (typeof this.date === "string")?moment(this.date):moment();
this.draw();
},
draw: function() {
moment.lang(this.i18n);
this.$.date.innerHTML = this.day.format("MMM YYYY");
var wdivs = this.$.calendar.querySelectorAll("div.w");
wdivs = [].slice.call(wdivs);
var that =this;
wdivs.forEach(function(wdiv) {
that.$.calendar.removeChild(wdiv);
});
// first day of the calendar ( premier jour de la semaine du 1er )
var fday = moment(this.day).date(1).weekday(0);
var weekdays = document.createElement("div");
var div;
weekdays.classList.add("row","w");
for(var i=0;i<7;i++) {
div = document.createElement("div");
div.innerHTML = moment().weekday(i).format("dd");
weekdays.appendChild(div);
}
div = document.createElement("div");
div.classList.add("th_actions");
div.innerHTML = 'Actions';
weekdays.appendChild(div);
this.$.calendar.appendChild(weekdays);
var today = moment();
var weekdiv;
for(var w=0;w<6;w++) {
weekdiv = document.createElement("div");
weekdiv.classList.add("row","w");
var startDay = fday.format('YYYY-MM-DD');
for(var d=0;d<7;d++) {
div = document.createElement("div");
div.classList.add("day");
div.innerHTML = fday.date();
if(fday.year()==today.year() && fday.month()==today.month() && fday.date()==today.date() ) {
div.classList.add("today");
}
if(fday.month()!=this.day.month()) div.classList.add("d1");
if([0,6].indexOf(fday.day()) !== -1) div.classList.add("we");
div.setAttribute("data-date",fday.format("YYYY-MM-DD"));
div.addEventListener("click",function(){ that.dateClick(this.getAttribute("data-date")) },false);
weekdiv.appendChild(div);
fday.add('d',1);
}
div = document.createElement("div");
div.classList.add("td_action");
if (this.data[startDay]) {
this.addActionText(startDay, div);
}
weekdiv.appendChild(div);
this.$.calendar.appendChild(weekdiv);
}
},
addActionText: function(startDay, div){
var info = this.data[startDay],
that = this;
if (info.edit) {
a = document.createElement('a');
a.classList.add('a_edit');
a.setAttribute("data-date", startDay);
a.setAttribute('data-type', 'edit')
a.innerHTML = 'Edit';
a.addEventListener("click", function(){
that.fire('selectAction', {
type: this.getAttribute("data-type"),
date: this.getAttribute("data-date")
});
},false);
div.appendChild(a);
}
if (info.report) {
b = document.createElement('a');
b.classList.add('a_report');
b.setAttribute("data-date", startDay);
b.setAttribute('data-type', 'report')
b.innerHTML = 'Report';
b.addEventListener("click", function(){
that.fire('selectAction', {
type: this.getAttribute("data-type"),
date: this.getAttribute("data-date")
});
},false);
div.appendChild(b);
}
},
i18nChanged: function(oldValue, newValue) {
this.i18n = newValue?newValue:navigator.language || navigator.userLanguage;
this.draw();
},
dateChanged: function(oldValue, newValue) {
moment.lang(this.i18n);
switch(typeof newValue) {
case "number":
case "object":
this.day = moment(newValue);
break;
case "string":
this.day = moment(newValue,"L");
if(this.day.isValid() === false) {
this.day = moment(newValue);
}
break;
default:
this.day = moment();
}
if(this.day.isValid() === false ) {
this.day = moment();
}
this.draw();
},
prevMonth: function(event,detail,sender) {
this.day.add("M",-1);
this.draw();
this.fire('dateChange', this.day.valueOf());
},
nextMonth: function(event,detail,sender) {
this.day.add("M",1);
this.draw();
this.fire('dateChange', this.day.valueOf());
},
today: function(event,detail,sender) {
// if(this.day.format("YYYY-MM") === moment().format("YYYY-MM")) return;
this.day = moment();
this.draw();
this.fire('dateChange', this.day.valueOf());
},
dateClick: function(day) {
moment.lang(this.i18n);
this.fire('select', { day:moment(day).format("L"), time:moment(day).valueOf()});
}
});
</script>
</polymer-element>