* Refactored rendering of date formats

* Increased jQuery version to 1.4
This commit is contained in:
Tobi Schäfer 2011-01-24 18:22:59 +00:00
parent 5f0f4bcf72
commit e35356ddfa
3 changed files with 59 additions and 43 deletions

View file

@ -738,25 +738,39 @@ function formatNumber(number, pattern) {
/** /**
* *
* @param {Date} date * @param {Date} date
* @param {pattern} pattern * @param {format} format
* @returns {String} The formatted date string * @returns {String} The formatted date string
*/ */
function formatDate(date, pattern) { function formatDate(date, format) {
if (!date) { if (!date) {
return null; return null;
} }
pattern || (pattern = "short"); var pattern, site = res.handlers.site;
var site = res.handlers.site; var locale = site.getLocale();
var format = site.metadata.get(pattern.toLowerCase() + "DateFormat"); var type = java.text.DateFormat.FULL;
if (!format) { switch (format) {
format = global[pattern.toUpperCase() + "DATEFORMAT"] || pattern; case null:
pattern = java.text.DateFormat.getDateTimeInstance(type, type, locale).toPattern();
break;
case "date":
pattern = java.text.DateFormat.getDateInstance(type, locale).toPattern();
break;
case "time":
pattern = java.text.DateFormat.getTimeInstance(type, locale).toPattern();
break;
case "short":
case "long":
pattern = site[format.toLowerCase() + "DateFormat"];
break;
default:
pattern = format;
} }
try { try {
return date.format(format, site.getLocale(), site.getTimeZone()); return date.format(pattern, site.getLocale(), site.getTimeZone());
} catch (ex) { } catch (ex) {
return "[Macro error: Invalid date format]"; return "[Invalid date format]";
} }
return; return String.EMPTY;
} }
/** /**
@ -823,11 +837,11 @@ function getLocales(language) {
locale = locales[i]; locale = locales[i];
localeString = locale.toString(); localeString = locale.toString();
if (!localeString.contains("_")) { if (!localeString.contains("_")) {
result.push({ result.push({
value: localeString, value: localeString,
display: locale.getDisplayName(locale), display: locale.getDisplayName(locale),
"class": jala.i18n.getCatalog(jala.i18n.getLocale(localeString)) ? "translated" : "" "class": jala.i18n.getCatalog(jala.i18n.getLocale(localeString)) ? "translated" : ""
}); });
} }
} }
result.sort(new String.Sorter("display")); result.sort(new String.Sorter("display"));
@ -881,29 +895,31 @@ function getTimeZones(language) {
/** /**
* *
* @param {String} type * @param {String} type
* @param {String} language * @param {java.util.Locale} locale
* @returns {Array[]} An array containing the corresponding date formats * @returns {Array[]} An array containing the corresponding date formats
*/ */
function getDateFormats(type, language) { function getDateFormats(type, locale, timeZone) {
var patterns; type || (type = "short");
if (type === "short") { locale || (locale = java.util.Locale.getDefault());
patterns = [SHORTDATEFORMAT, "yyyy/MM/dd HH:mm", timeZone || (timeZone = java.util.TimeZone.getDefault());
"yyyy.MM.dd, HH:mm", "d. MMMM, HH:mm", "MMMM d, HH:mm", var types = [type];
"d. MMM, HH:mm", "MMM d, HH:mm", "EEE, d. MMM, HH:mm", types.push(type === "long" ? "full" : "medium");
"EEE MMM d, HH:mm", "EEE, HH:mm", "EE, HH:mm", "HH:mm"]; var now = new Date, result = [], patterns = {};
} else if (type === "long") { for each (let dateType in types) {
patterns = [LONGDATEFORMAT, "EEEE, MMMM dd, yyyy, HH:mm", let dateFormat = java.text.DateFormat[dateType.toUpperCase()];
"EE, d. MMM. yyyy, HH:mm", "EE MMM dd, yyyy, HH:mm", for each (let timeType in ["short", "medium", "long", "full"]) {
"EE yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm", "d. MMMM yyyy, HH:mm", let timeFormat = java.text.DateFormat[timeType.toUpperCase()];
"MMMM d, yyyy, HH:mm", "d. MMM yyyy, HH:mm", "MMM d, yyyy, HH:mm"]; let sdf = java.text.DateFormat.getDateTimeInstance(dateFormat, timeFormat, locale);
} let pattern = sdf.toPattern();
var result = [], sdf; if (patterns[pattern]) {
var locale = getLocale(language); continue;
var now = new Date; }
for each (var pattern in patterns) { patterns[pattern] = true;
sdf = new java.text.SimpleDateFormat(pattern, locale); sdf.setTimeZone(timeZone);
result.push([encodeForm(pattern), sdf.format(now)]); result.push([encodeForm(pattern), sdf.format(now)]);
}
} }
result.push([encodeForm(Date.ISOFORMAT), now.format(Date.ISOFORMAT, locale, timeZone)]);
return result; return result;
} }

View file

@ -213,8 +213,8 @@ Site.prototype.constructor = function(name, title) {
pageSize: 3, pageSize: 3,
locale: root.getLocale().toString(), locale: root.getLocale().toString(),
timeZone: root.getTimeZone().getID(), timeZone: root.getTimeZone().getID(),
longDateFormat: LONGDATEFORMAT, longDateFormat: root.longDateFormat,
shortDateFormat: SHORTDATEFORMAT shortDateFormat: root.shortDateFormat
}); });
return this; return this;
@ -318,7 +318,7 @@ Site.prototype.getFormOptions = function(name) {
case "layout": case "layout":
return this.getLayouts(); return this.getLayouts();
case "longDateFormat": case "longDateFormat":
return getDateFormats("long", this.getLocale()); return getDateFormats("long", this.getLocale(), this.getTimeZone());
case "mode": case "mode":
return Site.getModes(); return Site.getModes();
case "notificationMode": case "notificationMode":
@ -328,7 +328,7 @@ Site.prototype.getFormOptions = function(name) {
case "status": case "status":
return Site.getStatus(); return Site.getStatus();
case "shortDateFormat": case "shortDateFormat":
return getDateFormats("short", this.getLocale()); return getDateFormats("short", this.getLocale(), this.getTimeZone());
case "timeZone": case "timeZone":
return getTimeZones(this.getLocale()); return getTimeZones(this.getLocale());
case "callbackMode": case "callbackMode":
@ -406,7 +406,7 @@ Site.prototype.main_js_action = function() {
res.dependsOn(Root.VERSION); res.dependsOn(Root.VERSION);
res.digest(); res.digest();
this.renderSkin("$Site#include", this.renderSkin("$Site#include",
{href:"http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"}); {href:"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"});
this.renderSkin("$Site#include", {href: root.getStaticUrl("antville-1.2.js")}); this.renderSkin("$Site#include", {href: root.getStaticUrl("antville-1.2.js")});
this.renderSkin("$Site#include", {href: this.href("user.js")}); this.renderSkin("$Site#include", {href: this.href("user.js")});
return; return;

View file

@ -49,7 +49,7 @@
</form> </form>
<% #date %> <% #date %>
<div class="dayHeader"><% story.created "EEEE, d. MMMM yyyy" %></div> <div class="dayHeader"><% story.created date %></div>
<% #content %> <% #content %>
<div class="storyTitle"> <div class="storyTitle">
@ -57,7 +57,7 @@
</div> </div>
<div class="storyDate"> <div class="storyDate">
<% story.creator link suffix=, %> <% story.creator link suffix=, %>
<% story.created HH:mm suffix="h" %> <% story.created time %>
</div> </div>
<div><% story.text | story.format %></div> <div><% story.text | story.format %></div>