PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/mvc/tools/tools-select-content.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 242 lines | 172 code | 33 blank | 37 comment | 32 complexity | 1e795e110fa4ab7d65972019956e1c2d MD5 | raw file
Possible License(s): CC-BY-3.0
  1. // dependencies
  2. define(['utils/utils', 'mvc/ui/ui-misc', 'mvc/ui/ui-tabs', 'mvc/tools/tools-template'], function(Utils, Ui, Tabs, ToolTemplate) {
  3. var View = Backbone.View.extend({
  4. // initialize
  5. initialize : function(app, options) {
  6. // link app and options
  7. this.app = app;
  8. this.options = options;
  9. // link this
  10. var self = this;
  11. // add element
  12. this.setElement('<div/>');
  13. // list of select fields
  14. this.list = {};
  15. // radio button options
  16. var radio_buttons = [];
  17. // set initial state
  18. if (!options.multiple) {
  19. this.current = 'single';
  20. } else {
  21. this.current = 'multiple';
  22. }
  23. // add single dataset selector
  24. if (!options.multiple) {
  25. radio_buttons.push({icon: 'fa-file-o', label : 'Single dataset', value : 'single'});
  26. this.select_single = new Ui.Select.View({
  27. onchange : function() {
  28. self.trigger('change');
  29. }
  30. });
  31. this.list['single'] = {
  32. field: this.select_single,
  33. type : 'hda'
  34. };
  35. }
  36. // add multiple dataset selector
  37. radio_buttons.push({icon: 'fa-files-o', label : 'Multiple datasets', value : 'multiple' });
  38. this.select_multiple = new Ui.Select.View({
  39. multiple : true,
  40. onchange : function() {
  41. self.trigger('change');
  42. }
  43. });
  44. this.list['multiple'] = {
  45. field: this.select_multiple,
  46. type : 'hda'
  47. };
  48. // add collection selector
  49. radio_buttons.push({icon: 'fa-folder-o', label : 'List of datasets', value : 'collection' });
  50. this.select_collection = new Ui.Select.View({
  51. onchange : function() {
  52. self.trigger('change');
  53. }
  54. });
  55. this.list['collection'] = {
  56. field: this.select_collection,
  57. type : 'hdca'
  58. };
  59. // create button
  60. this.button_type = new Ui.RadioButton.View({
  61. value : this.current,
  62. data : radio_buttons,
  63. onchange: function(value) {
  64. self.current = value;
  65. self.refresh();
  66. self.trigger('change');
  67. }
  68. });
  69. // add batch mode information
  70. this.$batch = $(ToolTemplate.batchMode());
  71. // add elements to dom
  72. this.$el.append(Utils.wrap(this.button_type.$el));
  73. for (var i in this.list) {
  74. this.$el.append(this.list[i].field.$el);
  75. }
  76. this.$el.append(this.$batch);
  77. // update options
  78. this.update(options.data);
  79. // refresh view
  80. this.refresh();
  81. // add change event. fires on trigger
  82. this.on('change', function() {
  83. if (options.onchange) {
  84. options.onchange(self.value());
  85. }
  86. });
  87. },
  88. /** Indicate that select fields are being updated */
  89. wait: function() {
  90. for (var i in this.list) {
  91. this.list[i].field.wait();
  92. }
  93. },
  94. /** Indicate that the options update has been completed */
  95. unwait: function() {
  96. for (var i in this.list) {
  97. this.list[i].field.unwait();
  98. }
  99. },
  100. /** Update content selector */
  101. update: function(options) {
  102. // identify dataset options
  103. var dataset_options = [];
  104. for (var i in options.hda) {
  105. var hda = options.hda[i];
  106. dataset_options.push({
  107. label: hda.hid + ': ' + hda.name,
  108. value: hda.id
  109. });
  110. }
  111. // identify collection options
  112. var collection_options = [];
  113. for (var i in options.hdca) {
  114. var hdca = options.hdca[i];
  115. collection_options.push({
  116. label: hdca.hid + ': ' + hdca.name,
  117. value: hdca.id
  118. });
  119. }
  120. // update selection fields
  121. this.select_single && this.select_single.update(dataset_options);
  122. this.select_multiple.update(dataset_options);
  123. this.select_collection.update(collection_options);
  124. // add to content list
  125. this.app.content.add(options);
  126. },
  127. /** Return the currently selected dataset values */
  128. value : function (dict) {
  129. // update current value
  130. if (dict && dict.values) {
  131. try {
  132. // create list
  133. var list = [];
  134. for (var i in dict.values) {
  135. list.push(dict.values[i].id);
  136. }
  137. // identify suitable select field
  138. if (dict && dict.values.length > 0 && dict.values[0].src == 'hcda') {
  139. this.current = 'collection';
  140. this.select_collection.value(list[0]);
  141. } else {
  142. if (list.length > 1 || this.options.multiple) {
  143. this.current = 'multiple';
  144. this.select_multiple.value(list);
  145. } else {
  146. this.current = 'single';
  147. this.select_single.value(list[0]);
  148. }
  149. }
  150. this.refresh();
  151. // check if value has been set
  152. var select = this._select();
  153. if (!select.validate()) {
  154. select.value(select.first());
  155. }
  156. } catch (err) {
  157. console.debug('tools-select-content::value() - Skipped.');
  158. }
  159. }
  160. // transform into an array
  161. var id_list = this._select().value();
  162. if (!(id_list instanceof Array)) {
  163. id_list = [id_list];
  164. }
  165. // prepare result dict
  166. var result = {
  167. batch : !this.options.multiple,
  168. values : []
  169. }
  170. // append to dataset ids
  171. for (var i in id_list) {
  172. result.values.push({
  173. id : id_list[i],
  174. src : this.list[this.current].type
  175. });
  176. }
  177. // return
  178. return result;
  179. },
  180. /** Validate current selection
  181. */
  182. validate: function() {
  183. return this._select().validate();
  184. },
  185. /** Refreshes data selection view */
  186. refresh: function() {
  187. for (var i in this.list) {
  188. var $el = this.list[i].field.$el;
  189. if (this.current == i) {
  190. $el.show();
  191. } else {
  192. $el.hide();
  193. }
  194. }
  195. if (this.current != 'single' && !this.options.multiple) {
  196. this.$batch.show();
  197. } else {
  198. this.$batch.hide();
  199. }
  200. },
  201. /** Assists in selecting the current field */
  202. _select: function() {
  203. return this.list[this.current].field;
  204. }
  205. });
  206. return {
  207. View: View
  208. }
  209. });