-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathsettings.js
More file actions
408 lines (385 loc) · 10.6 KB
/
Copy pathsettings.js
File metadata and controls
408 lines (385 loc) · 10.6 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import $ from 'jquery'
import { version } from '../package.json'
import { sendFileAPI } from './utils/abilities'
import { warnOnce } from './utils/warnings'
import { unique, once, upperCase, normalizeUrl } from './utils'
import { isWindowDefined } from './utils/is-window-defined'
import { MAX_SQUARE_SIDE } from './utils/canvas-size'
var indexOf = [].indexOf
// settings
var arrayOptions,
constrainOptions,
constraints,
defaultPreviewUrlCallback,
defaults,
flagOptions,
intOptions,
integration,
integrationToUserAgent,
normalize,
parseCrop,
parseShrink,
presets,
script,
str2arr,
transformOptions,
transforms,
urlOptions
defaults = {
// developer hooks
live: true,
manualStart: false,
locale: null,
localePluralize: null,
localeTranslations: null,
// widget & dialog settings
systemDialog: false,
crop: false,
previewStep: false,
imagesOnly: false,
clearable: false,
multiple: false,
multipleMax: 1000,
multipleMin: 1,
multipleMaxStrict: false,
imageShrink: false,
pathValue: true,
tabs: 'file camera url facebook gdrive gphotos dropbox instagram evernote flickr onedrive',
preferredTypes: '',
inputAcceptTypes: '', // '' means default, null means "disable accept"
// upload settings
doNotStore: false,
publicKey: null,
secureSignature: '',
secureExpire: '',
pusherKey: '79ae88bd931ea68464d9',
cdnBase: 'https://ucarecdn.com',
urlBase: 'https://upload.uploadcare.com',
socialBase: 'https://social.uploadcare.com',
previewProxy: null,
previewUrlCallback: null,
remoteTabSessionKey: null,
// fine tuning
imagePreviewMaxSize: 25 * 1024 * 1024,
multipartMinSize: 10 * 1024 * 1024,
multipartPartSize: 5 * 1024 * 1024,
multipartMinLastPartSize: 1024 * 1024,
multipartConcurrency: 4,
multipartMaxAttempts: 3,
parallelDirectUploads: 10,
passWindowOpen: false,
// camera
cameraMirrorDefault: true,
// camera recording
enableAudioRecording: true,
enableVideoRecording: true,
videoPreferredMimeTypes: null,
audioBitsPerSecond: null,
videoBitsPerSecond: null,
// maintain settings
scriptBase: `//ucarecdn.com/widget/${version}/uploadcare/`,
debugUploads: false,
integration: ''
}
transforms = {
multipleMax: {
from: 0,
to: 1000
}
}
constraints = {
multipleMax: {
min: 1,
max: 1000
}
}
presets = {
tabs: {
all: 'file camera url facebook gdrive gphotos dropbox instagram evernote flickr onedrive box vk huddle unsplash',
default: defaults.tabs
}
}
// integration setting from data attributes of script tag
script =
isWindowDefined() &&
(document.currentScript ||
(function () {
var scripts
scripts = document.getElementsByTagName('script')
return scripts[scripts.length - 1]
})())
integration = isWindowDefined() && $(script).data('integration')
if (integration && integration != null) {
defaults = $.extend(defaults, { integration })
}
str2arr = function (value) {
if (!$.isArray(value)) {
value = $.trim(value)
value = value ? value.split(' ') : []
}
return value
}
arrayOptions = function (settings, keys) {
var hasOwnProperty = Object.prototype.hasOwnProperty
var i, item, j, key, len, len1, source, value
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i]
value = source = str2arr(settings[key])
if (hasOwnProperty.apply(presets, [key])) {
value = []
for (j = 0, len1 = source.length; j < len1; j++) {
item = source[j]
if (hasOwnProperty.apply(presets[key], [item])) {
value = value.concat(str2arr(presets[key][item]))
} else {
value.push(item)
}
}
}
settings[key] = unique(value)
}
return settings
}
urlOptions = function (settings, keys) {
var i, key, len
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i]
if (settings[key] != null) {
settings[key] = normalizeUrl(settings[key])
}
}
return settings
}
flagOptions = function (settings, keys) {
var i, key, len, value
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i]
if (!(settings[key] != null)) {
continue
}
value = settings[key]
if (typeof value === 'string') {
// "", "..." -> true
// "false", "disabled" -> false
value = $.trim(value).toLowerCase()
settings[key] = !(value === 'false' || value === 'disabled')
} else {
settings[key] = !!value
}
}
return settings
}
intOptions = function (settings, keys) {
var i, key, len
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i]
if (settings[key] != null) {
settings[key] = parseInt(settings[key])
}
}
return settings
}
integrationToUserAgent = function (settings) {
settings._userAgent = `UploadcareWidget/${version}/${
settings.publicKey
} (JavaScript${settings.integration ? `; ${settings.integration}` : ''})`
return settings
}
transformOptions = function (settings, transforms) {
var key, transform
for (key in transforms) {
transform = transforms[key]
if (settings[key] != null) {
if (settings[key] === transform.from) {
settings[key] = transform.to
}
}
}
return settings
}
constrainOptions = function (settings, constraints) {
var key, max, min
for (key in constraints) {
;({ min, max } = constraints[key])
if (settings[key] != null) {
settings[key] = Math.min(Math.max(settings[key], min), max)
}
}
return settings
}
parseCrop = function (val) {
var ratio, reRatio
reRatio = /^([0-9]+)([x:])([0-9]+)\s*(|upscale|minimum)$/i
ratio = reRatio.exec($.trim(val.toLowerCase())) || []
return {
downscale: ratio[2] === 'x',
upscale: !!ratio[4],
notLess: ratio[4] === 'minimum',
preferedSize: ratio.length ? [+ratio[1], +ratio[3]] : undefined
}
}
parseShrink = function (val) {
const reShrink = /^([0-9]+)x([0-9]+)(?:\s+(\d{1,2}|100)%)?$/i
const shrink = reShrink.exec($.trim(val.toLowerCase())) || []
if (!shrink.length) {
return false
}
const size = shrink[1] * shrink[2]
const maxSize = MAX_SQUARE_SIDE * MAX_SQUARE_SIDE
if (size > maxSize) {
warnOnce(
`Shrinked size can not be larger than ${Math.floor(
maxSize / 1000 / 1000
)}MP. ` +
`You have set ${shrink[1]}x${shrink[2]} (` +
`${Math.ceil(size / 1000 / 100) / 10}MP).`
)
return false
}
return {
quality: shrink[3] ? shrink[3] / 100 : undefined,
size: size
}
}
defaultPreviewUrlCallback = function (url, info) {
var addAmpersand, addName, addQuery, queryPart
if (!this.previewProxy) {
return url
}
addQuery = !/\?/.test(this.previewProxy)
addName = addQuery || !/=$/.test(this.previewProxy)
addAmpersand = !addQuery && !/[&?=]$/.test(this.previewProxy)
queryPart = encodeURIComponent(url)
if (addName) {
queryPart = 'url=' + queryPart
}
if (addAmpersand) {
queryPart = '&' + queryPart
}
if (addQuery) {
queryPart = '?' + queryPart
}
return this.previewProxy + queryPart
}
normalize = function (settings) {
var skydriveIndex
arrayOptions(settings, ['tabs', 'preferredTypes', 'videoPreferredMimeTypes'])
urlOptions(settings, ['cdnBase', 'socialBase', 'urlBase', 'scriptBase'])
flagOptions(settings, [
'doNotStore',
'imagesOnly',
'multiple',
'clearable',
'pathValue',
'previewStep',
'systemDialog',
'debugUploads',
'multipleMaxStrict',
'cameraMirrorDefault'
])
intOptions(settings, [
'multipleMax',
'multipleMin',
'multipartMinSize',
'multipartPartSize',
'multipartMinLastPartSize',
'multipartConcurrency',
'multipartMaxAttempts',
'parallelDirectUploads'
])
transformOptions(settings, transforms)
constrainOptions(settings, constraints)
integrationToUserAgent(settings)
if (settings.crop !== false && !$.isArray(settings.crop)) {
if (/^(disabled?|false|null)$/i.test(settings.crop)) {
settings.crop = false
} else if ($.isPlainObject(settings.crop)) {
// old format
settings.crop = [settings.crop]
} else {
settings.crop = $.map(('' + settings.crop).split(','), parseCrop)
}
}
if (settings.imageShrink && !$.isPlainObject(settings.imageShrink)) {
settings.imageShrink = parseShrink(settings.imageShrink)
}
if (settings.crop || settings.multiple) {
settings.previewStep = true
}
if (!sendFileAPI) {
settings.systemDialog = false
}
if (settings.validators) {
settings.validators = settings.validators.slice()
}
if (settings.previewProxy && !settings.previewUrlCallback) {
settings.previewUrlCallback = defaultPreviewUrlCallback
}
skydriveIndex = settings.tabs.indexOf('skydrive')
if (skydriveIndex !== -1) {
settings.tabs[skydriveIndex] = 'onedrive'
}
return settings
}
// global variables only
const globals = function () {
var key, scriptSettings, value
scriptSettings = {}
for (key in defaults) {
value = window[`UPLOADCARE_${upperCase(key)}`]
if (value != null) {
scriptSettings[key] = value
}
}
return scriptSettings
}
// Defaults + global variables + global overrides (once from uploadcare.start)
// Not publicly-accessible
const common = once(function (settings, ignoreGlobals) {
var result
if (!ignoreGlobals) {
defaults = $.extend(defaults, globals())
}
result = normalize($.extend(defaults, settings || {}))
waitForSettings.fire(result)
return result
})
// Defaults + global variables + global overrides + local overrides
const build = function (settings) {
var result
result = $.extend({}, common())
if (!$.isEmptyObject(settings)) {
result = normalize($.extend(result, settings))
}
return result
}
const waitForSettings = isWindowDefined() && $.Callbacks('once memory')
const CssCollector = class CssCollector {
constructor() {
this.urls = []
this.styles = []
}
addUrl(url) {
if (!/^https?:\/\//i.test(url)) {
throw new Error('Embedded urls should be absolute. ' + url)
}
if (!(indexOf.call(this.urls, url) >= 0)) {
return this.urls.push(url)
}
}
addStyle(style) {
return this.styles.push(style)
}
}
const emptyKeyText =
'<div class="uploadcare--tab__content">\n<div class="uploadcare--text uploadcare--text_size_large uploadcare--tab__title">Hello!</div>\n<div class="uploadcare--text">Your <a class="uploadcare--link" href="https://uploadcare.com/dashboard/">public key</a> is not set.</div>\n<div class="uploadcare--text">Add this to the <head> tag to start uploading files:</div>\n<div class="uploadcare--text uploadcare--text_pre"><script>\nUPLOADCARE_PUBLIC_KEY = \'your_public_key\';\n</script></div>\n</div>'
export {
defaults,
presets,
emptyKeyText,
globals,
build,
common,
waitForSettings,
CssCollector
}