galaxy-central-manageapi /client/galaxy/scripts/mvc/collection/collection-model.js

Language Javascript Lines 484
MD5 Hash d3b4716dd57efa4b92ed31cd75a7e0e8
Repository https://bitbucket.org/remy_d1/galaxy-central-manageapi View Raw File View Project SPDX
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
define([
    "mvc/dataset/dataset-model",
    "mvc/base-mvc",
    "utils/localization"
], function( DATASET_MODEL, BASE_MVC, _l ){
//==============================================================================
/*
Notes:

Terminology:
    DatasetCollection/DC : a container of datasets or nested DatasetCollections
    Element/DatasetCollectionElement/DCE : an item contained in a DatasetCollection
    HistoryDatasetCollectionAssociation/HDCA: a DatasetCollection contained in a history


This all seems too complex unfortunately:

- Terminology collision between DatasetCollections (DCs) and Backbone Collections.
- In the DatasetCollections API JSON, DC Elements use a 'Has A' stucture to *contain*
    either a dataset or a nested DC. This would make the hierarchy much taller. I've
    decided to merge the contained JSON with the DC element json - making the 'has a'
    relation into an 'is a' relation. This seems simpler to me and allowed a lot of
    DRY in both models and views, but may make tracking or tracing within these models
    more difficult (since DatasetCollectionElements are now *also* DatasetAssociations
    or DatasetCollections (nested)). This also violates the rule of thumb about
    favoring aggregation over inheritance.
- Currently, there are three DatasetCollection subclasses: List, Pair, and ListPaired.
    These each should a) be usable on their own, b) be usable in the context of
    nesting within a collection model (at least in the case of ListPaired), and
    c) be usable within the context of other container models (like History or
    LibraryFolder, etc.). I've tried to separate/extract classes in order to
    handle those three situations, but it's proven difficult to do in a simple,
    readable manner.
- Ideally, histories and libraries would inherit from the same server models as
    dataset collections do since they are (in essence) dataset collections themselves -
    making the whole nested structure simpler. This would be a large, error-prone
    refactoring and migration.

Many of the classes and heirarchy are meant as extension points so, while the
relations and flow may be difficult to understand initially, they'll allow us to
handle the growth or flux dataset collection in the future (w/o actually implementing
any YAGNI).

*/
//_________________________________________________________________________________________________ ELEMENTS
/** @class mixin for Dataset collection elements.
 *      When collection elements are passed from the API, the underlying element is
 *          in a sub-object 'object' (IOW, a DCE representing an HDA will have HDA json in element.object).
 *      This mixin uses the constructor and parse methods to merge that JSON with the DCE attribtues
 *          effectively changing a DCE from a container to a subclass (has a --> is a).
 */
var DatasetCollectionElementMixin = {

    /** default attributes used by elements in a dataset collection */
    defaults : {
        model_class         : 'DatasetCollectionElement',
        element_identifier  : null,
        element_index       : null,
        element_type        : null
    },

    /** merge the attributes of the sub-object 'object' into this model */
    _mergeObject : function( attributes ){
        _.extend( attributes, attributes.object );
        delete attributes.object;
        return attributes;
    },

    /** override to merge this.object into this */
    constructor : function( attributes, options ){
        this.debug( '\t DatasetCollectionElement.constructor:', attributes, options );
        attributes = this._mergeObject( attributes );
        Backbone.Model.apply( this, arguments );
    },

    /** when the model is fetched, merge this.object into this */
    parse : function( response, options ){
        var attributes = response;
        attributes = this._mergeObject( attributes );
        return attributes;
    }
};

//TODO: unused?
/** @class Concrete class of Generic DatasetCollectionElement */
var DatasetCollectionElement = Backbone.Model
    .extend( BASE_MVC.LoggableMixin )
    .extend( DatasetCollectionElementMixin );

    
//==============================================================================
/** @class Base/Abstract Backbone collection for Generic DCEs. */
var DCECollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend(
/** @lends DCECollection.prototype */{
    model: DatasetCollectionElement,

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

//TODO: unused?
    /** Set up.
     *  @see Backbone.Collection#initialize
     */
    initialize : function( attributes, options ){
        this.debug( this + '(DCECollection).initialize:', attributes, options );
        options = options || {};
        //this._setUpListeners();
    },

    /** String representation. */
    toString : function(){
         return ([ 'DatasetCollectionElementCollection(', this.length, ')' ].join( '' ));
    }
});


//==============================================================================
/** @class Backbone model for a dataset collection element that is a dataset (HDA).
 */
var DatasetDCE = DATASET_MODEL.DatasetAssociation.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
/** @lends DatasetDCE.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    defaults : _.extend( {},
        DATASET_MODEL.DatasetAssociation.prototype.defaults,
        DatasetCollectionElementMixin.defaults
    ),

    // because all objects have constructors (as this hashmap would even if this next line wasn't present)
    //  the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
    //  - re-apply manually it now
    /** call the mixin constructor */
    constructor : function( attributes, options ){
        this.debug( '\t DatasetDCE.constructor:', attributes, options );
        //DATASET_MODEL.DatasetAssociation.prototype.constructor.call( this, attributes, options );
        DatasetCollectionElementMixin.constructor.call( this, attributes, options );
    },

//TODO: unused?
    /** set up */
    initialize : function( attributes, options ){
        this.debug( this + '(DatasetDCE).initialize:', attributes, options );
        DATASET_MODEL.DatasetAssociation.prototype.initialize.call( this, attributes, options );
    },

    /** String representation. */
    toString : function(){
        var objStr = this.get( 'element_identifier' );
        return ([ 'DatasetDCE(', objStr, ')' ].join( '' ));
    }
}));


