0
|
1 define([
|
|
2 "./safe",
|
|
3 "./format-compile",
|
|
4 "dojo/number",
|
|
5 "dojo/date/locale",
|
|
6 "dojo/_base/array" ], function(safe, compile, number, date, array) {
|
|
7
|
|
8 // {short,medium,full,long}-{date,time}
|
|
9 var convert = function(value, pattern) {
|
|
10 if (!pattern)
|
|
11 return value.toString();
|
|
12
|
|
13 if (pattern.toLocaleLowerCase() == "json") {
|
|
14 var cache = [];
|
|
15 return JSON.stringify(value, function(k, v) {
|
|
16 if (!safe.isPrimitive(v)) {
|
|
17 var id = array.indexOf(cache, v);
|
|
18 if (id >= 0)
|
|
19 return "@ref-" + id;
|
|
20 else
|
|
21 return v;
|
|
22 } else {
|
|
23 return v;
|
|
24 }
|
|
25 });
|
|
26 }
|
|
27
|
|
28 if (safe.isNumber(value)) {
|
|
29 var nopt = {};
|
|
30 if (pattern.indexOf("!") === 0) {
|
|
31 nopt.round = -1;
|
|
32 pattern = pattern.substr(1);
|
|
33 }
|
|
34 nopt.pattern = pattern;
|
|
35 return number.format(value, nopt);
|
|
36 } else if (value instanceof Date) {
|
|
37 var m = pattern.match(/^(\w+)-(\w+)$/);
|
|
38 if (m)
|
|
39 return date.format(value, {
|
|
40 selector : m[2],
|
|
41 formatLength : m[1]
|
|
42 });
|
|
43 else
|
|
44 return date.format(value, {
|
|
45 selector : "date",
|
|
46 datePattern : pattern
|
|
47 });
|
|
48 } else {
|
|
49 return value.toString(pattern);
|
|
50 }
|
|
51 };
|
|
52
|
|
53 function formatter(format) {
|
|
54 var data;
|
|
55
|
|
56 if (arguments.length <= 1)
|
|
57 return format;
|
|
58
|
|
59 data = Array.prototype.slice.call(arguments, 1);
|
|
60
|
|
61 var template = compile(format);
|
|
62
|
|
63 return template(function(name, pattern) {
|
|
64 var value = data[name];
|
|
65 return !safe.isNull(value) ? convert(value, pattern) : "";
|
|
66 });
|
|
67 }
|
|
68
|
|
69 formatter.compile = function(format) {
|
|
70 var template = compile(format);
|
|
71
|
|
72 return function() {
|
|
73 var data = arguments;
|
|
74
|
|
75 return template(function(name, pattern) {
|
|
76 var value = data[name];
|
|
77 return !safe.isNull(value) ? convert(value, pattern) : "";
|
|
78 });
|
|
79 };
|
|
80 };
|
|
81
|
|
82 formatter.convert = convert;
|
|
83
|
|
84 return formatter;
|
|
85 }); |