-
Notifications
You must be signed in to change notification settings - Fork 199
/
Hash.js
148 lines (133 loc) · 5.02 KB
/
Hash.js
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
define( [
'dojo/_base/declare',
'dojo/_base/array',
'dojo/store/util/QueryResults',
'JBrowse/Util',
'JBrowse/Store/Hash',
'JBrowse/Model/Location'
],
function(
declare,
array,
QueryResults,
Util,
HashStore,
Location
) {
return declare( HashStore,
{
constructor: function( args ) {
this.tooManyMatchesMessage = args.tooManyMatchesMessage || '(too many matches to display)';
// generate stopPrefixes
var stopPrefixes = this.stopPrefixes = {};
// make our stopPrefixes an object as { prefix: true, ... }
// with all possible prefixes of our stop prefixes
if( args.stopPrefixes ) {
var prefixesInput = typeof args.stopPrefixes == 'string'
? [ args.stopPrefixes ] : args.stopPrefixes;
dojo.forEach( prefixesInput, function(prefix) {
while( prefix.length ) {
stopPrefixes[prefix] = true;
prefix = prefix.substr( 0, prefix.length - 1 );
}
});
}
},
_nameRecordToItem: function( nameRecord ) {
if( nameRecord.hitLimit ) {
// it's a too-many-matches marker
return { name: this.tooManyMatchesMessage, hitLimit: true };
}
else {
// it's an actual name record
var item = {};
if( typeof nameRecord == 'object' ) {
item.name = nameRecord[0];
item.location = new Location({
ref: nameRecord[3],
start: parseInt( nameRecord[4] ),
end: parseInt( nameRecord[5] ),
tracks: [ ((this.meta||{}).track_names||{})[ nameRecord[1] ] ]
});
} else {
item.name = nameRecord;
}
return item;
}
},
_makeResults: function( nameRecords ) {
// convert the name records into dojo.store-compliant data
// items, sort them by name and location
var results = array.map( nameRecords, dojo.hitch(this,'_nameRecordToItem') )
.sort( function( a, b ) {
return a.name.localeCompare( b.name )
|| a.location.localeCompare( b.location );
});
var last;
var hitLimit;
// aggregate them and make labels for them. for names with
// multiple locations, make a multipleLocations member.
results = array.filter( results, function( i ) {
if( i.hitLimit ) {
hitLimit = i;
if( ! hitLimit.label )
hitLimit.label = hitLimit.name || 'too many matches';
return false;
}
else if( last && last.name == i.name ) {
last.label = last.name + ' <span class="multipleLocations">multiple locations</span>';
if( last.multipleLocations ) {
last.multipleLocations.push( i.location );
} else {
last.multipleLocations = [last.location,i.location];
delete last.location;
}
return false;
}
last = i;
last.label = last.name
+ ( last.location ? ' <span class="locString">'+last.location+'</span>'
: ''
);
return true;
});
if( hitLimit )
results.push( hitLimit );
return QueryResults( results );
},
// case-insensitive, and supports prefix queries like 'foo*'
query: function( query, options ) {
// remove trailing asterisks from query.name
var thisB = this;
var name = ( query.name || '' ).toString();
var trailingStar = /\*$/;
if( trailingStar.test( name ) ) {
name = name.replace( trailingStar, '' );
return this._getEntry( name )
.then( function( value ) {
value = value || {};
return thisB._makeResults( ( value.exact || [] ).concat( value.prefix || [] ) );
});
}
else {
return this._getEntry( name )
.then( function( value ) {
return thisB._makeResults( (value||{}).exact || [] );
});
}
},
get: function( id ) {
return this._getEntry( id )
.then( function( bucket ) {
var nameRec = (bucket.exact||[])[0];
return nameRec ? this._nameRecordToItem( nameRec ) : null;
});
},
_getEntry: function( key ) {
return this._getBucket(key)
.then( function( bucket ) {
return bucket[key];
});
}
});
});