-
Notifications
You must be signed in to change notification settings - Fork 967
/
tool-form-base.js
269 lines (253 loc) · 9.32 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
/**
This is the base class of the tool form plugin. This class is e.g. inherited by the regular and the workflow tool form.
*/
import Utils from "utils/utils";
import Deferred from "utils/deferred";
import Ui from "mvc/ui/ui-misc";
import FormBase from "mvc/form/form-view";
import Citations from "components/citations.vue";
import Vue from "libs/vue";
export default FormBase.extend({
initialize: function(options) {
var self = this;
this.deferred = new Deferred();
FormBase.prototype.initialize.call(this, options);
// optional model update
this._update(this.model.get("initialmodel"));
// listen to history panel
if (this.model.get("listen_to_history") && parent.Galaxy && parent.Galaxy.currHistoryPanel) {
this.listenTo(parent.Galaxy.currHistoryPanel.collection, "change", () => {
self.model.get("onchange")();
});
}
// destroy dom elements
this.$el.on("remove", () => {
self._destroy();
});
},
/** Allows tool form variation to update tool model */
_update: function(callback) {
var self = this;
callback = callback || this.model.get("buildmodel");
if (callback) {
this.deferred.reset();
this.deferred.execute(process => {
callback(process, self);
process.then(() => {
self._render();
});
});
} else {
this._render();
}
},
/** Wait for deferred build processes before removal */
_destroy: function() {
var self = this;
this.$el.off().hide();
this.deferred.execute(() => {
FormBase.prototype.remove.call(self);
Galaxy.emit.debug("tool-form-base::_destroy()", "Destroy view.");
});
},
/** Build form */
_render: function() {
var self = this;
var options = this.model.attributes;
this.model.set({
title:
options.fixed_title ||
`<b>${options.name}</b> ${options.description} (Galaxy Version ${options.version})`,
operations: !options.hide_operations && this._operations(),
onchange: function() {
self.deferred.reset();
self.deferred.execute(process => {
self.model.get("postchange")(process, self);
});
}
});
this.render();
if (!this.model.get("collapsible")) {
this.$el.append(
$("<div/>")
.addClass("ui-margin-top-large")
.append(this._footer())
);
}
this.show_message &&
this.message.update({
status: "success",
message: `Now you are using '${options.name}' version ${options.version}, id '${options.id}'.`,
persistent: false
});
this.show_message = true;
},
/** Create tool operation menu */
_operations: function() {
var self = this;
var options = this.model.attributes;
// 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)
self.model.set("id", options.id.replace(options.version, this.version));
self.model.set("version", this.version);
self._update();
}
});
}
}
} 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?",
onclick: function() {
window.open(`${options.biostar_url}/p/new/post/`);
}
});
menu_button.addMenu({
icon: "fa-search",
title: "Search",
onclick: function() {
window.open(`${options.biostar_url}/local/search/page/?q=${options.name}`);
}
});
}
menu_button.addMenu({
icon: "fa-share",
title: "Share",
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",
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",
onclick: function() {
if (!this.requirements_visible || self.portlet.collapsed) {
this.requirements_visible = true;
self.portlet.expand();
self.message.update({
persistent: true,
message: self._templateRequirements(options),
status: "info"
});
} else {
this.requirements_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",
onclick: function() {
window.open(options.sharable_url);
}
});
}
// add tool menu webhooks
$.getJSON("/api/webhooks/tool-menu/all", webhooks => {
_.each(webhooks, webhook => {
if (webhook.activate && webhook.config.function) {
menu_button.addMenu({
icon: webhook.config.icon,
title: webhook.config.title,
onclick: function() {
var func = new Function("options", webhook.config.function);
func(options);
}
});
}
});
});
return {
menu: menu_button,
versions: versions_button
};
},
/** Create footer */
_footer: function() {
var options = this.model.attributes;
var $el = $("<div/>").append(this._templateHelp(options));
if (options.citations) {
var citationInstance = Vue.extend(Citations);
var vm = document.createElement('div');
$el.append(vm);
new citationInstance({
propsData: {
id: options.id,
source: 'tools'}})
.$mount(vm);
}
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, (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://galaxyproject.org/tools/requirements/")
.text("here");
return $("<span/>")
.append(`${requirements_message}. Click `)
.append(requirements_link)
.append(" for more information.");
}
return "No requirements found.";
}
});