//==============================================================================
/** @class DCECollection of DatasetDCE's (a list of datasets, a pair of datasets).
 */
var DatasetDCECollection = DCECollection.extend(
/** @lends DatasetDCECollection.prototype */{
    model: DatasetDCE,

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

//TODO: unused?
    /** set up */
    initialize : function( attributes, options ){
        this.debug( this + '(DatasetDCECollection).initialize:', attributes, options );
        DCECollection.prototype.initialize.call( this, attributes, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'DatasetDCECollection(', this.length, ')' ].join( '' ));
    }
});


//_________________________________________________________________________________________________ COLLECTIONS
/** @class Backbone model for Dataset Collections.
 *      The DC API returns an array of JSON objects under the attribute elements.
 *      This model:
 *          - removes that array/attribute ('elements') from the model,
 *          - creates a bbone collection (of the class defined in the 'collectionClass' attribute),
 *          - passes that json onto the bbone collection
 *          - caches the bbone collection in this.elements
 */
var DatasetCollection = Backbone.Model
        .extend( BASE_MVC.LoggableMixin )
        .extend( BASE_MVC.SearchableModelMixin )
.extend(/** @lends DatasetCollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    /** default attributes for a model */
    defaults : {
        /* 'list', 'paired', or 'list:paired' */
        collection_type     : null,
        //??
        deleted             : false
    },

    /** Which class to use for elements */
    collectionClass : DCECollection,

    /** set up: create elements instance var and (on changes to elements) update them  */
    initialize : function( model, options ){
        this.debug( this + '(DatasetCollection).initialize:', model, options, this );
        //historyContent.HistoryContent.prototype.initialize.call( this, attrs, options );
        this.elements = this._createElementsModel();
        this.on( 'change:elements', function(){
            this.log( 'change:elements' );
//TODO: prob. better to update the collection instead of re-creating it
            this.elements = this._createElementsModel();
        });
    },

    /** move elements model attribute to full collection */
    _createElementsModel : function(){
        this.debug( this + '._createElementsModel', this.collectionClass, this.get( 'elements' ), this.elements );
//TODO: same patterns as DatasetCollectionElement _createObjectModel - refactor to BASE_MVC.hasSubModel?
        var elements = this.get( 'elements' ) || [];
        this.unset( 'elements', { silent: true });
        this.elements = new this.collectionClass( elements );
        //this.debug( 'collectionClass:', this.collectionClass + '', this.elements );
        return this.elements;
    },

    // ........................................................................ common queries
    /** pass the elements back within the model json when this is serialized */
    toJSON : function(){
        var json = Backbone.Model.prototype.toJSON.call( this );
        if( this.elements ){
            json.elements = this.elements.toJSON();
        }
        return json;
    },

    /** is the collection done with updates and ready to be used? (finished running, etc.) */
    inReadyState : function(){
//TODO: state currenly unimplemented for collections
        return true;
    },

    //TODO:?? the following are the same interface as DatasetAssociation - can we combine?
    /** Does the DC contain any elements yet? Is a fetch() required? */
    hasDetails : function(){
//TODO: this is incorrect for (accidentally) empty collections
        this.debug( 'hasDetails:', this.elements.length );
        return this.elements.length !== 0;
    },

    /** Given the filters, what models in this.elements would be returned? */
    getVisibleContents : function( filters ){
        // filters unused for now
        return this.elements;
    },

    // ........................................................................ ajax
    /** save this dataset, _Mark_ing it as deleted (just a flag) */
    'delete' : function( options ){
        if( this.get( 'deleted' ) ){ return jQuery.when(); }
        return this.save( { deleted: true }, options );
    },
    /** save this dataset, _Mark_ing it as undeleted */
    undelete : function( options ){
        if( !this.get( 'deleted' ) || this.get( 'purged' ) ){ return jQuery.when(); }
        return this.save( { deleted: false }, options );
    },

    /** Is this collection deleted or purged? */
    isDeletedOrPurged : function(){
        return ( this.get( 'deleted' ) || this.get( 'purged' ) );
    },

    // ........................................................................ searchable
    /** searchable attributes for collections */
    searchAttributes : [
        'name'
    ],

    // ........................................................................ misc
    /** String representation */
    toString : function(){
        var idAndName = [ this.get( 'id' ), this.get( 'name' ) || this.get( 'element_identifier' ) ];
        return 'DatasetCollection(' + ( idAndName.join(',') ) + ')';
    }
});


