-
Notifications
You must be signed in to change notification settings - Fork 969
/
workflow.js
386 lines (356 loc) · 15.7 KB
/
workflow.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
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
/** Workflow view */
import * as mod_toastr from "libs/toastr";
import * as Backbone from "backbone";
import * as _ from "underscore";
import TAGS from "mvc/tag";
import WORKFLOWS from "mvc/workflow/workflow-model";
import QueryStringParsing from "utils/query-string-parsing";
import _l from "utils/localization";
import LoadingIndicator from "ui/loading-indicator";
/* global $ */
/* global Galaxy */
/** View of the individual workflows */
const WorkflowItemView = Backbone.View.extend({
tagName: "tr", // name of (orphan) root tag in this.el
initialize: function() {
_.bindAll(
this,
"render",
"_rowTemplate",
"renderTagEditor",
"_templateActions",
"removeWorkflow",
"copyWorkflow"
); // every function that uses 'this' as the current object should be in here
mod_toastr.options.timeOut = 1500;
},
events: {
"click #show-in-tool-panel": "showInToolPanel",
"click #delete-workflow": "removeWorkflow",
"click #rename-workflow": "renameWorkflow",
"click #copy-workflow": "copyWorkflow"
},
render: function() {
$(this.el).html(this._rowTemplate());
return this;
},
showInToolPanel: function() {
// This reloads the whole page, so that the workflow appears in the tool panel.
// Ideally we would notify only the tool panel of a change
this.model.save(
{ show_in_tool_panel: !this.model.get("show_in_tool_panel") },
{
success: function() {
window.location = `${Galaxy.root}workflows/list`;
}
}
);
},
removeWorkflow: function() {
const wfName = this.model.get("name");
if (window.confirm(`Are you sure you want to delete workflow '${wfName}'?`)) {
this.model.destroy({
success: function() {
mod_toastr.success(`Successfully deleted workflow '${wfName}'`);
}
});
this.remove();
}
},
renameWorkflow: function() {
const oldName = this.model.get("name");
const newName = window.prompt(`Enter a new Name for workflow '${oldName}'`, oldName);
if (newName) {
this.model.save(
{ name: newName },
{
success: function() {
mod_toastr.success(`Successfully renamed workflow '${oldName}' to '${newName}'`);
}
}
);
this.render();
}
},
copyWorkflow: function() {
const oldName = this.model.get("name");
$.getJSON(`${this.model.urlRoot}/${this.model.id}/download`, wfJson => {
let newName = `Copy of ${oldName}`;
const currentOwner = this.model.get("owner");
if (currentOwner != Galaxy.user.attributes.username) {
newName += ` shared by user ${currentOwner}`;
}
wfJson.name = newName;
this.collection.create(wfJson, {
at: 0,
wait: true,
success: function() {
mod_toastr.success(`Successfully copied workflow '${oldName}' to '${newName}'`);
},
error: function(model, resp, options) {
// signature seems to have changed over the course of backbone dev
// see https://github.com/jashkenas/backbone/issues/2606#issuecomment-19289483
mod_toastr.error(options.errorThrown);
}
});
}).error((jqXHR, textStatus, errorThrown) => {
mod_toastr.error(jqXHR.responseJSON.err_msg);
});
},
_rowTemplate: function() {
let show = this.model.get("show_in_tool_panel");
let wfId = this.model.id;
const checkboxHtml = `<input id="show-in-tool-panel" type="checkbox" class="show-in-tool-panel" ${
show ? `checked="${show}"` : ""
} value="${wfId}">`;
return `
<td>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button">${_.escape(this.model.get("name"))}
<span class="caret"></span>
</button>
${this._templateActions()}
</div>
</td>
<td>
<div class="${wfId} tags-display">
</div>
</td>
<td>
${this.model.get("owner") === Galaxy.user.attributes.username ? "You" : this.model.get("owner")}
</td>
<td>${this.model.get("number_of_steps")}</td>
<td>${this.model.get("published") ? "Yes" : "No"}</td>
<td>${checkboxHtml}</td>`;
},
renderTagEditor: function() {
const TagEditor = new TAGS.TagsEditor({
model: this.model,
el: $.find(`.${this.model.id}.tags-display`),
workflow_mode: true
});
TagEditor.toggle(true);
TagEditor.render();
},
/** Template for user actions for workflows */
_templateActions: function() {
if (this.model.get("owner") === Galaxy.user.attributes.username) {
return `<div class="dropdown-menu">
<a class="dropdown-item" href="${Galaxy.root}workflow/editor?id=${this.model.id}">Edit</a>
<a class="dropdown-item" href="${Galaxy.root}workflows/run?id=${this.model.id}">Run</a>
<a class="dropdown-item" href="${Galaxy.root}workflow/sharing?id=${this.model.id}">Share</a>
<a class="dropdown-item" href="${Galaxy.root}api/workflows/${this.model.id}/download?format=json-download">Download</a>
<a class="dropdown-item" id="copy-workflow" style="cursor: pointer;">Copy</a>
<a class="dropdown-item" id="rename-workflow" style="cursor: pointer;">Rename</a>
<a class="dropdown-item" href="${Galaxy.root}workflow/display_by_id?id=${this.model.id}">View</a>
<a class="dropdown-item" id="delete-workflow" style="cursor: pointer;">Delete</a>
</div>`;
} else {
return `<ul class="dropdown-menu">
<li><a href="${Galaxy.root}workflow/display_by_username_and_slug?username=${this.model.get("owner")}&slug=${this.model.get("slug")}">View</a></li>
<li><a href="${Galaxy.root}workflows/run?id=${this.model.id}">Run</a></li>
<li><a id="copy-workflow" style="cursor: pointer;">Copy</a></li>
<li><a class="link-confirm-shared-${this.model.id}" href="${Galaxy.root}workflow/sharing?unshare_me=True&id=${this.model.id}">Remove</a></li>
</ul>`;
}
}
});
/** View of the main workflow list page */
const WorkflowListView = Backbone.View.extend({
title: _l("Workflows"),
active_tab: "workflow",
initialize: function() {
LoadingIndicator.markViewAsLoading(this);
_.bindAll(this, "adjustActiondropdown");
this.collection = new WORKFLOWS.WorkflowCollection();
this.collection.fetch().done(this.render());
this.collection.bind("add", this.appendItem);
this.collection.on("sync", this.render, this);
},
events: {
dragleave: "unhighlightDropZone",
drop: "drop",
dragover: function(ev) {
$(".hidden_description_layer").addClass("dragover");
$(".menubutton").addClass("background-none");
ev.preventDefault();
}
},
unhighlightDropZone: function() {
$(".hidden_description_layer").removeClass("dragover");
$(".menubutton").removeClass("background-none");
},
drop: function(e) {
// TODO: check that file is valid galaxy workflow
this.unhighlightDropZone();
e.preventDefault();
const files = e.dataTransfer.files;
for (let i = 0, f; (f = files[i]); i++) {
this.readWorkflowFiles(f);
}
},
readWorkflowFiles: function(f) {
const reader = new FileReader();
reader.onload = theFile => {
let wf_json;
try {
wf_json = JSON.parse(reader.result);
} catch (e) {
mod_toastr.error(`Could not read file '${f.name}'. Verify it is a valid Galaxy workflow`);
wf_json = null;
}
if (wf_json) {
this.collection.create(wf_json, {
at: 0,
wait: true,
success: function() {
mod_toastr.success(`Successfully imported workflow '${wf_json.name}'`);
},
error: function(model, resp, options) {
mod_toastr.error(options.errorThrown);
}
});
}
};
reader.readAsText(f, "utf-8");
},
_showArgErrors: _.once(() => {
// Parse args out of params, display if there's a message.
const msg_text = QueryStringParsing.get("message");
const msg_status = QueryStringParsing.get("status");
if (msg_status === "error") {
mod_toastr.error(_.escape(msg_text || "Unknown Error, please report this to an administrator."));
} else if (msg_text) {
mod_toastr.info(_.escape(msg_text));
}
}),
render: function() {
// Add workflow header
const header = this._templateHeader();
// Add the actions buttons
const templateActions = this._templateActionButtons();
const tableTemplate = this._templateWorkflowTable();
this.$el.html(header + templateActions + tableTemplate);
_.each(this.collection.models, item => {
// in case collection is not empty
this.appendItem(item);
this.confirmDelete(item);
});
const minQueryLength = 3;
this.searchWorkflow(this.$(".search-wf"), this.$(".workflow-search tr"), minQueryLength);
this.adjustActiondropdown();
this._showArgErrors();
return this;
},
appendItem: function(item) {
const workflowItemView = new WorkflowItemView({
model: item,
collection: this.collection
});
$(".workflow-search").append(workflowItemView.render().el);
workflowItemView.renderTagEditor();
},
/** Add confirm box before removing/unsharing workflow */
confirmDelete: function(workflow) {
const $el_shared_wf_link = this.$(`.link-confirm-shared-${workflow.id}`);
$el_shared_wf_link.click(() =>
window.confirm(`Are you sure you want to remove the shared workflow '${workflow.attributes.name}'?`)
);
},
/** Implement client side workflow search/filtering */
searchWorkflow: function($el_searchinput, $el_tabletr, min_querylen) {
$el_searchinput.on("keyup", function() {
const query = $(this).val();
// Filter when query is at least 3 characters
// otherwise show all rows
if (query.length >= min_querylen) {
// Ignore the query's case using 'i'
const regular_expression = new RegExp(query, "i");
$el_tabletr.hide();
$el_tabletr
.filter(function() {
// Apply regular expression on each row's text
// and show when there is a match
return regular_expression.test($(this).text());
})
.show();
} else {
$el_tabletr.show();
}
});
},
/** Ajust the position of dropdown with respect to table */
adjustActiondropdown: function() {
$(this.el).on("show.bs.dropdown", function() {
$(this.el).css("overflow", "inherit");
});
$(this.el).on("hide.bs.dropdown", function() {
$(this.el).css("overflow", "auto");
});
},
/** Template for no workflow */
_templateNoWorkflow: function() {
return '<div class="wf-nodata"> You have no workflows. </div>';
},
/** Template for actions buttons */
_templateActionButtons: function() {
return `<ul class="manage-table-actions"><li><input class="search-wf form-control" type="text" autocomplete="off" placeholder="search for workflow..."></li><li><a class="action-button fa fa-plus wf-action" id="new-workflow" title="Create new workflow" href="${
Galaxy.root
}workflows/create"></a></li><li><a class="action-button fa fa-upload wf-action" id="import-workflow" title="Upload or import workflow" href="${
Galaxy.root
}workflows/import_workflow"></a></li></ul>`;
},
/** Template for workflow table */
_templateWorkflowTable: function() {
const tableHtml =
'<table class="table colored"><thead>' +
'<tr class="header">' +
"<th>Name</th>" +
"<th>Tags</th>" +
"<th>Owner</th>" +
"<th># of Steps</th>" +
"<th>Published</th>" +
"<th>Show in tools panel</th>" +
"</tr></thead>";
return `${tableHtml}<tbody class="workflow-search "><div class="hidden_description_layer"><p>Drop workflow files here to import</p></tbody></table></div>`;
},
/** Main template */
_templateHeader: function() {
return (
'<div class="page-container">' +
'<div class="user-workflows wf">' +
'<div class="response-message"></div>' +
"<h2>" +
_l("Your workflows") +
"</h2>" +
"</div>" +
"</div>"
);
}
});
const ImportWorkflowView = Backbone.View.extend({
initialize: function() {
this.setElement("<div/>");
this.render();
},
/** Open page to import workflow */
render: function() {
$.getJSON(`${Galaxy.root}workflow/upload_import_workflow`, options => {
this.$el.empty().append(this._mainTemplate(options));
});
},
/** Template for the import workflow page */
_mainTemplate: function(options) {
return `<div class='toolForm'><div class='toolFormTitle'>Import Galaxy workflow</div><div class='toolFormBody'><form name='import_workflow' id='import_workflow' action='${
Galaxy.root
}workflow/upload_import_workflow' enctype='multipart/form-data' method='POST'><div class='form-row'><label>Galaxy workflow URL:</label><input type='text' name='url' class='input-url' value='${
options.url
}' size='40'><div class='toolParamHelp' style='clear: both;'>If the workflow is accessible via a URL, enter the URL above and click <b>Import</b>.</div><div style='clear: both'></div></div><div class='form-row'><label>Galaxy workflow file:</label><div class='form-row-input'><input type='file' name='file_data' class='input-file'/></div><div class='toolParamHelp' style='clear: both;'>If the workflow is in a file on your computer, choose it and then click <b>Import</b>.</div><div style='clear: both'></div></div><div class='form-row'><input type='submit' class='primary-button wf-import' name='import_button' value='Import'></div></form><hr/><div class='form-row'><label>Import a Galaxy workflow from myExperiment:</label><div class='form-row-input'><a href='${
options.myexperiment_target_url
}'> Visit myExperiment</a></div><div class='toolParamHelp' style='clear: both;'>Click the link above to visit myExperiment and browse for Galaxy workflows.</div><div style='clear: both'></div></div></div></div>`;
}
});
export default {
View: WorkflowListView,
ImportWorkflowView: ImportWorkflowView
};