PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/afgane/galaxy-central
JavaScript | 211 lines | 158 code | 17 blank | 36 comment | 28 complexity | 773ad5e0d46d1ef542a8986c266e0ec2 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(
  9. mod_masthead,
  10. mod_baseMVC,
  11. mod_utils,
  12. mod_toastr,
  13. mod_library_model,
  14. mod_library_libraryrow_view
  15. ){
  16. var LibraryListView = Backbone.View.extend({
  17. el: '#libraries_element',
  18. events: {
  19. 'click .sort-libraries-link' : 'sort_clicked'
  20. },
  21. defaults: {
  22. page_count: null,
  23. show_page: null
  24. },
  25. /**
  26. * Initialize and fetch the libraries from server.
  27. * Async render afterwards.
  28. * @param {object} options an object with options
  29. */
  30. initialize : function( options ){
  31. this.options = _.defaults( this.options || {}, this.defaults, options );
  32. var that = this;
  33. this.modal = null;
  34. // map of library model ids to library views = cache
  35. this.rowViews = {};
  36. // collection of {Item}s
  37. this.collection = new mod_library_model.Libraries();
  38. this.collection.fetch({
  39. success: function(){
  40. that.render();
  41. },
  42. error: function( model, response ){
  43. if ( typeof response.responseJSON !== "undefined" ){
  44. mod_toastr.error( response.responseJSON.err_msg );
  45. } else {
  46. mod_toastr.error( 'An error ocurred.' );
  47. }
  48. }
  49. });
  50. },
  51. /**
  52. * Render the libraries table either from the object's own collection,
  53. * or from a given array of library models,
  54. * or render an empty list in case no data is given.
  55. */
  56. render: function ( options ) {
  57. this.options = _.extend( this.options, options );
  58. var template = this.templateLibraryList();
  59. var libraries_to_render = null;
  60. var models = null;
  61. $( ".tooltip" ).hide();
  62. if ( typeof options !== 'undefined' ){
  63. models = typeof options.models !== 'undefined' ? options.models : null;
  64. }
  65. if ( this.collection !== null && models === null ){
  66. this.sortLibraries();
  67. if ( Galaxy.libraries.preferences.get( 'with_deleted' ) ){
  68. libraries_to_render = this.collection.models;
  69. } else {
  70. libraries_to_render = this.collection.where( { deleted: false } );
  71. }
  72. } else if ( models !== null ){
  73. libraries_to_render = models;
  74. } else {
  75. libraries_to_render = [];
  76. }
  77. // pagination
  78. if ( this.options.show_page === null || this.options.show_page < 1 ){
  79. this.options.show_page = 1;
  80. }
  81. this.options.total_libraries_count = libraries_to_render.length
  82. var page_start = ( Galaxy.libraries.preferences.get( 'library_page_size' ) * ( this.options.show_page - 1 ) );
  83. this.options.page_count = Math.ceil( this.options.total_libraries_count / Galaxy.libraries.preferences.get( 'library_page_size' ) );
  84. if ( this.options.total_libraries_count > 0 && ( page_start < this.options.total_libraries_count ) ){
  85. libraries_to_render = libraries_to_render.slice( page_start, page_start + Galaxy.libraries.preferences.get( 'library_page_size' ) );
  86. this.options.libraries_shown = libraries_to_render.length;
  87. // User requests page with no libraries
  88. if ( Galaxy.libraries.preferences.get( 'library_page_size' ) * this.options.show_page > ( this.options.total_libraries_count + Galaxy.libraries.preferences.get( 'library_page_size' ) ) ){
  89. libraries_to_render = [];
  90. }
  91. this.$el.html( template({
  92. length: 1,
  93. order: Galaxy.libraries.preferences.get( 'sort_order' )
  94. }));
  95. Galaxy.libraries.libraryToolbarView.renderPaginator( this.options );
  96. this.renderRows( libraries_to_render );
  97. } else {
  98. this.$el.html( template({
  99. length: 0,
  100. order: Galaxy.libraries.preferences.get( 'sort_order' )
  101. }));
  102. Galaxy.libraries.libraryToolbarView.renderPaginator( this.options );
  103. }
  104. $( "#center [data-toggle]" ).tooltip();
  105. $( "#center" ).css( 'overflow','auto' );
  106. },
  107. /**
  108. * Render all given models as rows in the library list
  109. * @param {array} libraries_to_render array of library models to render
  110. */
  111. renderRows: function( libraries_to_render ){
  112. for ( var i = 0; i < libraries_to_render.length; i++ ) {
  113. var library = libraries_to_render[i];
  114. // search whether we have the item cached
  115. var cachedView = _.findWhere( this.rowViews, { id: library.get( 'id' ) } );
  116. if ( cachedView !== undefined && this instanceof Backbone.View ){
  117. cachedView.delegateEvents();
  118. this.$el.find( '#library_list_body' ).append( cachedView.el );
  119. } else {
  120. this.renderOne( { library: library } )
  121. }
  122. }
  123. },
  124. /**
  125. * Create a view for the given model and add it to the libraries view.
  126. * @param {Library} model of the view that will be rendered
  127. */
  128. renderOne: function( options ){
  129. var library = options.library;
  130. var rowView = new mod_library_libraryrow_view.LibraryRowView( library );
  131. this.$el.find( '#library_list_body' ).append( rowView.el );
  132. // save new rowView to cache
  133. this.rowViews[ library.get( 'id' ) ] = rowView;
  134. },
  135. /**
  136. * Table heading was clicked, update sorting preferences and re-render.
  137. * @return {[type]} [description]
  138. */
  139. sort_clicked : function(){
  140. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  141. Galaxy.libraries.preferences.set({'sort_order': 'desc'});
  142. } else {
  143. Galaxy.libraries.preferences.set({'sort_order': 'asc'});
  144. }
  145. this.render();
  146. },
  147. /**
  148. * Sort the underlying collection according to the parameters received.
  149. * Currently supports only sorting by name.
  150. */
  151. sortLibraries: function(){
  152. if (Galaxy.libraries.preferences.get('sort_by') === 'name'){
  153. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  154. this.collection.sortByNameAsc();
  155. } else if (Galaxy.libraries.preferences.get('sort_order') === 'desc'){
  156. this.collection.sortByNameDesc();
  157. }
  158. }
  159. },
  160. redirectToHome: function(){
  161. window.location = '../';
  162. },
  163. redirectToLogin: function(){
  164. window.location = '/user/login';
  165. },
  166. // MMMMMMMMMMMMMMMMMM
  167. // === TEMPLATES ====
  168. // MMMMMMMMMMMMMMMMMM
  169. templateLibraryList: function(){
  170. tmpl_array = [];
  171. tmpl_array.push('<div class="library_container table-responsive">');
  172. tmpl_array.push('<% if(length === 0) { %>');
  173. tmpl_array.push('<div>There are no libraries visible to you here. If you expected some to show up please consult the <a href="https://wiki.galaxyproject.org/Admin/DataLibraries/LibrarySecurity" target="_blank">library security wikipage</a> or visit the <a href="https://biostar.usegalaxy.org/" target="_blank">Galaxy support site</a>.</div>');
  174. tmpl_array.push('<% } else{ %>');
  175. tmpl_array.push('<table class="grid table table-condensed">');
  176. tmpl_array.push(' <thead>');
  177. 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>');
  178. tmpl_array.push(' <th style="width:22%;">description</th>');
  179. tmpl_array.push(' <th style="width:22%;">synopsis</th> ');
  180. tmpl_array.push(' <th style="width:26%;"></th>');
  181. tmpl_array.push(' </thead>');
  182. tmpl_array.push(' <tbody id="library_list_body">');
  183. // library item views will attach here
  184. tmpl_array.push(' </tbody>');
  185. tmpl_array.push('</table>');
  186. tmpl_array.push('<% }%>');
  187. tmpl_array.push('</div>');
  188. return _.template(tmpl_array.join(''));
  189. },
  190. });
  191. return {
  192. LibraryListView: LibraryListView
  193. };
  194. });