Skip to content

Commit

Permalink
refs #15108. Improve AMD + some fixes. Reviewed by apeller, might nee…
Browse files Browse the repository at this point in the history
…d further improvements like totally getting rid of dojox. !strict.

git-svn-id: http://svn.dojotoolkit.org/src/dojox/trunk@28269 560b804f-0ae3-0310-86f3-f6aa0a117693
  • Loading branch information
cjolif committed Apr 2, 2012
1 parent 056036f commit 68d143a
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 187 deletions.
29 changes: 14 additions & 15 deletions date/buddhist.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
define(["dojo/_base/kernel", "dojo/date", "./buddhist/Date"], function(dojo, dd, buddhistDate){
dojo.getObject("date.buddhist", true, dojox);
dojo.experimental("dojox.date.buddhist");
define(["..", "dojo/_base/lang", "dojo/date", "./buddhist/Date"], function(dojox, lang, dd, BDate){
var dbuddhist = lang.getObject("date.buddhist", true, dojox);

// Utility methods to do arithmetic calculations with buddhist.Dates

dojox.date.buddhist.getDaysInMonth = function(/*buddhist.Date*/dateObject){
dbuddhist.getDaysInMonth = function(/*buddhist.Date*/dateObject){
return dd.getDaysInMonth(dateObject.toGregorian());
};

dojox.date.buddhist.isLeapYear = function(/*buddhist.Date*/dateObject){
dbuddhist.isLeapYear = function(/*buddhist.Date*/dateObject){
return dd.isLeapYear(dateObject.toGregorian());
};

//FIXME: reduce compare, add, diff also
dojox.date.buddhist.compare = function(/*buddhist.Date*/date1, /*buddhist.Date*/date2, /*String?*/portion){
dbuddhist.compare = function(/*buddhist.Date*/date1, /*buddhist.Date*/date2, /*String?*/portion){
// summary:
// Compare two buddhist date objects by date, time, or both.
return dd.compare(date1,date2, portion); // int
};


dojox.date.buddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/interval, /*int*/amount){
dbuddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/interval, /*int*/amount){
// based on and similar to dojo.date.add
// summary:
// Add to a Date in intervals of different size, from milliseconds to years
Expand All @@ -33,7 +32,7 @@ dojox.date.buddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/i
// amount:
// How much to add to the date.

var newBuddDate = new buddhistDate(date);
var newBuddDate = new BDate(date);

switch(interval){
case "day":
Expand Down Expand Up @@ -98,7 +97,7 @@ dojox.date.buddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/i
return newBuddDate; // dojox.date.buddhist.Date
};

dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*dojox.date.buddhist.Date?*/date2, /*String?*/interval){
dbuddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*dojox.date.buddhist.Date?*/date2, /*String?*/interval){
// based on and similar to dojo.date.difference
// summary:
// date2 - date1
Expand All @@ -109,14 +108,14 @@ dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*d
// "millisecond", "week", "weekday"
// Defaults to "day".

date2 = date2 || new buddhistDate();
date2 = date2 || new BDate();
interval = interval || "day";
var yearDiff = date2.getFullYear() - date1.getFullYear();
var delta = 1; // Integer return value
switch(interval){
case "weekday":
var days = Math.round(dojox.date.buddhist.difference(date1, date2, "day"));
var weeks = parseInt(dojox.date.buddhist.difference(date1, date2, "week"));
var days = Math.round(dbuddhist.difference(date1, date2, "day"));
var weeks = parseInt(dbuddhist.difference(date1, date2, "week"));
var mod = days % 7;

// Even number of weeks
Expand All @@ -132,7 +131,7 @@ dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*d
mod = days % 7;
// Mark the date advanced by the number of
// round weeks (may be zero)
var dtMark = new buddhistDate(date2);
var dtMark = new BDate(date2);
dtMark.setDate(dtMark.getDate(true)+(weeks*7));
var dayMark = dtMark.getDay();

Expand Down Expand Up @@ -215,7 +214,7 @@ dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*d
case "week":
// Truncate instead of rounding
// Don't use Math.floor -- value may be negative
delta = parseInt(dojox.date.buddhist.difference(date1, date2, "day")/7);
delta = parseInt(dbuddhist.difference(date1, date2, "day")/7);
break;
case "day":
delta /= 24;
Expand All @@ -236,5 +235,5 @@ dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*d
// Round for fractional values and DST leaps
return Math.round(delta); // Number (integer)
};
return dojox.date.buddhist;
return dbuddhist;
});
18 changes: 8 additions & 10 deletions date/buddhist/Date.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
define([
"dojo/_base/kernel",
"dojo/_base/lang",
"dojo/_base/declare",
"dojo/date"
], function(dojo, declare, dd){
], function(lang, declare, dd){

dojo.getObject("date.buddhist.Date", true, dojox);
dojo.experimental("dojox.date.buddhist.Date");

dojo.declare("dojox.date.buddhist.Date", null, {
var BDate = declare("dojox.date.buddhist.Date", null, {

_date: 0,
_month: 0,
Expand All @@ -21,7 +18,7 @@ dojo.declare("dojox.date.buddhist.Date", null, {
constructor: function(){
// summary: This is the constructor
// description:
// This fucntion initialize the date object values
// This function initialize the date object values
//
// example:
// | var date1 = new dojox.date.buddhist.Date();
Expand Down Expand Up @@ -247,7 +244,8 @@ dojo.declare("dojox.date.buddhist.Date", null, {

toString: function(){
// summary: This returns a string representation of the date in "dd, MM, YYYY HH:MM:SS" format
return this._date + ", " + this._month + ", " + this._year + " " + this._hours + ":" + this._minutes + ":" + this._seconds; // String
return isNaN(this._date)?"Invalid Date":
this._date + ", " + this._month + ", " + this._year + " " + this._hours + ":" + this._minutes + ":" + this._seconds; // String
},

//FIXME: remove this and replace usage with dojox.date.buddhist.getDaysInMonth?
Expand Down Expand Up @@ -280,9 +278,9 @@ dojo.declare("dojox.date.buddhist.Date", null, {
}
});

dojox.date.buddhist.Date.prototype.valueOf = function(){
BDate.prototype.valueOf = function(){
return this.toGregorian().valueOf();
};

return dojox.date.buddhist.Date;
return BDate;
});
53 changes: 26 additions & 27 deletions date/buddhist/locale.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./Date", "dojo/i18n!dojo/cldr/nls/buddhist"],
function(dojo, dd, i18n, regexp, string, buddhistDate){
define(["../..", "dojo/_base/lang", "dojo/_base/array", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./Date", "dojo/i18n!dojo/cldr/nls/buddhist"],
function(dojox, lang, arr, dd, i18n, regexp, string, BDate){

dojo.getObject("date.buddhist.locale", true, dojox);
dojo.experimental("dojox.date.buddhist.locale");
var blocale = lang.getObject("date.buddhist.locale", true, dojox);

// Format a pattern without literals
function formatPattern(dateObject, bundle, locale, fullYear, pattern){
Expand Down Expand Up @@ -77,7 +76,7 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
break;
case 'z':
// We only have one timezone to offer; the one from the browser
s = dojo.date.getTimezoneName(dateObject.toGregorian());
s = dd.getTimezoneName(dateObject.toGregorian());
if(s){ break; }
l = 4;
// fallthrough... use GMT if tz not available
Expand All @@ -102,18 +101,18 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
});
}

dojox.date.buddhist.locale.format = function(/*buddhist.Date*/dateObject, /*object?*/options){
blocale.format = function(/*buddhist.Date*/dateObject, /*object?*/options){
// based on and similar to dojo.date.locale.format
// summary:
// Format a Date object as a String, using settings.
options = options || {};

var locale = i18n.normalizeLocale(options.locale);
var formatLength = options.formatLength || 'short';
var bundle = dojox.date.buddhist.locale._getBuddhistBundle(locale);
var bundle = blocale._getBuddhistBundle(locale);
var str = [];

var sauce = dojo.hitch(this, formatPattern, dateObject, bundle, locale, options.fullYear);
var sauce = lang.hitch(this, formatPattern, dateObject, bundle, locale, options.fullYear);
if(options.selector == "year"){
var year = dateObject.getFullYear();
return year;
Expand All @@ -131,19 +130,19 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
return result; // String
};

dojox.date.buddhist.locale.regexp = function(/*Object?*/options){
blocale.regexp = function(/*Object?*/options){
// based on and similar to dojo.date.locale.regexp
// summary:
// Builds the regular needed to parse a buddhist.Date
return dojox.date.buddhist.locale._parseInfo(options).regexp; // String
return blocale._parseInfo(options).regexp; // String
};

dojox.date.buddhist.locale._parseInfo = function(/*Object?*/options){
blocale._parseInfo = function(/*Object?*/options){
/* based on and similar to dojo.date.locale._parseInfo */

options = options || {};
var locale = i18n.normalizeLocale(options.locale);
var bundle = dojox.date.buddhist.locale._getBuddhistBundle(locale);
var bundle = blocale._getBuddhistBundle(locale);
var formatLength = options.formatLength || 'short';
var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
Expand All @@ -159,17 +158,17 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./

var tokens = [];

var re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
var re = _processPattern(pattern, lang.hitch(this, _buildDateTimeRE, tokens, bundle, options));
return {regexp: re, tokens: tokens, bundle: bundle};
};

dojox.date.buddhist.locale.parse = function(/*String*/value, /*Object?*/options){
blocale.parse = function(/*String*/value, /*Object?*/options){
// based on and similar to dojo.date.locale.parse
// summary: This function parses string date value according to options
value = value.replace(/[\u200E\u200F\u202A-\u202E]/g, ""); //remove special chars

if(!options){options={};}
var info = dojox.date.buddhist.locale._parseInfo(options);
var info = blocale._parseInfo(options);

var tokens = info.tokens, bundle = info.bundle;
var re = new RegExp("^" + info.regexp + "$");
Expand All @@ -186,7 +185,7 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
var amPm = "";
var mLength = 0;
var widthList = ["abbr", "wide", "narrow"];
var valid = dojo.every(match, function(v, i){
var valid = arr.every(match, function(v, i){
if(!i){return true;}
var token=tokens[i-1];
var l=token.length;
Expand All @@ -201,9 +200,9 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
//Tolerate abbreviating period in month part
//Case-insensitive comparison
v = v.replace(".","").toLowerCase();
months = dojo.map(months, function(s){ return s ? s.replace(".","").toLowerCase() : s; } );
months = arr.map(months, function(s){ return s ? s.replace(".","").toLowerCase() : s; } );
}
v = dojo.indexOf(months, v);
v = arr.indexOf(months, v);
if(v == -1){
return false;
}
Expand Down Expand Up @@ -263,7 +262,7 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
}else if(amPm === 'a' && hours == 12){
result[3] = 0; //12am -> 0
}
var dateObject = new buddhistDate(result[0], result[1], result[2], result[3], result[4], result[5], result[6]);
var dateObject = new BDate(result[0], result[1], result[2], result[3], result[4], result[5], result[6]);
return dateObject;
};

Expand All @@ -282,7 +281,7 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
var chunks = pattern.match(/(''|[^'])+/g);
var literal = pattern.charAt(0) == "'";

dojo.forEach(chunks, function(chunk, i){
arr.forEach(chunks, function(chunk, i){
if(!chunk){
chunks[i]='';
}else{
Expand Down Expand Up @@ -365,29 +364,29 @@ define(["dojo/main", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./
}

var _customFormats = [];
dojox.date.buddhist.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
blocale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
// summary:
// Add a reference to a bundle containing localized custom formats to be
// used by date/time formatting and parsing routines.
_customFormats.push({pkg:packageName,name:bundleName});
};

dojox.date.buddhist.locale._getBuddhistBundle = function(/*String*/locale){
blocale._getBuddhistBundle = function(/*String*/locale){
var buddhist = {};
dojo.forEach(_customFormats, function(desc){
arr.forEach(_customFormats, function(desc){
var bundle = i18n.getLocalization(desc.pkg, desc.name, locale);
buddhist = dojo.mixin(buddhist, bundle);
buddhist = lang.mixin(buddhist, bundle);
}, this);
return buddhist; /*Object*/
};

dojox.date.buddhist.locale.addCustomFormats("dojo.cldr","buddhist");
blocale.addCustomFormats("dojo.cldr","buddhist");

dojox.date.buddhist.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale, /*buddhist Date Object?*/date){
blocale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale, /*buddhist Date Object?*/date){
// summary:
// Used to get localized strings from dojo.cldr for day or month names.
var label;
var lookup = dojox.date.buddhist.locale._getBuddhistBundle(locale);
var lookup = blocale._getBuddhistBundle(locale);
var props = [item, context, type];
if(context == 'standAlone'){
var key = props.join('-');
Expand Down
Loading

0 comments on commit 68d143a

Please sign in to comment.