-
Notifications
You must be signed in to change notification settings - Fork 966
/
tool-form.js
178 lines (173 loc) · 8.86 KB
/
tool-form.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
/**
This is the regular tool form.
*/
define([ 'utils/utils', 'mvc/ui/ui-misc', 'mvc/ui/ui-modal', 'mvc/tool/tool-form-base' ],
function( Utils, Ui, Modal, ToolFormBase ) {
var View = ToolFormBase.extend({
initialize: function( options ) {
var self = this;
options.listen_to_history = true;
options.always_refresh = false;
this.modal = parent.Galaxy.modal || new Modal.View();
ToolFormBase.prototype.initialize.call( this, Utils.merge({
customize: function( options ) {
// build execute button
options.buttons = {
execute: execute_btn = new Ui.Button({
icon : 'fa-check',
tooltip : 'Execute: ' + options.name + ' (' + options.version + ')',
title : 'Execute',
cls : 'ui-button btn btn-primary',
floating : 'clear',
onclick : function() {
execute_btn.wait();
self.portlet.disable();
self.submit( options, function() {
execute_btn.unwait();
self.portlet.enable();
} );
}
})
};
// remap feature
if ( options.job_id && options.job_remap ) {
options.inputs[ 'rerun_remap_job_id' ] = {
label : 'Resume dependencies from this job',
name : 'rerun_remap_job_id',
type : 'select',
display : 'radio',
ignore : '__ignore__',
value : '__ignore__',
options : [ [ 'Yes', options.job_id ], [ 'No', '__ignore__' ] ],
help : 'The previous run of this tool failed and other tools were waiting for it to finish successfully. Use this option to resume those tools using the new output(s) of this tool run.'
}
}
}
}, options ) );
},
/** Submit a regular job.
* @param{dict} options - Specifies tool id and version
* @param{function} callback - Called when request has completed
*/
submit: function( options, callback ) {
var self = this;
var job_def = {
tool_id : options.id,
tool_version : options.version,
inputs : this.data.create()
}
this.trigger( 'reset' );
if ( !self.validate( job_def ) ) {
Galaxy.emit.debug( 'tool-form::submit()', 'Submission canceled. Validation failed.' );
callback && callback();
return;
}
if ( options.action !== Galaxy.root + 'tool_runner/index' ) {
var $f = $( '<form/>' ).attr( { action: options.action, method: options.method, enctype: options.enctype } );
_.each( job_def.inputs, function( value, key ) { $f.append( $( '<input/>' ).attr( { 'name': key, 'value': value } ) ) } );
$f.hide().appendTo( 'body' ).submit().remove();
callback && callback();
return;
}
Galaxy.emit.debug( 'tool-form::submit()', 'Validation complete.', job_def );
Utils.request({
type : 'POST',
url : Galaxy.root + 'api/tools',
data : job_def,
success : function( response ) {
callback && callback();
self.$el.empty().append( self._templateSuccess( response ) );
parent.Galaxy && parent.Galaxy.currHistoryPanel && parent.Galaxy.currHistoryPanel.refreshContents();
},
error : function( response ) {
callback && callback();
Galaxy.emit.debug( 'tool-form::submit', 'Submission failed.', response );
var input_found = false;
if ( response && response.err_data ) {
var error_messages = self.data.matchResponse( response.err_data );
for (var input_id in error_messages) {
self.highlight( input_id, error_messages[ input_id ]);
input_found = true;
break;
}
}
if ( !input_found ) {
self.modal.show({
title : 'Job submission failed',
body : ( response && response.err_msg ) || self._templateError( job_def ),
buttons : {
'Close' : function() {
self.modal.hide();
}
}
});
}
}
});
},
/** Validate job dictionary.
* @param{dict} job_def - Job execution dictionary
*/
validate: function( job_def ) {
var job_inputs = job_def.inputs;
var batch_n = -1;
var batch_src = null;
for ( var job_input_id in job_inputs ) {
var input_value = job_inputs[ job_input_id ];
var input_id = this.data.match( job_input_id );
var input_field = this.field_list[ input_id ];
var input_def = this.input_list[ input_id ];
if ( !input_id || !input_def || !input_field ) {
Galaxy.emit.debug('tool-form::validate()', 'Retrieving input objects failed.');
continue;
}
if ( !input_def.optional && input_value == null ) {
this.highlight( input_id );
return false;
}
if ( input_value && input_value.batch ) {
var n = input_value.values.length;
var src = n > 0 && input_value.values[ 0 ] && input_value.values[ 0 ].src;
if ( src ) {
if ( batch_src === null ) {
batch_src = src;
} else if ( batch_src !== src ) {
this.highlight( input_id, 'Please select either dataset or dataset list fields for all batch mode fields.' );
return false;
}
}
if ( batch_n === -1 ) {
batch_n = n;
} else if ( batch_n !== n ) {
this.highlight( input_id, 'Please make sure that you select the same number of inputs for all batch mode fields. This field contains <b>' + n + '</b> selection(s) while a previous field contains <b>' + batch_n + '</b>.' );
return false;
}
}
}
return true;
},
_templateSuccess: function( response ) {
if ( response.jobs && response.jobs.length > 0 ) {
var njobs = response.jobs.length;
var njobs_text = njobs == 1 ? '1 job has' : njobs + ' jobs have';
var $message = $( '<div/>' ).addClass( 'donemessagelarge' )
.append( $( '<p/>' ).text( njobs_text + ' been successfully added to the queue - resulting in the following datasets:' ) );
_.each( response.outputs, function( output ) {
$message.append( $( '<p/>' ).addClass( 'messagerow' ).append( $( '<b/>' ).text( output.hid + ': ' + output.name ) ) );
});
$message.append( $( '<p/>' ).append( '<b/>' ).text( 'You can check the status of queued jobs and view the resulting data by refreshing the History pane. When the job has been run the status will change from \'running\' to \'finished\' if completed successfully or \'error\' if problems were encountered.' ) );
return $message;
} else {
return this._templateError( response );
}
},
_templateError: function( response ) {
return $( '<div/>' ).addClass( 'errormessagelarge' )
.append( $( '<p/>' ).text( 'The server could not complete the request. Please contact the Galaxy Team if this error persists.' ) )
.append( $( '<pre/>' ).text( JSON.stringify( response, null, 4 ) ) );
}
});
return {
View: View
};
});