PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/natefoo/galaxy-central
JavaScript | 483 lines | 214 code | 78 blank | 191 comment | 8 complexity | d3b4716dd57efa4b92ed31cd75a7e0e8 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/dataset/dataset-model",
  3. "mvc/base-mvc",
  4. "utils/localization"
  5. ], function( DATASET_MODEL, BASE_MVC, _l ){
  6. //==============================================================================
  7. /*
  8. Notes:
  9. Terminology:
  10. DatasetCollection/DC : a container of datasets or nested DatasetCollections
  11. Element/DatasetCollectionElement/DCE : an item contained in a DatasetCollection
  12. HistoryDatasetCollectionAssociation/HDCA: a DatasetCollection contained in a history
  13. This all seems too complex unfortunately:
  14. - Terminology collision between DatasetCollections (DCs) and Backbone Collections.
  15. - In the DatasetCollections API JSON, DC Elements use a 'Has A' stucture to *contain*
  16. either a dataset or a nested DC. This would make the hierarchy much taller. I've
  17. decided to merge the contained JSON with the DC element json - making the 'has a'
  18. relation into an 'is a' relation. This seems simpler to me and allowed a lot of
  19. DRY in both models and views, but may make tracking or tracing within these models
  20. more difficult (since DatasetCollectionElements are now *also* DatasetAssociations
  21. or DatasetCollections (nested)). This also violates the rule of thumb about
  22. favoring aggregation over inheritance.
  23. - Currently, there are three DatasetCollection subclasses: List, Pair, and ListPaired.
  24. These each should a) be usable on their own, b) be usable in the context of
  25. nesting within a collection model (at least in the case of ListPaired), and
  26. c) be usable within the context of other container models (like History or
  27. LibraryFolder, etc.). I've tried to separate/extract classes in order to
  28. handle those three situations, but it's proven difficult to do in a simple,
  29. readable manner.
  30. - Ideally, histories and libraries would inherit from the same server models as
  31. dataset collections do since they are (in essence) dataset collections themselves -
  32. making the whole nested structure simpler. This would be a large, error-prone
  33. refactoring and migration.
  34. Many of the classes and heirarchy are meant as extension points so, while the
  35. relations and flow may be difficult to understand initially, they'll allow us to
  36. handle the growth or flux dataset collection in the future (w/o actually implementing
  37. any YAGNI).
  38. */
  39. //_________________________________________________________________________________________________ ELEMENTS
  40. /** @class mixin for Dataset collection elements.
  41. * When collection elements are passed from the API, the underlying element is
  42. * in a sub-object 'object' (IOW, a DCE representing an HDA will have HDA json in element.object).
  43. * This mixin uses the constructor and parse methods to merge that JSON with the DCE attribtues
  44. * effectively changing a DCE from a container to a subclass (has a --> is a).
  45. */
  46. var DatasetCollectionElementMixin = {
  47. /** default attributes used by elements in a dataset collection */
  48. defaults : {
  49. model_class : 'DatasetCollectionElement',
  50. element_identifier : null,
  51. element_index : null,
  52. element_type : null
  53. },
  54. /** merge the attributes of the sub-object 'object' into this model */
  55. _mergeObject : function( attributes ){
  56. _.extend( attributes, attributes.object );
  57. delete attributes.object;
  58. return attributes;
  59. },
  60. /** override to merge this.object into this */
  61. constructor : function( attributes, options ){
  62. this.debug( '\t DatasetCollectionElement.constructor:', attributes, options );
  63. attributes = this._mergeObject( attributes );
  64. Backbone.Model.apply( this, arguments );
  65. },
  66. /** when the model is fetched, merge this.object into this */
  67. parse : function( response, options ){
  68. var attributes = response;
  69. attributes = this._mergeObject( attributes );
  70. return attributes;
  71. }
  72. };
  73. //TODO: unused?
  74. /** @class Concrete class of Generic DatasetCollectionElement */
  75. var DatasetCollectionElement = Backbone.Model
  76. .extend( BASE_MVC.LoggableMixin )
  77. .extend( DatasetCollectionElementMixin );
  78. //==============================================================================
  79. /** @class Base/Abstract Backbone collection for Generic DCEs. */
  80. var DCECollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend(
  81. /** @lends DCECollection.prototype */{
  82. model: DatasetCollectionElement,
  83. /** logger used to record this.log messages, commonly set to console */
  84. //logger : console,
  85. //TODO: unused?
  86. /** Set up.
  87. * @see Backbone.Collection#initialize
  88. */
  89. initialize : function( attributes, options ){
  90. this.debug( this + '(DCECollection).initialize:', attributes, options );
  91. options = options || {};
  92. //this._setUpListeners();
  93. },
  94. /** String representation. */
  95. toString : function(){
  96. return ([ 'DatasetCollectionElementCollection(', this.length, ')' ].join( '' ));
  97. }
  98. });
  99. //==============================================================================
  100. /** @class Backbone model for a dataset collection element that is a dataset (HDA).
  101. */
  102. var DatasetDCE = DATASET_MODEL.DatasetAssociation.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
  103. /** @lends DatasetDCE.prototype */{
  104. /** logger used to record this.log messages, commonly set to console */
  105. //logger : console,
  106. defaults : _.extend( {},
  107. DATASET_MODEL.DatasetAssociation.prototype.defaults,
  108. DatasetCollectionElementMixin.defaults
  109. ),
  110. // because all objects have constructors (as this hashmap would even if this next line wasn't present)
  111. // the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
  112. // - re-apply manually it now
  113. /** call the mixin constructor */
  114. constructor : function( attributes, options ){
  115. this.debug( '\t DatasetDCE.constructor:', attributes, options );
  116. //DATASET_MODEL.DatasetAssociation.prototype.constructor.call( this, attributes, options );
  117. DatasetCollectionElementMixin.constructor.call( this, attributes, options );
  118. },
  119. //TODO: unused?
  120. /** set up */
  121. initialize : function( attributes, options ){
  122. this.debug( this + '(DatasetDCE).initialize:', attributes, options );
  123. DATASET_MODEL.DatasetAssociation.prototype.initialize.call( this, attributes, options );
  124. },
  125. /** String representation. */
  126. toString : function(){
  127. var objStr = this.get( 'element_identifier' );
  128. return ([ 'DatasetDCE(', objStr, ')' ].join( '' ));
  129. }
  130. }));
  131. //==============================================================================
  132. /** @class DCECollection of DatasetDCE's (a list of datasets, a pair of datasets).
  133. */
  134. var DatasetDCECollection = DCECollection.extend(
  135. /** @lends DatasetDCECollection.prototype */{
  136. model: DatasetDCE,
  137. /** logger used to record this.log messages, commonly set to console */
  138. //logger : console,
  139. //TODO: unused?
  140. /** set up */
  141. initialize : function( attributes, options ){
  142. this.debug( this + '(DatasetDCECollection).initialize:', attributes, options );
  143. DCECollection.prototype.initialize.call( this, attributes, options );
  144. },
  145. /** String representation. */
  146. toString : function(){
  147. return ([ 'DatasetDCECollection(', this.length, ')' ].join( '' ));
  148. }
  149. });
  150. //_________________________________________________________________________________________________ COLLECTIONS
  151. /** @class Backbone model for Dataset Collections.
  152. * The DC API returns an array of JSON objects under the attribute elements.
  153. * This model:
  154. * - removes that array/attribute ('elements') from the model,
  155. * - creates a bbone collection (of the class defined in the 'collectionClass' attribute),
  156. * - passes that json onto the bbone collection
  157. * - caches the bbone collection in this.elements
  158. */
  159. var DatasetCollection = Backbone.Model
  160. .extend( BASE_MVC.LoggableMixin )
  161. .extend( BASE_MVC.SearchableModelMixin )
  162. .extend(/** @lends DatasetCollection.prototype */{
  163. /** logger used to record this.log messages, commonly set to console */
  164. //logger : console,
  165. /** default attributes for a model */
  166. defaults : {
  167. /* 'list', 'paired', or 'list:paired' */
  168. collection_type : null,
  169. //??
  170. deleted : false
  171. },
  172. /** Which class to use for elements */
  173. collectionClass : DCECollection,
  174. /** set up: create elements instance var and (on changes to elements) update them */
  175. initialize : function( model, options ){
  176. this.debug( this + '(DatasetCollection).initialize:', model, options, this );
  177. //historyContent.HistoryContent.prototype.initialize.call( this, attrs, options );
  178. this.elements = this._createElementsModel();
  179. this.on( 'change:elements', function(){
  180. this.log( 'change:elements' );
  181. //TODO: prob. better to update the collection instead of re-creating it
  182. this.elements = this._createElementsModel();
  183. });
  184. },
  185. /** move elements model attribute to full collection */
  186. _createElementsModel : function(){
  187. this.debug( this + '._createElementsModel', this.collectionClass, this.get( 'elements' ), this.elements );
  188. //TODO: same patterns as DatasetCollectionElement _createObjectModel - refactor to BASE_MVC.hasSubModel?
  189. var elements = this.get( 'elements' ) || [];
  190. this.unset( 'elements', { silent: true });
  191. this.elements = new this.collectionClass( elements );
  192. //this.debug( 'collectionClass:', this.collectionClass + '', this.elements );
  193. return this.elements;
  194. },
  195. // ........................................................................ common queries
  196. /** pass the elements back within the model json when this is serialized */
  197. toJSON : function(){
  198. var json = Backbone.Model.prototype.toJSON.call( this );
  199. if( this.elements ){
  200. json.elements = this.elements.toJSON();
  201. }
  202. return json;
  203. },
  204. /** is the collection done with updates and ready to be used? (finished running, etc.) */
  205. inReadyState : function(){
  206. //TODO: state currenly unimplemented for collections
  207. return true;
  208. },
  209. //TODO:?? the following are the same interface as DatasetAssociation - can we combine?
  210. /** Does the DC contain any elements yet? Is a fetch() required? */
  211. hasDetails : function(){
  212. //TODO: this is incorrect for (accidentally) empty collections
  213. this.debug( 'hasDetails:', this.elements.length );
  214. return this.elements.length !== 0;
  215. },
  216. /** Given the filters, what models in this.elements would be returned? */
  217. getVisibleContents : function( filters ){
  218. // filters unused for now
  219. return this.elements;
  220. },
  221. // ........................................................................ ajax
  222. /** save this dataset, _Mark_ing it as deleted (just a flag) */
  223. 'delete' : function( options ){
  224. if( this.get( 'deleted' ) ){ return jQuery.when(); }
  225. return this.save( { deleted: true }, options );
  226. },
  227. /** save this dataset, _Mark_ing it as undeleted */
  228. undelete : function( options ){
  229. if( !this.get( 'deleted' ) || this.get( 'purged' ) ){ return jQuery.when(); }
  230. return this.save( { deleted: false }, options );
  231. },
  232. /** Is this collection deleted or purged? */
  233. isDeletedOrPurged : function(){
  234. return ( this.get( 'deleted' ) || this.get( 'purged' ) );
  235. },
  236. // ........................................................................ searchable
  237. /** searchable attributes for collections */
  238. searchAttributes : [
  239. 'name'
  240. ],
  241. // ........................................................................ misc
  242. /** String representation */
  243. toString : function(){
  244. var idAndName = [ this.get( 'id' ), this.get( 'name' ) || this.get( 'element_identifier' ) ];
  245. return 'DatasetCollection(' + ( idAndName.join(',') ) + ')';
  246. }
  247. });
  248. //==============================================================================
  249. /** Model for a DatasetCollection containing datasets (non-nested).
  250. */
  251. var ListDatasetCollection = DatasetCollection.extend(
  252. /** @lends ListDatasetCollection.prototype */{
  253. /** logger used to record this.log messages, commonly set to console */
  254. //logger : console,
  255. /** override since we know the collection will only contain datasets */
  256. collectionClass : DatasetDCECollection,
  257. //TODO: unused?
  258. initialize : function( attrs, options ){
  259. this.debug( this + '(ListDatasetCollection).initialize:', attrs, options );
  260. DatasetCollection.prototype.initialize.call( this, attrs, options );
  261. },
  262. /** String representation. */
  263. toString : function(){
  264. return ([ 'ListDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  265. }
  266. });
  267. //==============================================================================
  268. /** Model for a DatasetCollection containing fwd/rev datasets (a list of 2).
  269. */
  270. var PairDatasetCollection = ListDatasetCollection.extend(
  271. /** @lends PairDatasetCollection.prototype */{
  272. /** logger used to record this.log messages, commonly set to console */
  273. //logger : console,
  274. //TODO: unused?
  275. /** */
  276. initialize : function( attrs, options ){
  277. this.debug( this + '(PairDatasetCollection).initialize:', attrs, options );
  278. ListDatasetCollection.prototype.initialize.call( this, attrs, options );
  279. },
  280. /** String representation. */
  281. toString : function(){
  282. return ([ 'PairDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  283. }
  284. });
  285. //_________________________________________________________________________________________________ NESTED COLLECTIONS
  286. // this is where things get weird, man. Weird.
  287. //TODO: it might be possible to compact all the following...I think.
  288. //==============================================================================
  289. /** @class Backbone model for a Generic DatasetCollectionElement that is also a DatasetCollection
  290. * (a nested collection). Currently only list:paired.
  291. */
  292. var NestedDCDCE = DatasetCollection.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
  293. /** @lends NestedDCDCE.prototype */{
  294. /** logger used to record this.log messages, commonly set to console */
  295. //logger : console,
  296. // because all objects have constructors (as this hashmap would even if this next line wasn't present)
  297. // the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
  298. // - re-apply manually it now
  299. /** call the mixin constructor */
  300. constructor : function( attributes, options ){
  301. this.debug( '\t NestedDCDCE.constructor:', attributes, options );
  302. DatasetCollectionElementMixin.constructor.call( this, attributes, options );
  303. },
  304. /** String representation. */
  305. toString : function(){
  306. var objStr = ( this.object )?( '' + this.object ):( this.get( 'element_identifier' ) );
  307. return ([ 'NestedDCDCE(', objStr, ')' ].join( '' ));
  308. }
  309. }));
  310. //==============================================================================
  311. /** @class Backbone collection containing Generic NestedDCDCE's (nested dataset collections).
  312. */
  313. var NestedDCDCECollection = DCECollection.extend(
  314. /** @lends NestedDCDCECollection.prototype */{
  315. /** logger used to record this.log messages, commonly set to console */
  316. //logger : console,
  317. /** This is a collection of nested collections */
  318. model: NestedDCDCE,
  319. //TODO: unused?
  320. /** set up */
  321. initialize : function( attrs, options ){
  322. this.debug( this + '(NestedDCDCECollection).initialize:', attrs, options );
  323. DCECollection.prototype.initialize.call( this, attrs, options );
  324. },
  325. /** String representation. */
  326. toString : function(){
  327. return ([ 'NestedDCDCECollection(', this.length, ')' ].join( '' ));
  328. }
  329. });
  330. //==============================================================================
  331. /** @class Backbone model for a paired dataset collection within a list:paired dataset collection.
  332. */
  333. var NestedPairDCDCE = PairDatasetCollection.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
  334. /** @lends NestedPairDCDCE.prototype */{
  335. //TODO:?? possibly rename to NestedDatasetCollection?
  336. /** logger used to record this.log messages, commonly set to console */
  337. //logger : console,
  338. // because all objects have constructors (as this hashmap would even if this next line wasn't present)
  339. // the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
  340. // - re-apply manually it now
  341. /** This is both a collection and a collection element - call the constructor */
  342. constructor : function( attributes, options ){
  343. this.debug( '\t NestedPairDCDCE.constructor:', attributes, options );
  344. //DatasetCollection.constructor.call( this, attributes, options );
  345. DatasetCollectionElementMixin.constructor.call( this, attributes, options );
  346. },
  347. /** String representation. */
  348. toString : function(){
  349. var objStr = ( this.object )?( '' + this.object ):( this.get( 'element_identifier' ) );
  350. return ([ 'NestedPairDCDCE(', objStr, ')' ].join( '' ));
  351. }
  352. }));
  353. //==============================================================================
  354. /** @class Backbone collection for a backbone collection containing paired dataset collections.
  355. */
  356. var NestedPairDCDCECollection = NestedDCDCECollection.extend(
  357. /** @lends PairDCDCECollection.prototype */{
  358. /** logger used to record this.log messages, commonly set to console */
  359. //logger : console,
  360. /** We know this collection is composed of only nested pair collections */
  361. model: NestedPairDCDCE,
  362. //TODO: unused?
  363. /** set up */
  364. initialize : function( attrs, options ){
  365. this.debug( this + '(NestedPairDCDCECollection).initialize:', attrs, options );
  366. NestedDCDCECollection.prototype.initialize.call( this, attrs, options );
  367. },
  368. /** String representation. */
  369. toString : function(){
  370. return ([ 'NestedPairDCDCECollection(', this.length, ')' ].join( '' ));
  371. }
  372. });
  373. //==============================================================================
  374. /** @class Backbone Model for a DatasetCollection (list) that contains DatasetCollections (pairs).
  375. */
  376. var ListPairedDatasetCollection = DatasetCollection.extend(
  377. /** @lends ListPairedDatasetCollection.prototype */{
  378. /** logger used to record this.log messages, commonly set to console */
  379. //logger : console,
  380. /** list:paired is the only collection that itself contains collections */
  381. collectionClass : NestedPairDCDCECollection,
  382. //TODO: unused?
  383. /** set up */
  384. initialize : function( attributes, options ){
  385. this.debug( this + '(ListPairedDatasetCollection).initialize:', attributes, options );
  386. DatasetCollection.prototype.initialize.call( this, attributes, options );
  387. },
  388. /** String representation. */
  389. toString : function(){
  390. return ([ 'ListPairedDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
  391. }
  392. });
  393. //==============================================================================
  394. return {
  395. ListDatasetCollection : ListDatasetCollection,
  396. PairDatasetCollection : PairDatasetCollection,
  397. ListPairedDatasetCollection : ListPairedDatasetCollection
  398. };
  399. });