source: trunk/prototype/plugins/datejs/sugarpak-debug.js @ 7143

Revision 7143, 15.9 KB checked in by antonio, 12 years ago (diff)

Ticket #3085 - Corrigida inconsistência com o formato de hora no Expresso Calendar

Line 
1/**
2 * @version: 1.0 Alpha-1
3 * @author: Coolite Inc. http://www.coolite.com/
4 * @date: 2008-04-13
5 * @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved.
6 * @license: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
7 * @website: http://www.datejs.com/
8 */
9
10/**
11 **************************************************************
12 ** SugarPak - Domain Specific Language -  Syntactical Sugar **
13 **************************************************************
14 */
15 
16(function () {
17    var $D = Date, $P = $D.prototype, $C = $D.CultureInfo, $N = Number.prototype;
18
19    // private
20    $P._orient = +1;
21
22    // private
23    $P._nth = null;
24
25    // private
26    $P._is = false;
27
28    // private
29    $P._same = false;
30   
31    // private
32    $P._isSecond = false;
33
34    // private
35    $N._dateElement = "day";
36
37    /**
38     * Moves the date to the next instance of a date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
39     * Example
40    <pre><code>
41    Date.today().next().friday();
42    Date.today().next().fri();
43    Date.today().next().march();
44    Date.today().next().mar();
45    Date.today().next().week();
46    </code></pre>
47     *
48     * @return {Date}    date
49     */
50    $P.next = function () {
51        this._orient = +1;
52        return this;
53    };
54
55    /**
56     * Creates a new Date (Date.today()) and moves the date to the next instance of the date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
57     * Example
58    <pre><code>
59    Date.next().friday();
60    Date.next().fri();
61    Date.next().march();
62    Date.next().mar();
63    Date.next().week();
64    </code></pre>
65     *
66     * @return {Date}    date
67     */   
68    $D.next = function () {
69        return $D.today().next();
70    };
71
72    /**
73     * Moves the date to the previous instance of a date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
74     * Example
75    <pre><code>
76    Date.today().last().friday();
77    Date.today().last().fri();
78    Date.today().last().march();
79    Date.today().last().mar();
80    Date.today().last().week();
81    </code></pre>
82     * 
83     * @return {Date}    date
84     */
85    $P.last = $P.prev = $P.previous = function () {
86        this._orient = -1;
87        return this;
88    };
89
90    /**
91     * Creates a new Date (Date.today()) and moves the date to the previous instance of the date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
92     * Example
93    <pre><code>
94    Date.last().friday();
95    Date.last().fri();
96    Date.previous().march();
97    Date.prev().mar();
98    Date.last().week();
99    </code></pre>
100     * 
101     * @return {Date}    date
102     */
103    $D.last = $D.prev = $D.previous = function () {
104        return $D.today().last();
105    };   
106
107    /**
108     * Performs a equality check when followed by either a month name, day name or .weekday() function.
109     * Example
110    <pre><code>
111    Date.today().is().friday(); // true|false
112    Date.today().is().fri();
113    Date.today().is().march();
114    Date.today().is().mar();
115    </code></pre>
116     * 
117     * @return {Boolean}    true|false
118     */
119    $P.is = function () {
120        this._is = true;
121        return this;
122    };
123
124    /**
125     * Determines if two date objects occur on/in exactly the same instance of the subsequent date part function.
126     * The function .same() must be followed by a date part function (example: .day(), .month(), .year(), etc).
127     *
128     * An optional Date can be passed in the date part function. If now date is passed as a parameter, 'Now' is used.
129     *
130     * The following example demonstrates how to determine if two dates fall on the exact same day.
131     *
132     * Example
133    <pre><code>
134    var d1 = Date.today(); // today at 00:00
135    var d2 = new Date();   // exactly now.
136
137    // Do they occur on the same day?
138    d1.same().day(d2); // true
139   
140     // Do they occur on the same hour?
141    d1.same().hour(d2); // false, unless d2 hour is '00' (midnight).
142   
143    // What if it's the same day, but one year apart?
144    var nextYear = Date.today().add(1).year();
145
146    d1.same().day(nextYear); // false, because the dates must occur on the exact same day.
147    </code></pre>
148     *
149     * Scenario: Determine if a given date occurs during some week period 2 months from now.
150     *
151     * Example
152    <pre><code>
153    var future = Date.today().add(2).months();
154    return someDate.same().week(future); // true|false;
155    </code></pre>
156     * 
157     * @return {Boolean}    true|false
158     */   
159    $P.same = function () {
160        this._same = true;
161        this._isSecond = false;
162        return this;
163    };
164
165    /**
166     * Determines if the current date/time occurs during Today. Must be preceded by the .is() function.
167     * Example
168    <pre><code>
169    someDate.is().today();    // true|false
170    new Date().is().today();  // true
171    Date.today().is().today();// true
172    Date.today().add(-1).day().is().today(); // false
173    </code></pre>
174     * 
175     * @return {Boolean}    true|false
176     */   
177    $P.today = function () {
178        return this.same().day();
179    };
180
181    /**
182     * Determines if the current date is a weekday. This function must be preceded by the .is() function.
183     * Example
184    <pre><code>
185    Date.today().is().weekday(); // true|false
186    </code></pre>
187     * 
188     * @return {Boolean}    true|false
189     */
190    $P.weekday = function () {
191        if (this._is) {
192            this._is = false;
193            return (!this.is().sat() && !this.is().sun());
194        }
195        return false;
196    };
197
198    /**
199     * Sets the Time of the current Date instance. A string "6:15 pm" or config object {hour:18, minute:15} are accepted.
200     * Example
201    <pre><code>
202    // Set time to 6:15pm with a String
203    Date.today().at("6:15pm");
204
205    // Set time to 6:15pm with a config object
206    Date.today().at({hour:18, minute:15});
207    </code></pre>
208     * 
209     * @return {Date}    date
210     */
211    $P.at = function (time) {
212        return (typeof time === "string") ? $D.parse(this.toString("d") + " " + time) : this.set(time);
213    };
214       
215    /**
216     * Creates a new Date() and adds this (Number) to the date based on the preceding date element function (eg. second|minute|hour|day|month|year).
217     * Example
218    <pre><code>
219    // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.
220    (3).days().fromNow();
221    (6).months().fromNow();
222
223    // Declared Number variables do not require parentheses.
224    var n = 6;
225    n.months().fromNow();
226    </code></pre>
227     * 
228     * @return {Date}    A new Date instance
229     */
230    $N.fromNow = $N.after = function (date) {
231        var c = {};
232        c[this._dateElement] = this;
233        return ((!date) ? new Date() : date.clone()).add(c);
234    };
235
236    /**
237     * Creates a new Date() and subtract this (Number) from the date based on the preceding date element function (eg. second|minute|hour|day|month|year).
238     * Example
239    <pre><code>
240    // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.
241    (3).days().ago();
242    (6).months().ago();
243
244    // Declared Number variables do not require parentheses.
245    var n = 6;
246    n.months().ago();
247    </code></pre>
248     * 
249     * @return {Date}    A new Date instance
250     */
251    $N.ago = $N.before = function (date) {
252        var c = {};
253        c[this._dateElement] = this * -1;
254        return ((!date) ? new Date() : date.clone()).add(c);
255    };
256
257    // Do NOT modify the following string tokens. These tokens are used to build dynamic functions.
258    // All culture-specific strings can be found in the CultureInfo files. See /trunk/src/globalization/.
259    var dx = ("sunday monday tuesday wednesday thursday friday saturday").split(/\s/),
260        mx = ("january february march april may june july august september october november december").split(/\s/),
261        px = ("Millisecond Second Minute Hour Day Week Month Year").split(/\s/),
262        pxf = ("Milliseconds Seconds Minutes Hours Date Week Month FullYear").split(/\s/),
263                nth = ("final first second third fourth fifth").split(/\s/),
264        de;
265
266   /**
267     * Returns an object literal of all the date parts.
268     * Example
269    <pre><code>
270        var o = new Date().toObject();
271       
272        // { year: 2008, month: 4, week: 20, day: 13, hour: 18, minute: 9, second: 32, millisecond: 812 }
273       
274        // The object properties can be referenced directly from the object.
275       
276        alert(o.day);  // alerts "13"
277        alert(o.year); // alerts "2008"
278    </code></pre>
279     * 
280     * @return {Date}    An object literal representing the original date object.
281     */
282    $P.toObject = function () {
283        var o = {};
284        for (var i = 0; i < px.length; i++) {
285            o[px[i].toLowerCase()] = this["get" + pxf[i]]();
286        }
287        return o;
288    };
289   
290   /**
291     * Returns a date created from an object literal. Ignores the .week property if set in the config.
292     * Example
293    <pre><code>
294        var o = new Date().toObject();
295       
296        return Date.fromObject(o); // will return the same date.
297
298    var o2 = {month: 1, day: 20, hour: 18}; // birthday party!
299    Date.fromObject(o2);
300    </code></pre>
301     * 
302     * @return {Date}    An object literal representing the original date object.
303     */   
304    $D.fromObject = function(config) {
305        config.week = null;
306        return Date.today().set(config);
307    };
308       
309    // Create day name functions and abbreviated day name functions (eg. monday(), friday(), fri()).
310    var df = function (n) {
311        return function () {
312            if (this._is) {
313                this._is = false;
314                return this.getDay() == n;
315            }
316            if (this._nth !== null) {
317                // If the .second() function was called earlier, remove the _orient
318                // from the date, and then continue.
319                // This is required because 'second' can be used in two different context.
320                //
321                // Example
322                //
323                //   Date.today().add(1).second();
324                //   Date.march().second().monday();
325                //
326                // Things get crazy with the following...
327                //   Date.march().add(1).second().second().monday(); // but it works!!
328                // 
329                if (this._isSecond) {
330                    this.addSeconds(this._orient * -1);
331                }
332                // make sure we reset _isSecond
333                this._isSecond = false;
334
335                var ntemp = this._nth;
336                this._nth = null;
337                var temp = this.clone().moveToLastDayOfMonth();
338                this.moveToNthOccurrence(n, ntemp);
339                if (this > temp) {
340                    throw new RangeError($D.getDayName(n) + " does not occur " + ntemp + " times in the month of " + $D.getMonthName(temp.getMonth()) + " " + temp.getFullYear() + ".");
341                }
342                return this;
343            }                   
344            return this.moveToDayOfWeek(n, this._orient);
345        };
346    };
347   
348    var sdf = function (n) {
349        return function () {
350            var t = $D.today(), shift = n - t.getDay();
351            if (n === 0 && $C.firstDayOfWeek === 1 && t.getDay() !== 0) {
352                shift = shift + 7;
353            }
354            return t.addDays(shift);
355        };
356    };
357       
358    for (var i = 0; i < dx.length; i++) {
359        // Create constant static Day Name variables. Example: Date.MONDAY or Date.MON
360        $D[dx[i].toUpperCase()] = $D[dx[i].toUpperCase().substring(0, 3)] = i;
361
362        // Create Day Name functions. Example: Date.monday() or Date.mon()
363        $D[dx[i]] = $D[dx[i].substring(0, 3)] = sdf(i);
364
365        // Create Day Name instance functions. Example: Date.today().next().monday()
366        $P[dx[i]] = $P[dx[i].substring(0, 3)] = df(i);
367    }
368   
369    // Create month name functions and abbreviated month name functions (eg. january(), march(), mar()).
370    var mf = function (n) {
371        return function () {
372            if (this._is) {
373                this._is = false;
374                return this.getMonth() === n;
375            }
376            return this.moveToMonth(n, this._orient);
377        };
378    };
379   
380    var smf = function (n) {
381        return function () {
382            return $D.today().set({ month: n, day: 1 });
383        };
384    };
385   
386    for (var j = 0; j < mx.length; j++) {
387        // Create constant static Month Name variables. Example: Date.MARCH or Date.MAR
388        $D[mx[j].toUpperCase()] = $D[mx[j].toUpperCase().substring(0, 3)] = j;
389
390        // Create Month Name functions. Example: Date.march() or Date.mar()
391        $D[mx[j]] = $D[mx[j].substring(0, 3)] = smf(j);
392
393        // Create Month Name instance functions. Example: Date.today().next().march()
394        $P[mx[j]] = $P[mx[j].substring(0, 3)] = mf(j);
395    }
396   
397    // Create date element functions and plural date element functions used with Date (eg. day(), days(), months()).
398    var ef = function (j) {
399        return function () {
400            // if the .second() function was called earlier, the _orient
401            // has alread been added. Just return this and reset _isSecond.
402            if (this._isSecond) {
403                this._isSecond = false;
404                return this;
405            }
406
407            if (this._same) {
408                this._same = this._is = false;
409                var o1 = this.toObject(),
410                    o2 = (arguments[0] || new Date()).toObject(),
411                    v = "",
412                    k = j.toLowerCase();
413                   
414                for (var m = (px.length - 1); m > -1; m--) {
415                    v = px[m].toLowerCase();
416                    if (o1[v] != o2[v]) {
417                        return false;
418                    }
419                    if (k == v) {
420                        break;
421                    }
422                }
423                return true;
424            }
425           
426            if (j.substring(j.length - 1) != "s") {
427                j += "s";
428            }
429            return this["add" + j](this._orient);
430        };
431    };
432   
433   
434    var nf = function (n) {
435        return function () {
436            this._dateElement = n;
437            return this;
438        };
439    };
440   
441    for (var k = 0; k < px.length; k++) {
442        de = px[k].toLowerCase();
443   
444        // Create date element functions and plural date element functions used with Date (eg. day(), days(), months()).
445        $P[de] = $P[de + "s"] = ef(px[k]);
446       
447        // Create date element functions and plural date element functions used with Number (eg. day(), days(), months()).
448        $N[de] = $N[de + "s"] = nf(de);
449    }
450   
451    $P._ss = ef("Second");
452       
453    var nthfn = function (n) {
454        return function (dayOfWeek) {
455            if (this._same) {
456                return this._ss(arguments[0]);
457            }
458            if (dayOfWeek || dayOfWeek === 0) {
459                return this.moveToNthOccurrence(dayOfWeek, n);
460            }
461            this._nth = n;
462
463            // if the operator is 'second' add the _orient, then deal with it later...
464            if (n === 2 && (dayOfWeek === undefined || dayOfWeek === null)) {
465                this._isSecond = true;
466                return this.addSeconds(this._orient);
467            }
468            return this;
469        };
470    };
471
472    for (var l = 0; l < nth.length; l++) {
473        $P[nth[l]] = (l === 0) ? nthfn(-1) : nthfn(l);
474    }
475}());
Note: See TracBrowser for help on using the repository browser.