-
Notifications
You must be signed in to change notification settings - Fork 966
/
tool-form-base.js
330 lines (311 loc) · 14.1 KB
/
tool-form-base.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
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
/**
This is the base class of the tool form plugin. This class is e.g. inherited by the regular and the workflow tool form.
*/
define(['utils/utils', 'utils/deferred', 'mvc/ui/ui-misc', 'mvc/form/form-view',
'mvc/citation/citation-model', 'mvc/citation/citation-view'],
function(Utils, Deferred, Ui, FormBase, CitationModel, CitationView) {
return FormBase.extend({
initialize: function(options) {
var self = this;
FormBase.prototype.initialize.call(this, options);
this.deferred = new Deferred();
if (options.inputs) {
this._buildForm(options);
} else {
this.deferred.execute(function(process) {
self._buildModel(process, options, true);
});
}
// Listen to history panel
if ( options.listen_to_history && parent.Galaxy && parent.Galaxy.currHistoryPanel ) {
this.listenTo( parent.Galaxy.currHistoryPanel.collection, 'change', function() {
this.refresh();
});
}
},
/** Listen to history panel changes and update the tool form */
refresh: function() {
var self = this;
self.deferred.reset();
this.deferred.execute( function (process){
self._updateModel( process)
});
},
/** Wait for deferred build processes before removal */
remove: function() {
var self = this;
this.$el.hide();
this.deferred.execute(function(){
FormBase.prototype.remove.call(self);
Galaxy.emit.debug('tool-form-base::remove()', 'Destroy view.');
});
},
/** Build form */
_buildForm: function(options) {
var self = this;
this.options = Utils.merge(options, this.options);
this.options = Utils.merge({
icon : options.icon,
title : '<b>' + options.name + '</b> ' + options.description + ' (Galaxy Version ' + options.version + ')',
operations : !this.options.hide_operations && this._operations(),
onchange : function() {
self.refresh();
}
}, this.options);
this.options.customize && this.options.customize( this.options );
this.render();
if ( !this.options.collapsible ) {
this.$el.append( $( '<div/>' ).addClass( 'ui-margin-top-large' ).append( this._footer() ) );
}
},
/** Builds a new model through api call and recreates the entire form
*/
_buildModel: function(process, options, hide_message) {
var self = this;
this.options.id = options.id;
this.options.version = options.version;
// build request url
var build_url = '';
var build_data = {};
if ( options.job_id ) {
build_url = Galaxy.root + 'api/jobs/' + options.job_id + '/build_for_rerun';
} else {
build_url = Galaxy.root + 'api/tools/' + options.id + '/build';
if ( Galaxy.params && Galaxy.params.tool_id == options.id ) {
build_data = $.extend( {}, Galaxy.params );
options.version && ( build_data[ 'tool_version' ] = options.version );
}
}
// get initial model
Utils.get({
url : build_url,
data : build_data,
success : function(new_model) {
new_model = new_model.tool_model || new_model;
if( !new_model.display ) {
window.location = Galaxy.root;
return;
}
self._buildForm(new_model);
!hide_message && self.message.update({
status : 'success',
message : 'Now you are using \'' + self.options.name + '\' version ' + self.options.version + ', id \'' + self.options.id + '\'.',
persistent : false
});
Galaxy.emit.debug('tool-form-base::initialize()', 'Initial tool model ready.', new_model);
process.resolve();
},
error : function(response, xhr) {
var error_message = ( response && response.err_msg ) || 'Uncaught error.';
if ( xhr.status == 401 ) {
window.location = Galaxy.root + 'user/login?' + $.param({ redirect : Galaxy.root + '?tool_id=' + self.options.id });
} else if ( self.$el.is(':empty') ) {
self.$el.prepend((new Ui.Message({
message : error_message,
status : 'danger',
persistent : true,
large : true
})).$el);
} else {
Galaxy.modal && Galaxy.modal.show({
title : 'Tool request failed',
body : error_message,
buttons : {
'Close' : function() {
Galaxy.modal.hide();
}
}
});
}
Galaxy.emit.debug('tool-form::initialize()', 'Initial tool model request failed.', response);
process.reject();
}
});
},
/** Request a new model for an already created tool form and updates the form inputs
*/
_updateModel: function(process) {
// link this
var self = this;
var model_url = this.options.update_url || Galaxy.root + 'api/tools/' + this.options.id + '/build';
var current_state = {
tool_id : this.options.id,
tool_version : this.options.version,
inputs : $.extend(true, {}, self.data.create())
}
this.wait(true);
// log tool state
Galaxy.emit.debug('tool-form-base::_updateModel()', 'Sending current state.', current_state);
// post job
Utils.request({
type : 'POST',
url : model_url,
data : current_state,
success : function(new_model) {
self.update(new_model['tool_model'] || new_model);
self.options.update && self.options.update(new_model);
self.wait(false);
Galaxy.emit.debug('tool-form-base::_updateModel()', 'Received new model.', new_model);
process.resolve();
},
error : function(response) {
Galaxy.emit.debug('tool-form-base::_updateModel()', 'Refresh request failed.', response);
process.reject();
}
});
},
/** Create tool operation menu
*/
_operations: function() {
var self = this;
var options = this.options;
// button for version selection
var versions_button = new Ui.ButtonMenu({
icon : 'fa-cubes',
title : (!options.narrow && 'Versions') || null,
tooltip : 'Select another tool version'
});
if (!options.sustain_version && options.versions && options.versions.length > 1) {
for (var i in options.versions) {
var version = options.versions[i];
if (version != options.version) {
versions_button.addMenu({
title : 'Switch to ' + version,
version : version,
icon : 'fa-cube',
onclick : function() {
// here we update the tool version (some tools encode the version also in the id)
var id = options.id.replace(options.version, this.version);
var version = this.version;
// queue model request
self.deferred.reset();
self.deferred.execute(function(process) {
self._buildModel(process, {id: id, version: version})
});
}
});
}
}
} else {
versions_button.$el.hide();
}
// button for options e.g. search, help
var menu_button = new Ui.ButtonMenu({
icon : 'fa-caret-down',
title : (!options.narrow && 'Options') || null,
tooltip : 'View available options'
});
if(options.biostar_url) {
menu_button.addMenu({
icon : 'fa-question-circle',
title : 'Question?',
tooltip : 'Ask a question about this tool (Biostar)',
onclick : function() {
window.open(options.biostar_url + '/p/new/post/');
}
});
menu_button.addMenu({
icon : 'fa-search',
title : 'Search',
tooltip : 'Search help for this tool (Biostar)',
onclick : function() {
window.open(options.biostar_url + '/local/search/page/?q=' + options.name);
}
});
};
menu_button.addMenu({
icon : 'fa-share',
title : 'Share',
tooltip : 'Share this tool',
onclick : function() {
prompt('Copy to clipboard: Ctrl+C, Enter', window.location.origin + Galaxy.root + 'root?tool_id=' + options.id);
}
});
// add admin operations
if (Galaxy.user && Galaxy.user.get('is_admin')) {
menu_button.addMenu({
icon : 'fa-download',
title : 'Download',
tooltip : 'Download this tool',
onclick : function() {
window.location.href = Galaxy.root + 'api/tools/' + options.id + '/download';
}
});
}
// button for version selection
if (options.requirements && options.requirements.length > 0) {
menu_button.addMenu({
icon : 'fa-info-circle',
title : 'Requirements',
tooltip : 'Display tool requirements',
onclick : function() {
if (!this.visible || self.portlet.collapsed ) {
this.visible = true;
self.portlet.expand();
self.message.update({
persistent : true,
message : self._templateRequirements(options),
status : 'info'
});
} else {
this.visible = false;
self.message.update({
message : ''
});
}
}
});
}
// add toolshed url
if (options.sharable_url) {
menu_button.addMenu({
icon : 'fa-external-link',
title : 'See in Tool Shed',
tooltip : 'Access the repository',
onclick : function() {
window.open(options.sharable_url);
}
});
}
return {
menu : menu_button,
versions : versions_button
}
},
/** Create footer
*/
_footer: function() {
var options = this.options;
var $el = $( '<div/>' ).append( this._templateHelp( options ) );
if ( options.citations ) {
var $citations = $( '<div/>' );
var citations = new CitationModel.ToolCitationCollection();
citations.tool_id = options.id;
var citation_list_view = new CitationView.CitationListView({ el: $citations, collection: citations });
citation_list_view.render();
citations.fetch();
$el.append( $citations );
}
return $el;
},
/** Templates
*/
_templateHelp: function( options ) {
var $tmpl = $( '<div/>' ).addClass( 'ui-form-help' ).append( options.help );
$tmpl.find( 'a' ).attr( 'target', '_blank' );
return $tmpl;
},
_templateRequirements: function( options ) {
var nreq = options.requirements.length;
if ( nreq > 0 ) {
var requirements_message = 'This tool requires ';
_.each( options.requirements, function( req, i ) {
requirements_message += req.name + ( req.version ? ' (Version ' + req.version + ')' : '' ) + ( i < nreq - 2 ? ', ' : ( i == nreq - 2 ? ' and ' : '' ) );
});
var requirements_link = $( '<a/>' ).attr( 'target', '_blank' ).attr( 'href', 'https://wiki.galaxyproject.org/Tools/Requirements' ).text( 'here' );
return $( '<span/>' ).append( requirements_message + '. Click ' ).append( requirements_link ).append( ' for more information.' );
}
return 'No requirements found.';
}
});
});