3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('calendarnavigator', function(Y) {
10
* Provides a plugin which adds navigation controls to Calendar.
12
* @module calendarnavigator
14
var CONTENT_BOX = "contentBox",
16
RENDERED = "rendered",
17
getCN = Y.ClassNameManager.getClassName,
18
substitute = Y.substitute,
21
CALENDAR = 'calendar',
22
CALENDARNAV = 'calendarnav',
23
CAL_HD = getCN(CALENDAR, 'header'),
24
CAL_PREV_M = getCN(CALENDARNAV, 'prevmonth'),
25
CAL_NEXT_M = getCN(CALENDARNAV, 'nextmonth'),
26
ydate = Y.DataType.Date;
28
* A plugin class which adds navigation controls to Calendar.
30
* @class CalendarNavigator
31
* @extends Plugin.Base
34
function CalendarNavigator(config) {
35
CalendarNavigator.superclass.constructor.apply(this, arguments);
39
* The namespace for the plugin. This will be the property on the widget, which will
40
* reference the plugin instance, when it's plugged in.
45
* @default "navigator"
47
CalendarNavigator.NS = "navigator";
50
* The NAME of the CalendarNavigator class. Used to prefix events generated
51
* by the plugin class.
56
* @default "pluginCalendarNavigator"
58
CalendarNavigator.NAME = "pluginCalendarNavigator";
62
* Static property used to define the default attribute
63
* configuration for the plugin.
69
CalendarNavigator.ATTRS = {
72
* The number of months to shift by when the control arrows are clicked.
74
* @attribute shiftByMonths
84
* The CSS classnames for the calendar navigator controls.
85
* @property CALENDARNAV_STRINGS
91
CalendarNavigator.CALENDARNAV_STRINGS = {
92
prev_month_class: CAL_PREV_M,
93
next_month_class: CAL_NEXT_M
97
* The template for the calendar navigator previous month control.
98
* @property PREV_MONTH_CONTROL_TEMPLATE
103
CalendarNavigator.PREV_MONTH_CONTROL_TEMPLATE = '<div class="yui3-u {prev_month_class}" style="width:15px;">' +
107
* The template for the calendar navigator next month control.
108
* @property NEXT_MONTH_CONTROL_TEMPLATE
114
CalendarNavigator.NEXT_MONTH_CONTROL_TEMPLATE = '<div class="yui3-u {next_month_class}" style="width:15px;">' +
119
Y.extend(CalendarNavigator, Y.Plugin.Base, {
122
* The initializer lifecycle implementation. Modifies the host widget's
123
* render to add navigation controls.
125
* @method initializer
126
* @param {Object} config The user configuration for the plugin
128
initializer : function(config) {
130
// After the host has rendered its UI, place the navigation cotnrols
131
this.afterHostMethod("renderUI", this._initNavigationControls);
135
* The initializer destructor implementation. Responsible for destroying the initialized
136
* control mechanisms.
140
destructor : function() {
145
* Private utility method that subtracts months from the host calendar date
146
* based on the control click and the shiftByMonths property.
148
* @method _subtractMonths
149
* @param {Event} ev Click event from the controls
152
_subtractMonths : function (ev) {
153
var host = this.get(HOST);
154
var oldDate = host.get("date");
155
host.set("date", ydate.addMonths(oldDate, -1*this.get("shiftByMonths")));
160
* Private utility method that adds months to the host calendar date
161
* based on the control click and the shiftByMonths property.
164
* @param {Event} ev Click event from the controls
167
_addMonths : function (ev) {
168
var host = this.get(HOST);
169
var oldDate = host.get("date");
170
host.set("date", ydate.addMonths(oldDate, this.get("shiftByMonths")));
175
* Private render assist method that renders the previous month control
177
* @method _renderPrevControls
180
_renderPrevControls : function () {
181
var prevControlNode = create(substitute (CalendarNavigator.PREV_MONTH_CONTROL_TEMPLATE,
182
CalendarNavigator.CALENDARNAV_STRINGS));
183
prevControlNode.on("click", this._subtractMonths, this);
184
prevControlNode.on("selectstart", function (ev) {ev.preventDefault();});
186
return prevControlNode;
190
* Private render assist method that renders the next month control
192
* @method _renderNextControls
195
_renderNextControls : function () {
196
var nextControlNode = create(substitute (CalendarNavigator.NEXT_MONTH_CONTROL_TEMPLATE,
197
CalendarNavigator.CALENDARNAV_STRINGS));
198
nextControlNode.on("click", this._addMonths, this);
199
nextControlNode.on("selectstart", function (ev) {ev.preventDefault();});
201
return nextControlNode;
205
* Protected render assist method that initialized and renders the navigation controls.
206
* @method _initNavigationControls
209
_initNavigationControls : function() {
210
var host = this.get(HOST);
211
var headerCell = host.get(CONTENT_BOX).one("." + CAL_HD);
212
headerCell.prepend(this._renderPrevControls(host));
213
headerCell.append(this._renderNextControls(host));
217
Y.namespace("Plugin").CalendarNavigator = CalendarNavigator;
220
}, '3.4.1' ,{requires:['plugin', 'classnamemanager', 'datatype-date', 'node', 'substitute']});