-
Notifications
You must be signed in to change notification settings - Fork 1
/
param.js
executable file
·274 lines (247 loc) · 8.82 KB
/
param.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Timemap.js Copyright 2010 Nick Rabinowitz.
* Licensed under the MIT License (see LICENSE.txt)
*/
/**
* @fileOverview
* This file defines the Param class, which is used to get, set, and serialize
* different fields on TimeMap and TimeMapItem objects.
*
* @author Nick Rabinowitz (www.nickrabinowitz.com)
*/
// save a few bytes
(function() {
/**
* @name TimeMap.params
* @namespace Namespace for parameter classes
*/
var params = TimeMap.params = {
/**
* @class
* A parameter, with methods to get, set, and serialize the current value.
*
* @constructor
* @param {String} paramName String name of the parameter
* @param {Object} options Container for named arguments
* @param {String} [sourceName] String name of the source element, if different
* @param {Function} [options.get] Function to get the current param value
* @param {Function} [options.set] Function to set the param to a new value
* @param {Function} [options.setConfig] Function to set a new value in a config object
* @param {Function} [options.fromStr] Function to parse the value from a string
* @param {Function} [options.toStr] Function to serialize the current value to a string
* @param {Function} [options.setConfigXML] Function to parse the value from an XML node and set to config
*/
Param: function(paramName, options) {
var param = this,
options = options || {};
/**
* String name of this param
* @name TimeMap.params.Param#paramName
* @type String
*/
param.paramName = paramName;
/**
* String name of the source element, if different
* @name TimeMap.params.Param#sourceName
*/
param.sourceName = options.sourceName || paramName;
/**
* Get the current state value from a TimeMap or TimeMapItem object
* @name TimeMap.params.Param#get
* @function
*
* @param {TimeMap|TimeMapItem} o Object to inspect
* @return {mixed} Current state value
*/
param.get = options.get;
/**
* Set the current state value on a TimeMap or TimeMapItem object
* @name TimeMap.params.Param#set
* @function
*
* @param {TimeMap|TimeMapItem} o Object to modify
* @param {mixed} value Value to set
*/
param.set = options.set;
/**
* Set a new value on a config object for TimeMap.init()
* @name TimeMap.params.Param#setConfig
* @function
* @see TimeMap.init
*
* @param {Object} config Config object to modify
* @param {mixed} value Value to set
*/
param.setConfig = options.setConfig || function(config, value) {
// default: set at top level
config[paramName] = value;
};
/**
* Parse a state value from a string
* @name TimeMap.params.Param#fromString
* @function
*
* @param {String} s String to parse
* @return {mixed} Current state value
*/
param.fromString = options.fromStr || function(s) {
// default: param is a string
return s;
};
/**
* Serialize a state value as a string
* @name TimeMap.params.Param#toString
* @function
*
* @param {mixed} value Value to serialize
* @return {String} Serialized string
*/
param.toString = options.toStr || function(value) {
// default: use the built-in string method
return value.toString();
};
/**
* Get the current value as a string
* @name TimeMap.params.Param#getString
* @function
*
* @param {TimeMap|TimeMapItem} o Object to inspect
*/
param.getString = function(o) {
param.toString(param.get(o));
};
/**
* Set the current state value from a string
* @name TimeMap.params.Param#setString
* @function
*
* @param {TimeMap|TimeMapItem} o Object to modify
* @param {String} s String version of value to set
*/
param.setString = function(o, s) {
param.set(o, param.fromString(s));
};
/**
* Set a config object based on an XML tag
* @name TimeMap.params.Param#setConfigXML
* @function
*
* @param {Object} config Config object to modify
* @param {XML NodeList} node Parent node of the desired tag
*/
param.setConfigXML = options.setConfigXML || function(config, node) {
var tagName = param.sourceName,
nameParts = tagName.split(':'),
ns;
// deal with namespaced tags
if (nameParts.length > 1) {
tagName = nameParts[1];
ns = nameParts[0];
}
// set to config
param.setConfig(config, TimeMap.util.getTagValue(node, tagName, ns));
};
},
/**
* @class
* A convenience class for those parameters which deal with a value
* in the options of a TimeMap or TimeMapItem object, setting some
* additional default functions.
*
* @augments TimeMap.params.Param
*
* @constructor
* @param {String} paramName String name of the option parameter
* @param {Object} [options] Container for named arguments (see {@link TimeMap.params.Param})
*/
OptionParam: function(paramName, options) {
options = options || {};
var defaults = {
/**
* Get the current state value from the opts object of a TimeMap or TimeMapItem
* @name TimeMap.params.OptionParam#get
* @function
*
* @param {TimeMap|TimeMapItem} o Object to inspect
* @return {mixed} Current state value
*/
get: function(o) {
return o.opts[paramName];
},
/**
* Set the state value in the opts object of a TimeMap or TimeMapItem
* @name TimeMap.params.OptionParam#set
*
* @param {TimeMap|TimeMapItem} o Object to modify
* @param {mixed} value Value to set
*/
set: function(o, value) {
o.opts[paramName] = value;
},
/**
* Set a new value on a config object for TimeMap.init() or a particular item
* @name TimeMap.params.OptionParam#setConfig
* @function
*
* @param {Object} config Config object to modify
* @param {mixed} value Value to set
*/
setConfig: function(config, value) {
config.options = config.options || {};
config.options[paramName] = value;
}
};
options = TimeMap.util.merge(options, defaults);
return new params.Param(paramName, options);
}
};
/*----------------------------------------------------------------------------
* TimeMapItem params
*---------------------------------------------------------------------------*/
/**
* @namespace Namespace for parameters used for loading data into a TimeMapItem
* object. Because these are intended for loading, only setConfig is defined.
*/
TimeMap.loaders.base.prototype.params = {
/**
* Item title
* @type TimeMap.params.Param
*/
title: new params.Param("title"),
/**
* Item start date
* @type TimeMap.params.Param
*/
start: new params.Param("start"),
/**
* Item end date
* @type TimeMap.params.Param
*/
end: new params.Param("end"),
/**
* Item description
* @type TimeMap.params.OptionParam
*/
description: new params.OptionParam("description"),
/**
* Item latitude
* @type TimeMap.params.Param
*/
lat: new params.Param("lat", {
setConfig: function(config, value) {
config.point = config.point || {};
config.point.lat = value;
}
}),
/**
* Item longitude
* @type TimeMap.params.Param
*/
lon: new params.Param("lon", {
setConfig: function(config, value) {
config.point = config.point || {};
config.point.lon = value;
}
})
};
})();