PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/client/galaxy/scripts/mvc/library/library-librarylist-view.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 217 lines | 161 code | 20 blank | 36 comment | 32 complexity | 01f32f655ec892e121ddbedb287ab435 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. /**
  22. * Initialize and fetch the libraries from server.
  23. * Async render afterwards.
  24. * @param {object} options an options object
  25. */
  26. defaults: {
  27. page_count: null,
  28. show_page: null
  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. if ( ( this.options.page_size != null ) && ( this.options.page_size == parseInt( this.options.page_size ) ) ) {
  59. Galaxy.libraries.preferences.set( { 'library_page_size': parseInt( this.options.page_size ) } );
  60. }
  61. $( ".tooltip" ).hide();
  62. // this.options.show_page = this.options.show_page || 1;
  63. var template = this.templateLibraryList();
  64. var libraries_to_render = null;
  65. var models = null;
  66. if ( this.options.show_page === null || this.options.show_page < 1 ){
  67. this.options.show_page = 1;
  68. }
  69. if ( typeof options !== 'undefined' ){
  70. models = typeof options.models !== 'undefined' ? options.models : null;
  71. }
  72. if ( this.collection !== null && models === null ){
  73. this.sortLibraries();
  74. if ( Galaxy.libraries.preferences.get( 'with_deleted' ) ){
  75. libraries_to_render = this.collection.models;
  76. } else {
  77. libraries_to_render = this.collection.where( { deleted: false } );
  78. }
  79. } else if ( models !== null ){
  80. libraries_to_render = models;
  81. } else {
  82. libraries_to_render = [];
  83. }
  84. this.options.total_libraries_count = libraries_to_render.length
  85. var page_start = ( Galaxy.libraries.preferences.get( 'library_page_size' ) * ( this.options.show_page - 1 ) );
  86. this.options.page_count = Math.ceil( this.options.total_libraries_count / Galaxy.libraries.preferences.get( 'library_page_size' ) );
  87. if ( this.options.total_libraries_count > 0 && ( page_start < this.options.total_libraries_count ) ){
  88. libraries_to_render = libraries_to_render.slice( page_start, page_start + Galaxy.libraries.preferences.get( 'library_page_size' ) );
  89. this.options.libraries_shown = libraries_to_render.length;
  90. // User requests page with no libraries
  91. if ( Galaxy.libraries.preferences.get( 'library_page_size' ) * this.options.show_page > ( this.options.total_libraries_count + Galaxy.libraries.preferences.get( 'library_page_size' ) ) ){
  92. libraries_to_render = [];
  93. }
  94. this.$el.html( template({
  95. length: 1,
  96. order: Galaxy.libraries.preferences.get( 'sort_order' )
  97. }));
  98. Galaxy.libraries.libraryToolbarView.renderPaginator( this.options );
  99. this.renderRows( libraries_to_render );
  100. } else {
  101. this.$el.html( template({
  102. length: 0,
  103. order: Galaxy.libraries.preferences.get( 'sort_order' )
  104. }));
  105. Galaxy.libraries.libraryToolbarView.renderPaginator( this.options );
  106. }
  107. $( "#center [data-toggle]" ).tooltip();
  108. $( "#center" ).css( 'overflow','auto' );
  109. },
  110. /**
  111. * Render all given models as rows in the library list
  112. * @param {array} libraries_to_render array of library models to render
  113. */
  114. renderRows: function( libraries_to_render ){
  115. for ( var i = 0; i < libraries_to_render.length; i++ ) {
  116. var library = libraries_to_render[i];
  117. // search whether we have the item cached
  118. var cachedView = _.findWhere( this.rowViews, { id: library.get( 'id' ) } );
  119. if ( cachedView !== undefined && this instanceof Backbone.View ){
  120. cachedView.delegateEvents();
  121. this.$el.find( '#library_list_body' ).append( cachedView.el );
  122. } else {
  123. this.renderOne( { library: library } )
  124. }
  125. }
  126. },
  127. /**
  128. * Create a view for the given model and add it to the libraries view.
  129. * @param {Library} model of the view that will be rendered
  130. */
  131. renderOne: function( options ){
  132. var library = options.library;
  133. var rowView = new mod_library_libraryrow_view.LibraryRowView( library );
  134. this.$el.find( '#library_list_body' ).append( rowView.el );
  135. // save new rowView to cache
  136. this.rowViews[ library.get( 'id' ) ] = rowView;
  137. },
  138. /**
  139. * Table heading was clicked, update sorting preferences and re-render.
  140. * @return {[type]} [description]
  141. */
  142. sort_clicked : function(){
  143. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  144. Galaxy.libraries.preferences.set({'sort_order': 'desc'});
  145. } else {
  146. Galaxy.libraries.preferences.set({'sort_order': 'asc'});
  147. }
  148. this.render();
  149. },
  150. /**
  151. * Sort the underlying collection according to the parameters received.
  152. * Currently supports only sorting by name.
  153. */
  154. sortLibraries: function(){
  155. if (Galaxy.libraries.preferences.get('sort_by') === 'name'){
  156. if (Galaxy.libraries.preferences.get('sort_order') === 'asc'){
  157. this.collection.sortByNameAsc();
  158. } else if (Galaxy.libraries.preferences.get('sort_order') === 'desc'){
  159. this.collection.sortByNameDesc();
  160. }
  161. }
  162. },
  163. redirectToHome: function(){
  164. window.location = '../';
  165. },
  166. redirectToLogin: function(){
  167. window.location = '/user/login';
  168. },
  169. // MMMMMMMMMMMMMMMMMM
  170. // === TEMPLATES ====
  171. // MMMMMMMMMMMMMMMMMM
  172. templateLibraryList: function(){
  173. tmpl_array = [];
  174. tmpl_array.push('<div class="library_container table-responsive">');
  175. tmpl_array.push('<% if(length === 0) { %>');
  176. 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>');
  177. tmpl_array.push('<% } else{ %>');
  178. tmpl_array.push('<table class="grid table table-condensed">');
  179. tmpl_array.push(' <thead>');
  180. 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>');
  181. tmpl_array.push(' <th style="width:22%;">description</th>');
  182. tmpl_array.push(' <th style="width:22%;">synopsis</th> ');
  183. tmpl_array.push(' <th style="width:26%;"></th>');
  184. tmpl_array.push(' </thead>');
  185. tmpl_array.push(' <tbody id="library_list_body">');
  186. // library item views will attach here
  187. tmpl_array.push(' </tbody>');
  188. tmpl_array.push('</table>');
  189. tmpl_array.push('<% }%>');
  190. tmpl_array.push('</div>');
  191. return _.template(tmpl_array.join(''));
  192. },
  193. });
  194. return {
  195. LibraryListView: LibraryListView
  196. };
  197. });