Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
414 lines (351 loc) · 10.3 KB
/
Copy pathutils.js
File metadata and controls
414 lines (351 loc) · 10.3 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
409
410
411
412
413
414
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
/**
* Move settings to foreground
*/
function reopenSettings() {
navigator.mozApps.getSelf(). getSelfCB(evt) {
var app = evt.target.result;
app.launch('settings');
};
}
/**
* Open a link with a web activity
*/
function openLink(url) {
if (url.startsWith('tel:')) { // dial a phone number
new MozActivity({
name: 'dial',
data: { type: 'webtelephony/number', number: url.substr(4) }
});
} else if (!url.startsWith('#')) { // browse a URL
new MozActivity({
name: 'view',
data: { type: 'url', url: url }
});
}
}
/**
* These so-called "dialog boxes" are just standard Settings panels (<section
* role="region" />) with reset/submit buttons: these buttons both return to the
* previous panel when clicked, and each button has its own (optional) callback.
*/
function openDialog(dialogID, onSubmit, onReset) {
if ('#' + dialogID == Settings.currentPanel)
return;
var origin = Settings.currentPanel;
// Load dialog contents and show it.
Settings.currentPanel = dialogID;
var dialog = document.getElementById(dialogID);
var submit = dialog.querySelector('[type=submit]');
if (submit) {
submit. onsubmit() {
if (typeof 'function')
(onSubmit.bind(dialog))();
Settings.currentPanel = origin; // hide dialog box
};
}
var reset = dialog.querySelector('[type=reset]');
if (reset) {
reset. onreset() {
if (typeof 'function')
(onReset.bind(dialog))();
Settings.currentPanel = origin; // hide dialog box
};
}
}
/**
* JSON loader
*/
function loadJSON(href, callback) {
if (!callback)
return;
var xhr = new XMLHttpRequest();
xhr. {
console.error('Failed to fetch file: ' + href, xhr.statusText);
};
xhr. {
callback(xhr.response);
};
xhr.open('GET', href, true); // async
xhr.responseType = 'json';
xhr.send();
}
/**
* L10n helper
*/
var localize = navigator.mozL10n.localize;
/**
* Helper class for formatting file size strings
* required by *_storage.js
*/
var FileSizeFormatter = (function FileSizeFormatter(fixed) {
function getReadableFileSize(size, digits) { // in: size in Bytes
if (size === undefined)
return {};
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = 0;
while (size >= 1024) {
size /= 1024;
++i;
}
var sizeString = size.toFixed(digits || 0);
var sizeDecimal = parseFloat(sizeString);
return {
size: sizeDecimal.toString(),
unit: units[i]
};
}
return { getReadableFileSize: getReadableFileSize };
})();
/**
* Helper class for getting available/used storage
* required by *_storage.js
*/
var DeviceStorageHelper = (function DeviceStorageHelper() {
function getStat(type, callback) {
var deviceStorage = navigator.getDeviceStorage(type);
if (!deviceStorage) {
console.error('Cannot get DeviceStorage for: ' + type);
return;
}
deviceStorage.freeSpace(). {
var freeSpace = e.target.result;
deviceStorage.usedSpace(). {
var usedSpace = e.target.result;
callback(usedSpace, freeSpace, type);
};
};
}
function getFreeSpace(callback) {
var deviceStorage = navigator.getDeviceStorage('sdcard');
if (!deviceStorage) {
console.error('Cannot get free space size in sdcard');
return;
}
deviceStorage.freeSpace(). {
var freeSpace = e.target.result;
callback(freeSpace);
};
}
function showFormatedSize(element, l10nId, size) {
if (size === undefined || isNaN(size)) {
element.textContent = '';
return;
}
// KB - 3 KB (nearest ones), MB, GB - 1.2 MB (nearest tenth)
var fixedDigits = (size < 1024 * 1024) ? 0 : 1;
var sizeInfo = FileSizeFormatter.getReadableFileSize(size, fixedDigits);
var _ = navigator.mozL10n.get;
element.textContent = _(l10nId, {
size: sizeInfo.size,
unit: _('byteUnit-' + sizeInfo.unit)
});
}
return {
getStat: getStat,
getFreeSpace: getFreeSpace,
showFormatedSize: showFormatedSize
};
})();
/**
* Connectivity accessors
*/
var getMobileConnection = function() {
var navigator = window.navigator;
// XXX: check bug-926169
// this is used to keep all tests passing while introducing multi-sim APIs
var mobileConnection = navigator.mozMobileConnection ||
navigator.mozMobileConnections &&
navigator.mozMobileConnections[0];
if (mobileConnection && mobileConnection.data)
return mobileConnection;
};
var getBluetooth = function() {
return navigator.mozBluetooth;
};
var getNfc = function() {
var navigator = window.navigator;
if ('mozNfc' in navigator) {
return navigator.mozNfc;
}
return null;
};
/**
* The function returns an object of the supporting state of category of network
* types. The categories are 'gsm' and 'cdma'.
*/
function getSupportedNetworkInfo(mobileConneciton, callback) {
var types = [
'wcdma/gsm',
'gsm',
'wcdma',
'wcdma/gsm-auto',
'cdma/evdo',
'cdma',
'evdo',
'wcdma/gsm/cdma/evdo'
];
if (!mobileConneciton)
return;
var _hwSupportedTypes = mobileConneciton.supportedNetworkTypes;
var _result = {
gsm: _hwSupportedTypes.indexOf('gsm') !== -1,
cdma: _hwSupportedTypes.indexOf('cdma') !== -1,
wcdma: _hwSupportedTypes.indexOf('wcdma') !== -1,
evdo: _hwSupportedTypes.indexOf('evdo') !== -1,
networkTypes: null
};
var _networkTypes = [];
for (var i = 0; i < types.length; i++) {
var type = types[i];
var subtypes = type.split('/');
var allSubTypesSupported = true;
for (var j = 0; j < subtypes.length; j++) {
allSubTypesSupported =
allSubTypesSupported && _result[subtypes[j].split('-')[0]];
}
if (allSubTypesSupported)
_networkTypes.push(type);
}
if (_networkTypes.length !== 0) {
_result.networkTypes = _networkTypes;
}
callback(_result);
}
function isIP(address) {
return /^\d+\.\d+\.\d+\.\d+$/.test(address);
}
// Remove additional 0 in front of IP digits.
// Notice that this is not following standard dot-decimal notation, just for
// possible error tolarance.
// (Values starting with 0 stand for octal representation by standard)
function sanitizeAddress(input) {
if (isIP(input)) {
return input.replace(/0*(\d+)/g, '$1');
} else {
return input;
}
}
function getTruncated(oldName, options) {
// options
var maxLine = options.maxLine || 2;
var node = options.node;
var ellipsisIndex = options.ellipsisIndex || 3;
var ellipsisCharacter = options.ellipsisCharacter || '...';
if (node === null) {
return oldName;
}
// used variables and functions
function hitsNewline(oldHeight, newHeight) {
return oldHeight !== newHeight;
}
var newName = '';
var oldHeight;
var newHeight;
var baseHeight;
var currentLine;
var ellipsisAt;
var hasNewEllipsisPoint = true;
var nameBeforeEllipsis = [];
var nameBeforeEllipsisString;
var nameAfterEllipsis = oldName.slice(-ellipsisIndex);
var realVisibility = node.style.visibility;
var realWordBreak = node.style.wordBreak;
/*
* Hide UI, because we are manipulating DOM
*/
node.style.visibility = 'hidden';
/*
* Force breaking on boundaries
*/
node.s
9324
tyle.wordBreak = 'break-all';
/*
* Get the base height to count the currentLine at first
*/
node.textContent = '.';
baseHeight = node.clientHeight;
node.textContent = '';
var needEllipsis = oldName.split('').some(function(character, index) {
nameBeforeEllipsis.push(character);
nameBeforeEllipsisString = nameBeforeEllipsis.join('');
oldHeight = node.clientHeight;
node.textContent = nameBeforeEllipsisString +
ellipsisCharacter + nameAfterEllipsis;
newHeight = node.clientHeight;
/*
* When index is 0, we have to update currentLine according to
* the first assignment (it is possible that at first the currentLine
* is not 0 if the width of node is too small)
*/
if (index === 0) {
currentLine = Math.floor(newHeight / baseHeight);
}
if (hitsNewline(oldHeight, newHeight) && index !== 0) {
/*
* The reason why we have to check twice is because there is a
* situation that truncated string is overflowed but there is
* still room for original string.
*
* In this way, we have to memorize the ellipsis index and
* slice `nameBeforeEllipsis` to the index in the end.
*/
var testHeight;
node.textContent = nameBeforeEllipsisString;
testHeight = node.clientHeight;
if (hitsNewline(oldHeight, testHeight)) {
/*
* We have to make it true again to keep the ellipsisAt
* up to date.
*/
hasNewEllipsisPoint = true;
currentLine += 1;
} else {
/*
* This is the situation that we still have room, so we have
* to keep the ellipsisAt value for later use.
*/
if (hasNewEllipsisPoint) {
ellipsisAt = index;
hasNewEllipsisPoint = false;
}
}
}
if (currentLine > maxLine) {
if (index === 0) {
/*
* It means that at first, the whole string is already in
* an overflowed situation, you have to avoid this situation.
* And we will bypass oldName back to you.
*
* There are some options for you :
*
* 1. Check options.ellipsisCharacter
* 2. Check options.maxLine
* 3. Check node's width (maybe too narrow)
*/
console.log(
'Your string is in a overflowed situation, ' +
'please check your options');
}
/*
* Remove the last character, because it causes the overflow
*/
nameBeforeEllipsis.pop();
node.textContent = '';
return true;
}
});
// restore UI
node.style.visibility = realVisibility;
node.style.wordBreak = realWordBreak;
if (!needEllipsis) {
newName = oldName;
} else {
newName += nameBeforeEllipsis.join('').slice(0, ellipsisAt);
newName += ellipsisCharacter;
newName += nameAfterEllipsis;
}
return newName;
}