-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehaviour.js
155 lines (126 loc) · 4.13 KB
/
behaviour.js
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
// Constants
var OP_PLUS = "plus";
var OP_MINUS = "minus";
var DATE_FMT = "%e %b %Y";
// Parameters
var pivot = new Date(); // Today
var op = OP_PLUS;
var incr = 3;
var incrUnit = "weeks";
var result = null;
YUI().use('overlay', 'calendar', 'node', 'event', function (Y) {
// References to the UI bits
var datePivot = Y.one("#date-pivot");
var dateOp = Y.one("#date-op");
var dateIncr = Y.one("#date-incr");
var dateIncrUnit = Y.one("#date-incr-unit");
var dateResult = Y.one("#date-result");
var calClose = Y.one("#cal-close");
// Do whatever operation the UI currently shows
function doOp(a, b) {
if (op == OP_PLUS) return a + b;
if (op == OP_MINUS) return a - b;
return null;
}
// Called on adjustment of any parameters to recalculate result
function refreshDisplay() {
// Update display of inputs
datePivot.setHTML(Y.DataType.Date.format(pivot, {format:DATE_FMT}));
dateOp.setHTML(op);
dateIncrUnit.set('value', incrUnit);
dateIncr.set('value', isNaN(incr)?(""):(incr));
// Compute new result
newResult = new Date(pivot);
if (incrUnit == "days") {
newResult.setDate(doOp(pivot.getDate(),incr));
} else if (incrUnit == "weeks") {
newResult.setDate(doOp(pivot.getDate(),incr*7));
} else if (incrUnit == "months") {
newResult.setMonth(doOp(pivot.getMonth(),incr));
}
// Update result
result = newResult;
dateResult.setHTML(Y.DataType.Date.format(result, {format:DATE_FMT}));
/*dateResult.render();*/
}
Y.on("domready", function (e) {
hide_cal();
refreshDisplay();
dateIncr.get('childNodes').remove();
for (i = 0; i < 100; ) {
dateIncr.append('<option>' + ++i + '</option>');
}
});
///////////////////////////////////////////////////////////////////
// SETUP //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Calendar overlay
var calOverlay = new Y.Overlay({
srcNode: "#calendar-overlay",
width:'80%',
height: '70%',
centered: true});
// Calendar
var cal = new Y.Calendar({
contentBox: "#insert-cal-here",
width:'100%',
showPrevMonth: true,
showNextMonth: true,});
function show_cal () {
calOverlay.render();
cal.render();
calOverlay.show();
cal.show();
calClose.show();
}
function hide_cal () {
calOverlay.hide();
cal.hide();
calClose.hide();
}
///////////////////////////////////////////////////////////////////
// EVENTS /////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Date change
datePivot.on('click', function (e) {
// display overlay
show_cal();
// Listen for selection change, update variables, display
cal.on("selectionChange", function (ev) {
// Will only be one selection unless we allow mutliple
pivot = ev.newSelection[0];
// Expose main UI
hide_cal();
refreshDisplay();
});
});
// Close link for overlay
Y.one('#cal-close').on('click', function (e) {
hide_cal();
});
// Toggle the operation
dateOp.on('click', function (e) {
current = this.getHTML();
if (current == OP_PLUS) {
op = OP_MINUS;
} else if (current == OP_MINUS) {
op = OP_PLUS;
} else {
alert("Well, something is broken.");
}
refreshDisplay();
});
dateIncr.on('change', function (e) {
newIncr = parseInt(this.get('value'));
if (isNaN(newIncr)) {
incr = 1;
} else {
incr = newIncr;
}
refreshDisplay();
});
dateIncrUnit.on('change', function (e) {
incrUnit = this.get('value');
refreshDisplay();
});
});