1 // MISC 2 3 /** 4 * @namespace 5 */ 6 7 var Util = {}; 8 9 Util.is_ie = navigator.appVersion.indexOf('MSIE') >= 0; 10 Util.is_ie6 = navigator.appVersion.indexOf('MSIE 6') >= 0; 11 Util.addCommas = function(nStr) 12 { 13 nStr += ''; 14 var x = nStr.split('.'); 15 var x1 = x[0]; 16 var x2 = x.length > 1 ? '.' + x[1] : ''; 17 var rgx = /(\d+)(\d{3})/; 18 while (rgx.test(x1)) { 19 x1 = x1.replace(rgx, '$1' + ',' + '$2'); 20 } 21 return x1 + x2; 22 }; 23 24 Util.wheel = function(event){ 25 var delta = 0; 26 if (!event) event = window.event; 27 if (event.wheelDelta) { 28 delta = event.wheelDelta/120; 29 if (window.opera) delta = -delta; 30 } else if (event.detail) { delta = -event.detail/3; } 31 return Math.round(delta); //Safari Round 32 }; 33 34 Util.isRightButton = function(e) { 35 if (!e) var e = window.event; 36 if (e.which) return e.which == 3; 37 else if (e.button) return e.button == 2; 38 }; 39 40 Util.getViewportWidth = function() { 41 var width = 0; 42 if( document.documentElement && document.documentElement.clientWidth ) { 43 width = document.documentElement.clientWidth; 44 } 45 else if( document.body && document.body.clientWidth ) { 46 width = document.body.clientWidth; 47 } 48 else if( window.innerWidth ) { 49 width = window.innerWidth - 18; 50 } 51 return width; 52 }; 53 54 Util.getViewportHeight = function() { 55 var height = 0; 56 if( document.documentElement && document.documentElement.clientHeight ) { 57 height = document.documentElement.clientHeight; 58 } 59 else if( document.body && document.body.clientHeight ) { 60 height = document.body.clientHeight; 61 } 62 else if( window.innerHeight ) { 63 height = window.innerHeight - 18; 64 } 65 return height; 66 }; 67 68 Util.findNearest = function(numArray, num) { 69 var minIndex = 0; 70 var min = Math.abs(num - numArray[0]); 71 for (var i = 0; i < numArray.length; i++) { 72 if (Math.abs(num - numArray[i]) < min) { 73 minIndex = i; 74 min = Math.abs(num - numArray[i]); 75 } 76 } 77 return minIndex; 78 }; 79 80 /** 81 * replace variables in a template string with values 82 * @param template String with variable names in curly brackets 83 * e.g., "http://foo/{bar}?arg={baz} 84 * @param fillWith object with attribute-value mappings 85 * e.g., {'bar': 'someurl', 'baz': 'valueforbaz'} 86 * @returns the template string with variables in fillWith replaced 87 * e.g., 'htp://foo/someurl?arg=valueforbaz' 88 */ 89 Util.fillTemplate = function(template, fillWith) { 90 return template.replace(/\{([^}]+)\}/g, 91 function(match, group) { 92 if (fillWith[group] !== undefined) 93 return fillWith[group]; 94 else 95 return "{" + group + "}"; 96 }); 97 }; 98 99 /** 100 * function to load a specified resource only once 101 * @param {Object} dojoXhrArgs object containing arguments for dojo.xhrGet, 102 * like <code>url</code> and <code>handleAs</code> 103 * @param {Object} stateObj object that stores the state of the load 104 * @param {Function} successCallback function to call on a successful load 105 * @param {Function} errorCallback function to call on an unsuccessful load 106 */ 107 Util.maybeLoad = function ( dojoXhrArgs, stateObj, successCallback, errorCallback) { 108 if (stateObj.state) { 109 if ("loaded" == stateObj.state) { 110 successCallback(stateObj.data); 111 } else if ("error" == stateObj.state) { 112 errorCallback(); 113 } else if ("loading" == stateObj.state) { 114 stateObj.successCallbacks.push(successCallback); 115 if (errorCallback) stateObj.errorCallbacks.push(errorCallback); 116 } 117 } else { 118 stateObj.state = "loading"; 119 stateObj.successCallbacks = [successCallback]; 120 stateObj.errorCallbacks = [errorCallback]; 121 122 var args = dojo.clone( dojoXhrArgs ); 123 args.load = function(o) { 124 stateObj.state = "loaded"; 125 stateObj.data = o; 126 var cbs = stateObj.successCallbacks; 127 for (var c = 0; c < cbs.length; c++) cbs[c](o); 128 }; 129 args.error = function(error) { 130 console.error(''+error); 131 stateObj.state = "error"; 132 var cbs = stateObj.errorCallbacks; 133 for (var c = 0; c < cbs.length; c++) cbs[c](); 134 }; 135 136 dojo.xhrGet( args ); 137 } 138 }; 139 140 /** 141 * updates a with values from b, recursively 142 */ 143 Util.deepUpdate = function(a, b) { 144 for (var prop in b) { 145 if ((prop in a) 146 && ("object" == typeof b[prop]) 147 && ("object" == typeof a[prop]) ) { 148 Util.deepUpdate(a[prop], b[prop]); 149 } else if( typeof a[prop] == 'undefined' || typeof b[prop] != undefined ){ 150 a[prop] = b[prop]; 151 } 152 } 153 return a; 154 }; 155 156 // from http://bugs.dojotoolkit.org/ticket/5794 157 Util.resolveUrl = function(baseUrl, relativeUrl) { 158 // summary: 159 // This takes a base url and a relative url and resolves the target url. 160 // For example: 161 // resolveUrl("http://www.domain.com/path1/path2","../path3") ->"http://www.domain.com/path1/path3" 162 // 163 if (relativeUrl.match(/\w+:\/\//)) 164 return relativeUrl; 165 if (relativeUrl.charAt(0)=='/') { 166 baseUrl = baseUrl.match(/.*\/\/[^\/]*/); 167 return (baseUrl ? baseUrl[0] : '') + relativeUrl; 168 } 169 //TODO: handle protocol relative urls: ://www.domain.com 170 baseUrl = baseUrl.substring(0,baseUrl.length - baseUrl.match(/[^\/]*$/)[0].length);// clean off the trailing path 171 if (relativeUrl == '.') 172 return baseUrl; 173 while (relativeUrl.substring(0,3) == '../') { 174 baseUrl = baseUrl.substring(0,baseUrl.length - baseUrl.match(/[^\/]*\/$/)[0].length); 175 relativeUrl = relativeUrl.substring(3); 176 } 177 return baseUrl + relativeUrl; 178 }; 179 180 Util.parseLocString = function( locstring ) { 181 locstring = dojo.trim( locstring ); 182 183 // (chromosome) ( start ) ( sep ) ( end ) 184 var matches = locstring.match(/^(((\S*)\s*:)?\s*(-?[\d,.']+)\s*(\.\.|-|\s+))?\s*(-?[\d,.']+)$/i); 185 //matches potentially contains location components: 186 //matches[3] = chromosome (optional) 187 //matches[4] = start base (optional) 188 //matches[6] = end base (or center base, if it's the only one) 189 190 if( !matches ) 191 return null; 192 193 // parses a number from a locstring that's a coordinate, and 194 // converts it from 1-based to interbase coordinates 195 var parseCoord = function( coord ) { 196 coord = (coord+'').replace(/\D/g,''); 197 var num = parseInt( coord, 10 ); 198 return typeof num == 'number' && !isNaN(num) ? num : null; 199 }; 200 201 return { 202 start: parseCoord( matches[4] )-1, 203 end: parseCoord( matches[6] ), 204 ref: matches[3] 205 }; 206 }; 207 208 Util.assembleLocString = function( loc_in ) { 209 var s = '', 210 types = { start: 'number', end: 'number', ref: 'string' }, 211 location = {} 212 ; 213 214 // filter the incoming loc_in to only pay attention to slots that we 215 // know how to handle 216 for( var slot in types ) { 217 if( types[slot] == typeof loc_in[slot] 218 && (types[slot] != 'number' || !isNaN(loc_in[slot])) //filter any NaNs 219 ) { 220 location[slot] = loc_in[slot]; 221 } 222 } 223 224 //finally assemble our string 225 if( 'ref' in location ) { 226 s += location.ref; 227 if( location.start || location.end ) 228 s += ':'; 229 } 230 if( 'start' in location ) { 231 s += (Math.round(location.start)+1).toFixed(0).toLocaleString(); 232 if( 'end' in location ) 233 s+= '..'; 234 } 235 if( 'end' in location ) 236 s += Math.round(location.end).toFixed(0).toLocaleString(); 237 238 return s; 239 }; 240 241 // given a possible reference sequence name and an object as { 'foo': 242 // <refseq foo>, ... }, try to match that reference sequence name 243 // against the actual name of one of the reference sequences. returns 244 // the reference sequence record, or null 245 // if none matched. 246 Util.matchRefSeqName = function( name, refseqs ) { 247 for( var ref in refseqs ) { 248 if( ! refseqs.hasOwnProperty(ref) ) 249 continue; 250 251 var ucname = name.toUpperCase(); 252 var ucref = ref.toUpperCase(); 253 254 if( ucname == ucref 255 || "CHR" + ucname == ucref 256 || ucname == "CHR" + ucref 257 ) { 258 return refseqs[ref]; 259 } 260 } 261 return null; 262 }; 263 264 /** 265 * Wrap a handler function to be called 1ms later in a window timeout. 266 * This will usually give a better stack trace for figuring out where 267 * errors are happening. 268 */ 269 Util.debugHandler = function( context, func ) { 270 return function() { 271 var args = arguments; 272 window.setTimeout( function() { 273 var f = func; 274 if( typeof f == 'string' ) 275 f = context[f]; 276 f.apply(context,args); 277 }, 1); 278 }; 279 }; 280 281 Util.ucFirst = function(str) { 282 if( typeof str != 'string') return undefined; 283 return str.charAt(0).toUpperCase() + str.slice(1); 284 }; 285 286 /** 287 * Uniqify an array. 288 * @param stuff {Array} array of stuff 289 * @param normalizer {Function} optional function to be called on each 290 * element. by default, just compares by stringification 291 */ 292 Util.uniq = function( stuff, normalizer ) { 293 normalizer = normalizer || function(t) { 294 return ''+t; 295 }; 296 var result = [], 297 seen = {}; 298 dojo.forEach( stuff, function(thing) { 299 var norm = normalizer(thing); 300 if( !seen[ normalizer(thing) ] ) 301 result.push( thing ); 302 seen[norm] = true; 303 }); 304 return result; 305 }; 306 307 Util.crc32Table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D"; 308 309 /** 310 * Does a (deep) crc32 of any object. 311 * @returns {Number} 312 */ 313 Util.objectFingerprint = function( obj ) { 314 var crc = 0, 315 x = 0, 316 y = 0; 317 318 var table = Util.crc32Table; 319 var crcstr = function( str ) { 320 for( var i = 0, iTop = str.length; i < iTop; i++ ) { 321 y = ( crc ^ str.charCodeAt( i ) ) & 0xFF; 322 x = "0x" + table.substr( y * 9, 8 ); 323 crc = ( crc >>> 8 ) ^ x; 324 } 325 }; 326 327 if( typeof obj == 'object' ) { 328 for( var prop in obj ) { 329 crcstr( '' + Util.objectFingerprint( prop )); 330 crcstr( '' + Util.objectFingerprint( obj[prop] )); 331 } 332 } else { 333 crcstr( ''+obj ); 334 } 335 return crc; 336 }; 337 338 339 340 if (!Array.prototype.reduce) 341 { 342 Array.prototype.reduce = function(fun /*, initial*/) 343 { 344 var len = this.length; 345 if (typeof fun != "function") 346 throw new TypeError(); 347 348 // no value to return if no initial value and an empty array 349 if (len == 0 && arguments.length == 1) 350 throw new TypeError(); 351 352 var i = 0; 353 if (arguments.length >= 2) 354 { 355 var rv = arguments[1]; 356 } 357 else 358 { 359 do 360 { 361 if (i in this) 362 { 363 rv = this[i++]; 364 break; 365 } 366 367 // if array contains no values, no initial value to return 368 if (++i >= len) 369 throw new TypeError(); 370 } 371 while (true); 372 } 373 374 for (; i < len; i++) 375 { 376 if (i in this) 377 rv = fun.call(null, rv, this[i], i, this); 378 } 379 380 return rv; 381 }; 382 } 383 384 if (!Array.prototype.map) 385 { 386 Array.prototype.map = function(fun /*, thisp */) 387 { 388 "use strict"; 389 390 if (this === void 0 || this === null) 391 throw new TypeError(); 392 393 var t = Object(this); 394 var len = t.length >>> 0; 395 if (typeof fun !== "function") 396 throw new TypeError(); 397 398 var res = new Array(len); 399 var thisp = arguments[1]; 400 for (var i = 0; i < len; i++) 401 { 402 if (i in t) 403 res[i] = fun.call(thisp, t[i], i, t); 404 } 405 406 return res; 407 }; 408 } 409 410 if (!Array.prototype.indexOf) 411 { 412 Array.prototype.indexOf = function(searchElement /*, fromIndex */) 413 { 414 "use strict"; 415 416 if (this === void 0 || this === null) 417 throw new TypeError(); 418 419 var t = Object(this); 420 var len = t.length >>> 0; 421 if (len === 0) 422 return -1; 423 424 var n = 0; 425 if (arguments.length > 0) 426 { 427 n = Number(arguments[1]); 428 if (n !== n) // shortcut for verifying if it's NaN 429 n = 0; 430 else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) 431 n = (n > 0 || -1) * Math.floor(Math.abs(n)); 432 } 433 434 if (n >= len) 435 return -1; 436 437 var k = n >= 0 438 ? n 439 : Math.max(len - Math.abs(n), 0); 440 441 for (; k < len; k++) 442 { 443 if (k in t && t[k] === searchElement) 444 return k; 445 } 446 return -1; 447 }; 448 } 449 450 /** 451 * @class 452 */ 453 function Finisher(fun) { 454 this.fun = fun; 455 this.count = 0; 456 } 457 458 Finisher.prototype.inc = function() { 459 this.count++; 460 }; 461 462 Finisher.prototype.dec = function() { 463 this.count--; 464 this.finish(); 465 }; 466 467 Finisher.prototype.finish = function() { 468 if (this.count <= 0) this.fun(); 469 }; 470 471 472 /* 473 474 Copyright (c) 2007-2010 The Evolutionary Software Foundation 475 476 Created by Mitchell Skinner <mitch_skinner@berkeley.edu> 477 478 This package and its accompanying libraries are free software; you can 479 redistribute it and/or modify it under the terms of the LGPL (either 480 version 2.1, or at your option, any later version) or the Artistic 481 License 2.0. Refer to LICENSE for the full license text. 482 483 */ 484