-
Notifications
You must be signed in to change notification settings - Fork 967
/
workflow.js
224 lines (204 loc) · 10.6 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
/** Workflow view */
define( [ 'mvc/tool/tool-form-composite' ], function( ToolForm ) {
/** View of the main workflow list page */
var View = Backbone.View.extend({
initialize: function() {
this.setElement( '<div/>' );
this.render();
},
render: function() {
var self = this,
min_query_length = 3;
$.getJSON( Galaxy.root + 'api/workflows/', function( workflows ) {
var $el_workflow = null;
// Add workflow header
self.$el.empty().append( self._templateHeader() );
// Add user actions message if any
self.build_messages( self );
$el_workflow = self.$el.find( '.user-workflows' );
// Add the actions buttons
$el_workflow.append( self._templateActionButtons() );
if( workflows.length > 0) {
$el_workflow.append( self._templateWorkflowTable( self, workflows) );
self.adjust_actiondropdown( $el_workflow );
// Register delete and run workflow events
_.each( workflows, function( wf ) {
self.confirm_delete( self, wf );
});
// Register search workflow event
self.search_workflow( self, self.$el.find( '.search-wf' ), self.$el.find( '.workflow-search tr' ), min_query_length );
}
else {
$el_workflow.append( self._templateNoWorkflow() );
}
});
},
/** Build messages after user action */
build_messages: function( self ) {
var $el_message = self.$el.find( '.response-message' ),
status = self.get_querystring( 'status' ),
message = self.get_querystring( 'message' );
if( message && message !== null && message !== "" ) {
$el_message.addClass( status + 'message' );
$el_message.html( '<p>' + message + '</p>' );
}
else {
$el_message.html("");
}
},
/** Add confirm box before removing/unsharing workflow */
confirm_delete: function( self, workflow ) {
var $el_wf_link = self.$el.find( '.link-confirm-' + workflow.id ),
$el_shared_wf_link = self.$el.find( '.link-confirm-shared-' + workflow.id );
$el_wf_link.click( function() {
return confirm( "Are you sure you want to delete workflow '" + workflow.name + "'?" );
});
$el_shared_wf_link.click( function() {
return confirm( "Are you sure you want to remove the shared workflow '" + workflow.name + "'?" );
});
},
/** Implement client side workflow search/filtering */
search_workflow: function( self, $el_searchinput, $el_tabletr, min_querylen ) {
$el_searchinput.on( 'keyup', function () {
var 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'
var 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();
}
});
},
/** Get querystrings from url */
get_querystring: function( key ) {
return decodeURIComponent( window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent( key ).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1") );
},
/** Ajust the position of dropdown with respect to table */
adjust_actiondropdown: function( $el ) {
$el.on( 'show.bs.dropdown', function () {
$el.css( "overflow", "inherit" );
});
$el.on( 'hide.bs.dropdown', function () {
$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 +'workflow/create">' +
'</a>' +
'</li>' +
'<li>' +
'<a class="action-button fa fa-upload wf-action" id="import-workflow" title="Upload or import workflow" href="'+ Galaxy.root +'workflow/import_workflow">' +
'</a>' +
'</li>' +
'</ul>';
},
/** Template for workflow table */
_templateWorkflowTable: function( self, workflows ) {
var tableHtml = "", trHtml = "";
tableHtml = tableHtml + '<table class="table colored"><thead>' +
'<tr class="header">' +
'<th class="wf-td">Name</th>' +
'<th class="wf-td">Owner</th>' +
'<th class="wf-td"># of Steps</th>' +
'<th class="wf-td">Published</th>' +
'</tr></thead>';
_.each( workflows, function( wf ) {
trHtml = trHtml + '<tr>' +
'<td class="wf-td wf-dpd">' +
'<div class="dropdown">' +
'<button class="menubutton" type="button" data-toggle="dropdown">' +
_.escape( wf.name ) + '<span class="caret"></span>' +
'</button>' +
self._templateActions( wf ) +
'</div>' +
'</td>' +
'<td>' + ( wf.owner === Galaxy.user.attributes.username ? "You" : wf.owner ) +'</td>' +
'<td class="wf-td">' + wf.number_of_steps + '</td>' +
'<td class="wf-td">' + ( wf.published ? "Yes" : "No" ) + '</td>' +
'</tr>';
});
return tableHtml + '<tbody class="workflow-search">' + trHtml + '</tbody></table>';
},
/** Template for user actions for workflows */
_templateActions: function( workflow ) {
if( workflow.owner === Galaxy.user.attributes.username ) {
return '<ul class="dropdown-menu action-dpd">' +
'<li><a href="'+ Galaxy.root +'workflow/editor?id='+ workflow.id +'">Edit</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/run?id='+ workflow.id +'">Run</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/sharing?id='+ workflow.id +'">Share or Download</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/copy?id='+ workflow.id +'">Copy</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/rename?id='+ workflow.id +'">Rename</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/display_by_id?id='+ workflow.id +'">View</a></li>' +
'<li><a class="link-confirm-'+ workflow.id +'" href="'+ Galaxy.root +'workflow/delete?id='+ workflow.id +'">Delete</a></li>' +
'</ul>';
}
else {
return '<ul class="dropdown-menu action-dpd">' +
'<li><a href="'+ Galaxy.root +'workflow/display_by_username_and_slug?username='+ workflow.owner +'&slug='+ workflow.slug +'">View</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/run?id='+ workflow.id +'">Run</a></li>' +
'<li><a href="'+ Galaxy.root +'workflow/copy?id='+ workflow.id +'">Copy</a></li>' +
'<li><a class="link-confirm-shared-'+ workflow.id +'" href="'+ Galaxy.root +'workflow/sharing?unshare_me=True&id='+ workflow.id +'">Remove</a></li>' +
'</ul>';
}
},
/** Main template */
_templateHeader: function() {
return '<div class="page-container">' +
'<div class="user-workflows wf">' +
'<div class="response-message"></div>' +
'<h2>Your workflows</h2>' +
'</div>'+
'<div class="other-options wf">' +
'<h2>Other options</h2>' +
'<a class="action-button fa fa-cog wf-action" href="'+ Galaxy.root +'workflow/configure_menu" title="Configure your workflow menu">' +
'<span>Configure your workflow menu</span>' +
'</a>' +
'</div>' +
'</div>';
}
});
var Run_Workflow_View = Backbone.View.extend({
initialize: function( options ) {
this.setElement( '<div/>' );
this.run_workflow( options );
},
/** Open workflow to run */
run_workflow: function() {
var self = this,
id = self.get_querystring( 'id' ),
url = Galaxy.root + 'workflow/run_workflow?id=' + id;
$.getJSON(url, function( response ) {
var wf_parsed = JSON.parse( JSON.stringify( response ) );
var form = new ToolForm.View( wf_parsed[0] );
self.$el.empty().append( form.$el );
});
},
/** Get querystrings from url */
get_querystring: function( key ) {
return decodeURIComponent( window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent( key ).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1") );
},
});
return {
View : View,
Run_Workflow_View : Run_Workflow_View
};
});