//==============================================================================
/** Model for a DatasetCollection containing datasets (non-nested).
 */
var ListDatasetCollection = DatasetCollection.extend(
/** @lends ListDatasetCollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    /** override since we know the collection will only contain datasets */
    collectionClass : DatasetDCECollection,

//TODO: unused?
    initialize : function( attrs, options ){
        this.debug( this + '(ListDatasetCollection).initialize:', attrs, options );
        DatasetCollection.prototype.initialize.call( this, attrs, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'ListDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
    }
});


//==============================================================================
/** Model for a DatasetCollection containing fwd/rev datasets (a list of 2).
 */
var PairDatasetCollection = ListDatasetCollection.extend(
/** @lends PairDatasetCollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

//TODO: unused?
    /**  */
    initialize : function( attrs, options ){
        this.debug( this + '(PairDatasetCollection).initialize:', attrs, options );
        ListDatasetCollection.prototype.initialize.call( this, attrs, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'PairDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
    }
});


//_________________________________________________________________________________________________ NESTED COLLECTIONS
// this is where things get weird, man. Weird.
//TODO: it might be possible to compact all the following...I think.
//==============================================================================
/** @class Backbone model for a Generic DatasetCollectionElement that is also a DatasetCollection
 *      (a nested collection). Currently only list:paired.
 */
