galaxy-central-hbc /static/scripts/mvc/collection/collection-model.js

Language Javascript Lines 251
MD5 Hash 60f32c5960ce906cc15c30ae29dce91d
Repository https://bitbucket.org/hbc/galaxy-central-hbc/ 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
define([
    "mvc/dataset/hda-model",
    "mvc/base-mvc",
    "utils/localization"
], function( HDA_MODEL, BASE_MVC, _l ){
//==============================================================================
/** @class Backbone model for Dataset collection elements.
 *      DC Elements contain a sub-model named 'object'. This class moves that
 *      'object' from the JSON in the attributes list to a full, instantiated
 *      sub-model found in this.object. This is done on intialization and
 *      everytime the 'change:object' event is fired.
 *
 *  @borrows LoggableMixin#logger as #logger
 *  @borrows LoggableMixin#log as #log
 *  @constructs
 */
var DatasetCollectionElement = Backbone.Model.extend( BASE_MVC.LoggableMixin ).extend(
/** @lends DatasetCollectionElement.prototype */{
    //TODO:?? this model may be unneccessary - it reflects the api structure, but...
    //  if we munge the element with the element.object at parse, we can flatten the entire hierarchy

    /** logger used to record this.log messages, commonly set to console */
    // comment this out to suppress log output
    //logger              : console,

    defaults : {
        id                  : null,
        model_class         : 'DatasetCollectionElement',
        element_identifier  : null,
        element_index       : null,
        element_type        : null
    },

    /** Set up.
     *  @see Backbone.Collection#initialize
     */
    initialize : function( model, options ){
        this.info( this + '.initialize:', model, options );
        options = options || {};
        //this._setUpListeners();

        this.object = this._createObjectModel();
        this.on( 'change:object', function(){
            //console.log( 'change:object' );
//TODO: prob. better to update the sub-model instead of re-creating it
            this.object = this._createObjectModel();
        });
    },

    _createObjectModel : function(){
        //console.log( '_createObjectModel', this.get( 'object' ), this.object );
        //TODO: same patterns as HDCA _createElementsModel - refactor to BASE_MVC.hasSubModel?
        if( _.isUndefined( this.object ) ){ this.object = null; }
        if( !this.get( 'object' ) ){ return this.object; }

        var object = this.get( 'object' );
        this.unset( 'object', { silent: true });

        this.debug( 'DCE, element_type:', this.get( 'element_type' ) );
        switch( this.get( 'element_type' ) ){
            case 'dataset_collection':
                this.object = new DatasetCollection( object );
                break;
            case 'hda':
                this.object = new HDA_MODEL.HistoryDatasetAssociation( object );
                break;
            default:
                throw new TypeError( 'Unknown element_type: ' + this.get( 'element_type' ) );
        }
        return this.object;
    },

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


//==============================================================================
/** @class Backbone collection for DCEs.
 *      NOTE: used *only* in second level of list:paired collections (a
 *      collection that contains collections)
 *
 *  @borrows LoggableMixin#logger as #logger
 *  @borrows LoggableMixin#log as #log
 *  @constructs
 */
var DatasetCollectionElementCollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend(
/** @lends DatasetCollectionElementCollection.prototype */{
    model: DatasetCollectionElement,

    // comment this out to suppress log output
    /** logger used to record this.log messages, commonly set to console */
    //logger              : console,

    /** Set up.
     *  @see Backbone.Collection#initialize
     */
    initialize : function( models, options ){
        options = options || {};
        this.info( this + '.initialize:', models, options );
        //this._setUpListeners();
    },

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


//==============================================================================
/** @class Backbone model for Dataset Collections.
 *      DCs contain a bbone collection named 'elements' using the class found in
 *      this.collectionClass (gen. DatasetCollectionElementCollection). DCs move
 *      that 'object' from the JSON in the attributes list to a full, instantiated
 *      collection found in this.elements. This is done on intialization and
 *      everytime the 'change:elements' event is fired.
 *
 *  @borrows LoggableMixin#logger as #logger
 *  @borrows LoggableMixin#log as #log
 *  @constructs
 */
var DatasetCollection = Backbone.Model.extend( BASE_MVC.LoggableMixin ).extend(
/** @lends ListDatasetCollection.prototype */{

    //logger : console,

    /** default attributes for a model */
    defaults : {
        collection_type     : 'list'
    },

    collectionClass : DatasetCollectionElementCollection,

    /**  */
    initialize : function( model, options ){
        this.info( 'DatasetCollection.initialize:', model, options );
        //historyContent.HistoryContent.prototype.initialize.call( this, attrs, options );
        this.elements = this._createElementsModel();
//TODO:?? no way to use parse here?
        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.log( '_createElementsModel', this.get( 'elements' ), this.elements );
//TODO: same patterns as DatasetCollectionElement _createObjectModel - refactor to BASE_MVC.hasSubModel?
        var elements = this.get( 'elements' ) || [];
        this.info( 'elements:', elements );
        this.unset( 'elements', { silent: true });
        this.elements = new this.collectionClass( elements );
        return this.elements;
    },

    hasDetails : function(){
//TODO: this is incorrect for (accidentally) empty collections
        return this.elements.length !== 0;
    },

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


//==============================================================================
/** @class Backbone collection for a collection of collection collections collecting correctly.  */
var DatasetCollectionCollection = Backbone.Collection.extend( BASE_MVC.LoggableMixin ).extend({

    model: DatasetCollection,

    ///** logger used to record this.log messages, commonly set to console */
    //// comment this out to suppress log output
    //logger              : console,

    /** Set up.
     *  @see Backbone.Collection#initialize
     */
    initialize : function( models, options ){
        options = options || {};
        this.info( 'DatasetCollectionCollection.initialize:', models, options );
        //this._setUpListeners();
    },

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


//NOTE: the following prototypes may not be necessary - but I wanted to specifiy
//  them (for now) and allow for the possibility of unique functionality
//==============================================================================
var ListDatasetCollection = DatasetCollection.extend(
/** @lends ListDatasetCollection.prototype */{

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


//==============================================================================
var PairDatasetCollection = DatasetCollection.extend(
/** @lends ListDatasetCollection.prototype */{

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


//==============================================================================
var ListPairedDatasetCollection = DatasetCollection.extend(
/** @lends ListDatasetCollection.prototype */{

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

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


//==============================================================================
    return {
        DatasetCollectionElement            : DatasetCollectionElement,
        DatasetCollectionElementCollection  : DatasetCollectionElementCollection,
        DatasetCollection                   : DatasetCollection,
        DatasetCollectionCollection         : DatasetCollectionCollection,
        ListDatasetCollection               : ListDatasetCollection,
        PairDatasetCollection               : PairDatasetCollection,
        ListPairedDatasetCollection         : ListPairedDatasetCollection
    };
});
Back to Top