forked from mapbox/mapbox-studio-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.js
108 lines (93 loc) · 4.03 KB
/
atom.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
// Stopgap atom.js file for handling normal browser things that atom
// does not yet have stable from the browser-side API.
// - Opening external links in default web browser
// - Saving files/downloads to disk
$(document).ready(function() {
if (typeof process === 'undefined') return;
if (typeof process.versions['atom-shell'] === undefined) return;
var remote = require('remote');
var shell = require('shell');
var url = require('url');
var path = require('path');
$('body').on('click', 'a', function(ev) {
var uri = url.parse(ev.currentTarget.href);
// Opening external URLs.
if (uri.hostname && uri.hostname !== 'localhost') {
shell.openExternal(ev.currentTarget.href);
return false;
}
// File saving.
var fileTypes = {
tm2z: 'Package',
mbtiles: 'Tiles',
png: 'Image',
jpg: 'Image',
jpeg: 'Image'
}
var typeExtension = (uri.pathname || '').split('.').pop().toLowerCase();
var typeLabel = fileTypes[typeExtension];
// Passthrough for all other extensions.
if (!typeLabel) return undefined;
// HOME is undefined on windows
if (process.platform === 'win32') process.env.HOME = process.env.USERPROFILE;
// The placement of these requires is potentially sensitive on
// atom-shell + Windows. Reorder/move in this procedure with caution.
var fs = remote.require('fs');
var http = remote.require('http');
var dialog = remote.require('dialog');
// Show save dialog and make a GET request, piping
// the response into the specified filePath.
var defaultPath = path.join(process.env.HOME,'Untitled ' + typeLabel + '.' + typeExtension);
var filePath = dialog.showSaveDialog({
title: 'Save ' + typeLabel,
defaultPath: defaultPath,
filters: [{ name: typeExtension.toUpperCase(), extensions: [typeExtension]}]
});
if (!filePath) return false;
window.Modal.show('atomexporting');
function error(err) {
window.Modal.close();
window.Modal.show('error', err);
}
function finish() {
window.Modal.close();
window.Modal.show('atomcomplete');
}
http.get(uri, function(res) {
if (res.statusCode !== 200) return error(new Error('Got HTTP code ' + res.statusCode));
try {
var writeStream = fs.createWriteStream(filePath);
} catch(err) {
return error(err);
}
// The order of event listeners here appears to be sensitive
// when dealing with atom-shell + Windows. Previous ordering based
// on expected node-core like behavior (e.g. adding the 'finish')
// listener after res.pipe() is called can lead to the handler
// never being called.
writeStream.on('error', error);
writeStream.on('finish', finish);
res.on('error', error);
res.pipe(writeStream);
}).on('error', error);
return false;
});
if (window.Modal) {
window.Modal.options.templates.modalatomexporting = function() {
return "\
<div id='atom-loading' class='modal-body contain round col6 space-bottom4 dark fill-dark'>\
<h3 class='center pad1y pad2x keyline-bottom'>Exporting</h3>\
<div class='row2 loading contain'></div>\
</div>";
};
window.Modal.options.templates.modalatomcomplete = function() {
return "\
<div id='atom-loading' class='modal-body contain round col6 space-bottom4 dark fill-dark'>\
<h3 class='center pad1y pad2x keyline-bottom'>Exporting</h3>\
<div class='row2 pad2 contain clearfix'>\
<a href='#' class='js-close margin3 col6 button icon check'>Export complete</a>\
</div>\
</div>";
};
}
});