-
Notifications
You must be signed in to change notification settings - Fork 199
/
ExportMixin.js
275 lines (243 loc) · 12.6 KB
/
ExportMixin.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
define( [
'dojo/_base/array',
'dojo/aspect',
'dojo/has',
'JBrowse/Util',
'JBrowse/View/Cloud/gdriveUpload',
'dijit/form/Button',
'dijit/form/RadioButton',
'dijit/Dialog'
],
function( array, aspect, has, Util, gdriveUpload, dijitButton, dijitRadioButton, dijitDialog ) {
/**
* Mixin for a track that can export its data.
* @lends JBrowse.View.Track.ExportMixin
*/
return {
_canExport: function() {
if( this.config.noExport )
return false;
var visibleRegion = this.browser.view.visibleRegion();
var wholeRefSeqRegion = { ref: this.refSeq.name, start: this.refSeq.start, end: this.refSeq.end };
var canExportVisibleRegion = this._canExportRegion( visibleRegion );
var canExportWholeRef = this._canExportRegion( wholeRefSeqRegion );
return canExportVisibleRegion || canExportWholeRef;
},
_possibleExportRegions: function() {
return [
// the visible region
(function() {
var r = dojo.clone( this.browser.view.visibleRegion() );
r.description = 'Visible region';
r.name = 'visible';
return r;
}.call(this)),
// whole reference sequence
{ ref: this.refSeq.name, start: this.refSeq.start, end: this.refSeq.end, description: 'Whole reference sequence', name: 'wholeref' }
];
},
_exportDialogContent: function() {
// note that the `this` for this content function is not the track, it's the menu-rendering context
var possibleRegions = this.track._possibleExportRegions();
// for each region, calculate its length and determine whether we can export it
array.forEach( possibleRegions, function( region ) {
region.length = Math.round( region.end - region.start + 1 );
region.canExport = this._canExportRegion( region );
},this.track);
var form = dojo.create('form', { onSubmit: function() { return false; } });
form.innerHTML = ''
+ ' <fieldset class="region">'
+ ' <legend>Region to save</legend>'
+ function() {
var regions = '';
var checked = 0;
array.forEach( possibleRegions, function(r) {
var locstring = Util.assembleLocString(r);
regions += ' <input '+( r.canExport ? checked++ ? '' : ' checked="checked"' : ' disabled="disabled"' )
+ ' type="radio" data-dojo-type="dijit.form.RadioButton" name="region" id="region_'+r.name+'"'
+ ' value="'+locstring+'" />'
+ ' <label '+( r.canExport ? '' : ' class="ghosted"')+' for="region_'+r.name+'">'+r.description+' - <span class="locString">'
+ locstring+'</span> ('+Util.humanReadableNumber(r.length)+(r.canExport ? 'b' : 'b, too large')+')</label>'
+ ' <br>';
});
return regions;
}.call(this)
+ ' </fieldset>'
+ ' '
+ ' <fieldset class="saveto">'
+ ' <legend>Save to</legend>'
+ function() {
var dlSource = ''
+ '<input type="radio" checked="checked" data-dojo-type="dijit.form.RadioButton"'
+ ' name="saveto" id="hdd" value="Hard Drive" />'
+ '<label for="hdd">Hard Drive</label><br>'
+ '<input type="radio" data-dojo-type="dijit.form.RadioButton" name="saveto"'
+ ' id="gd" value="Google Drive" />'
+ '<label for="gd">Google Drive</label><br>';
return dlSource;
}.call(this)
+ ' </fieldset>'
+ ' '
+ ' <fieldset class="format">'
+ ' <legend>Format</legend>'
+ function() {
var fmts = '';
var checked = 0;
array.forEach( this.track._exportFormats(), function(fmt) {
fmts += ' <input type="radio" '+ (checked++?'':'checked="checked" ')
+' data-dojo-type="dijit.form.RadioButton" name="format" id="format'+fmt+'" value="'+fmt+'" />'
+ ' <label for="format'+fmt+'">'+fmt+'</label>'
+ ' <br>';
});
return fmts;
}.call(this)
+ ' </fieldset>';
var actionBar = dojo.create( 'div', {
className: 'dijitDialogPaneActionBar'
});
// note that the `this` for this content function is not the track, it's the menu-rendering context
var dialog = this.dialog;
new dijitButton({ iconClass: 'dijitIconDelete', onClick: dojo.hitch(dialog,'hide'), label: 'Cancel' })
.placeAt( actionBar );
var viewButton = new dijitButton({ iconClass: 'dijitIconTask',
label: 'View',
disabled: ! array.some(possibleRegions,function(r) { return r.canExport; }),
onClick: dojo.hitch( this.track, function() {
viewButton.set('disabled',true);
viewButton.set('iconClass','jbrowseIconBusy');
var region = this._readRadio( form.elements.region );
var format = this._readRadio( form.elements.format );
var saveto = this._readRadio( form.elements.saveto );
this.exportRegion( region, format, function(output) {
dialog.hide();
var text = dojo.create('textarea', {
rows: 30,
wrap: 'soft',
cols: 80,
readonly: true
});
text.value = output;
var actionBar = dojo.create( 'div', {
className: 'dijitDialogPaneActionBar'
});
var exportView = new dijitDialog({
className: 'export-view-dialog',
title: format + ' export - <span class="locString">'+ region+'</span> ('+Util.humanReadableNumber(output.length)+'b)',
content: [ text, actionBar ]
});
new dijitButton({ iconClass: 'dijitIconDelete',
label: 'Close', onClick: dojo.hitch( exportView, 'hide' )
})
.placeAt(actionBar);
// data URL download doesn't work on IE < 10
if( ! (has('ie') < 10) ) {
new dijitButton(
{
iconClass: 'dijitIconSave',
label: 'Save',
onClick: dojo.hitch(this, function() {
exportView.hide();
dojo.hitch(this, "saveTrack", "output", "saveto");//TODO fix this line see :185?
})
}).placeAt(actionBar);
}
aspect.after( exportView, 'hide', function() {
text.parentNode.removeChild( text ); // manually unhook and free the (possibly huge) text area
text = null;
exportView.destroyRecursive();
});
exportView.show();
});
})})
.placeAt( actionBar );
// don't show a download button if the user is using IE older
// than 10, cause it won't work.
if( ! (has('ie') < 10) ) {
var dlButton = new dijitButton({ iconClass: 'dijitIconSave',
label: 'Save',
disabled: ! array.some(possibleRegions,function(r) { return r.canExport; }),
onClick: dojo.hitch( this.track, function() {
var format = this._readRadio( form.elements.format );
var region = this._readRadio( form.elements.region );
var saveto = this._readRadio( form.elements.saveto );
dlButton.set('disabled',true);
dlButton.set('iconClass','jbrowseIconBusy');
this.exportRegion( region, format, dojo.hitch(this,function( output ) {
dialog.hide();
this.saveTrack(output, format, saveto);
}));
})})
.placeAt( actionBar );
}
return [ form, actionBar ];
},
saveTrack: function (output,format, saveto) {
if (saveto === 'Hard Drive') {
window.location.href="data:application/x-"+format.toLowerCase()+","+escape(output);
} else if(saveto === 'Google Drive') {
var gdriveUploader = new gdriveUpload;
gdriveUploader.uploadFile({name:"name", data:output, format:format}, alert("upload complete"));
}
},
// cross-platform function for (portably) reading the value of a radio control. sigh. *rolls eyes*
_readRadio: function( r ) {
if( r.length ) {
for( var i = 0; i<r.length; i++ ) {
if( r[i].checked )
return r[i].value;
}
}
return r.value;
},
exportRegion: function( region, format, callback ) {
// parse the locstring if necessary
if( typeof region == 'string' )
region = Util.parseLocString( region );
// we can only export from the currently-visible reference
// sequence right now
if( region.ref != this.refSeq.name ) {
console.error("cannot export data for ref seq "+region.ref+", "
+ "exporting is currently only supported for the "
+ "currently-visible reference sequence" );
return;
}
require( ['JBrowse/View/Export/'+format], dojo.hitch(this,function( exportDriver ) {
new exportDriver({
refSeq: this.refSeq,
track: this,
store: this.store
}).exportRegion( region, callback );
}));
},
_trackMenuOptions: function() {
var opts = this.inherited(arguments);
if( ! this.config.noExport )
// add a "Save track data as" option to the track menu
opts.push({ label: 'Save track data',
iconClass: 'dijitIconSave',
disabled: ! this._canExport(),
action: 'bareDialog',
content: this._exportDialogContent,
dialog: { id: 'exportDialog', className: 'export-dialog' }
});
return opts;
},
_canExportRegion: function( l ) {
//console.log('can generic export?');
if( ! l ) return false;
// if we have a maxExportSpan configured for this track, use it.
if( typeof this.config.maxExportSpan == 'number' || typeof this.config.maxExportSpan == 'string' ) {
return l.end - l.start + 1 <= this.config.maxExportSpan;
}
else {
// if we know the store's feature density, then use that with
// a limit of maxExportFeatures or 5,000 features
var storeStats = this.store.getGlobalStats();
if( storeStats.featureDensity )
return storeStats.featureDensity*(l.end - l.start) <= ( this.config.maxExportFeatures || 5000 );
}
// otherwise, i guess we can export
return true;
}
};
});