summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js
authorGiulio Cesare Solaroli <giulio.cesare@clipperz.com>2011-10-02 23:56:18 (UTC)
committer Giulio Cesare Solaroli <giulio.cesare@clipperz.com>2011-10-02 23:56:18 (UTC)
commitef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff)
treec403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js
parent597ecfbc0249d83e1b856cbd558340c01237a360 (diff)
downloadclipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip
clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz
clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2
First version of the newly restructured repository
Diffstat (limited to 'frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js268
1 files changed, 268 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js b/frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js
new file mode 100644
index 0000000..303ad2b
--- a/dev/null
+++ b/frontend/beta/js/YUI-extensions/grid/editor/DateEditor.js
@@ -0,0 +1,268 @@
1/**
2 * @class YAHOO.ext.grid.DateEditor
3 * @extends YAHOO.ext.grid.CellEditor
4Provides a date editor field, and optionally a DatePicker. The DateEditor provides a method to override (showCalendar) if you don't want to use the built in DatePicker control. The reason I chose to use my own DatePicker control rather than the nice YUI Calendar component is my control was very easy to override events to make it work well with the grid. It's also only 5k compressed, while the YUI Calendar is 40k compressed. The DatePicker supports left/right keys to move months, up/down keys to move years and the mouse wheel to quickly go through the months. The DateEditor supports the following configuration options:
5<ul class="list">
6<li><i>format</i> - The date format for the editor. The format is identical to <a href="http://www.php.net/date">PHP date()</a> and text is allowed. Credit for that goes to <a style="font-weight:normal;" href="http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/">this fantastic date library</a>. This format is for the editor only and doesn't affect the rendering of the cell when not in edit mode. Your rendering function can use any date format it wants.</li>
7<li><i>minValue</i> - The minimum allowed date. Can be either a Javascript date object or a string date in the specified format.</li>
8<li><i>maxValue</i> - The maximum allowed date. Can be either a Javascript date object or a string date in the specified format.</li>
9<li><i>minText</i> - The tooltip to display when the date in the cell is before minValue.</li>
10<li><i>maxText</i> - The tooltip to display when the date in the cell is after maxValue.</li>
11<li><i>invalidText</i> - The text to display when the date in the field is invalid (for example: 02/31/06)</li>
12<li><i>disabledDays</i> - An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday.</li>
13<li><i>disabledDaysText</i> - The tooltip to display when the date in the cell (or DatePicker) falls on a disabled day.</li>
14<li><i>disabledDates</i> - An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular expression so they are very powerful. For example, ["03/08/2003", "09/16/2003"] would disable those dates, but ["03/08", "09/16"] would disable them for every year. If you are using short years, you will want to use ^ to tell the regular expression to only match the beginning like ["^03/08"]. To disable March of 2006: ["03/../2006"] or every March ["^03"]. In order to support regular expressions, if you are using a date format that has "." in it, you will have to escape the dot when restricting dates. For example: ["03\\.08\\.03"].</li>
15<li><i>disabledDatesText</i> - The tooltip to display when the date in the cell (or DatePicker) falls on a disabled date.</li>
16<li><i>allowBlank</i> - True if the cell is allowed to be empty.</li>
17<li><i>blankText</i> - The tooltip (error message) to display when the cell is empty and is not allowed to be.</li>
18<li><i>validator</i> - Any custom validation function you want called. The function must return true if the data is valid or an error message otherwise.</li>
19<li><i>validationDelay</i> - The delay in milliseconds for validation. Each time the user types something the field is validated after a specified delay, setting this value allows you to customize that delay (for example, if your custom validation routine is slow).</li>
20</ul>
21For more information on using this editor, see <a href="http://www.jackslocum.com/yui/2006/09/10/adding-built-in-editing-support-to-the-yahoo-ui-extensions-grid/">this blog post</a>.
22* @constructor
23* Create a new DateEditor
24* @param {Object} config
25 */
26YAHOO.ext.grid.DateEditor = function(config){
27 var div = document.createElement('span');
28 div.className = 'ygrid-editor ygrid-editor-container';
29
30 var element = document.createElement('input');
31 element.type = 'text';
32 element.tabIndex = 1;
33 element.setAttribute('autocomplete', 'off');
34 div.appendChild(element);
35
36 var pick = document.createElement('span');
37 pick.className = 'pick-button';
38 div.appendChild(pick);
39
40 document.body.appendChild(div);
41
42 this.div = getEl(div, true);
43 this.element = getEl(element, true);
44 this.pick = getEl(pick, true);
45
46 this.colIndex = null;
47 this.rowIndex = null;
48 this.grid = null;
49 this.editing = false;
50 this.originalValue = null;
51 this.initialized = false;
52 this.callback = null;
53
54 this.cal = null;
55 this.mouseDownHandler = YAHOO.ext.EventManager.wrap(this.handleMouseDown, this, true);
56
57 YAHOO.ext.util.Config.apply(this, config);
58 if(typeof this.minValue == 'string') this.minValue = this.parseDate(this.minValue);
59 if(typeof this.maxValue == 'string') this.maxValue = this.parseDate(this.maxValue);
60 this.ddMatch = /ddnone/;
61 if(this.disabledDates){
62 var dd = this.disabledDates;
63 var re = "(?:";
64 for(var i = 0; i < dd.length; i++){
65 re += dd[i];
66 if(i != dd.length-1) re += "|";
67 }
68 this.ddMatch = new RegExp(re + ")");
69 }
70};
71
72YAHOO.ext.grid.DateEditor.prototype = {
73 init : function(grid, bodyElement, callback){
74 if(this.initialized) return;
75
76 this.initialized = true;
77 this.callback = callback;
78 this.grid = grid;
79 bodyElement.appendChild(this.div.dom);
80 this.initEvents();
81 },
82
83 initEvents : function(){
84 var stopOnEnter = function(e){
85 if(e.browserEvent.keyCode == e.RETURN){
86 this.stopEditing(true);
87 }else if(e.browserEvent.keyCode == e.ESC){
88 this.setValue(this.originalValue);
89 this.stopEditing(true);
90 }
91 }
92 this.element.mon('keydown', stopOnEnter, this, true);
93 var vtask = new YAHOO.ext.util.DelayedTask(this.validate, this);
94 this.element.mon('keyup', vtask.delay.createDelegate(vtask, [this.validationDelay]));
95 this.pick.on('click', this.showCalendar, this, true);
96 },
97
98 startEditing : function(value, row, cell){
99 this.originalValue = value;
100 this.rowIndex = row.rowIndex;
101 this.colIndex = cell.columnIndex;
102 this.cell = cell;
103 this.setValue(value);
104 this.validate();
105 var cellbox = getEl(cell, true).getBox();
106 this.div.setBox(cellbox, true);
107 this.element.setWidth(cellbox.width-this.pick.getWidth());
108 this.editing = true;
109 YAHOO.util.Event.on(document, "mousedown", this.mouseDownHandler);
110 this.show();
111 },
112
113 stopEditing : function(focusCell){
114 if(this.editing){
115 YAHOO.util.Event.removeListener(document, "mousedown", this.mouseDownHandler);
116 this.editing = false;
117 var newValue = this.getValue();
118 this.hide();
119 //if(focusCell){try{this.cell.focus();}catch(e){}}// try to give the cell focus so keyboard nav still works
120 if(this.originalValue != newValue){
121 this.callback(newValue, this.rowIndex, this.colIndex);
122 }
123 }
124 },
125
126 setValue : function(value){
127 this.element.dom.value = this.formatDate(value);
128 this.validate();
129 },
130
131 getValue : function(){
132 if(!this.validate()){
133 return this.originalValue;
134 }else{
135 var value = this.element.dom.value;
136 if(value.length < 1){
137 return value;
138 } else{
139 return this.parseDate(value);
140 }
141 }
142 },
143
144 show : function() {
145 this.div.show();
146 this.element.focus();
147 this.validate();
148 },
149
150 hide : function(){
151 try{
152 this.element.dom.blur();
153 }catch(e){}
154 this.div.hide();
155 },
156
157 validate : function(){
158 var dom = this.element.dom;
159 var value = dom.value;
160 if(value.length < 1){ // if it's blank
161 if(this.allowBlank){
162 dom.title = '';
163 this.element.removeClass('ygrid-editor-invalid');
164 return true;
165 }else{
166 dom.title = this.blankText;
167 this.element.addClass('ygrid-editor-invalid');
168 return false;
169 }
170 }
171 value = this.parseDate(value);
172 if(!value){
173 dom.title = this.invalidText.replace('%0', dom.value).replace('%1', this.format);
174 this.element.addClass('ygrid-editor-invalid');
175 return false;
176 }
177 var time = value.getTime();
178 if(this.minValue && time < this.minValue.getTime()){
179 dom.title = this.minText.replace('%0', this.formatDate(this.minValue));
180 this.element.addClass('ygrid-editor-invalid');
181 return false;
182 }
183 if(this.maxValue && time > this.maxValue.getTime()){
184 dom.title = this.maxText.replace('%0', this.formatDate(this.maxValue));
185 this.element.addClass('ygrid-editor-invalid');
186 return false;
187 }
188 if(this.disabledDays){
189 var day = value.getDay();
190 for(var i = 0; i < this.disabledDays.length; i++) {
191 if(day === this.disabledDays[i]){
192 dom.title = this.disabledDaysText;
193 this.element.addClass('ygrid-editor-invalid');
194 return false;
195 }
196 }
197 }
198 var fvalue = this.formatDate(value);
199 if(this.ddMatch.test(fvalue)){
200 dom.title = this.disabledDatesText.replace('%0', fvalue);
201 this.element.addClass('ygrid-editor-invalid');
202 return false;
203 }
204 var msg = this.validator(value);
205 if(msg !== true){
206 dom.title = msg;
207 this.element.addClass('ygrid-editor-invalid');
208 return false;
209 }
210 dom.title = '';
211 this.element.removeClass('ygrid-editor-invalid');
212 return true;
213 },
214
215 handleMouseDown : function(e){
216 var t = e.getTarget();
217 var dom = this.div.dom;
218 if(t != dom && !YAHOO.util.Dom.isAncestor(dom, t)){
219 this.stopEditing();
220 }
221 },
222
223 showCalendar : function(value){
224 if(this.cal == null){
225 this.cal = new YAHOO.ext.DatePicker(this.div.dom.parentNode.parentNode);
226 }
227 this.cal.minDate = this.minValue;
228 this.cal.maxDate = this.maxValue;
229 this.cal.disabledDatesRE = this.ddMatch;
230 this.cal.disabledDatesText = this.disabledDatesText;
231 this.cal.disabledDays = this.disabledDays;
232 this.cal.disabledDaysText = this.disabledDaysText;
233 this.cal.format = this.format;
234 if(this.minValue){
235 this.cal.minText = this.minText.replace('%0', this.formatDate(this.minValue));
236 }
237 if(this.maxValue){
238 this.cal.maxText = this.maxText.replace('%0', this.formatDate(this.maxValue));
239 }
240 var r = this.div.getRegion();
241 this.cal.show(r.left, r.bottom, this.getValue(), this.setValue.createDelegate(this));
242 },
243
244 parseDate : function(value){
245 if(!value || value instanceof Date) return value;
246 return Date.parseDate(value, this.format);
247 },
248
249 formatDate : function(date){
250 if(!date || !(date instanceof Date)) return date;
251 return date.format(this.format);
252 }
253};
254
255YAHOO.ext.grid.DateEditor.prototype.format = 'm/d/y';
256YAHOO.ext.grid.DateEditor.prototype.disabledDays = null;
257YAHOO.ext.grid.DateEditor.prototype.disabledDaysText = '';
258YAHOO.ext.grid.DateEditor.prototype.disabledDates = null;
259YAHOO.ext.grid.DateEditor.prototype.disabledDatesText = '';
260YAHOO.ext.grid.DateEditor.prototype.allowBlank = true;
261YAHOO.ext.grid.DateEditor.prototype.minValue = null;
262YAHOO.ext.grid.DateEditor.prototype.maxValue = null;
263YAHOO.ext.grid.DateEditor.prototype.minText = 'The date in this field must be after %0';
264YAHOO.ext.grid.DateEditor.prototype.maxText = 'The date in this field must be before %0';
265YAHOO.ext.grid.DateEditor.prototype.blankText = 'This field cannot be blank';
266YAHOO.ext.grid.DateEditor.prototype.invalidText = '%0 is not a valid date - it must be in the format %1';
267YAHOO.ext.grid.DateEditor.prototype.validationDelay = 200;
268YAHOO.ext.grid.DateEditor.prototype.validator = function(){return true;};