PageRenderTime 63ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/mvc/library/library-model.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 220 lines | 156 code | 31 blank | 33 comment | 20 complexity | f216963da4a03e151dfea4487667b628 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([], function() {
  2. // ============================================================================
  3. // LIBRARY RELATED MODELS
  4. var Library = Backbone.Model.extend({
  5. urlRoot: '/api/libraries/',
  6. /** based on show_deleted would this lib show in the list of lib's?
  7. * @param {Boolean} show_deleted are we including deleted libraries?
  8. */
  9. isVisible : function(show_deleted){
  10. var isVisible = true;
  11. if( (!show_deleted) && (this.get('deleted')) ){
  12. isVisible = false;
  13. }
  14. return isVisible;
  15. }
  16. });
  17. var Libraries = Backbone.Collection.extend({
  18. url: '/api/libraries',
  19. model: Library,
  20. sort_key: 'name', // default
  21. sort_order: null, // default
  22. initialize : function(options){
  23. options = options || {};
  24. },
  25. /** Get every 'shown' library in this collection based on deleted filter
  26. * @param {Boolean} show_deleted are we including deleted libraries?
  27. * @returns array of library models
  28. */
  29. getVisible : function(show_deleted, filters){
  30. filters = filters || [];
  31. var filteredLibraries = new Libraries( this.filter( function( item ){
  32. return item.isVisible(show_deleted);
  33. }));
  34. return filteredLibraries;
  35. },
  36. /** Sort collection by library name (ascending) and return the sorted
  37. * collection
  38. */
  39. sortByNameAsc: function(){
  40. this.comparator = function(libraryA, libraryB){
  41. if (libraryA.get('name').toLowerCase() > libraryB.get('name').toLowerCase()) {
  42. return 1; // after
  43. }
  44. if (libraryB.get('name').toLowerCase() > libraryA.get('name').toLowerCase()) {
  45. return -1; // before
  46. }
  47. return 0; // equal
  48. };
  49. this.sort();
  50. return this;
  51. },
  52. /** Sort collection by library name (descending) and return the sorted
  53. * collection
  54. */
  55. sortByNameDesc: function(){
  56. this.comparator = function(libraryA, libraryB){
  57. if (libraryA.get('name').toLowerCase() > libraryB.get('name').toLowerCase()) {
  58. return -1; // before
  59. }
  60. if (libraryB.get('name').toLowerCase() > libraryA.get('name').toLowerCase()) {
  61. return 1; // after
  62. }
  63. return 0; // equal
  64. };
  65. this.sort();
  66. return this;
  67. }
  68. });
  69. // ============================================================================
  70. // FOLDER RELATED MODELS
  71. var Item = Backbone.Model.extend({
  72. urlRoot : '/api/libraries/datasets/'
  73. });
  74. var Ldda = Backbone.Model.extend({
  75. urlRoot : '/api/libraries/datasets/'
  76. });
  77. var FolderAsModel = Backbone.Model.extend({
  78. urlRoot: '/api/folders'
  79. });
  80. var Folder = Backbone.Collection.extend({
  81. model: Item,
  82. /** Sort collection by item name (ascending) and return the sorted
  83. * collection. Folders go before datasets.
  84. */
  85. sortByNameAsc: function(){
  86. this.comparator = function(itemA, itemB){
  87. if (itemA.get('type') === itemB.get('type')){
  88. if (itemA.get('name').toLowerCase() > itemB.get('name').toLowerCase()) {
  89. return 1; // after
  90. }
  91. if (itemB.get('name').toLowerCase() > itemA.get('name').toLowerCase()) {
  92. return -1; // before
  93. }
  94. return 0; // equal
  95. } else {
  96. if (itemA.get('type') === 'folder'){
  97. return -1; // folder is always before dataset
  98. } else {
  99. return 1;
  100. }
  101. }
  102. };
  103. this.sort();
  104. return this;
  105. },
  106. /** Sort collection by item name (descending) and return the sorted
  107. * collection. Folders go before datasets.
  108. */
  109. sortByNameDesc: function(){
  110. this.comparator = function(itemA, itemB){
  111. if (itemA.get('type') === itemB.get('type')){
  112. if (itemA.get('name').toLowerCase() > itemB.get('name').toLowerCase()) {
  113. return -1; // after
  114. }
  115. if (itemB.get('name').toLowerCase() > itemA.get('name').toLowerCase()) {
  116. return 1; // before
  117. }
  118. return 0; // equal
  119. } else {
  120. if (itemA.get('type') === 'folder'){
  121. return -1; // folder is always before dataset
  122. } else {
  123. return 1;
  124. }
  125. }
  126. };
  127. this.sort();
  128. return this;
  129. }
  130. });
  131. var FolderContainer = Backbone.Model.extend({
  132. defaults : {
  133. folder : new Folder(),
  134. urlRoot : "/api/folders/",
  135. id : "unknown"
  136. },
  137. parse : function(obj) {
  138. // response is not a simple array, it contains metadata
  139. // this will update the inner collection
  140. this.get("folder").reset(obj.folder_contents);
  141. return obj;
  142. }
  143. });
  144. // ============================================================================
  145. // HISTORY RELATED MODELS
  146. // TODO UNITE
  147. var HistoryItem = Backbone.Model.extend({
  148. urlRoot : '/api/histories/'
  149. });
  150. var HistoryContents = Backbone.Collection.extend({
  151. urlRoot : '/api/histories/',
  152. initialize: function(options){
  153. this.id = options.id;
  154. },
  155. url : function(){
  156. return this.urlRoot + this.id + '/contents';
  157. },
  158. model : HistoryItem
  159. });
  160. var GalaxyHistory = Backbone.Model.extend({
  161. urlRoot : '/api/histories/'
  162. });
  163. var GalaxyHistories = Backbone.Collection.extend({
  164. url : '/api/histories',
  165. model : GalaxyHistory
  166. });
  167. // ============================================================================
  168. // JSTREE MODEL
  169. /** Represents folder structure parsable by the jstree component.
  170. *
  171. */
  172. var Jstree = Backbone.Model.extend({
  173. urlRoot: '/api/ftp_files'
  174. });
  175. return {
  176. Library: Library,
  177. FolderAsModel : FolderAsModel,
  178. Libraries : Libraries,
  179. Item : Item,
  180. Ldda : Ldda,
  181. Folder : Folder,
  182. FolderContainer : FolderContainer,
  183. HistoryItem : HistoryItem,
  184. HistoryContents : HistoryContents,
  185. GalaxyHistory : GalaxyHistory,
  186. GalaxyHistories : GalaxyHistories,
  187. Jstree: Jstree
  188. };
  189. });