Dotclear

source: admin/js/date-picker.js @ 2566:9bf417837888

Revision 2566:9bf417837888, 11.3 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Add some people in CREDITS, remove trailing spaces and tabs.

RevLine 
[0]1function datePicker(target)
2{
3     if (!document.getElementById) { return; }
[2566]4
[0]5     if (!target || target.nodeName.toLowerCase() != 'input') {
6          return;
7     }
[2566]8
[0]9     this.target = target;
10     this.oTable = document.createElement('table');
11     this.oBody = document.createElement('tbody');
12     this.oDates = new Array();
13     this.oMonth = document.createElement('span');
14     this.oYear = document.createElement('span');
15     this.oHour = document.createElement('input');
16     this.oMinute = document.createElement('input');
17     this.oTable.id = 'dc_datepicker_'+target.id;
18     this.oTable.className = 'date-picker';
[2566]19
[0]20     var cur = 1;
21     var oRow, oHeading, oSpan;
[2566]22
[0]23     // Set title
24     oRow = document.createElement('tr');
[2566]25
[0]26     // Month block
27     oHeading = document.createElement('th');
28     oHeading.colSpan = 4;
29     oHeading.className = 'date-picker-month';
[2566]30
[0]31     var nav = document.createElement('span');
32     nav.appendChild(document.createTextNode(String.fromCharCode(171)));
33     nav.fn = this.changeMonth;
34     nav.obj = this;
35     nav.onclick = function() { this.fn.call(this.obj,-1); };
36     nav.className = 'date-picker-control';
37     oHeading.appendChild(nav);
[2566]38
[0]39     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
[2566]40
[0]41     nav = document.createElement('span');
42     nav.appendChild(document.createTextNode(String.fromCharCode(187)));
43     nav.fn = this.changeMonth;
44     nav.obj = this;
45     nav.onclick = function() { this.fn.call(this.obj,+1); };
46     nav.className = 'date-picker-control';
47     oHeading.appendChild(nav);
[2566]48
[0]49     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
[2566]50
[0]51     oHeading.appendChild(this.oMonth);
[2566]52
[0]53     oRow.appendChild(oHeading);
[2566]54
[0]55     // Year block
56     oHeading = document.createElement('th');
57     oHeading.colSpan = 3;
58     oHeading.className = 'date-picker-year';
[2566]59
[0]60     oHeading.appendChild(this.oYear);
[2566]61
[0]62     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
[2566]63
[0]64     nav = document.createElement('span');
65     nav.appendChild(document.createTextNode(String.fromCharCode(171)));
66     nav.fn = this.changeYear;
67     nav.obj = this;
68     nav.onclick = function() { this.fn.call(this.obj,-1); };
69     nav.className = 'date-picker-control';
70     oHeading.appendChild(nav);
[2566]71
[0]72     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
[2566]73
[0]74     nav = document.createElement('span');
75     nav.appendChild(document.createTextNode(String.fromCharCode(187)));
76     nav.fn = this.changeYear;
77     nav.obj = this;
78     nav.onclick = function() { this.fn.call(this.obj,+1); };
79     nav.className = 'date-picker-control';
80     oHeading.appendChild(nav);
[2566]81
[0]82     oRow.appendChild(oHeading);
[2566]83
[0]84     this.oBody.appendChild(oRow);
[2566]85
[0]86     // Create legend
87     oRow = document.createElement('tr');
88     var cday;
89     for (i=0; i<this.days.length; i++) {
90          cday = this.days[i].substring(0,1).toUpperCase();
91          oHeading = document.createElement('th');
92          oHeading.appendChild(document.createTextNode(cday));
93          oHeading.setAttribute('title',this.days[i]);
94          oRow.appendChild(oHeading);
95     }
96     this.oBody.appendChild(oRow);
[2566]97
[0]98     // Create 6 rows of 7 cols for days
99     for (var i=0; i<6; i++) {
100          oRow = document.createElement('tr');
[2566]101
[0]102          for (var j=0; j<7; j++) {
103               this.oDates[cur] = document.createElement('td');
104               this.oDates[cur].appendChild(document.createTextNode(
105                    String.fromCharCode(160)));
106               oRow.appendChild(this.oDates[cur]);
107               cur++;
108          }
[2566]109
[0]110          this.oBody.appendChild(oRow);
111     }
[2566]112
[0]113     // Time controls
114     oRow = document.createElement('tr');
[2566]115
[0]116     oHeading = document.createElement('th');
117     oHeading.className = 'date-picker-control';
118     oHeading.appendChild(document.createTextNode('!'));
119     oHeading.setAttribute('title',this.now_msg);
120     oHeading.fn = this.sendNow;
121     oHeading.obj = this;
122     oHeading.onclick = function() { this.fn.call(this.obj); };
[2566]123
[0]124     oRow.appendChild(oHeading);
[2566]125
[0]126     oHeading = document.createElement('th');
127     oHeading.colSpan = 5;
[2566]128
[0]129     oSpan = document.createElement('span');
130     oSpan.className = 'date-picker-control';
131     oSpan.appendChild(document.createTextNode('-'));
132     oSpan.fn = this.changeHour;
133     oSpan.obj = this;
134     oSpan.onclick = function() { this.fn.call(this.obj,-1); };
135     oHeading.appendChild(oSpan);
136     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
137     oSpan = document.createElement('span');
138     oSpan.className = 'date-picker-control';
139     oSpan.appendChild(document.createTextNode('+'));
140     oSpan.fn = this.changeHour;
141     oSpan.obj = this;
142     oSpan.onclick = function() { this.fn.call(this.obj,+1); };
143     oHeading.appendChild(oSpan);
144     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
[2566]145
[0]146     this.oHour.size = 3;
147     oHeading.appendChild(this.oHour);
[2566]148
[0]149     oHeading.appendChild(document.createTextNode(' : '));
[2566]150
[0]151     this.oMinute.size = 3;
152     oHeading.appendChild(this.oMinute);
[2566]153
[0]154     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
155     oSpan = document.createElement('span');
156     oSpan.className = 'date-picker-control';
157     oSpan.appendChild(document.createTextNode('-'));
158     oSpan.fn = this.changeMinute;
159     oSpan.obj = this;
160     oSpan.onclick = function() { this.fn.call(this.obj,-1); };
161     oHeading.appendChild(oSpan);
162     oHeading.appendChild(document.createTextNode(String.fromCharCode(160)));
163     oSpan = document.createElement('span');
164     oSpan.className = 'date-picker-control';
165     oSpan.appendChild(document.createTextNode('+'));
166     oSpan.fn = this.changeMinute;
167     oSpan.obj = this;
168     oSpan.onclick = function() { this.fn.call(this.obj,+1); };
[2566]169
[0]170     oHeading.appendChild(oSpan);
[2566]171
[0]172     oRow.appendChild(oHeading);
[2566]173
[0]174     // Close control
175     oHeading = document.createElement('th');
176     oHeading.className = 'date-picker-control';
177     oHeading.appendChild(document.createTextNode('x'));
178     oHeading.setAttribute('title',this.close_msg);
179     oHeading.fn = this.close;
180     oHeading.obj = this;
181     oHeading.onclick = function() { this.fn.call(this.obj); };
[2566]182
[0]183     oRow.appendChild(oHeading);
[2566]184
[0]185     this.oBody.appendChild(oRow);
186};
187
188datePicker.prototype = {
189     year: 0,
190     month: 0,
191     day: 0,
192     hour: 0,
193     minute: 0,
[2566]194
[0]195     img_src: '',
196     img_top: '0.2em',
197     now_msg: 'now',
198     close_msg: 'close',
[2566]199
[0]200     days: new Array('Monday','Tuesday','Wednesday','Thursday','Friday',
201     'Saturday','Sunday'),
[2566]202
[0]203     months: new Array('January','February','March','April','May','June',
204     'July','August','September','October','November','December'),
[2566]205
206
[0]207     setDate: function() {
208          if (this.numberOfDays() < this.day) {
209               this.day = this.numberOfDays();
210          }
[2566]211
[0]212          while (this.oYear.hasChildNodes()) {
213               this.oYear.removeChild(this.oYear.firstChild)
214          }
215          this.oYear.appendChild(document.createTextNode(this.year));
[2566]216
[0]217          while (this.oMonth.hasChildNodes()) {
218               this.oMonth.removeChild(this.oMonth.firstChild)
219          }
220          this.oMonth.appendChild(document.createTextNode(
221               this.months[this.month-1]));
[2566]222
[0]223          var firstDay = this.firstDay();
224          var nbDays = this.numberOfDays();
[2566]225
[0]226          // Empty days
227          for (var i=1; i<=42; i++) {
228               while (this.oDates[i].hasChildNodes()) {
229                    this.oDates[i].removeChild(this.oDates[i].firstChild)
230               }
231               this.oDates[i].appendChild(document.createTextNode('-'));
232               this.oDates[i].className = '';
233               this.oDates[i].onclick = function() { return; };
234          }
[2566]235
[0]236          // Insert days from the first day to the last
237          for (i=1; i<=nbDays; i++) {
238               var j=firstDay+i-1;
[2566]239
[0]240               while (this.oDates[j].hasChildNodes()) {
241                    this.oDates[j].removeChild(this.oDates[j].firstChild)
242               }
[2566]243
[0]244               this.oDates[j].appendChild(document.createTextNode(i));
245               this.oDates[j].index = i;
246               this.oDates[j].fn = this.sendDate;
247               this.oDates[j].obj = this;
248               this.oDates[j].onclick = function() { this.fn.call(this.obj,this.index); };
249               if (i == this.day) {
250                    this.oDates[j].className = 'date-picker-today';
251               } else {
252                    this.oDates[j].className = 'date-picker-day';
253               }
254          }
[2566]255
[0]256          // Set time
257          this.setHour(this.hour);
258          this.setMinute(this.minute);
259     },
[2566]260
[0]261     setHour: function(h) {
262          if (h < 0) { h = 23; }
263          if (h > 23) { h = 0; }
264          if (h < 10) { h = '0'+h; }
[2566]265
[0]266          this.hour = h*1;
267          this.oHour.value = h;
268     },
[2566]269
[0]270     setMinute: function(m) {
271          if (m < 0) { m = 59; }
272          if (m > 59) { m = 0; }
273          if (m < 10) { m = '0'+m; }
[2566]274
[0]275          this.minute = m*1;
276          this.oMinute.value = m;
277     },
[2566]278
[0]279     changeMonth: function(dir) {
280          var y = this.year;
281          var m = this.month;
282          m = m+dir;
[2566]283
[0]284          if (m > 12) { this.month = 1; this.year++; }
285          else if ( m < 1) { this.month = 12; this.year--; }
286          else { this.month = m; }
[2566]287
[0]288          this.setDate();
289     },
[2566]290
[0]291     changeYear: function(dir) {
292          this.year = this.year + dir;
293          this.setDate();
294     },
[2566]295
[0]296     changeHour: function(dir) {
297          this.setHour(this.hour*1+dir);
298     },
[2566]299
[0]300     changeMinute: function(dir) {
301          this.setMinute(this.minute*1+dir);
302     },
[2566]303
[0]304     sendDate: function(d) {
305          var m = this.month;
306          var hour = this.oHour.value*1;
307          var minute = this.oMinute.value*1;
[2566]308
[0]309          if (hour < 0 || hour > 23 || isNaN(hour)) { hour = 0; }
310          if (minute < 0 || minute > 59 || isNaN(minute)) { minute = 0; }
[2566]311
[0]312          if (m < 10) { m = '0'+m; }
313          if (d < 10) { d = '0'+d; }
314          if (hour < 10) { hour = '0'+hour; }
315          if (minute < 10) { minute = '0'+minute; }
[2566]316
[0]317          this.target.value = this.year+'-'+m+'-'+d+' '+hour+':'+minute;
318          this.close();
319     },
[2566]320
[0]321     sendNow: function() {
322          var dt = new Date();
323          var y = dt.getFullYear();
324          var m = dt.getMonth() + 1;
325          var d = dt.getDate();
326          var h = dt.getHours();
327          var i = dt.getMinutes();
[2566]328
[0]329          if (m < 10) { m = '0'+m; }
330          if (d < 10) { d = '0'+d; }
331          if (h < 10) { h = '0'+h; }
332          if (i < 10) { i = '0'+i; }
[2566]333
[0]334          this.target.value = y+'-'+m+'-'+d+' '+h+':'+i;
335          this.close();
336     },
[2566]337
[0]338     close: function() {
339          document.body.removeChild(this.oTable);
340     },
[2566]341
[0]342     numberOfDays: function() {
343          var res = 31;
344          if (this.month == 4 || this.month == 6 || this.month == 9 ||
345          this.month == 11) {
346               res = 30;
347          } else if (this.month == 2) {
348               res = 28;
349               if (this.year%4 == 0 && (this.year%100 != 0 ||
350               this.year%400 == 0)) {
351                    res = 29;
352               }
353          }
[2566]354
[0]355          return res;
356     },
[2566]357
[0]358     firstDay: function() {
359          var dt = new Date(this.year,this.month-1,1);
360          var res = dt.getDay();
[2566]361
[0]362          if (res == 0) {
363               res = 7;
364          }
[2566]365
[0]366          return res;
367     },
[2566]368
[0]369     show: function() {
370          // Parsing target value
371          var re = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/;
372          var match = re.exec(this.target.value);
373          if (match) {
374               this.year = match[1]*1;
375               this.month = match[2]*1;
376               this.day = match[3]*1;
377               this.hour = match[4]*1;
378               this.minute = match[5]*1;
379          } else {
380               var dt = new Date();
381               this.year = dt.getFullYear();
382               this.month = dt.getMonth() + 1;
383               this.day = dt.getDate();
384               this.hour = dt.getHours();
385               this.minute = dt.getMinutes();
386          }
[2566]387
[0]388          this.oTable.appendChild(this.oBody);
389          this.setDate();
390          this.setPosition();
391          document.body.appendChild(this.oTable);
392          this.oHour.focus();
393     },
[2566]394
[0]395     setPosition: function() {
396          var t_x = this.findPosX(this.target);
397          var t_y = this.findPosY(this.target);
398          var o_h = this.oTable.offsetHeight;
399          var o_w = this.oTable.offsetWidth;
[2566]400
[0]401          this.oTable.style.position = 'absolute';
402          this.oTable.style.zIndex = '100';
403          this.oTable.style.top = t_y+'px';
404          this.oTable.style.left = t_x+'px';
405     },
[2566]406
[0]407     findPosX: function(obj) {
408          var curleft = 0;
409          if(obj.offsetParent) {
410               while(1) {
411                    curleft += obj.offsetLeft;
412                    if(!obj.offsetParent) {
413                         break;
414                    }
415                    obj = obj.offsetParent;
416               }
417          } else if(obj.x) {
418               curleft += obj.x;
419          }
420          return curleft;
421     },
[2566]422
[0]423     findPosY: function(obj) {
424          var curtop = 0;
425          if(obj.offsetParent) {
426               while(1) {
427                    curtop += obj.offsetTop;
428                    if(!obj.offsetParent) {
429                         break;
430                    }
431                    obj = obj.offsetParent;
432               }
433          } else if(obj.y) {
434               curtop += obj.y;
435          }
436          return curtop;
437     },
[2566]438
[0]439     draw: function() {
440          var imgE = document.createElement('img');
441          imgE.src = this.img_src;
442          imgE.style.position = 'absolute';
443          imgE.style.top = this.img_top;
444          imgE.style.left = (this.target.clientWidth+4)+'px';
445          imgE.obj = this;
446          imgE.fn = this.show;
447          imgE.onclick = function() { this.fn.apply(this.obj); };
[2566]448
[0]449          this.target.parentNode.style.position = 'relative';
450          this.target.parentNode.insertBefore(imgE,this.target.nextSibling);
451     }
452};
Note: See TracBrowser for help on using the repository browser.

Sites map