PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/client/galaxy/scripts/mvc/tools.js

https://bitbucket.org/dan/galaxy-central
JavaScript | 776 lines | 514 code | 100 blank | 162 comment | 42 complexity | ec1f41d5b793e12160cf0278ea058dd2 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. /**
  2. * Model, view, and controller objects for Galaxy tools and tool panel.
  3. */
  4. define( ["libs/underscore", "viz/trackster/util", "mvc/data" ],
  5. function(_, util, data) {
  6. /**
  7. * Mixin for tracking model visibility.
  8. */
  9. var VisibilityMixin = {
  10. hidden: false,
  11. show: function() {
  12. this.set("hidden", false);
  13. },
  14. hide: function() {
  15. this.set("hidden", true);
  16. },
  17. toggle: function() {
  18. this.set("hidden", !this.get("hidden"));
  19. },
  20. is_visible: function() {
  21. return !this.attributes.hidden;
  22. }
  23. };
  24. /**
  25. * A tool parameter.
  26. */
  27. var ToolParameter = Backbone.Model.extend({
  28. defaults: {
  29. name: null,
  30. label: null,
  31. type: null,
  32. value: null,
  33. html: null,
  34. num_samples: 5
  35. },
  36. initialize: function(options) {
  37. this.attributes.html = unescape(this.attributes.html);
  38. },
  39. copy: function() {
  40. return new ToolParameter(this.toJSON());
  41. },
  42. set_value: function(value) {
  43. this.set('value', value || '');
  44. }
  45. });
  46. var ToolParameterCollection = Backbone.Collection.extend({
  47. model: ToolParameter
  48. });
  49. /**
  50. * A data tool parameter.
  51. */
  52. var DataToolParameter = ToolParameter.extend({});
  53. /**
  54. * An integer tool parameter.
  55. */
  56. var IntegerToolParameter = ToolParameter.extend({
  57. set_value: function(value) {
  58. this.set('value', parseInt(value, 10));
  59. },
  60. /**
  61. * Returns samples from a tool input.
  62. */
  63. get_samples: function() {
  64. return d3.scale.linear()
  65. .domain([this.get('min'), this.get('max')])
  66. .ticks(this.get('num_samples'));
  67. }
  68. });
  69. var FloatToolParameter = IntegerToolParameter.extend({
  70. set_value: function(value) {
  71. this.set('value', parseFloat(value));
  72. }
  73. });
  74. /**
  75. * A select tool parameter.
  76. */
  77. var SelectToolParameter = ToolParameter.extend({
  78. /**
  79. * Returns tool options.
  80. */
  81. get_samples: function() {
  82. return _.map(this.get('options'), function(option) {
  83. return option[0];
  84. });
  85. }
  86. });
  87. // Set up dictionary of parameter types.
  88. ToolParameter.subModelTypes = {
  89. 'integer': IntegerToolParameter,
  90. 'float': FloatToolParameter,
  91. 'data': DataToolParameter,
  92. 'select': SelectToolParameter
  93. };
  94. /**
  95. * A Galaxy tool.
  96. */
  97. var Tool = Backbone.Model.extend({
  98. // Default attributes.
  99. defaults: {
  100. id: null,
  101. name: null,
  102. description: null,
  103. target: null,
  104. inputs: [],
  105. outputs: []
  106. },
  107. urlRoot: galaxy_config.root + 'api/tools',
  108. initialize: function(options) {
  109. // Set parameters.
  110. this.set('inputs', new ToolParameterCollection(_.map(options.inputs, function(p) {
  111. var p_class = ToolParameter.subModelTypes[p.type] || ToolParameter;
  112. return new p_class(p);
  113. })));
  114. },
  115. /**
  116. *
  117. */
  118. toJSON: function() {
  119. var rval = Backbone.Model.prototype.toJSON.call(this);
  120. // Convert inputs to JSON manually.
  121. rval.inputs = this.get('inputs').map(function(i) { return i.toJSON(); });
  122. return rval;
  123. },
  124. /**
  125. * Removes inputs of a particular type; this is useful because not all inputs can be handled by
  126. * client and server yet.
  127. */
  128. remove_inputs: function(types) {
  129. var tool = this,
  130. incompatible_inputs = tool.get('inputs').filter( function(input) {
  131. return ( types.indexOf( input.get('type') ) !== -1);
  132. });
  133. tool.get('inputs').remove(incompatible_inputs);
  134. },
  135. /**
  136. * Returns object copy, optionally including only inputs that can be sampled.
  137. */
  138. copy: function(only_samplable_inputs) {
  139. var copy = new Tool(this.toJSON());
  140. // Return only samplable inputs if flag is set.
  141. if (only_samplable_inputs) {
  142. var valid_inputs = new Backbone.Collection();
  143. copy.get('inputs').each(function(input) {
  144. if (input.get_samples()) {
  145. valid_inputs.push(input);
  146. }
  147. });
  148. copy.set('inputs', valid_inputs);
  149. }
  150. return copy;
  151. },
  152. apply_search_results: function(results) {
  153. ( _.indexOf(results, this.attributes.id) !== -1 ? this.show() : this.hide() );
  154. return this.is_visible();
  155. },
  156. /**
  157. * Set a tool input's value.
  158. */
  159. set_input_value: function(name, value) {
  160. this.get('inputs').find(function(input) {
  161. return input.get('name') === name;
  162. }).set('value', value);
  163. },
  164. /**
  165. * Set many input values at once.
  166. */
  167. set_input_values: function(inputs_dict) {
  168. var self = this;
  169. _.each(_.keys(inputs_dict), function(input_name) {
  170. self.set_input_value(input_name, inputs_dict[input_name]);
  171. });
  172. },
  173. /**
  174. * Run tool; returns a Deferred that resolves to the tool's output(s).
  175. */
  176. run: function() {
  177. return this._run();
  178. },
  179. /**
  180. * Rerun tool using regions and a target dataset.
  181. */
  182. rerun: function(target_dataset, regions) {
  183. return this._run({
  184. action: 'rerun',
  185. target_dataset_id: target_dataset.id,
  186. regions: regions
  187. });
  188. },
  189. /**
  190. * Returns input dict for tool's inputs.
  191. */
  192. get_inputs_dict: function() {
  193. var input_dict = {};
  194. this.get('inputs').each(function(input) {
  195. input_dict[input.get('name')] = input.get('value');
  196. });
  197. return input_dict;
  198. },
  199. /**
  200. * Run tool; returns a Deferred that resolves to the tool's output(s).
  201. * NOTE: this method is a helper method and should not be called directly.
  202. */
  203. _run: function(additional_params) {
  204. // Create payload.
  205. var payload = _.extend({
  206. tool_id: this.id,
  207. inputs: this.get_inputs_dict()
  208. }, additional_params);
  209. // Because job may require indexing datasets, use server-side
  210. // deferred to ensure that job is run. Also use deferred that
  211. // resolves to outputs from tool.
  212. var run_deferred = $.Deferred(),
  213. ss_deferred = new util.ServerStateDeferred({
  214. ajax_settings: {
  215. url: this.urlRoot,
  216. data: JSON.stringify(payload),
  217. dataType: "json",
  218. contentType: 'application/json',
  219. type: "POST"
  220. },
  221. interval: 2000,
  222. success_fn: function(response) {
  223. return response !== "pending";
  224. }
  225. });
  226. // Run job and resolve run_deferred to tool outputs.
  227. $.when(ss_deferred.go()).then(function(result) {
  228. run_deferred.resolve(new data.DatasetCollection().reset(result));
  229. });
  230. return run_deferred;
  231. }
  232. });
  233. _.extend(Tool.prototype, VisibilityMixin);
  234. /**
  235. * Tool view.
  236. */
  237. var ToolView = Backbone.View.extend({
  238. });
  239. /**
  240. * Wrap collection of tools for fast access/manipulation.
  241. */
  242. var ToolCollection = Backbone.Collection.extend({
  243. model: Tool
  244. });
  245. /**
  246. * Label or section header in tool panel.
  247. */
  248. var ToolSectionLabel = Backbone.Model.extend(VisibilityMixin);
  249. /**
  250. * Section of tool panel with elements (labels and tools).
  251. */
  252. var ToolSection = Backbone.Model.extend({
  253. defaults: {
  254. elems: [],
  255. open: false
  256. },
  257. clear_search_results: function() {
  258. _.each(this.attributes.elems, function(elt) {
  259. elt.show();
  260. });
  261. this.show();
  262. this.set("open", false);
  263. },
  264. apply_search_results: function(results) {
  265. var all_hidden = true,
  266. cur_label;
  267. _.each(this.attributes.elems, function(elt) {
  268. if (elt instanceof ToolSectionLabel) {
  269. cur_label = elt;
  270. cur_label.hide();
  271. }
  272. else if (elt instanceof Tool) {
  273. if (elt.apply_search_results(results)) {
  274. all_hidden = false;
  275. if (cur_label) {
  276. cur_label.show();
  277. }
  278. }
  279. }
  280. });
  281. if (all_hidden) {
  282. this.hide();
  283. }
  284. else {
  285. this.show();
  286. this.set("open", true);
  287. }
  288. }
  289. });
  290. _.extend(ToolSection.prototype, VisibilityMixin);
  291. /**
  292. * Tool search that updates results when query is changed. Result value of null
  293. * indicates that query was not run; if not null, results are from search using
  294. * query.
  295. */
  296. var ToolSearch = Backbone.Model.extend({
  297. defaults: {
  298. search_hint_string: "search tools",
  299. min_chars_for_search: 3,
  300. spinner_url: "",
  301. clear_btn_url: "",
  302. search_url: "",
  303. visible: true,
  304. query: "",
  305. results: null,
  306. // ESC (27) will clear the input field and tool search filters
  307. clear_key: 27
  308. },
  309. initialize: function() {
  310. this.on("change:query", this.do_search);
  311. },
  312. /**
  313. * Do the search and update the results.
  314. */
  315. do_search: function() {
  316. var query = this.attributes.query;
  317. // If query is too short, do not search.
  318. if (query.length < this.attributes.min_chars_for_search) {
  319. this.set("results", null);
  320. return;
  321. }
  322. // Do search via AJAX.
  323. var q = query + '*';
  324. // Stop previous ajax-request
  325. if (this.timer) {
  326. clearTimeout(this.timer);
  327. }
  328. // Start a new ajax-request in X ms
  329. $("#search-clear-btn").hide();
  330. $("#search-spinner").show();
  331. var self = this;
  332. this.timer = setTimeout(function () {
  333. $.get(self.attributes.search_url, { query: q }, function (data) {
  334. self.set("results", data);
  335. $("#search-spinner").hide();
  336. $("#search-clear-btn").show();
  337. }, "json" );
  338. }, 200 );
  339. },
  340. clear_search: function() {
  341. this.set("query", "");
  342. this.set("results", null);
  343. }
  344. });
  345. _.extend(ToolSearch.prototype, VisibilityMixin);
  346. /**
  347. * Tool Panel.
  348. */
  349. var ToolPanel = Backbone.Model.extend({
  350. initialize: function(options) {
  351. this.attributes.tool_search = options.tool_search;
  352. this.attributes.tool_search.on("change:results", this.apply_search_results, this);
  353. this.attributes.tools = options.tools;
  354. this.attributes.layout = new Backbone.Collection( this.parse(options.layout) );
  355. },
  356. /**
  357. * Parse tool panel dictionary and return collection of tool panel elements.
  358. */
  359. parse: function(response) {
  360. // Recursive function to parse tool panel elements.
  361. var self = this,
  362. // Helper to recursively parse tool panel.
  363. parse_elt = function(elt_dict) {
  364. var type = elt_dict.model_class;
  365. // There are many types of tools; for now, anything that ends in 'Tool'
  366. // is treated as a generic tool.
  367. if ( type.indexOf('Tool') === type.length - 4 ) {
  368. return self.attributes.tools.get(elt_dict.id);
  369. }
  370. else if (type === 'ToolSection') {
  371. // Parse elements.
  372. var elems = _.map(elt_dict.elems, parse_elt);
  373. elt_dict.elems = elems;
  374. return new ToolSection(elt_dict);
  375. }
  376. else if (type === 'ToolSectionLabel') {
  377. return new ToolSectionLabel(elt_dict);
  378. }
  379. };
  380. return _.map(response, parse_elt);
  381. },
  382. clear_search_results: function() {
  383. this.get('layout').each(function(panel_elt) {
  384. if (panel_elt instanceof ToolSection) {
  385. panel_elt.clear_search_results();
  386. }
  387. else {
  388. // Label or tool, so just show.
  389. panel_elt.show();
  390. }
  391. });
  392. },
  393. apply_search_results: function() {
  394. var results = this.get('tool_search').get('results');
  395. if (results === null) {
  396. this.clear_search_results();
  397. return;
  398. }
  399. var cur_label = null;
  400. this.get('layout').each(function(panel_elt) {
  401. if (panel_elt instanceof ToolSectionLabel) {
  402. cur_label = panel_elt;
  403. cur_label.hide();
  404. }
  405. else if (panel_elt instanceof Tool) {
  406. if (panel_elt.apply_search_results(results)) {
  407. if (cur_label) {
  408. cur_label.show();
  409. }
  410. }
  411. }
  412. else {
  413. // Starting new section, so clear current label.
  414. cur_label = null;
  415. panel_elt.apply_search_results(results);
  416. }
  417. });
  418. }
  419. });
  420. /**
  421. * View classes for Galaxy tools and tool panel.
  422. *
  423. * Views use precompiled Handlebars templates for rendering. Views update as needed
  424. * based on (a) model/collection events and (b) user interactions; in this sense,
  425. * they are controllers are well and the HTML is the real view in the MVC architecture.
  426. */
  427. /**
  428. * Base view that handles visibility based on model's hidden attribute.
  429. */
  430. var BaseView = Backbone.View.extend({
  431. initialize: function() {
  432. this.model.on("change:hidden", this.update_visible, this);
  433. this.update_visible();
  434. },
  435. update_visible: function() {
  436. ( this.model.attributes.hidden ? this.$el.hide() : this.$el.show() );
  437. }
  438. });
  439. /**
  440. * Link to a tool.
  441. */
  442. var ToolLinkView = BaseView.extend({
  443. tagName: 'div',
  444. render: function() {
  445. // create element
  446. var $link = $('<div/>');
  447. $link.append(Handlebars.templates.tool_link(this.model.toJSON()));
  448. // open upload dialog for upload tool
  449. if (this.model.id === 'upload1') {
  450. $link.find('a').on('click', function(e) {
  451. e.preventDefault();
  452. Galaxy.upload.show();
  453. });
  454. }
  455. // add element
  456. this.$el.append($link);
  457. return this;
  458. }
  459. });
  460. /**
  461. * Panel label/section header.
  462. */
  463. var ToolSectionLabelView = BaseView.extend({
  464. tagName: 'div',
  465. className: 'toolPanelLabel',
  466. render: function() {
  467. this.$el.append( $("<span/>").text(this.model.attributes.text) );
  468. return this;
  469. }
  470. });
  471. /**
  472. * Panel section.
  473. */
  474. var ToolSectionView = BaseView.extend({
  475. tagName: 'div',
  476. className: 'toolSectionWrapper',
  477. initialize: function() {
  478. BaseView.prototype.initialize.call(this);
  479. this.model.on("change:open", this.update_open, this);
  480. },
  481. render: function() {
  482. // Build using template.
  483. this.$el.append( Handlebars.templates.panel_section(this.model.toJSON()) );
  484. // Add tools to section.
  485. var section_body = this.$el.find(".toolSectionBody");
  486. _.each(this.model.attributes.elems, function(elt) {
  487. if (elt instanceof Tool) {
  488. var tool_view = new ToolLinkView({model: elt, className: "toolTitle"});
  489. tool_view.render();
  490. section_body.append(tool_view.$el);
  491. }
  492. else if (elt instanceof ToolSectionLabel) {
  493. var label_view = new ToolSectionLabelView({model: elt});
  494. label_view.render();
  495. section_body.append(label_view.$el);
  496. }
  497. else {
  498. // TODO: handle nested section bodies?
  499. }
  500. });
  501. return this;
  502. },
  503. events: {
  504. 'click .toolSectionTitle > a': 'toggle'
  505. },
  506. /**
  507. * Toggle visibility of tool section.
  508. */
  509. toggle: function() {
  510. this.model.set("open", !this.model.attributes.open);
  511. },
  512. /**
  513. * Update whether section is open or close.
  514. */
  515. update_open: function() {
  516. (this.model.attributes.open ?
  517. this.$el.children(".toolSectionBody").slideDown("fast") :
  518. this.$el.children(".toolSectionBody").slideUp("fast")
  519. );
  520. }
  521. });
  522. var ToolSearchView = Backbone.View.extend({
  523. tagName: 'div',
  524. id: 'tool-search',
  525. className: 'bar',
  526. events: {
  527. 'click': 'focus_and_select',
  528. 'keyup :input': 'query_changed',
  529. 'click #search-clear-btn': 'clear'
  530. },
  531. render: function() {
  532. this.$el.append( Handlebars.templates.tool_search(this.model.toJSON()) );
  533. if (!this.model.is_visible()) {
  534. this.$el.hide();
  535. }
  536. this.$el.find('[title]').tooltip();
  537. return this;
  538. },
  539. focus_and_select: function() {
  540. this.$el.find(":input").focus().select();
  541. },
  542. clear: function() {
  543. this.model.clear_search();
  544. this.$el.find(":input").val(this.model.attributes.search_hint_string);
  545. this.focus_and_select();
  546. return false;
  547. },
  548. query_changed: function( evData ) {
  549. // check for the 'clear key' (ESC) first
  550. if( ( this.model.attributes.clear_key ) &&
  551. ( this.model.attributes.clear_key === evData.which ) ){
  552. this.clear();
  553. return false;
  554. }
  555. this.model.set("query", this.$el.find(":input").val());
  556. }
  557. });
  558. /**
  559. * Tool panel view. Events triggered include:
  560. * tool_link_click(click event, tool_model)
  561. */
  562. var ToolPanelView = Backbone.View.extend({
  563. tagName: 'div',
  564. className: 'toolMenu',
  565. /**
  566. * Set up view.
  567. */
  568. initialize: function() {
  569. this.model.get('tool_search').on("change:results", this.handle_search_results, this);
  570. },
  571. render: function() {
  572. var self = this;
  573. // Render search.
  574. var search_view = new ToolSearchView( { model: this.model.get('tool_search') } );
  575. search_view.render();
  576. self.$el.append(search_view.$el);
  577. // Render panel.
  578. this.model.get('layout').each(function(panel_elt) {
  579. if (panel_elt instanceof ToolSection) {
  580. var section_title_view = new ToolSectionView({model: panel_elt});
  581. section_title_view.render();
  582. self.$el.append(section_title_view.$el);
  583. }
  584. else if (panel_elt instanceof Tool) {
  585. var tool_view = new ToolLinkView({model: panel_elt, className: "toolTitleNoSection"});
  586. tool_view.render();
  587. self.$el.append(tool_view.$el);
  588. }
  589. else if (panel_elt instanceof ToolSectionLabel) {
  590. var label_view = new ToolSectionLabelView({model: panel_elt});
  591. label_view.render();
  592. self.$el.append(label_view.$el);
  593. }
  594. });
  595. // Setup tool link click eventing.
  596. self.$el.find("a.tool-link").click(function(e) {
  597. // Tool id is always the first class.
  598. var
  599. tool_id = $(this).attr('class').split(/\s+/)[0],
  600. tool = self.model.get('tools').get(tool_id);
  601. self.trigger("tool_link_click", e, tool);
  602. });
  603. return this;
  604. },
  605. handle_search_results: function() {
  606. var results = this.model.get('tool_search').get('results');
  607. if (results && results.length === 0) {
  608. $("#search-no-results").show();
  609. }
  610. else {
  611. $("#search-no-results").hide();
  612. }
  613. }
  614. });
  615. /**
  616. * View for working with a tool: setting parameters and inputs and executing the tool.
  617. */
  618. var ToolFormView = Backbone.View.extend({
  619. className: 'toolForm',
  620. render: function() {
  621. this.$el.children().remove();
  622. this.$el.append( Handlebars.templates.tool_form(this.model.toJSON()) );
  623. }
  624. });
  625. /**
  626. * Integrated tool menu + tool execution.
  627. */
  628. var IntegratedToolMenuAndView = Backbone.View.extend({
  629. className: 'toolMenuAndView',
  630. initialize: function() {
  631. this.tool_panel_view = new ToolPanelView({collection: this.collection});
  632. this.tool_form_view = new ToolFormView();
  633. },
  634. render: function() {
  635. // Render and append tool panel.
  636. this.tool_panel_view.render();
  637. this.tool_panel_view.$el.css("float", "left");
  638. this.$el.append(this.tool_panel_view.$el);
  639. // Append tool form view.
  640. this.tool_form_view.$el.hide();
  641. this.$el.append(this.tool_form_view.$el);
  642. // On tool link click, show tool.
  643. var self = this;
  644. this.tool_panel_view.on("tool_link_click", function(e, tool) {
  645. // Prevents click from activating link:
  646. e.preventDefault();
  647. // Show tool that was clicked on:
  648. self.show_tool(tool);
  649. });
  650. },
  651. /**
  652. * Fetch and display tool.
  653. */
  654. show_tool: function(tool) {
  655. var self = this;
  656. tool.fetch().done( function() {
  657. self.tool_form_view.model = tool;
  658. self.tool_form_view.render();
  659. self.tool_form_view.$el.show();
  660. $('#left').width("650px");
  661. });
  662. }
  663. });
  664. // Exports
  665. return {
  666. ToolParameter: ToolParameter,
  667. IntegerToolParameter: IntegerToolParameter,
  668. SelectToolParameter: SelectToolParameter,
  669. Tool: Tool,
  670. ToolCollection: ToolCollection,
  671. ToolSearch: ToolSearch,
  672. ToolPanel: ToolPanel,
  673. ToolPanelView: ToolPanelView,
  674. ToolFormView: ToolFormView
  675. };
  676. });