]> source.dussan.org Git - jquery-ui.git/commitdiff
Calendar: Move custom date object to ui namespace
authorFelix Nagel <info@felixnagel.com>
Sun, 27 Sep 2015 16:35:35 +0000 (18:35 +0200)
committerFelix Nagel <info@felixnagel.com>
Sun, 27 Sep 2015 16:45:53 +0000 (18:45 +0200)
demos/bootstrap.js
external/date.js [deleted file]
tests/lib/bootstrap.js
tests/unit/date/core.js
ui/date.js [new file with mode: 0644]
ui/widgets/calendar.js

index 2f2b3209aa2ed3a9b08b17582c3d059343becdfd..faffcbdd89bd523de2c14decace6ae8625ecabc2 100644 (file)
@@ -73,7 +73,6 @@ require.config( {
        baseUrl: "../../ui",
        paths: {
                cldr: "../external/cldrjs/cldr",
-               date: "../external/date",
                globalize: "../external/globalize/globalize",
                "globalize-locales": "../external/localization",
                jquery: "../external/jquery/jquery",
diff --git a/external/date.js b/external/date.js
deleted file mode 100644 (file)
index b1f10dd..0000000
+++ /dev/null
@@ -1,241 +0,0 @@
-/*
- * Calendar math built on jquery-global
- *
- * Based on Marc Grabanski's jQuery Date Plugin
- * http://marcgrabanski.com/articles/jquery-date-plugin
- */
-( function( factory ) {
-       if ( typeof define === "function" && define.amd ) {
-
-               // AMD. Register as an anonymous module.
-               define( [
-                       "jquery"
-               ], factory );
-       } else {
-
-               // Browser globals
-               factory( jQuery );
-       }
-}( function( $ ) {
-
-$.ui = $.ui || {};
-
-var _Date,
-       weekdaysRev = {
-               "sun": 0,
-               "mon": 1,
-               "tue": 2,
-               "wed": 3,
-               "thu": 4,
-               "fri": 5,
-               "sat": 6
-       };
-
-_Date = function( date, attributes ) {
-       if ( !( this instanceof _Date ) ) {
-               return new _Date( date, attributes );
-       }
-
-       this.setAttributes( attributes );
-
-       if ( typeof date === "string" && date.length ) {
-               this.dateObject = attributes.parse( date );
-       } else if ( $.type( date ) === "date" ) {
-               this.dateObject = date;
-       }
-
-       this.dateObject = this.dateObject || new Date();
-};
-
-$.extend( _Date.prototype, {
-
-       setAttributes: function( attributes ) {
-               this.attributes = attributes;
-               this.firstDay = weekdaysRev[ this.attributes.firstDay ];
-       },
-
-       // TODO: Same as the underlying Date object's terminology, but still misleading.
-       // TODO: We can use .setTime() instead of new Date and rename to setTimestamp.
-       setTime: function( time ) {
-               this.dateObject = new Date( time );
-               return this;
-       },
-
-       setDay: function( day ) {
-               var date = this.dateObject;
-               // FIXME: Why not to use .setDate?
-               this.dateObject = new Date( date.getFullYear(), date.getMonth(), day, date.getHours(),
-                       date.getMinutes(), date.getSeconds() );
-               return this;
-       },
-
-       setMonth: function( month ) {
-
-               // Overflow example:  Month is October 31 (yeah Halloween) and month is changed to April with 30 days,
-               // the new date will me May 1.  We will honor the month the user wants to set and if and overflow
-               // occurs, set to last day of month.
-               var date = this.dateObject,
-                       days = date.getDate(), year = date.getFullYear();
-               if ( days > this.daysInMonth( year, month ) ) {
-
-                       // Overflow
-                       days = this.daysInMonth( year, month );
-               }
-               this.dateObject = new Date( year, month, days, date.getHours(),
-                       date.getMinutes(), date.getSeconds() );
-               return this;
-       },
-
-       setYear: function( year ) {
-               var date = this.dateObject,
-                       day = date.getDate(),
-                       month = date.getMonth();
-
-               // Check if Leap, and February and day is 29th
-               if ( this.isLeapYear( year ) && month === 1 && day === 29 ) {
-
-                       // set day to last day of February
-                       day = this.daysInMonth( year, month );
-               }
-               this.dateObject = new Date( year, month, day, date.getHours(),
-                       date.getMinutes(), date.getSeconds() );
-               return this;
-       },
-
-       setFullDate: function( year, month, day ) {
-               this.dateObject = new Date( year, month, day );
-               return this;
-       },
-
-       adjust: function( period, offset ) {
-               var date = this.dateObject,
-                       day = period === "D" ? date.getDate() + offset : date.getDate(),
-                       month = period === "M" ? date.getMonth() + offset : date.getMonth(),
-                       year = period === "Y" ? date.getFullYear() + offset : date.getFullYear();
-
-               // If not day, update the day to the new month and year
-               if ( period !== "D" ) {
-                       day = Math.max( 1, Math.min( day, this.daysInMonth( year, month ) ) );
-               }
-               this.dateObject = new Date( year, month, day, date.getHours(),
-                       date.getMinutes(), date.getSeconds() );
-               return this;
-       },
-
-       daysInMonth: function( year, month ) {
-               var date = this.dateObject;
-               year = year || date.getFullYear();
-               month = month || date.getMonth();
-               return 32 - new Date( year, month, 32 ).getDate();
-       },
-
-       monthName: function() {
-               return this.attributes.formatMonth( this.dateObject );
-       },
-
-       day: function() {
-               return this.dateObject.getDate();
-       },
-
-       month: function() {
-               return this.dateObject.getMonth();
-       },
-
-       year: function() {
-               return this.dateObject.getFullYear();
-       },
-
-       isLeapYear: function( year ) {
-               year = year || this.dateObject.getFullYear();
-               return new Date( year, 1, 29 ).getMonth() === 1;
-       },
-
-       weekdays: function() {
-               var date,
-                       firstDay = this.firstDay,
-                       result = [];
-
-               // date = firstDay
-               date = new Date( this.dateObject.getTime() );
-               date.setDate( date.getDate() + firstDay - 1 - date.getDay() );
-
-               for ( var dow = 0; dow < 7; dow++ ) {
-                       date.setTime( date.getTime() + 86400000 );
-                       result.push({
-                               shortname: this.attributes.formatWeekdayShort( date ),
-                               fullname: this.attributes.formatWeekdayFull( date )
-                       });
-               }
-
-               return result;
-       },
-
-       days: function() {
-               var result = [],
-                       today = new _Date( new Date(), this.attributes ),
-                       date = this.dateObject,
-                       firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
-                       leadDays = ( firstDayOfMonth - this.firstDay + 7 ) % 7,
-                       rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7 ),
-                       printDate = new Date( this.year(), date.getMonth(), 1 - leadDays );
-               for ( var row = 0; row < rows; row++ ) {
-                       var week = result[ result.length ] = {
-                               number: this.attributes.formatWeekOfYear( printDate ),
-                               days: []
-                       };
-                       for ( var dayx = 0; dayx < 7; dayx++ ) {
-                               var day = week.days[ week.days.length ] = {
-                                       lead: printDate.getMonth() !== date.getMonth(),
-                                       date: printDate.getDate(),
-                                       month: printDate.getMonth(),
-                                       year: printDate.getFullYear(),
-                                       timestamp: printDate.getTime(),
-                                       today: today.equal( printDate )
-                               };
-                               day.render = day.selectable = !day.lead;
-                               if ( this.eachDay ) {
-                                       this.eachDay( day );
-                               }
-
-                               // TODO use adjust("D", 1)?
-                               printDate.setDate( printDate.getDate() + 1 );
-                       }
-               }
-               return result;
-       },
-
-       // specialized for multi-month template, could be used in general
-       months: function( add ) {
-               var clone,
-                       result = [ this ];
-
-               for ( var i = 0; i < add; i++ ) {
-                       clone = this.clone();
-                       clone.adjust( "M", i + 1 );
-                       result.push( clone );
-               }
-               result[ 0 ].first = true;
-               result[ result.length - 1 ].last = true;
-               return result;
-       },
-
-       clone: function() {
-               var date = this.dateObject;
-               return new _Date( new Date( date.getTime() ), this.attributes );
-       },
-
-       equal: function( other ) {
-               var format = function( date ) {
-                       return "" + date.getFullYear() + date.getMonth() + date.getDate();
-               };
-               return format( this.dateObject ) === format( other );
-       },
-
-       date: function() {
-               return this.dateObject;
-       }
-});
-
-return $.ui.calendarDate = _Date;
-
-} ) );
index 571621fa44ec4c71632000f0a5b9f344fc67fb49..89c4d489363410f722ce65e31be6a4916e69c48a 100644 (file)
@@ -2,7 +2,6 @@
 
 requirejs.config({
        paths: {
-               "date": "../../../external/date",
                "cldr": "../../../external/cldrjs/cldr",
                "globalize": "../../../external/globalize/globalize",
                "globalize-locales": "../../../external/localization",
@@ -20,7 +19,7 @@ requirejs.config({
                "ui": "../../../ui"
        },
        shim: {
-               "date": [ "globalize-locales" ],
+               "ui/date": [ "globalize-locales" ],
                "globalize-old/ja-JP": [ "globalize-old" ],
                "jquery-simulate": [ "jquery" ],
                "qunit-assert-close": [ "qunit" ],
index 91d368fcb952d53497c0320cb8ed707974c36bcb..349f5d87307abcf8d8e7f4dc376796dbdc81a518 100644 (file)
@@ -1,7 +1,7 @@
 define( [
        "jquery",
        "./helper",
-       "date"
+       "ui/date"
 ], function( $, testHelper ) {
 
 module( "date: core" );
@@ -10,12 +10,12 @@ var attributes = testHelper.getAttributes( "en" );
 
 test( "Instantiation", function() {
        expect( 2 );
-       ok( new $.ui.calendarDate( null, attributes ) instanceof $.ui.calendarDate, "constructor function" );
-       ok( $.ui.calendarDate( null, attributes ) instanceof $.ui.calendarDate, "instantiation without new" );
+       ok( new $.ui.date( null, attributes ) instanceof $.ui.date, "constructor function" );
+       ok( $.ui.date( null, attributes ) instanceof $.ui.date, "instantiation without new" );
 });
 
 test( "Check Sets and Gets", 6, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        equal( date.setYear( 2012 ).year(), 2012, "Set year and retrieve" );
        equal( date.setMonth( 9 ).month(), 9, "Set month and retrieve" );
        equal( date.setDay( 15 ).day(), 15, "Set day and retrieve" );
@@ -25,7 +25,7 @@ test( "Check Sets and Gets", 6, function() {
 });
 
 test( "Date Adjustments - Normal Use Cases", 10, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
 
        // Use October 15, 2012
        date.setFullDate( 2012, 9, 15 );
@@ -50,7 +50,7 @@ test( "Date Adjustments - Normal Use Cases", 10, function() {
 });
 
 test( "Date Adjustments - Month Overflow Edge Cases", 2, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
 
        // Use May 31 2012
        date.setFullDate( 2012, 4, 31 );
@@ -61,7 +61,7 @@ test( "Date Adjustments - Month Overflow Edge Cases", 2, function() {
 });
 
 test( "Date Adjustments - Leap Year Edge Cases", 1, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
 
        // Use February 29 2012 a Leap year
        date.setFullDate( 2012, 1, 29 );
@@ -70,7 +70,7 @@ test( "Date Adjustments - Leap Year Edge Cases", 1, function() {
 });
 
 test( "List days of Week", 2, function() {
-       var date = $.ui.calendarDate( null, attributes ),
+       var date = $.ui.date( null, attributes ),
                offset0 = [
                        { "fullname": "Sunday", "shortname": "Su" },
                        { "fullname": "Monday", "shortname": "Mo" },
@@ -91,12 +91,12 @@ test( "List days of Week", 2, function() {
                ];
 
        deepEqual( date.weekdays(), offset0, "Get weekdays with start of day on 0 (English)" );
-       date = $.ui.calendarDate( null, testHelper.getAttributes( "de" ) );
+       date = $.ui.date( null, testHelper.getAttributes( "de" ) );
        deepEqual( date.weekdays(), offset1, "Get weekdays with start of day on 1 (Germany)" );
 });
 
 test( "Leap Year Check", 8, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        ok( date.setYear( 2008 ).isLeapYear(), "2008 is a Leap Year" );
        ok( !date.setYear( 2009 ).isLeapYear(), "2009 is not a Leap Year" );
        ok( !date.setYear( 2010 ).isLeapYear(), "2010 is not a Leap Year" );
@@ -108,7 +108,7 @@ test( "Leap Year Check", 8, function() {
 });
 
 test( "Days in Month", 3, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        date.setFullDate( 2012, 1, 1 );
        equal( date.daysInMonth(), 29, "Leap Year implicit check for 29 days" );
        equal( date.daysInMonth( 2012, 1 ), 29, "Leap Year explicit check for 29 days" );
@@ -116,14 +116,14 @@ test( "Days in Month", 3, function() {
 });
 
 test( "Month Name", 2, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        equal( date.setMonth( 3 ).monthName(), "April", "Month name return April (English)" );
-       date = $.ui.calendarDate( null, testHelper.getAttributes( "de" ) );
+       date = $.ui.date( null, testHelper.getAttributes( "de" ) );
        equal( date.setMonth( 2 ).monthName(), "März", "Month name return March (German)" );
 });
 
 test( "Clone", 2, function() {
-       var date = $.ui.calendarDate( null, attributes ),
+       var date = $.ui.date( null, attributes ),
                date2 = date.clone();
        ok( date2, "Created cloned object" );
        notEqual( date.adjust( "Y", 1 ).year(), date2.year(), "Object manipulated independently" );
@@ -131,7 +131,7 @@ test( "Clone", 2, function() {
 
 test( "Days", 1, function() {
        // TODO needs work
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        date.eachDay = function( day ) {
                if ( day.lead && day.date > 20 ) {
                        day.selectable = false;
@@ -156,7 +156,7 @@ test( "Days", 1, function() {
 });
 
 test( "Months", 5, function(){
-       var date = $.ui.calendarDate( null, attributes ),
+       var date = $.ui.date( null, attributes ),
                firstMonth = date.months( 1 )[ 0 ],
                lastMonth = date.months( 1 )[ 1 ];
 
@@ -169,7 +169,7 @@ test( "Months", 5, function(){
 });
 
 test( "Equal", 4, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        date.setFullDate( 2012, 9, 16 );
        ok( date.equal( new Date( 2012, 9, 16 ) ), "Does date equal provide date" );
        ok( !date.equal( new Date( 2011, 9, 16 ) ), "Does date year not equal provide date" );
@@ -178,7 +178,7 @@ test( "Equal", 4, function() {
 });
 
 test( "Date", 1, function() {
-       var date = $.ui.calendarDate( null, attributes );
+       var date = $.ui.date( null, attributes );
        ok( date.date() instanceof Date, "Date returned" );
 });
 
diff --git a/ui/date.js b/ui/date.js
new file mode 100644 (file)
index 0000000..1d207a3
--- /dev/null
@@ -0,0 +1,237 @@
+/*
+ * Calendar math built on jquery-global
+ *
+ * Based on Marc Grabanski's jQuery Date Plugin
+ * http://marcgrabanski.com/articles/jquery-date-plugin
+ */
+( function( factory ) {
+       if ( typeof define === "function" && define.amd ) {
+
+               // AMD. Register as an anonymous module.
+               define( [
+                       "jquery", "./version" ], factory );
+       } else {
+
+               // Browser globals
+               factory( jQuery );
+       }
+}( function( $ ) {
+
+var weekdaysRev = {
+               "sun": 0,
+               "mon": 1,
+               "tue": 2,
+               "wed": 3,
+               "thu": 4,
+               "fri": 5,
+               "sat": 6
+       };
+
+$.ui.date = function( date, attributes ) {
+       if ( !( this instanceof $.ui.date ) ) {
+               return new $.ui.date( date, attributes );
+       }
+
+       this.setAttributes( attributes );
+
+       if ( typeof date === "string" && date.length ) {
+               this.dateObject = attributes.parse( date );
+       } else if ( $.type( date ) === "date" ) {
+               this.dateObject = date;
+       }
+
+       this.dateObject = this.dateObject || new Date();
+};
+
+$.extend( $.ui.date.prototype, {
+
+       setAttributes: function( attributes ) {
+               this.attributes = attributes;
+               this.firstDay = weekdaysRev[ this.attributes.firstDay ];
+       },
+
+       // TODO: Same as the underlying Date object's terminology, but still misleading.
+       // TODO: We can use .setTime() instead of new Date and rename to setTimestamp.
+       setTime: function( time ) {
+               this.dateObject = new Date( time );
+               return this;
+       },
+
+       setDay: function( day ) {
+               var date = this.dateObject;
+               // FIXME: Why not to use .setDate?
+               this.dateObject = new Date( date.getFullYear(), date.getMonth(), day, date.getHours(),
+                       date.getMinutes(), date.getSeconds() );
+               return this;
+       },
+
+       setMonth: function( month ) {
+
+               // Overflow example:  Month is October 31 (yeah Halloween) and month is changed to April with 30 days,
+               // the new date will me May 1.  We will honor the month the user wants to set and if and overflow
+               // occurs, set to last day of month.
+               var date = this.dateObject,
+                       days = date.getDate(), year = date.getFullYear();
+               if ( days > this.daysInMonth( year, month ) ) {
+
+                       // Overflow
+                       days = this.daysInMonth( year, month );
+               }
+               this.dateObject = new Date( year, month, days, date.getHours(),
+                       date.getMinutes(), date.getSeconds() );
+               return this;
+       },
+
+       setYear: function( year ) {
+               var date = this.dateObject,
+                       day = date.getDate(),
+                       month = date.getMonth();
+
+               // Check if Leap, and February and day is 29th
+               if ( this.isLeapYear( year ) && month === 1 && day === 29 ) {
+
+                       // set day to last day of February
+                       day = this.daysInMonth( year, month );
+               }
+               this.dateObject = new Date( year, month, day, date.getHours(),
+                       date.getMinutes(), date.getSeconds() );
+               return this;
+       },
+
+       setFullDate: function( year, month, day ) {
+               this.dateObject = new Date( year, month, day );
+               return this;
+       },
+
+       adjust: function( period, offset ) {
+               var date = this.dateObject,
+                       day = period === "D" ? date.getDate() + offset : date.getDate(),
+                       month = period === "M" ? date.getMonth() + offset : date.getMonth(),
+                       year = period === "Y" ? date.getFullYear() + offset : date.getFullYear();
+
+               // If not day, update the day to the new month and year
+               if ( period !== "D" ) {
+                       day = Math.max( 1, Math.min( day, this.daysInMonth( year, month ) ) );
+               }
+               this.dateObject = new Date( year, month, day, date.getHours(),
+                       date.getMinutes(), date.getSeconds() );
+               return this;
+       },
+
+       daysInMonth: function( year, month ) {
+               var date = this.dateObject;
+               year = year || date.getFullYear();
+               month = month || date.getMonth();
+               return 32 - new Date( year, month, 32 ).getDate();
+       },
+
+       monthName: function() {
+               return this.attributes.formatMonth( this.dateObject );
+       },
+
+       day: function() {
+               return this.dateObject.getDate();
+       },
+
+       month: function() {
+               return this.dateObject.getMonth();
+       },
+
+       year: function() {
+               return this.dateObject.getFullYear();
+       },
+
+       isLeapYear: function( year ) {
+               year = year || this.dateObject.getFullYear();
+               return new Date( year, 1, 29 ).getMonth() === 1;
+       },
+
+       weekdays: function() {
+               var date,
+                       firstDay = this.firstDay,
+                       result = [];
+
+               // date = firstDay
+               date = new Date( this.dateObject.getTime() );
+               date.setDate( date.getDate() + firstDay - 1 - date.getDay() );
+
+               for ( var dow = 0; dow < 7; dow++ ) {
+                       date.setTime( date.getTime() + 86400000 );
+                       result.push({
+                               shortname: this.attributes.formatWeekdayShort( date ),
+                               fullname: this.attributes.formatWeekdayFull( date )
+                       });
+               }
+
+               return result;
+       },
+
+       days: function() {
+               var result = [],
+                       today = new $.ui.date( new Date(), this.attributes ),
+                       date = this.dateObject,
+                       firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
+                       leadDays = ( firstDayOfMonth - this.firstDay + 7 ) % 7,
+                       rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7 ),
+                       printDate = new Date( this.year(), date.getMonth(), 1 - leadDays );
+               for ( var row = 0; row < rows; row++ ) {
+                       var week = result[ result.length ] = {
+                               number: this.attributes.formatWeekOfYear( printDate ),
+                               days: []
+                       };
+                       for ( var dayx = 0; dayx < 7; dayx++ ) {
+                               var day = week.days[ week.days.length ] = {
+                                       lead: printDate.getMonth() !== date.getMonth(),
+                                       date: printDate.getDate(),
+                                       month: printDate.getMonth(),
+                                       year: printDate.getFullYear(),
+                                       timestamp: printDate.getTime(),
+                                       today: today.equal( printDate )
+                               };
+                               day.render = day.selectable = !day.lead;
+                               if ( this.eachDay ) {
+                                       this.eachDay( day );
+                               }
+
+                               // TODO use adjust("D", 1)?
+                               printDate.setDate( printDate.getDate() + 1 );
+                       }
+               }
+               return result;
+       },
+
+       // specialized for multi-month template, could be used in general
+       months: function( add ) {
+               var clone,
+                       result = [ this ];
+
+               for ( var i = 0; i < add; i++ ) {
+                       clone = this.clone();
+                       clone.adjust( "M", i + 1 );
+                       result.push( clone );
+               }
+               result[ 0 ].first = true;
+               result[ result.length - 1 ].last = true;
+               return result;
+       },
+
+       clone: function() {
+               var date = this.dateObject;
+               return new $.ui.date( new Date( date.getTime() ), this.attributes );
+       },
+
+       equal: function( other ) {
+               var format = function( date ) {
+                       return "" + date.getFullYear() + date.getMonth() + date.getDate();
+               };
+               return format( this.dateObject ) === format( other );
+       },
+
+       date: function() {
+               return this.dateObject;
+       }
+});
+
+return $.ui.date;
+
+} ) );
index 25c1df001e94c2dfa2a4b5862144a88a29ad641e..63b09ede3e3e4ffc31bb659e6d0cbc06edf57d85 100644 (file)
@@ -22,7 +22,7 @@
                        "globalize",
                        "globalize/date",
                        "globalize-locales",
-                       "date",
+                       "../date",
                        "./button",
                        "../widget",
                        "../version",
@@ -78,7 +78,7 @@ return $.widget( "ui.calendar", {
 
                this._setLocale( this.options.locale, this.options.dateFormat );
 
-               this.date = new $.ui.calendarDate( this.options.value, this._calendarDateOptions );
+               this.date = new $.ui.date( this.options.value, this._calendarDateOptions );
                this.viewDate = this.date.clone();
                this.viewDate.eachDay = this.options.eachDay;