|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Two-Pane Calendar with Custom Rendering and Multiple Selection</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Two-Pane Calendar with Custom Rendering and Multiple Selection</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style> |
|
|
29 |
.example { |
|
|
30 |
font-size:15px; |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
.example h4 { |
|
|
34 |
border: none; |
|
|
35 |
text-transform: none; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
.example th { |
|
|
39 |
background: none; |
|
|
40 |
color: #000; |
|
|
41 |
text-transform: none; |
|
|
42 |
border: none; |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
</style> |
|
|
46 |
|
|
|
47 |
<div class="intro"> |
|
|
48 |
<p> |
|
|
49 |
This example demonstrates how to instantiate a Calendar, switch its template to a double-pane, and create custom renderers for its header and certain cells (based on rules), as well as turn on multiple date selection and disable certain dates from being selected. |
|
|
50 |
</p> |
|
|
51 |
<p><strong>The <code>selectionMode</code> in this example is set to <code>multiple</code></strong>, which allows additional dates to be selected if a <strong>Shift</strong> or <strong>Ctrl/Meta</strong> key is held down. This selection mode does not allow multiple selection on touchscreen devices; for such devices, use the <code>multiple-sticky</code> selection mode instead. |
|
|
52 |
</p> |
|
|
53 |
|
|
|
54 |
<p> |
|
|
55 |
<strong>There are two custom filtering rules provided in the example code.</strong> One matches all Saturdays and Sundays (weekends in the United States), and the other matches Tuesdays and Fridays. The first rule is used in conjunction with a custom renderer to set the corresponding date cell text color to red. The second rule is used to disable matching dates from selection and interaction. |
|
|
56 |
</p> |
|
|
57 |
</div> |
|
|
58 |
|
|
|
59 |
<div class="example yui3-skin-sam"> |
|
|
60 |
<style> |
|
|
61 |
.yui3-skin-sam .redtext { |
|
|
62 |
color:#ff0000; |
|
|
63 |
} |
|
|
64 |
</style> |
|
|
65 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
66 |
<div id="mycalendar"></div> |
|
|
67 |
</div> |
|
|
68 |
<script type="text/javascript"> |
|
|
69 |
YUI().use('calendar', 'datatype-date', 'datatype-date-math', function(Y) { |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
// Switch the calendar main template to the included two pane template |
|
|
73 |
Y.CalendarBase.CONTENT_TEMPLATE = Y.CalendarBase.TWO_PANE_TEMPLATE; |
|
|
74 |
|
|
|
75 |
// Create a new instance of calendar, setting the showing of previous |
|
|
76 |
// and next month's dates to true, and the selection mode to multiple |
|
|
77 |
// selected dates. Additionally, set the disabledDatesRule to a name of |
|
|
78 |
// the rule which, when matched, will force the date to be excluded |
|
|
79 |
// from being selected. Also configure the initial date on the calendar |
|
|
80 |
// to be July of 2011. |
|
|
81 |
var calendar = new Y.Calendar({ |
|
|
82 |
contentBox: "#mycalendar", |
|
|
83 |
width: "700px", |
|
|
84 |
showPrevMonth: true, |
|
|
85 |
showNextMonth: true, |
|
|
86 |
selectionMode: 'multiple', |
|
|
87 |
disabledDatesRule: "tuesdays_and_fridays", |
|
|
88 |
date: new Date(2011, 6, 1) |
|
|
89 |
}).render(); |
|
|
90 |
|
|
|
91 |
// Create a set of rules to match specific dates. In this case, |
|
|
92 |
// the "tuesdays_and_fridays" rule will match any Tuesday or Friday, |
|
|
93 |
// whereas the "all_weekends" rule will match any Saturday or Sunday. |
|
|
94 |
var rules = { |
|
|
95 |
"all": { |
|
|
96 |
"all": { |
|
|
97 |
"all": { |
|
|
98 |
"2,5": "tuesdays_and_fridays", |
|
|
99 |
"0,6": "all_weekends" |
|
|
100 |
} |
|
|
101 |
} |
|
|
102 |
} |
|
|
103 |
}; |
|
|
104 |
|
|
|
105 |
// Set the calendar customRenderer, provides the rules defined above, |
|
|
106 |
// as well as a filter function. The filter function receives a reference |
|
|
107 |
// to the node corresponding to the DOM element of the date that matched |
|
|
108 |
// one or more rule, along with the list of rules. Check if one of the |
|
|
109 |
// rules is "all_weekends", and if so, apply a custom CSS class to the |
|
|
110 |
// node. |
|
|
111 |
calendar.set("customRenderer", { |
|
|
112 |
rules: rules, |
|
|
113 |
filterFunction: function (date, node, rules) { |
|
|
114 |
if (Y.Array.indexOf(rules, 'all_weekends') >= 0) { |
|
|
115 |
node.addClass("redtext"); |
|
|
116 |
} |
|
|
117 |
} |
|
|
118 |
}); |
|
|
119 |
|
|
|
120 |
// Set a custom header renderer with a callback function, |
|
|
121 |
// which receives the current date and outputs a string. |
|
|
122 |
// use the Y.Datatype.Date format to format the date, and |
|
|
123 |
// the Datatype.Date math to add one month to the current |
|
|
124 |
// date, so both months can appear in the header (since |
|
|
125 |
// this is a two-pane calendar). |
|
|
126 |
calendar.set("headerRenderer", function (curDate) { |
|
|
127 |
var ydate = Y.DataType.Date, |
|
|
128 |
output = ydate.format(curDate, { |
|
|
129 |
format: "%B %Y" |
|
|
130 |
}) + " — " + ydate.format(ydate.addMonths(curDate, 1), { |
|
|
131 |
format: "%B %Y" |
|
|
132 |
}); |
|
|
133 |
return output; |
|
|
134 |
}); |
|
|
135 |
|
|
|
136 |
// When selection changes, output the fired event to the |
|
|
137 |
// console. the newSelection attribute in the event facade |
|
|
138 |
// will contain the list of currently selected dates (or be |
|
|
139 |
// empty if all dates have been deselected). |
|
|
140 |
calendar.on("selectionChange", function (ev) { |
|
|
141 |
Y.log(ev); |
|
|
142 |
}); |
|
|
143 |
|
|
|
144 |
}); |
|
|
145 |
|
|
|
146 |
</script> |
|
|
147 |
|
|
|
148 |
</div> |
|
|
149 |
|
|
|
150 |
<h2>Complete Example Source</h2> |
|
|
151 |
<p> |
|
|
152 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
153 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
154 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
155 |
</p> |
|
|
156 |
<pre class="code prettyprint"><style> |
|
|
157 |
.yui3-skin-sam .redtext { |
|
|
158 |
color:#ff0000; |
|
|
159 |
} |
|
|
160 |
</style> |
|
|
161 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
162 |
<div id="mycalendar"></div> |
|
|
163 |
</div> |
|
|
164 |
<script type="text/javascript"> |
|
|
165 |
YUI().use('calendar', 'datatype-date', 'datatype-date-math', function(Y) { |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
// Switch the calendar main template to the included two pane template |
|
|
169 |
Y.CalendarBase.CONTENT_TEMPLATE = Y.CalendarBase.TWO_PANE_TEMPLATE; |
|
|
170 |
|
|
|
171 |
// Create a new instance of calendar, setting the showing of previous |
|
|
172 |
// and next month's dates to true, and the selection mode to multiple |
|
|
173 |
// selected dates. Additionally, set the disabledDatesRule to a name of |
|
|
174 |
// the rule which, when matched, will force the date to be excluded |
|
|
175 |
// from being selected. Also configure the initial date on the calendar |
|
|
176 |
// to be July of 2011. |
|
|
177 |
var calendar = new Y.Calendar({ |
|
|
178 |
contentBox: "#mycalendar", |
|
|
179 |
width: "700px", |
|
|
180 |
showPrevMonth: true, |
|
|
181 |
showNextMonth: true, |
|
|
182 |
selectionMode: 'multiple', |
|
|
183 |
disabledDatesRule: "tuesdays_and_fridays", |
|
|
184 |
date: new Date(2011, 6, 1) |
|
|
185 |
}).render(); |
|
|
186 |
|
|
|
187 |
// Create a set of rules to match specific dates. In this case, |
|
|
188 |
// the "tuesdays_and_fridays" rule will match any Tuesday or Friday, |
|
|
189 |
// whereas the "all_weekends" rule will match any Saturday or Sunday. |
|
|
190 |
var rules = { |
|
|
191 |
"all": { |
|
|
192 |
"all": { |
|
|
193 |
"all": { |
|
|
194 |
"2,5": "tuesdays_and_fridays", |
|
|
195 |
"0,6": "all_weekends" |
|
|
196 |
} |
|
|
197 |
} |
|
|
198 |
} |
|
|
199 |
}; |
|
|
200 |
|
|
|
201 |
// Set the calendar customRenderer, provides the rules defined above, |
|
|
202 |
// as well as a filter function. The filter function receives a reference |
|
|
203 |
// to the node corresponding to the DOM element of the date that matched |
|
|
204 |
// one or more rule, along with the list of rules. Check if one of the |
|
|
205 |
// rules is "all_weekends", and if so, apply a custom CSS class to the |
|
|
206 |
// node. |
|
|
207 |
calendar.set("customRenderer", { |
|
|
208 |
rules: rules, |
|
|
209 |
filterFunction: function (date, node, rules) { |
|
|
210 |
if (Y.Array.indexOf(rules, 'all_weekends') >= 0) { |
|
|
211 |
node.addClass("redtext"); |
|
|
212 |
} |
|
|
213 |
} |
|
|
214 |
}); |
|
|
215 |
|
|
|
216 |
// Set a custom header renderer with a callback function, |
|
|
217 |
// which receives the current date and outputs a string. |
|
|
218 |
// use the Y.Datatype.Date format to format the date, and |
|
|
219 |
// the Datatype.Date math to add one month to the current |
|
|
220 |
// date, so both months can appear in the header (since |
|
|
221 |
// this is a two-pane calendar). |
|
|
222 |
calendar.set("headerRenderer", function (curDate) { |
|
|
223 |
var ydate = Y.DataType.Date, |
|
|
224 |
output = ydate.format(curDate, { |
|
|
225 |
format: "%B %Y" |
|
|
226 |
}) + " &mdash; " + ydate.format(ydate.addMonths(curDate, 1), { |
|
|
227 |
format: "%B %Y" |
|
|
228 |
}); |
|
|
229 |
return output; |
|
|
230 |
}); |
|
|
231 |
|
|
|
232 |
// When selection changes, output the fired event to the |
|
|
233 |
// console. the newSelection attribute in the event facade |
|
|
234 |
// will contain the list of currently selected dates (or be |
|
|
235 |
// empty if all dates have been deselected). |
|
|
236 |
calendar.on("selectionChange", function (ev) { |
|
|
237 |
Y.log(ev); |
|
|
238 |
}); |
|
|
239 |
|
|
|
240 |
}); |
|
|
241 |
|
|
|
242 |
</script></pre> |
|
|
243 |
|
|
|
244 |
</div> |
|
|
245 |
</div> |
|
|
246 |
</div> |
|
|
247 |
|
|
|
248 |
<div class="yui3-u-1-4"> |
|
|
249 |
<div class="sidebar"> |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
<div class="sidebox"> |
|
|
254 |
<div class="hd"> |
|
|
255 |
<h2 class="no-toc">Examples</h2> |
|
|
256 |
</div> |
|
|
257 |
|
|
|
258 |
<div class="bd"> |
|
|
259 |
<ul class="examples"> |
|
|
260 |
|
|
|
261 |
|
|
|
262 |
<li data-description="How to create a single-pane calendar with selectable dates"> |
|
|
263 |
<a href="calendar-simple.html">Simple Calendar with Selection</a> |
|
|
264 |
</li> |
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
<li data-description="How to create a multi-pane calendar with custom-rendered cells and multiple date selection."> |
|
|
269 |
<a href="calendar-multipane.html">Two-Pane Calendar with Custom Rendering and Multiple Selection</a> |
|
|
270 |
</li> |
|
|
271 |
|
|
|
272 |
|
|
|
273 |
</ul> |
|
|
274 |
</div> |
|
|
275 |
</div> |
|
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
</div> |
|
|
280 |
</div> |
|
|
281 |
</div> |
|
|
282 |
</div> |
|
|
283 |
|
|
|
284 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
285 |
<script>prettyPrint();</script> |
|
|
286 |
|
|
|
287 |
<script> |
|
|
288 |
YUI.Env.Tests = { |
|
|
289 |
examples: [], |
|
|
290 |
project: '../assets', |
|
|
291 |
assets: '../assets/calendar', |
|
|
292 |
name: 'calendar-multipane', |
|
|
293 |
title: 'Two-Pane Calendar with Custom Rendering and Multiple Selection', |
|
|
294 |
newWindow: '', |
|
|
295 |
auto: false |
|
|
296 |
}; |
|
|
297 |
YUI.Env.Tests.examples.push('calendar-simple'); |
|
|
298 |
YUI.Env.Tests.examples.push('calendar-multipane'); |
|
|
299 |
|
|
|
300 |
</script> |
|
|
301 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
302 |
|
|
|
303 |
|
|
|
304 |
|
|
|
305 |
</body> |
|
|
306 |
</html> |