source: Dev/branches/rest-dojo-ui/client/dojox/form/DateTextBox.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.3 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/lang",
4        "dojo/dom-style",
5        "dojox/widget/Calendar",
6        "dojox/widget/CalendarViews",
7        "dijit/form/_DateTimeTextBox",
8        "dijit/form/TextBox",
9        "dojo/_base/declare"
10], function(kernel, lang, domStyle, Calendar, CalendarViews, _DateTimeTextBox, TextBox, declare){
11kernel.experimental("dojox.form.DateTextBox");
12
13        /*=====
14                _DateTimeTextBox = dijit.form._DateTimeTextBox;
15        =====*/
16var DateTextBox = declare( "dojox.form.DateTextBox", _DateTimeTextBox,
17        {
18                // summary:
19                //              A validating, serializable, range-bound date text box with a popup calendar
20
21                // popupClass: String
22                //  The popup widget to use. In this case, a calendar with Day, Month and Year views.
23                popupClass: "dojox.widget.Calendar",
24                _selector: "date",
25
26                openDropDown: function(){
27                        this.inherited(arguments);
28                        domStyle.set(this.dropDown.domNode.parentNode, "position", "absolute");
29                }
30        }
31);
32
33
34declare( "dojox.form.DayTextBox", DateTextBox,
35        {
36                // summary:
37                //              A validating, serializable, range-bound date text box with a popup calendar that contains just months.
38
39                // popupClass: String
40                //  The popup widget to use. In this case, a calendar with just a Month view.
41                popupClass: "dojox.widget.DailyCalendar",
42
43                parse: function(displayVal){
44                        return displayVal;
45                },
46
47                format: function(value){
48                        return value.getDate ? value.getDate() : value;
49                },
50                validator: function(value){
51                        var num = Number(value);
52                        var isInt = /(^-?\d\d*$)/.test(String(value));
53                        return value == "" || value == null || (isInt && num >= 1 && num <= 31);
54                },
55
56                _setValueAttr: function(value, priorityChange, formattedValue){
57                        if(value){
58                                if(value.getDate){
59                                        value = value.getDate();
60                                }
61                        }
62                        TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
63                },
64
65                openDropDown: function(){
66                        this.inherited(arguments);
67
68                        this.dropDown.onValueSelected = lang.hitch(this, function(value){
69                                this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
70                                setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
71
72                                TextBox.prototype._setValueAttr.call(this, String(value.getDate()), true, String(value.getDate()));
73                        });
74                }
75        }
76);
77
78declare( "dojox.form.MonthTextBox", DateTextBox,
79        {
80                // summary:
81                //              A validating, serializable, range-bound date text box with a popup calendar that contains only years
82
83                // popupClass: String
84                //  The popup widget to use. In this case, a calendar with just a Year view.
85                popupClass: "dojox.widget.MonthlyCalendar",
86
87                selector: "date",
88
89                postMixInProperties: function(){
90                        this.inherited(arguments);
91                        this.constraints.datePattern = "MM";
92                },
93
94                format: function(value){
95                        if(!value && value !== 0){
96                                return 1;
97                        }
98                        if(value.getMonth){
99                                return value.getMonth() + 1;
100                        }
101                        return Number(value) + 1;
102                },
103
104                parse: function(value, constraints){
105                        return Number(value) - 1;
106                },
107
108                serialize: function(value, constraints){
109                        return String(value);
110                },
111
112                validator: function(value){
113                        var num = Number(value);
114                        var isInt = /(^-?\d\d*$)/.test(String(value));
115                        return value == "" || value == null || (isInt && num >= 1 && num <= 12);
116                },
117
118                _setValueAttr: function(value, priorityChange, formattedValue){
119                        if(value){
120                                if(value.getMonth){
121                                        value = value.getMonth();
122                                }
123                        }
124                        TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
125                },
126
127                openDropDown: function(){
128                        this.inherited(arguments);
129
130                        this.dropDown.onValueSelected = lang.hitch(this, function(value){
131                                this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
132                                setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
133                                TextBox.prototype._setValueAttr.call(this, value, true, value);
134                        });
135                }
136        }
137);
138
139
140declare( "dojox.form.YearTextBox", DateTextBox,
141        {
142                // summary:
143                //              A validating, serializable, range-bound date text box with a popup calendar that contains only years
144
145                popupClass: "dojox.widget.YearlyCalendar",
146
147                format: function(value){
148                        //console.log('Year format ' + value);
149                        if(typeof value == "string"){
150                                return value;
151                        }
152                        else if(value.getFullYear){
153                                return value.getFullYear();
154                        }
155                        return value;
156                },
157
158                validator: function(value){
159                        return value == "" || value == null || /(^-?\d\d*$)/.test(String(value));
160                },
161
162                _setValueAttr: function(value, priorityChange, formattedValue){
163                        if(value){
164                                if(value.getFullYear){
165                                        value = value.getFullYear();
166                                }
167                        }
168                        TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
169                },
170
171                openDropDown: function(){
172                        this.inherited(arguments);
173                        //console.log('yearly openDropDown and value = ' + this.get('value'));
174
175                        this.dropDown.onValueSelected = lang.hitch(this, function(value){
176                                this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
177                                setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
178                                TextBox.prototype._setValueAttr.call(this,value, true, value);
179                        });
180                },
181
182                parse: function(/*String*/value, /*dojo.date.locale.__FormatOptions*/constraints){
183                        return value || (this._isEmpty(value) ? null : undefined); // Date
184                },
185
186                filter: function(val){
187                        if(val && val.getFullYear){
188                                return val.getFullYear().toString();
189                        }
190                        return this.inherited(arguments);
191                }
192        }
193);
194return DateTextBox;
195});
Note: See TracBrowser for help on using the repository browser.