-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
executable file
·295 lines (269 loc) · 8.64 KB
/
util.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Adapted from https://github.com/GoogleChrome/chrome-app-samples/blob/master/samples/gdrive/js/util.js
Adapted by: Charlie Gorichanaz ([email protected])
Copyright 2012 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Eric Bidelman ([email protected])
*/
var Util = Util || {};
// Combines two JSON objects in one.
Util.merge = function(obj1, obj2) {
var obj = {};
for (var x in obj1) {
if (obj1.hasOwnProperty(x)) {
obj[x] = obj1[x];
}
}
for (var x in obj2) {
if (obj2.hasOwnProperty(x)) {
obj[x] = obj2[x];
}
}
return obj;
};
/**
* Turns a NodeList into an array.
*
* @param {NodeList} list The array-like object.
* @return {Array} The NodeList as an array.
*/
Util.toArray = function(list) {
return Array.prototype.slice.call(list || [], 0);
};
/**
* Urlencodes a JSON object of key/value query parameters.
* @param {Object} parameters Key value pairs representing URL parameters.
* @return {string} query parameters concatenated together.
*/
Util.stringify = function(parameters) {
var params = [];
for(var p in parameters) {
params.push(encodeURIComponent(p) + '=' +
encodeURIComponent(parameters[p]));
}
return params.join('&');
};
/**
* Creates a JSON object of key/value pairs
* @param {string} paramStr A string of Url query parmeters.
* For example: max-results=5&startindex=2&showfolders=true
* @return {Object} The query parameters as key/value pairs.
*/
Util.unstringify = function(paramStr) {
var parts = paramStr.split('&');
var params = {};
for (var i = 0, pair; pair = parts[i]; ++i) {
var param = pair.split('=');
params[decodeURIComponent(param[0])] = decodeURIComponent(param[1]);
}
return params;
};
/**
* Utility for formatting a date string.
* @param {string} msg The date in UTC format. Example: 2010-04-01T08:00:00Z.
* @return {string} The date formated as mm/dd/yy. Example: 04/01/10.
*/
Util.formatDate = function(dateStr) {
// TODO: allow for other date formats
var date = new Date(dateStr.split('T')[0]);
return [
date.getFullYear().toString(),
("0" + ( date.getMonth() + 1 ) ).slice(-2),
("0" + date.getDate() ).slice(-2),
].join('-');
};
/**
* Utility for formatting a Date object as a string in ISO 8601 format using UTC.
* @param {Date} d The date to format.
* @return {string} The formated date string in ISO 8601 format.
*/
Util.ISODateString = function(d) {
var pad = function(n) {
return n < 10 ? '0' + n : n;
};
return d.getUTCFullYear() + '-'
+ pad(d.getUTCMonth() + 1) + '-'
+ pad(d.getUTCDate()) + 'T'
+ pad(d.getUTCHours()) + ':'
+ pad(d.getUTCMinutes()) + ':'
+ pad(d.getUTCSeconds());// + 'Z'
};
/**
* Formats a string with the given parameters. The string to format must have
* placeholders that correspond to the index of the arguments passed and surrounded
* by curly braces (e.g. 'Some {0} string {1}').
*
* @param {string} var_args The string to be formatted should be the first
* argument followed by the variables to inject into the string
* @return {string} The string with the specified parameters injected
*/
Util.format = function(var_args) {
var args = Array.prototype.slice.call(arguments, 1);
return var_args.replace(/\{(\d+)\}/g, function(m, i) {
return args[i];
});
};
/**
* Generic sort function to pass to sort(), allowing for dynamically choosing
* the key to sort on at invocation.
* @param {string} prop The name of the property to sort on
* @return {int} Result for use in sort()'s comparison callback
*/
Util.sortBy = function(prop) {
return function(a, b) {
if (a[prop] < b[prop]) {
return 1;
}
if (a[prop] > b[prop]) {
return -1;
}
return 0;
}
}
/**
* Creates a base-64 encoded ASCII string from a "string" of binary data.
* @author https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa
* @param {string} str Data to encode
* @return {string} Base-64 encoded version of input
*/
Util.utf8_to_b64 = function(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
/**
* Decodes a base-64 encoded ASCII string to a "string" of binary data.
* @author https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa
* @param {string} str Base-64 encoded string
* @return {string} UTF8 encoded string
*/
Util.b64_to_utf8 = function(str) {
return decodeURIComponent(escape(window.atob(str)));
}
/**
* Escapes all potentially dangerous characters, so the resulting string can be
* safely inserted into attribute or element text.
* @author http://stackoverflow.com/a/7124052/172602
* @param {string} str String to escape
* @return {string} HTML escaped string
*/
Util.htmlEscape = function(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
/**
* Reverses escaping of potentially dangerous characters.
* @author http://stackoverflow.com/a/7124052/172602
* @param {string} str String to escape
* @return {string} HTML escaped string
*/
Util.htmlUnescape = function(value){
return String(value)
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&');
}
/**
* The next two functions were totally separate, but I wanted to combine them
* since they used the same parameters… but it ended up less graceful than I
* hoped due to mw and mh having meaning when explicitly blank
*/
/**
* Produce URL parameters for retrieving appropriately sized images.
*
* Note Google apparently doesn't support dimension requests larger than 2560
* pixels and has a max height of 1060 when width not specified
*
* @param {bool} c Whether to crop the image or not
* @param {float} hdpi Scaling factor for high density screens. Anything above
* 1 will result in an image URL that generates an image larger than the
* dimensions given. This is so the dimensions given can be used in the image
* tag, but the source image will be larger for better sharpness.
* @param {int} mw Max width desired
* @param {int} mh Max height desired
* @param {int} ow Original width
* @param {int} oh Original height
* @return {str} returns object with the code needed to generate image
* URL and the dimesions for building embed HTML
*/
Util.calcProps = function(c, hdpi, mw, mh, ow, oh) {
var dims = Util.calcDimensions(mw, mh, ow, oh);
var h = Math.round( hdpi * dims.h );
var w = Math.round( hdpi * dims.w );
var code = '/';
if(!mw && !mh) {
code += 'd';
} else {
if(!mw) {
code += 'h' + h;
} else if(!mh) {
code += 'w' + w;
} else {
if(w == h) {
code += 's' + w;
} else {
code = code + 'w' + w + '-h' + h;
}
if(c) code += '-c';
}
}
return { code: code, w: dims.w, h: dims.h };
/**
* old code that didn't allow crop on mismatched dimensions:
*
* if(c) return '/s' + ( s ? s : Math.min(ow, oh, 2560) ) + '-c';
* else return '/' + ( (mw || mh) ? '' : 'd' ) + (mw && 'w' + Math.min(mw, 2560)) + ((mw && mh) && '-') + ( mw ? (mh && 'h' + Math.min(mh, 2560)) : (mh && 'h' + Math.min(mh, 1060)) );
*/
}
/**
* Calculate dimensions based on settings and Google constraints
*
* Note Google apparently doesn't support dimension requests larger than 2560
* pixels and has a max height of 1060 when width not specified
*
* @param {int} mw Max width desired
* @param {int} mh Max height desired
* @param {int} ow Original width
* @param {int} oh Original height
* @return {str} returns object with the dimensions
*/
Util.calcDimensions = function(mw, mh, ow, oh) {
var w, h;
if(!mw && !mh) {
w = ow; h = oh;
} else {
if(!mw) {
h = Math.min(oh, mh, 1060);
w = ow / oh * h;
} else if(!mh) {
w = Math.min(ow, mw, 2560);
h = oh / ow * w;
} else {
var r = 1; // ratio of dimensions (max/orig)
if(ow > mw || oh > mh) {
var rw = mw / ow;
var rh = mh / oh;
if(rw > rh) r = rh;
else r = rw;
}
w = ow * r;
h = oh * r;
}
}
return {
w: Math.round(w),
h: Math.round(h)
};
}