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

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

https://bitbucket.org/hbc/galaxy-central-hbc/
JavaScript | 204 lines | 148 code | 28 blank | 28 comment | 20 complexity | 6090f9ca19f2a3767ffdf2008534b0b1 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 FolderAsModel = Backbone.Model.extend({
  75. urlRoot: '/api/folders'
  76. });
  77. var Folder = Backbone.Collection.extend({
  78. model: Item,
  79. /** Sort collection by item name (ascending) and return the sorted
  80. * collection. Folders go before datasets.
  81. */
  82. sortByNameAsc: function(){
  83. this.comparator = function(itemA, itemB){
  84. if (itemA.get('type') === itemB.get('type')){
  85. if (itemA.get('name').toLowerCase() > itemB.get('name').toLowerCase()) {
  86. return 1; // after
  87. }
  88. if (itemB.get('name').toLowerCase() > itemA.get('name').toLowerCase()) {
  89. return -1; // before
  90. }
  91. return 0; // equal
  92. } else {
  93. if (itemA.get('type') === 'folder'){
  94. return -1; // folder is always before dataset
  95. } else {
  96. return 1;
  97. }
  98. }
  99. };
  100. this.sort();
  101. return this;
  102. },
  103. /** Sort collection by item name (descending) and return the sorted
  104. * collection. Folders go before datasets.
  105. */
  106. sortByNameDesc: function(){
  107. this.comparator = function(itemA, itemB){
  108. if (itemA.get('type') === itemB.get('type')){
  109. if (itemA.get('name').toLowerCase() > itemB.get('name').toLowerCase()) {
  110. return -1; // after
  111. }
  112. if (itemB.get('name').toLowerCase() > itemA.get('name').toLowerCase()) {
  113. return 1; // before
  114. }
  115. return 0; // equal
  116. } else {
  117. if (itemA.get('type') === 'folder'){
  118. return -1; // folder is always before dataset
  119. } else {
  120. return 1;
  121. }
  122. }
  123. };
  124. this.sort();
  125. return this;
  126. }
  127. });
  128. var FolderContainer = Backbone.Model.extend({
  129. defaults : {
  130. folder : new Folder(),
  131. urlRoot : "/api/folders/",
  132. id : "unknown"
  133. },
  134. parse : function(obj) {
  135. // response is not a simple array, it contains metadata
  136. // this will update the inner collection
  137. this.get("folder").reset(obj.folder_contents);
  138. return obj;
  139. }
  140. });
  141. // ============================================================================
  142. // HISTORY RELATED MODELS
  143. // TODO UNITE
  144. var HistoryItem = Backbone.Model.extend({
  145. urlRoot : '/api/histories/'
  146. });
  147. var HistoryContents = Backbone.Collection.extend({
  148. urlRoot : '/api/histories/',
  149. initialize: function(options){
  150. this.id = options.id;
  151. },
  152. url : function(){
  153. return this.urlRoot + this.id + '/contents';
  154. },
  155. model : HistoryItem
  156. });
  157. var GalaxyHistory = Backbone.Model.extend({
  158. urlRoot : '/api/histories/'
  159. });
  160. var GalaxyHistories = Backbone.Collection.extend({
  161. url : '/api/histories',
  162. model : GalaxyHistory
  163. });
  164. return {
  165. Library: Library,
  166. FolderAsModel : FolderAsModel,
  167. Libraries : Libraries,
  168. Item : Item,
  169. Folder : Folder,
  170. FolderContainer : FolderContainer,
  171. HistoryItem : HistoryItem,
  172. HistoryContents : HistoryContents,
  173. GalaxyHistory : GalaxyHistory,
  174. GalaxyHistories : GalaxyHistories
  175. };
  176. });