]> git.proxmox.com Git - extjs.git/blame - extjs/examples/classic/calendar/src/form/EventWindow.js
add extjs 6.0.1 sources
[extjs.git] / extjs / examples / classic / calendar / src / form / EventWindow.js
CommitLineData
6527f429
DM
1/**\r
2 * @class Ext.calendar.form.EventWindow\r
3 * @extends Ext.Window\r
4 * <p>A custom window containing a basic edit form used for quick editing of events.</p>\r
5 * <p>This window also provides custom events specific to the calendar so that other calendar components can be easily\r
6 * notified when an event has been edited via this component.</p>\r
7 * @constructor\r
8 * @param {Object} config The config object\r
9 */\r
10Ext.define('Ext.calendar.form.EventWindow', {\r
11 extend: 'Ext.window.Window',\r
12 alias: 'widget.eventeditwindow',\r
13 \r
14 requires: [\r
15 'Ext.form.Panel',\r
16 'Ext.calendar.util.Date',\r
17 'Ext.calendar.data.EventModel',\r
18 'Ext.calendar.data.EventMappings'\r
19 ],\r
20\r
21 constructor: function(config) {\r
22 var formPanelCfg = {\r
23 xtype: 'form',\r
24 fieldDefaults: {\r
25 msgTarget: 'side',\r
26 labelWidth: 65\r
27 },\r
28 frame: false,\r
29 bodyStyle: 'background:transparent;padding:5px 10px 10px;',\r
30 bodyBorder: false,\r
31 border: false,\r
32 items: [{\r
33 itemId: 'title',\r
34 name: Ext.calendar.data.EventMappings.Title.name,\r
35 fieldLabel: 'Title',\r
36 xtype: 'textfield',\r
37 allowBlank: false,\r
38 emptyText: 'Event Title',\r
39 anchor: '100%'\r
40 },\r
41 {\r
42 xtype: 'daterangefield',\r
43 itemId: 'date-range',\r
44 name: 'dates',\r
45 anchor: '100%',\r
46 fieldLabel: 'When'\r
47 }]\r
48 };\r
49 \r
50 if (config.calendarStore) {\r
51 this.calendarStore = config.calendarStore;\r
52 delete config.calendarStore;\r
53 \r
54 formPanelCfg.items.push({\r
55 xtype: 'calendarpicker',\r
56 itemId: 'calendar',\r
57 name: Ext.calendar.data.EventMappings.CalendarId.name,\r
58 anchor: '100%',\r
59 store: this.calendarStore\r
60 });\r
61 }\r
62 \r
63 this.callParent([Ext.apply({\r
64 titleTextAdd: 'Add Event',\r
65 titleTextEdit: 'Edit Event',\r
66 width: 600,\r
67 autocreate: true,\r
68 border: true,\r
69 closeAction: 'hide',\r
70 modal: false,\r
71 resizable: false,\r
72 buttonAlign: 'left',\r
73 savingMessage: 'Saving changes...',\r
74 deletingMessage: 'Deleting event...',\r
75 layout: 'fit',\r
76 \r
77 defaultFocus: 'title',\r
78 onEsc: function(key, event) {\r
79 event.target.blur(); // Remove the focus to avoid doing the validity checks when the window is shown again.\r
80 this.onCancel();\r
81 },\r
82\r
83 fbar: [{\r
84 xtype: 'tbtext',\r
85 text: '<a href="#" id="tblink">Edit Details...</a>'\r
86 },\r
87 '->',\r
88 {\r
89 itemId: 'delete-btn',\r
90 text: 'Delete Event',\r
91 disabled: false,\r
92 handler: this.onDelete,\r
93 scope: this,\r
94 minWidth: 150,\r
95 hideMode: 'offsets'\r
96 },\r
97 {\r
98 text: 'Save',\r
99 disabled: false,\r
100 handler: this.onSave,\r
101 scope: this\r
102 },\r
103 {\r
104 text: 'Cancel',\r
105 disabled: false,\r
106 handler: this.onCancel,\r
107 scope: this\r
108 }],\r
109 items: formPanelCfg\r
110 },\r
111 config)]);\r
112 },\r
113\r
114 // private\r
115 newId: 10000,\r
116\r
117 /**\r
118 * @event eventadd\r
119 * Fires after a new event is added\r
120 * @param {Ext.calendar.form.EventWindow} this\r
121 * @param {Ext.calendar.EventRecord} rec The new {@link Ext.calendar.EventRecord record} that was added\r
122 */\r
123\r
124 /**\r
125 * @event eventupdate\r
126 * Fires after an existing event is updated\r
127 * @param {Ext.calendar.form.EventWindow} this\r
128 * @param {Ext.calendar.EventRecord} rec The new {@link Ext.calendar.EventRecord record} that was updated\r
129 */\r
130\r
131 /**\r
132 * @event eventdelete\r
133 * Fires after an event is deleted\r
134 * @param {Ext.calendar.form.EventWindow} this\r
135 * @param {Ext.calendar.EventRecord} rec The new {@link Ext.calendar.EventRecord record} that was deleted\r
136 */\r
137\r
138 /**\r
139 * @event eventcancel\r
140 * Fires after an event add/edit operation is canceled by the user and no store update took place\r
141 * @param {Ext.calendar.form.EventWindow} this\r
142 * @param {Ext.calendar.EventRecord} rec The new {@link Ext.calendar.EventRecord record} that was canceled\r
143 */\r
144\r
145 /**\r
146 * @event editdetails\r
147 * Fires when the user selects the option in this window to continue editing in the detailed edit form\r
148 * (by default, an instance of {@link Ext.calendar.EventEditForm}. Handling code should hide this window\r
149 * and transfer the current event record to the appropriate instance of the detailed form by showing it\r
150 * and calling {@link Ext.calendar.EventEditForm#loadRecord loadRecord}.\r
151 * @param {Ext.calendar.form.EventWindow} this\r
152 * @param {Ext.calendar.EventRecord} rec The {@link Ext.calendar.EventRecord record} that is currently being edited\r
153 */\r
154\r
155 // private\r
156 initComponent: function() {\r
157 this.callParent();\r
158\r
159 this.formPanel = this.items.items[0];\r
160 },\r
161\r
162 // private\r
163 afterRender: function() {\r
164 this.callParent();\r
165\r
166 this.el.addCls('ext-cal-event-win');\r
167\r
168 Ext.get('tblink').on('click', this.onEditDetailsClick, this);\r
169 \r
170 this.titleField = this.down('#title');\r
171 this.dateRangeField = this.down('#date-range');\r
172 this.calendarField = this.down('#calendar');\r
173 this.deleteButton = this.down('#delete-btn');\r
174 },\r
175 \r
176 // private\r
177 onEditDetailsClick: function(e){\r
178 e.stopEvent();\r
179 this.updateRecord(this.activeRecord, true);\r
180 this.fireEvent('editdetails', this, this.activeRecord, this.animateTarget);\r
181 },\r
182\r
183 /**\r
184 * Shows the window, rendering it first if necessary, or activates it and brings it to front if hidden.\r
185 * @param {Ext.data.Record/Object} o Either a {@link Ext.data.Record} if showing the form\r
186 * for an existing event in edit mode, or a plain object containing a StartDate property (and \r
187 * optionally an EndDate property) for showing the form in add mode. \r
188 * @param {String/Element} animateTarget (optional) The target element or id from which the window should\r
189 * animate while opening (defaults to null with no animation)\r
190 * @return {Ext.Window} this\r
191 */\r
192 show: function(o, animateTarget) {\r
193 // Work around the CSS day cell height hack needed for initial render in IE8/strict:\r
194 var me = this,\r
195 anim = (Ext.isIE8 && Ext.isStrict) ? null: animateTarget,\r
196 M = Ext.calendar.data.EventMappings,\r
197 data = {};\r
198\r
199 this.callParent([anim, function(){\r
200 me.titleField.focus(true);\r
201 }]);\r
202 \r
203 this.deleteButton[o.data && o.data[M.EventId.name] ? 'show': 'hide']();\r
204\r
205 var rec,\r
206 f = this.formPanel.form;\r
207\r
208 if (o.data) {\r
209 rec = o;\r
210 this.setTitle(rec.phantom ? this.titleTextAdd : this.titleTextEdit);\r
211 f.loadRecord(rec);\r
212 }\r
213 else {\r
214 this.setTitle(this.titleTextAdd);\r
215\r
216 var start = o[M.StartDate.name],\r
217 end = o[M.EndDate.name] || Ext.calendar.util.Date.add(start, {hours: 1});\r
218 \r
219 data[M.StartDate.name] = start;\r
220 data[M.EndDate.name] = end;\r
221 data[M.IsAllDay.name] = !!o[M.IsAllDay.name] || start.getDate() != Ext.calendar.util.Date.add(end, {millis: 1}).getDate();\r
222 rec = new Ext.calendar.data.EventModel(data);\r
223\r
224 f.reset();\r
225 f.loadRecord(rec);\r
226 }\r
227\r
228 if (this.calendarStore) {\r
229 this.calendarField.setValue(rec.data[M.CalendarId.name]);\r
230 }\r
231 this.dateRangeField.setValue(rec.data);\r
232 this.activeRecord = rec;\r
233\r
234 return this;\r
235 },\r
236\r
237 // private\r
238 roundTime: function(dt, incr) {\r
239 incr = incr || 15;\r
240 var m = parseInt(dt.getMinutes(), 10);\r
241 return dt.add('mi', incr - (m % incr));\r
242 },\r
243\r
244 // private\r
245 onCancel: function() {\r
246 this.cleanup(true);\r
247 this.fireEvent('eventcancel', this);\r
248 },\r
249\r
250 // private\r
251 cleanup: function(hide) {\r
252 if (this.activeRecord && this.activeRecord.dirty) {\r
253 this.activeRecord.reject();\r
254 }\r
255 delete this.activeRecord;\r
256\r
257 if (hide === true) {\r
258 // Work around the CSS day cell height hack needed for initial render in IE8/strict:\r
259 //var anim = afterDelete || (Ext.isIE8 && Ext.isStrict) ? null : this.animateTarget;\r
260 this.hide();\r
261 }\r
262 },\r
263\r
264 // private\r
265 updateRecord: function(record, keepEditing) {\r
266 var fields = record.getFields(),\r
267 values = this.formPanel.getForm().getValues(),\r
268 name,\r
269 M = Ext.calendar.data.EventMappings,\r
270 obj = {};\r
271\r
272 Ext.Array.each(fields, function(f) {\r
273 name = f.name;\r
274 if (name in values) {\r
275 obj[name] = values[name];\r
276 }\r
277 });\r
278 \r
279 var dates = this.dateRangeField.getValue();\r
280 obj[M.StartDate.name] = dates[0];\r
281 obj[M.EndDate.name] = dates[1];\r
282 obj[M.IsAllDay.name] = dates[2];\r
283\r
284 record.beginEdit();\r
285 record.set(obj);\r
286 \r
287 if (!keepEditing) {\r
288 record.endEdit();\r
289 }\r
290\r
291 return this;\r
292 },\r
293 \r
294 // private\r
295 onSave: function(){\r
296 if(!this.formPanel.form.isValid()){\r
297 return;\r
298 }\r
299 if(!this.updateRecord(this.activeRecord)){\r
300 this.onCancel();\r
301 return;\r
302 }\r
303 this.fireEvent(this.activeRecord.phantom ? 'eventadd' : 'eventupdate', this, this.activeRecord, this.animateTarget);\r
304\r
305 // Clear phantom and modified states.\r
306 this.activeRecord.commit();\r
307 },\r
308 \r
309 // private\r
310 onDelete: function(){\r
311 this.fireEvent('eventdelete', this, this.activeRecord, this.animateTarget);\r
312 }\r
313});