var NestedDCDCE = DatasetCollection.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
/** @lends NestedDCDCE.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    // because all objects have constructors (as this hashmap would even if this next line wasn't present)
    //  the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
    //  - re-apply manually it now
    /** call the mixin constructor */
    constructor : function( attributes, options ){
        this.debug( '\t NestedDCDCE.constructor:', attributes, options );
        DatasetCollectionElementMixin.constructor.call( this, attributes, options );
    },
    
    /** String representation. */
    toString : function(){
        var objStr = ( this.object )?( '' + this.object ):( this.get( 'element_identifier' ) );
        return ([ 'NestedDCDCE(', objStr, ')' ].join( '' ));
    }
}));


//==============================================================================
/** @class Backbone collection containing Generic NestedDCDCE's (nested dataset collections).
 */
var NestedDCDCECollection = DCECollection.extend(
/** @lends NestedDCDCECollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,
    
    /** This is a collection of nested collections */
    model: NestedDCDCE,

//TODO: unused?
    /** set up */
    initialize : function( attrs, options ){
        this.debug( this + '(NestedDCDCECollection).initialize:', attrs, options );
        DCECollection.prototype.initialize.call( this, attrs, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'NestedDCDCECollection(', this.length, ')' ].join( '' ));
    }
});


//==============================================================================
/** @class Backbone model for a paired dataset collection within a list:paired dataset collection.
 */
var NestedPairDCDCE = PairDatasetCollection.extend( BASE_MVC.mixin( DatasetCollectionElementMixin,
/** @lends NestedPairDCDCE.prototype */{
//TODO:?? possibly rename to NestedDatasetCollection?

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    // because all objects have constructors (as this hashmap would even if this next line wasn't present)
    //  the constructor in hcontentMixin won't be attached by BASE_MVC.mixin to this model
    //  - re-apply manually it now
    /** This is both a collection and a collection element - call the constructor */
    constructor : function( attributes, options ){
        this.debug( '\t NestedPairDCDCE.constructor:', attributes, options );
        //DatasetCollection.constructor.call( this, attributes, options );
        DatasetCollectionElementMixin.constructor.call( this, attributes, options );
    },

    /** String representation. */
    toString : function(){
        var objStr = ( this.object )?( '' + this.object ):( this.get( 'element_identifier' ) );
        return ([ 'NestedPairDCDCE(', objStr, ')' ].join( '' ));
    }
}));


//==============================================================================
/** @class Backbone collection for a backbone collection containing paired dataset collections.
 */
var NestedPairDCDCECollection = NestedDCDCECollection.extend(
/** @lends PairDCDCECollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    /** We know this collection is composed of only nested pair collections */
    model: NestedPairDCDCE,

//TODO: unused?
    /** set up */
    initialize : function( attrs, options ){
        this.debug( this + '(NestedPairDCDCECollection).initialize:', attrs, options );
        NestedDCDCECollection.prototype.initialize.call( this, attrs, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'NestedPairDCDCECollection(', this.length, ')' ].join( '' ));
    }
});


//==============================================================================
/** @class Backbone Model for a DatasetCollection (list) that contains DatasetCollections (pairs).
 */
var ListPairedDatasetCollection = DatasetCollection.extend(
/** @lends ListPairedDatasetCollection.prototype */{

    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    /** list:paired is the only collection that itself contains collections */
    collectionClass : NestedPairDCDCECollection,

//TODO: unused?
    /** set up */
    initialize : function( attributes, options ){
        this.debug( this + '(ListPairedDatasetCollection).initialize:', attributes, options );
        DatasetCollection.prototype.initialize.call( this, attributes, options );
    },

    /** String representation. */
    toString : function(){
         return ([ 'ListPairedDatasetCollection(', this.get( 'name' ), ')' ].join( '' ));
    }
});


//==============================================================================
    return {
        ListDatasetCollection               : ListDatasetCollection,
        PairDatasetCollection               : PairDatasetCollection,
        ListPairedDatasetCollection         : ListPairedDatasetCollection
    };
});
Back to Top