PageRenderTime 69ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 283 lines | 233 code | 28 blank | 22 comment | 45 complexity | 5e912f9a78926acfeab81cf8e8ba4069 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. // dependencies
  2. define([
  3. "galaxy.masthead",
  4. "utils/utils",
  5. "libs/toastr"],
  6. function(mod_masthead,
  7. mod_utils,
  8. mod_toastr) {
  9. // galaxy library row view
  10. var LibraryRowView = Backbone.View.extend({
  11. events: {
  12. 'click .edit_library_btn' : 'edit_button_clicked',
  13. 'click .cancel_library_btn' : 'cancel_library_modification',
  14. 'click .save_library_btn' : 'save_library_modification',
  15. 'click .delete_library_btn' : 'delete_library',
  16. 'click .undelete_library_btn' : 'undelete_library'
  17. },
  18. edit_mode: false,
  19. element_visibility_config: {
  20. upload_library_btn: false,
  21. edit_library_btn: false,
  22. permission_library_btn: false,
  23. save_library_btn: false,
  24. cancel_library_btn: false,
  25. delete_library_btn: false,
  26. undelete_library_btn: false
  27. },
  28. initialize : function(library){
  29. this.render(library);
  30. },
  31. render: function(library){
  32. if (typeof library === 'undefined'){
  33. library = Galaxy.libraries.libraryListView.collection.get(this.$el.data('id'));
  34. }
  35. this.prepareButtons(library);
  36. var tmpl = this.templateRow();
  37. this.setElement(tmpl({library:library, button_config: this.element_visibility_config, edit_mode: this.edit_mode}));
  38. this.$el.show();
  39. return this;
  40. },
  41. repaint: function(library){
  42. /* need to hide manually because of the element removal in setElement
  43. invoked in render() */
  44. $(".tooltip").hide();
  45. /* we need to store the old element to be able to replace it with
  46. new one */
  47. var old_element = this.$el;
  48. /* if user canceled the library param is undefined,
  49. if user saved and succeeded the updated library is rendered */
  50. this.render(library);
  51. old_element.replaceWith(this.$el);
  52. /* now we attach new tooltips to the newly created row element */
  53. this.$el.find("[data-toggle]").tooltip();
  54. },
  55. /**
  56. * Function modifies the visibility of buttons for
  57. * the filling of the row template of given library.
  58. */
  59. prepareButtons: function(library){
  60. vis_config = this.element_visibility_config;
  61. if (this.edit_mode === false){
  62. vis_config.save_library_btn = false;
  63. vis_config.cancel_library_btn = false;
  64. vis_config.delete_library_btn = false;
  65. if (library.get('deleted') === true ){
  66. vis_config.undelete_library_btn = true;
  67. vis_config.upload_library_btn = false;
  68. vis_config.edit_library_btn = false;
  69. vis_config.permission_library_btn = false;
  70. } else if (library.get('deleted') === false ) {
  71. vis_config.save_library_btn = false;
  72. vis_config.cancel_library_btn = false;
  73. vis_config.undelete_library_btn = false;
  74. if (library.get('can_user_add') === true){
  75. vis_config.upload_library_btn = true;
  76. }
  77. if (library.get('can_user_modify') === true){
  78. vis_config.edit_library_btn = true;
  79. }
  80. if (library.get('can_user_manage') === true){
  81. vis_config.permission_library_btn = true;
  82. }
  83. }
  84. } else if (this.edit_mode === true){
  85. vis_config.upload_library_btn = false;
  86. vis_config.edit_library_btn = false;
  87. vis_config.permission_library_btn = false;
  88. vis_config.save_library_btn = true;
  89. vis_config.cancel_library_btn = true;
  90. vis_config.delete_library_btn = true;
  91. vis_config.undelete_library_btn = false;
  92. }
  93. this.element_visibility_config = vis_config;
  94. },
  95. /* User clicked the 'edit' button on row so we render a new row
  96. that allows editing */
  97. edit_button_clicked: function(){
  98. this.edit_mode = true;
  99. this.repaint();
  100. },
  101. /* User clicked the 'cancel' button so we render normal rowView */
  102. cancel_library_modification: function(){
  103. // mod_toastr.info('Modifications canceled');
  104. this.edit_mode = false;
  105. this.repaint();
  106. },
  107. save_library_modification: function(){
  108. var library = Galaxy.libraries.libraryListView.collection.get(this.$el.data('id'));
  109. var is_changed = false;
  110. var new_name = this.$el.find('.input_library_name').val();
  111. if (typeof new_name !== 'undefined' && new_name !== library.get('name') ){
  112. if (new_name.length > 2){
  113. library.set("name", new_name);
  114. is_changed = true;
  115. } else{
  116. mod_toastr.warning('Library name has to be at least 3 characters long.');
  117. return;
  118. }
  119. }
  120. var new_description = this.$el.find('.input_library_description').val();
  121. if (typeof new_description !== 'undefined' && new_description !== library.get('description') ){
  122. library.set("description", new_description);
  123. is_changed = true;
  124. }
  125. var new_synopsis = this.$el.find('.input_library_synopsis').val();
  126. if (typeof new_synopsis !== 'undefined' && new_synopsis !== library.get('synopsis') ){
  127. library.set("synopsis", new_synopsis);
  128. is_changed = true;
  129. }
  130. if (is_changed){
  131. var row_view = this;
  132. library.save(null, {
  133. patch: true,
  134. success: function(library) {
  135. row_view.edit_mode = false;
  136. row_view.repaint(library);
  137. mod_toastr.success('Changes to library saved.');
  138. },
  139. error: function(model, response){
  140. if (typeof response.responseJSON !== "undefined"){
  141. mod_toastr.error(response.responseJSON.err_msg);
  142. } else {
  143. mod_toastr.error('An error occured while attempting to update the library.');
  144. }
  145. }
  146. });
  147. } else {
  148. this.edit_mode = false;
  149. this.repaint(library);
  150. mod_toastr.info('Nothing has changed.');
  151. }
  152. },
  153. delete_library: function(){
  154. var library = Galaxy.libraries.libraryListView.collection.get(this.$el.data('id'));
  155. var row_view = this;
  156. // mark the library deleted
  157. library.destroy({
  158. success: function (library) {
  159. library.set('deleted', true);
  160. // add the new deleted library back to the collection (Galaxy specialty)
  161. Galaxy.libraries.libraryListView.collection.add(library);
  162. row_view.edit_mode = false;
  163. if (Galaxy.libraries.preferences.get('with_deleted') === false){
  164. $('.tooltip').hide();
  165. row_view.repaint(library);
  166. row_view.$el.remove();
  167. } else if (Galaxy.libraries.preferences.get('with_deleted') === true){
  168. row_view.repaint(library);
  169. }
  170. mod_toastr.success('Library has been marked deleted.');
  171. },
  172. error: function(model, response){
  173. if (typeof response.responseJSON !== "undefined"){
  174. mod_toastr.error(response.responseJSON.err_msg);
  175. } else {
  176. mod_toastr.error('An error occured during deleting the library.');
  177. }
  178. }
  179. });
  180. },
  181. undelete_library: function(){
  182. var library = Galaxy.libraries.libraryListView.collection.get(this.$el.data('id'));
  183. var row_view = this;
  184. // mark the library undeleted
  185. library.url = library.urlRoot + library.id + '?undelete=true';
  186. library.destroy({
  187. success: function (library) {
  188. // add the newly undeleted library back to the collection
  189. // backbone does not accept changes through destroy, so update it too
  190. library.set('deleted', false);
  191. Galaxy.libraries.libraryListView.collection.add(library);
  192. row_view.edit_mode = false;
  193. row_view.repaint(library);
  194. mod_toastr.success('Library has been undeleted.');
  195. },
  196. error: function(model, response){
  197. if (typeof response.responseJSON !== "undefined"){
  198. mod_toastr.error(response.responseJSON.err_msg);
  199. } else {
  200. mod_toastr.error('An error occured while undeleting the library.');
  201. }
  202. }
  203. });
  204. },
  205. templateRow: function() {
  206. tmpl_array = [];
  207. tmpl_array.push(' <tr class="<% if(library.get("deleted") === true) { print("active") } %>" style="display:none;" data-id="<%- library.get("id") %>">');
  208. tmpl_array.push(' <% if(!edit_mode) { %>');
  209. tmpl_array.push(' <% if(library.get("deleted")) { %>');
  210. tmpl_array.push(' <td style="color:grey;"><span data-toggle="tooltip" data-placement="top" title="Marked deleted" style="color:grey;" class="fa fa-ban fa-lg deleted_lib_ico"> </span> <%- library.get("name") %></td>');
  211. tmpl_array.push(' <% } else { %>');
  212. tmpl_array.push(' <td><a href="#folders/<%- library.get("root_folder_id") %>"><%- library.get("name") %></a></td>');
  213. tmpl_array.push(' <% } %>');
  214. tmpl_array.push(' <% if(library.get("description")) { %>');
  215. tmpl_array.push(' <% if( (library.get("description")).length> 80 ) { %>');
  216. tmpl_array.push(' <td data-toggle="tooltip" data-placement="bottom" title="<%= _.escape(library.get("description")) %>"><%= _.escape(library.get("description")).substring(0, 80) + "..." %></td>');
  217. tmpl_array.push(' <% } else { %>');
  218. tmpl_array.push(' <td><%= _.escape(library.get("description"))%></td>');
  219. tmpl_array.push(' <% } %>');
  220. tmpl_array.push(' <% } else { %>');
  221. tmpl_array.push(' <td></td>');
  222. tmpl_array.push(' <% } %>');
  223. tmpl_array.push(' <% if(library.get("synopsis")) { %>');
  224. tmpl_array.push(' <% if( (library.get("synopsis")).length> 120 ) { %>');
  225. tmpl_array.push(' <td data-toggle="tooltip" data-placement="bottom" title="<%= _.escape(library.get("synopsis")) %>"><%= _.escape(library.get("synopsis")).substring(0, 120) + "..." %></td>');
  226. tmpl_array.push(' <% } else { %>');
  227. tmpl_array.push(' <td><%= _.escape(library.get("synopsis"))%></td>');
  228. tmpl_array.push(' <% } %>');
  229. tmpl_array.push(' <% } else { %>');
  230. tmpl_array.push(' <td></td>');
  231. tmpl_array.push(' <% } %>');
  232. tmpl_array.push(' <% } else if(edit_mode){ %>');
  233. tmpl_array.push(' <td><textarea rows="4" class="form-control input_library_name" placeholder="name" ><%- library.get("name") %></textarea></td>');
  234. tmpl_array.push(' <td><textarea rows="4" class="form-control input_library_description" placeholder="description" ><%- library.get("description") %></textarea></td>');
  235. tmpl_array.push(' <td><textarea rows="4" class="form-control input_library_synopsis" placeholder="synopsis" ><%- library.get("synopsis") %></textarea></td>');
  236. tmpl_array.push(' <% } %>');
  237. tmpl_array.push(' <td class="right-center">');
  238. tmpl_array.push(' <% if( (library.get("public")) && (library.get("deleted") === false) ) { %>');
  239. tmpl_array.push(' <span data-toggle="tooltip" data-placement="top" title="Unrestricted library" style="color:grey;" class="fa fa-globe fa-lg public_lib_ico"> </span>');
  240. tmpl_array.push(' <% }%>');
  241. tmpl_array.push(' <button data-toggle="tooltip" data-placement="top" title="Modify <%- library.get("name") %>" class="primary-button btn-xs edit_library_btn" type="button" style="<% if(button_config.edit_library_btn === false) { print("display:none;") } %>"><span class="fa fa-pencil"></span></button>');
  242. tmpl_array.push(' <a href="#library/<%- library.get("id") %>/permissions"> <button data-toggle="tooltip" data-placement="top" title="Modify permissions" class="primary-button btn-xs permission_library_btn" type="button" style="<% if(button_config.permission_library_btn === false) { print("display:none;") } %>"><span class="fa fa-group"></span></button></a>');
  243. tmpl_array.push(' <button data-toggle="tooltip" data-placement="top" title="Save changes" class="primary-button btn-xs save_library_btn" type="button" style="<% if(button_config.save_library_btn === false) { print("display:none;") } %>"><span class="fa fa-floppy-o"> Save</span></button>');
  244. tmpl_array.push(' <button data-toggle="tooltip" data-placement="top" title="Discard changes" class="primary-button btn-xs cancel_library_btn" type="button" style="<% if(button_config.cancel_library_btn === false) { print("display:none;") } %>"><span class="fa fa-times"> Cancel</span></button>');
  245. tmpl_array.push(' <button data-toggle="tooltip" data-placement="top" title="Delete <%- library.get("name") %>" class="primary-button btn-xs delete_library_btn" type="button" style="<% if(button_config.delete_library_btn === false) { print("display:none;") } %>"><span class="fa fa-trash-o"> Delete</span></button>');
  246. tmpl_array.push(' <button data-toggle="tooltip" data-placement="top" title="Undelete <%- library.get("name") %> " class="primary-button btn-xs undelete_library_btn" type="button" style="<% if(button_config.undelete_library_btn === false) { print("display:none;") } %>"><span class="fa fa-unlock"> Undelete</span></button>');
  247. tmpl_array.push(' </td>');
  248. tmpl_array.push(' </tr>');
  249. return _.template(tmpl_array.join(''));
  250. }
  251. });
  252. return {
  253. LibraryRowView: LibraryRowView
  254. };
  255. });