PageRenderTime 59ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/mvc/library/library-librarylist-view.js

https://bitbucket.org/hbc/galaxy-central-hbc/
JavaScript | 185 lines | 137 code | 26 blank | 22 comment | 24 complexity | 226b5b6479964b5eac8bc777e1e4576b MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "galaxy.masthead",
  3. "mvc/base-mvc",
  4. "utils/utils",
  5. "libs/toastr",
  6. "mvc/library/library-model",
  7. "mvc/library/library-libraryrow-view"],
  8. function(mod_masthead,
  9. mod_baseMVC,
  10. mod_utils,
  11. mod_toastr,
  12. mod_library_model,
  13. mod_library_libraryrow_view) {
  14. var LibraryListView = Backbone.View.extend({
  15. el: '#libraries_element',
  16. events: {
  17. 'click .sort-libraries-link' : 'sort_clicked'
  18. },
  19. modal: null,
  20. // collection of {Item}s
  21. collection: null,
  22. // map of library model ids to library views = cache
  23. rowViews: {},
  24. initialize : function(options){
  25. this.options = _.defaults(this.options || {}, options);
  26. var viewContext = this;
  27. this.rowViews = {};
  28. this.collection = new mod_library_model.Libraries();
  29. this.collection.fetch({
  30. success: function(){
  31. viewContext.render();
  32. },
  33. error: function(model, response){
  34. if (typeof response.responseJSON !== "undefined"){
  35. mod_toastr.error(response.responseJSON.err_msg);
  36. } else {
  37. mod_toastr.error('An error ocurred :(');
  38. }
  39. }
  40. });
  41. },
  42. /** Renders the libraries table either from the object's own collection,
  43. * or from a given array of library models,
  44. * or renders an empty list in case no data is given. */
  45. render: function (options) {
  46. var template = this.templateLibraryList();
  47. var libraries_to_render = null;
  48. var include_deleted = Galaxy.libraries.preferences.get('with_deleted');
  49. var models = null;
  50. if (typeof options !== 'undefined'){
  51. include_deleted = typeof options.with_deleted !== 'undefined' ? options.with_deleted : false;
  52. models = typeof options.models !== 'undefined' ? options.models : null;
  53. }
  54. if (this.collection !== null && models === null){
  55. this.sortLibraries();
  56. if (include_deleted){ // show all the libraries
  57. libraries_to_render = this.collection.models;
  58. } else{ // show only undeleted libraries
  59. libraries_to_render = this.collection.where({deleted: false});
  60. }
  61. } else if (models !== null){
  62. libraries_to_render = models;
  63. } else {
  64. libraries_to_render = [];
  65. }
  66. this.$el.html(template({length: libraries_to_render.length, order: Galaxy.libraries.preferences.get('sort_order') }));
  67. if (libraries_to_render.length > 0){
  68. this.renderRows(libraries_to_render);
  69. }
  70. // initialize the library tooltips
  71. $("#center [data-toggle]").tooltip();
  72. // modification of upper DOM element to show scrollbars due
  73. // to the #center element inheritance
  74. $("#center").css('overflow','auto');
  75. },
  76. /** Renders all given models as rows in the library list */
  77. renderRows: function(libraries_to_render){
  78. for (var i = 0; i < libraries_to_render.length; i++) {
  79. var library = libraries_to_render[i];
  80. var cachedView = _.findWhere(this.rowViews, {id: library.get('id')});
  81. if (cachedView !== undefined && this instanceof Backbone.View){
  82. cachedView.delegateEvents();
  83. this.$el.find('#library_list_body').append(cachedView.el);
  84. } else {
  85. this.renderOne({library:library})
  86. }
  87. }
  88. },
  89. /**
  90. * Creates a view for the given model and adds it to the libraries view.
  91. * @param {Library} model of the view that will be rendered
  92. */
  93. renderOne: function(options){
  94. var library = options.library;
  95. var rowView = new mod_library_libraryrow_view.LibraryRowView(library);
  96. // we want to prepend new item
  97. if (options.prepend){
  98. this.$el.find('#library_list_body').prepend(rowView.el);
  99. } else {
  100. this.$el.find('#library_list_body').append(rowView.el);
  101. }
  102. // save new rowView to cache
  103. this.rowViews[library.get('id')] = rowView;
  104. },
  105. /** Table heading was clicked, update sorting preferences and re-render */
  106. sort_clicked : function(){
  107. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  108. Galaxy.libraries.preferences.set({'sort_order': 'desc'});
  109. } else {
  110. Galaxy.libraries.preferences.set({'sort_order': 'asc'});
  111. }
  112. this.render();
  113. },
  114. /** Sorts the underlying collection according to the parameters received.
  115. Currently supports only sorting by name. */
  116. sortLibraries: function(){
  117. if (Galaxy.libraries.preferences.get('sort_by') === 'name'){
  118. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  119. this.collection.sortByNameAsc();
  120. } else if (Galaxy.libraries.preferences.get('sort_order') === 'desc'){
  121. this.collection.sortByNameDesc();
  122. }
  123. }
  124. },
  125. // MMMMMMMMMMMMMMMMMM
  126. // === TEMPLATES ====
  127. // MMMMMMMMMMMMMMMMMM
  128. templateLibraryList: function(){
  129. tmpl_array = [];
  130. tmpl_array.push('<div class="library_container table-responsive">');
  131. tmpl_array.push('<% if(length === 0) { %>');
  132. tmpl_array.push('<div>There are no libraries visible to you. If you expected some to show up please consult the <a href="https://wiki.galaxyproject.org/Admin/DataLibraries/LibrarySecurity">library security wikipage</a> or visit the <a href="https://biostar.usegalaxy.org/">Galaxy support site</a>.</div>');
  133. tmpl_array.push('<% } else{ %>');
  134. tmpl_array.push('<table class="grid table table-condensed">');
  135. tmpl_array.push(' <thead>');
  136. tmpl_array.push(' <th style="width:30%;"><a class="sort-libraries-link" title="Click to reverse order" href="#">name</a> <span title="Sorted alphabetically" class="fa fa-sort-alpha-<%- order %>"></span></th>');
  137. tmpl_array.push(' <th style="width:22%;">description</th>');
  138. tmpl_array.push(' <th style="width:22%;">synopsis</th> ');
  139. tmpl_array.push(' <th style="width:26%;"></th> ');
  140. tmpl_array.push(' </thead>');
  141. tmpl_array.push(' <tbody id="library_list_body">');
  142. // library item views will attach here
  143. tmpl_array.push(' </tbody>');
  144. tmpl_array.push('</table>');
  145. tmpl_array.push('<% }%>');
  146. tmpl_array.push('</div>');
  147. return _.template(tmpl_array.join(''));
  148. },
  149. redirectToHome: function(){
  150. window.location = '../';
  151. },
  152. redirectToLogin: function(){
  153. window.location = '/user/login';
  154. },
  155. });
  156. return {
  157. LibraryListView: LibraryListView
  158. };
  159. });