forked from cloudflarearchive/backgrid-moment-cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgrid-moment-cell.js
189 lines (142 loc) · 5.99 KB
/
backgrid-moment-cell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
backgrid-moment-cell
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT @license.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(["underscore", "backgrid", "moment"], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require("underscore"), require("backgrid"),
require("moment"));
} else {
// Browser globals
factory(root._, root.Backgrid, root.moment);
}
}(this, function (_, Backgrid, moment) {
/**
MomentFormatter converts bi-directionally any datetime values in any format
supported by [moment()](http://momentjs.com/docs/#/parsing/) to any
datetime format
[moment.fn.format()](http://momentjs.com/docs/#/displaying/format/)
supports.
@class Backgrid.Extension.MomentFormatter
@extends Backgrid.CellFormatter
@constructor
*/
var MomentFormatter = Backgrid.Extension.MomentFormatter = function (options) {
_.extend(this, this.defaults, options);
};
MomentFormatter.prototype = new Backgrid.CellFormatter;
_.extend(MomentFormatter.prototype, {
/**
@cfg {Object} options
@cfg {boolean} [options.modelInUnixOffset=false] Whether the model values
should be read/written as the number of milliseconds since UNIX Epoch.
@cfg {boolean} [options.modelInUnixTimestamp=false] Whether the model
values should be read/written as the number of seconds since UNIX Epoch.
@cfg {boolean} [options.modelInUTC=true] Whether the model values should
be read/written in UTC mode or local mode.
@cfg {string} [options.modelLang=moment.lang()] The locale the model
values should be read/written in.
@cfg {string} [options.modelFormat=moment.defaultFormat] The format this
moment formatter should use to read/write model values. Only meaningful if
the values are strings.
@cfg {boolean} [options.displayInUnixOffset=false] Whether the display
values should be read/written as the number of milliseconds since UNIX
Epoch.
@cfg {boolean} [options.displayInUnixTimestamp=false] Whether the display
values should be read/written as the number of seconds since UNIX Epoch.
@cfg {boolean} [options.displayInUTC=true] Whether the display values
should be read/written in UTC mode or local mode.
@cfg {string} [options.displayLang=moment.lang()] The locale the display
values should be read/written in.
@cfg {string} [options.displayFormat=moment.defaultFormat] The format
this moment formatter should use to read/write dislay values.
*/
defaults: {
modelInUnixOffset: false,
modelInUnixTimestamp: false,
modelInUTC: true,
modelLang: moment.lang(),
modelFormat: moment.defaultFormat,
displayInUnixOffset: false,
displayInUnixTimestamp: false,
displayInUTC: true,
displayLang: moment.lang(),
displayFormat: moment.defaultFormat
},
/**
Converts datetime values from the model for display.
@member Backgrid.Extension.MomentFormatter
@param {*} rawData
@return {string}
*/
fromRaw: function (rawData) {
if (rawData == null) return '';
var m = this.modelInUnixOffset ? moment(rawData) :
this.modelInUnixTimestamp ? moment.unix(rawData) :
this.modelInUTC ?
moment.utc(rawData, this.modelFormat, this.modelLang) :
moment(rawData, this.modelFormat, this.modelLang);
if (this.displayInUnixOffset) return +m;
if (this.displayInUnixTimestamp) return m.unix();
if (this.displayLang) m.lang(this.displayLang);
if (this.displayInUTC) m.utc(); else m.local();
return m.format(this.displayFormat);
},
/**
Converts datetime values from user input to model values.
@member Backgrid.Extension.MomentFormatter
@param {string} formattedData
@return {string}
*/
toRaw: function (formattedData) {
var m = this.displayInUnixOffset ? moment(+formattedData) :
this.displayInUnixTimestamp ? moment.unix(+formattedData) :
this.displayInUTC ?
moment.utc(formattedData, this.displayFormat, this.displayLang) :
moment(formattedData, this.displayFormat, this.displayLang);
if (!m || !m.isValid()) return;
if (this.modelInUnixOffset) return +m;
if (this.modelInUnixTimestamp) return m.unix();
if (this.modelLang) m.lang(this.modelLang);
if (this.modelInUTC) m.utc(); else m.local();
return m.format(this.modelFormat);
}
});
/**
Renders a datetime cell that uses a Backgrid.Extension.MomentFormatter to
convert and validate values.
@class Backgrid.Extension.MomentCell
@extends Backgrid.Cell
*/
var MomentCell = Backgrid.Extension.MomentCell = Backgrid.Cell.extend({
editor: Backgrid.InputCellEditor,
/** @property */
className: "moment-cell",
/** @property {Backgrid.CellFormatter} [formatter=Backgrid.Extension.MomentFormatter] */
formatter: MomentFormatter,
/**
Initializer. Accept Backgrid.Extension.MomentFormatter.options and
Backgrid.Cell.initialize required parameters.
*/
initialize: function (options) {
MomentCell.__super__.initialize.apply(this, arguments);
var formatterDefaults = MomentFormatter.prototype.defaults;
var formatterDefaultKeys = _.keys(formatterDefaults);
var instanceAttrs = _.pick(this, formatterDefaultKeys);
var formatterOptions = _.pick(options, formatterDefaultKeys);
_.extend(this.formatter, formatterDefaults, instanceAttrs, formatterOptions);
this.editor = this.editor.extend({
attributes: _.extend({}, this.editor.prototype.attributes || this.editor.attributes || {}, {
placeholder: this.formatter.displayFormat
})
});
}
});
_.extend(MomentCell.prototype, MomentFormatter.prototype.defaults);
}));