Skip to content

Commit

Permalink
375: Version C where there is flexibility to add more formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
Duc Tri Le committed Jan 3, 2012
1 parent 46b8662 commit 60197fe
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/dateinput/dateinput.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@


$.tools = $.tools || {version: '@VERSION'}; $.tools = $.tools || {version: '@VERSION'};


var instances = [], var instances = [],
tool, formatters = {},
tool,


// h=72, j=74, k=75, l=76, down=40, left=37, up=38, right=39 // h=72, j=74, k=75, l=76, down=40, left=37, up=38, right=39
KEYS = [75, 76, 38, 39, 74, 72, 40, 37], KEYS = [75, 76, 38, 39, 74, 72, 40, 37],
Expand All @@ -28,6 +29,7 @@


conf: { conf: {
format: 'mm/dd/yy', format: 'mm/dd/yy',
formatter: 'default',
selectors: false, selectors: false,
yearRange: [-5, 5], yearRange: [-5, 5],
lang: 'en', lang: 'en',
Expand Down Expand Up @@ -70,6 +72,10 @@
} }
}, },


addFormatter: function(name, fn) {
formatters[name] = fn;
},

localize: function(language, labels) { localize: function(language, labels) {
$.each(labels, function(key, val) { $.each(labels, function(key, val) {
labels[key] = val.split(","); labels[key] = val.split(",");
Expand Down Expand Up @@ -103,10 +109,9 @@
} }


// thanks: http://stevenlevithan.com/assets/misc/date.format.js // thanks: http://stevenlevithan.com/assets/misc/date.format.js
var Re = /d{1,4}|m{1,4}|yy(?:yy)?|"[^"]*"|'[^']*'/g, tmpTag = $("<a/>"); var tmpTag = $("<a/>");


function format(date, fmt, lang) { function format(formatter, date, text, lang) {

var d = date.getDate(), var d = date.getDate(),
D = date.getDay(), D = date.getDay(),
m = date.getMonth(), m = date.getMonth(),
Expand All @@ -124,16 +129,26 @@
yy: String(y).slice(2), yy: String(y).slice(2),
yyyy: y yyyy: y
}; };


var ret = fmt.replace(Re, function ($0) { var ret = formatters[formatter](text, date, flags, lang);
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});


// a small trick to handle special characters // a small trick to handle special characters
return tmpTag.html(ret).html(); return tmpTag.html(ret).html();


} }


tool.addFormatter('default', function(text, date, flags, lang) {
return text.replace(/d{1,4}|m{1,4}|yy(?:yy)?|"[^"]*"|'[^']*'/g, function ($0) {
return $0 in flags ? flags[$0] : $0;
});
});

tool.addFormatter('prefixed', function(text, date, flags, lang) {
return text.replace(/%(d{1,4}|m{1,4}|yy(?:yy)?|"[^"]*"|'[^']*')/g, function ($0, $1) {
return $1 in flags ? flags[$1] : $0;
});
});

function integer(val) { function integer(val) {
return parseInt(val, 10); return parseInt(val, 10);
} }
Expand Down Expand Up @@ -296,7 +311,7 @@
if (e.isDefaultPrevented()) { return; } if (e.isDefaultPrevented()) { return; }


// formatting // formatting
input.val(format(date, conf.format, conf.lang)); input.val(format(conf.formatter, date, conf.format, conf.lang));


// change // change
e.type = "change"; e.type = "change";
Expand Down Expand Up @@ -699,7 +714,7 @@
}, },


getValue: function(dateFormat) { getValue: function(dateFormat) {
return dateFormat ? format(value, dateFormat, conf.lang) : value; return dateFormat ? format(conf.formatter, value, dateFormat, conf.lang) : value;
}, },


isOpen: function() { isOpen: function() {
Expand Down

0 comments on commit 60197fe

Please sign in to comment.