1 dojo.declare( 'JBrowse.Model.modEncodeTrackMetadata', null,
  2 
  3 /**
  4  * @lends JBrowse.Model.modEncodeTrackMetadata.prototype
  5  */
  6 {
  7     /**
  8      * Track metadata datasource that understands the format of the
  9      * modencode.js track metadata JSON currently (May 2012) used at
 10      * data.modencode.org.
 11      * @constructor
 12      * @param args.url {String} URL to fetch the metadata JSON from
 13      */
 14     constructor: function( args ) {
 15         this.url = args.url;
 16     },
 17 
 18     // dojo.data.api.Read support
 19     getValue: function( i, attr, defaultValue ) {
 20         var v = i[attr];
 21         return typeof v == 'undefined' ? defaultValue : v;
 22     },
 23     getValues: function( i, attr ) {
 24         var a = [ i[attr] ];
 25         return typeof a[0] == 'undefined' ? [] : a;
 26     },
 27 
 28     getAttributes: function(item)  {
 29         return dojof.keys( item );
 30     },
 31 
 32     hasAttribute: function(item,attr) {
 33         return item.hasOwnProperty(attr);
 34     },
 35 
 36     containsValue: function(item, attribute, value) {
 37         return item[attribute] == value;
 38     },
 39 
 40     isItem: function(item) {
 41         return typeof item == 'object' && typeof item.label == 'string';
 42     },
 43 
 44     isItemLoaded: function() {
 45         return true;
 46     },
 47 
 48     loadItem: function( args ) {
 49     },
 50 
 51     // used by the dojo.data.util.simpleFetch mixin to implement fetch()
 52     _fetchItems: function( keywordArgs, findCallback, errorCallback ) {
 53         dojo.xhrGet({
 54             url: this.url,
 55             handleAs: 'json',
 56             load: dojo.hitch(this, function( data ) {
 57                 var items = [];
 58                 dojo.forEach( data.items || [], function(i) {
 59                     if( Array.isArray( i.Tracks ) )
 60                         dojo.forEach( i.Tracks, function(trackName) {
 61                             var item = dojo.clone(i);
 62                             item.key = item.label;
 63                             item.label = trackName;
 64                             delete item.Tracks;
 65                             items.push( item );
 66                         },this);
 67                 },this);
 68                 findCallback( items, keywordArgs );
 69             }),
 70             error: function(e) { errorCallback(e,keywordArgs); }
 71         });
 72     },
 73 
 74     getFeatures: function() {
 75         return {
 76 	    'dojo.data.api.Read': true,
 77 	    'dojo.data.api.Identity': true
 78 	};
 79     },
 80     close: function() {},
 81 
 82     getLabel: function(i) {
 83         return this.getValue(i,'key',undefined);
 84     },
 85     getLabelAttributes: function(i) {
 86         return ['key'];
 87     },
 88 
 89     // dojo.data.api.Identity support
 90     getIdentityAttributes: function() {
 91         return ['label'];
 92     },
 93     getIdentity: function(i) {
 94         return this.getValue(i, 'label', undefined);
 95     },
 96     fetchItemByIdentity: function(id) {
 97         return this.identIndex[id];
 98     }
 99 });
100 dojo.require('dojo.data.util.simpleFetch');
101 dojo.extend( JBrowse.Model.modEncodeTrackMetadata, dojo.data.util.simpleFetch );
102