forked from SwarmOnline/Ext.ux.TouchCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ext.ux.TouchCalendar.js
267 lines (220 loc) · 8.48 KB
/
Ext.ux.TouchCalendar.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* @copyright (c) 2012, by SwarmOnline.com
* @date 29th May 2012
* @version 0.1
* @documentation
* @website http://www.swarmonline.com
*/
/**
* @class Ext.ux.TouchCalendar
* @author Stuart Ashworth
*
* For use with Sencha Touch 2
*
* This extension wraps the Ext.ux.TouchCalendarView in a Ext.Carousel component and allows the calendar to respond to swipe
* gestures to switch the displayed period. It works by creating 3 Ext.ux.TouchCalendarViews and dynamically creating/removing
* views as the user moves back/forward through time.
*
* ![Ext.ux.TouchCalendar Screenshot](http://www.swarmonline.com/Ext.ux.TouchCalendar/screenshots/Ext.ux.TouchCalendar-month-ss.png)
*
* [Ext.ux.TouchCalendar Demo](http://www.swarmonline.com/Ext.ux.TouchCalendar/examples/Ext.ux.TouchCalendar.html)
*
*/
Ext.define('Ext.ux.TouchCalendar',{
extend: 'Ext.carousel.Carousel',
xtype: 'calendar',
config: {
/**
* @cfg {String} viewMode This config should not be used directly, instead the 'viewMode' config should be added to the 'viewConfig' config object. Use the setViewMode method to change the viewMode of the calendar at runtime.
* @accessor
*/
viewMode: 'month',
/**
* @cfg {Boolean} enableSwipeNavigate True to allow the calendar's period to be change by swiping across it.
*/
enableSwipeNavigate: true,
/**
* @cfg {Boolean/Object} enableSimpleEvents True to enable the Ext.ux.TouchCalendarSimpleEvents plugin. When true the Ext.ux.TouchCalendarSimpleEvents JS and CSS files
* must be included and an eventStore option, containing an Ext.data.Store instance, be given to the viewConfig. If an object is passed in this is used as the config for the plugin.
*/
enableSimpleEvents: false,
/**
* @cfg {Boolean/Object} enableEventBars True to enable the Ext.ux.TouchCalendarEvents plugin. When true the Ext.ux.TouchCalendarEvents JS and CSS files
* must be included and an eventStore option, containing an Ext.data.Store instance, be given to the viewConfig. If an object is passed in this is used as the config for the plugin.
*/
enableEventBars: false,
/**
* @cfg {Object} viewConfig A set of configuration options that will be applied to the TouchCalendarView component
*/
viewConfig: {
}
},
defaultViewConfig: {
viewMode: 'MONTH',
weekStart: 1,
bubbleEvents: ['selectionchange']
},
indicator: false,
initialize: function(){
this.viewConfig = Ext.applyIf(this.viewConfig || {}, this.defaultViewConfig);
this.viewConfig.currentDate = this.viewConfig.currentDate || this.viewConfig.value || new Date();
this.setViewMode(this.viewConfig.viewMode.toUpperCase());
this.initViews();
Ext.apply(this, {
cls: 'touch-calendar',
activeItem: (this.getEnableSwipeNavigate() ? 1: 0),
direction: 'horizontal'
});
this.setIndicator(false); // for some reason, indicator: false is not being applied unless explicitly set.
this.setActiveItem(1); // for some reason, activeItem: 1 is not being applied unless explicitly set.
this.on('selectionchange', this.onSelectionChange);
this.on('activeitemchange', this.onActiveItemChange);
if (this.getEnableSwipeNavigate()) {
// Bind the required listeners
this.on(this.element, {
drag: this.onDrag,
dragThreshold: 5,
dragend: this.onDragEnd,
direction: this.direction,
scope: this
});
this.element.addCls(this.baseCls + '-' + this.direction);
}
},
/**
* Builds the necessary configuration object for the creation of the TouchCalendarView.
* @param {Date} viewValue The date Value that the new TouchCalendarView will have
* @method
* @private
* @return {Object} The new config object for the view
*/
getViewConfig: function(viewValue){
var plugins = [];
if(this.getEnableSimpleEvents()){
var config = Ext.isObject(this.getEnableSimpleEvents()) ? this.getEnableSimpleEvents() : {};
plugins.push(Ext.create('Ext.ux.TouchCalendarSimpleEvents', config));
} else if (this.getEnableEventBars()){
var config = Ext.isObject(this.getEnableEventBars()) ? this.getEnableEventBars() : {};
plugins.push(Ext.create('Ext.ux.TouchCalendarEvents', config));
}
Ext.apply(this._viewConfig, {
plugins: plugins,
currentDate: viewValue,
viewMode: this.getViewMode(),
onTableHeaderTap: Ext.bind(this.onTableHeaderTap, this),
bubbleEvents: ['periodchange', 'eventtap', 'selectionchange']
});
return this._viewConfig;
},
getViewDate: function(date, i){
var scale = (this.getViewMode() === 'WEEK' ? 'DAY' : this.getViewMode().toUpperCase()),
number = (this.getViewMode() === 'WEEK' ? (8 * i) : i);
return Ext.Date.add(date, Ext.Date[scale], number)
},
/**
* Creates all the TouchCalendarView instances needed for the Calendar
* @method
* @private
* @return {void}
*/
initViews: function(){
var items = [];
var origCurrentDate = Ext.Date.clone(this.viewConfig.currentDate),
i = (this.getEnableSwipeNavigate() ? -1 : 0),
iMax = (this.getEnableSwipeNavigate() ? 1 : 0),
plugins = [];
// first out of view
var viewValue = this.getViewDate(origCurrentDate, -1);
items.push(
Ext.create('Ext.ux.TouchCalendarView', Ext.applyIf({
currentDate: viewValue
}, this.getViewConfig(viewValue)))
);
// active view
items.push(
Ext.create('Ext.ux.TouchCalendarView', Ext.ux.TouchCalendarView(this.getViewConfig(origCurrentDate)))
);
// second out of view (i.e. third)
viewValue = this.getViewDate(origCurrentDate, 1);
items.push(
Ext.create('Ext.ux.TouchCalendarView', Ext.ux.TouchCalendarView(Ext.applyIf({
currentDate: viewValue
}, this.getViewConfig(viewValue))))
);
this.setItems(items);
this.view = items[(this.getEnableSwipeNavigate() ? 1: 0)];
},
/**
* Override for the TouchCalendarView's onTableHeaderTap method which is executed when the view's header (specificly the arrows) is tapped.
* When using the TouchCalendar wrapper we must intercept it and use the carousel's prev/next methods to do the switching.
*/
onTableHeaderTap: function(e, el){
el = Ext.fly(el);
if (el.hasCls(this.view.getPrevPeriodCls()) || el.hasCls(this.view.getNextPeriodCls())) {
this[(el.hasCls(this.view.getPrevPeriodCls()) ? 'previous' : 'next')]();
}
},
applyViewMode: function(mode){
return mode.toUpperCase();
},
/**
* Changes the mode of the calendar to the specified string's value. Possible values are 'month', 'week' and 'day'.
* @method
* @returns {void}
*/
updateViewMode: function(mode){
this.viewConfig = this.viewConfig || {};
this.viewConfig.viewMode = mode;
if(this.view){
Ext.each(this.getInnerItems(), function(view, index){
view.currentDate = this.getViewDate(Ext.Date.clone(this.view.currentDate), index-1);
view.setViewMode(mode, true);
view.refresh();
}, this);
}
},
/**
* Returns the Date that is selected.
* @method
* @returns {Date} The selected date
*/
getValue: function(){
var selectedDates = this.view.getSelected();
return (selectedDates.length > 0) ? selectedDates : null;
},
/**
* Set selected date.
* @method
* @param {Date} v Date to select.
* @return {void}
*/
setValue: function(v) {
this.view.setValue(v)
},
/**
* Override of the onCardSwitch method which adds a new card to the end/beginning of the carousel depending on the direction configured with the next period's
* dates.
* @method
* @private
*/
onActiveItemChange: function(container, newCard, oldCard){
if (this.getEnableSwipeNavigate()) {
var items = this.getItems();
var newIndex = items.indexOf(newCard), oldIndex = items.indexOf(oldCard), direction = (newIndex > oldIndex) ? 'forward' : 'backward';
this.counter = (this.counter || 0) + 1;
if (direction === 'forward') {
this.remove(items.get(0));
var newCalendar = new Ext.ux.TouchCalendarView(this.getViewConfig(this.getViewDate(newCard.currentDate, 1)));
this.add(newCalendar);
}
else {
this.remove(items.get(items.getCount() - 1));
var newCalendar = new Ext.ux.TouchCalendarView(this.getViewConfig(this.getViewDate(newCard.currentDate, -1)));
this.insert(0, newCalendar);
}
this.view = newCard;
var dateRange = this.view.getPeriodMinMaxDate();
this.fireEvent('periodchange', this.view, dateRange.min.get('date'), dateRange.max.get('date'), direction);
}
}
});