Mercurial > repos > jankanis > blast2html
comparison Blast output page example trimmed_files/utils.js @ 1:ffe82516a83c
add trimmed down html example
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Tue, 06 May 2014 17:36:10 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:bad241dc701f | 1:ffe82516a83c |
---|---|
1 // $Id: utils.js,v 1.4 2014/02/26 14:47:32 zaretska Exp $ | |
2 | |
3 utils = { | |
4 | |
5 KeyCode_TAB: 9, | |
6 KeyCode_DELETE: 46, | |
7 KeyCode_BACKSPACE: 8, | |
8 KeyCode_LEFT_ARROW: 37, | |
9 KeyCode_RIGHT_ARROW: 39, | |
10 KeyCode_HOME: 36, | |
11 KeyCode_END: 35, | |
12 KeyCode_PAGE_UP: 33, | |
13 KeyCode_PAGE_DOWN: 34, | |
14 KeyCode_UP_ARROW: 38, | |
15 KeyCode_DOWN_ARROW: 40, | |
16 KeyCode_ESC: 27, | |
17 KeyCode_ENTER: 13, | |
18 KeyCode_SPACE: 32, | |
19 KeyCode_SHIFT_KEY: 16, | |
20 KeyCode_CTRL_KEY: 17, | |
21 KeyCode_ALT_KEY: 18, | |
22 KeyCode_LEFT_MS_WINDOWS_KEY: 91, | |
23 KeyCode_RIGHT_MS_WINDOWS_KEY: 92, | |
24 KeyCode_MS_MENU_KEY: 93, | |
25 | |
26 isObject: function(a) { return (a && typeof a == 'object'); }, | |
27 | |
28 isArray: function(a) { return this.isObject(a) && a.constructor == Array; }, | |
29 | |
30 insertInHtml: function(text, obj) { | |
31 if (document.all) { | |
32 obj.innerHTML += text; | |
33 } else { | |
34 var range = document.createRange(); | |
35 range.setStartAfter(obj); | |
36 var docFrag = range.createContextualFragment(text); | |
37 obj.appendChild(docFrag); | |
38 } | |
39 | |
40 }, | |
41 | |
42 replaceInHtml: function(text, obj) { | |
43 if (document.all) { | |
44 obj.innerHTML = text; | |
45 } else { | |
46 while (obj.hasChildNodes()) obj.removeChild(obj.firstChild); | |
47 var range = document.createRange(); | |
48 range.setStartAfter(obj); | |
49 var docFrag = range.createContextualFragment(text); | |
50 obj.appendChild(docFrag); | |
51 } | |
52 }, | |
53 | |
54 | |
55 getTargetObj: function(eEvent) { | |
56 var oTarget; | |
57 var e = eEvent || window.event; | |
58 if (e == null) return null; | |
59 if (e.srcElement == null) { | |
60 oTarget = e.target; | |
61 } else { | |
62 oTarget = e.srcElement; | |
63 } | |
64 while ( oTarget && oTarget.nodeType != 1 ) oTarget = oTarget.parentNode; | |
65 return oTarget; | |
66 }, | |
67 | |
68 | |
69 | |
70 | |
71 getParent: function(obj) { | |
72 if (obj) { | |
73 var result = obj.parentNode; | |
74 while (result && result.nodeType != 1) result = result.nextSibling; | |
75 if (result) return result; | |
76 } | |
77 return null; | |
78 }, | |
79 | |
80 getFirstChild: function(obj) { | |
81 if (obj) { | |
82 var result = obj.firstChild; | |
83 while (result && result.nodeType != 1) result = result.nextSibling; | |
84 if (result) return result; | |
85 } | |
86 return null; | |
87 }, | |
88 | |
89 getNextSibling: function(obj, tagName) { | |
90 if (obj) { | |
91 var result = obj.nextSibling; | |
92 if (tagName) { | |
93 var tn = tagName.toUpperCase(); | |
94 while (result && result.tagName != tn) result = result.nextSibling; | |
95 } else { | |
96 while (result && result.nodeType != 1) result = result.nextSibling; | |
97 } | |
98 return result; | |
99 } | |
100 return null; | |
101 }, | |
102 | |
103 getPreviousSibling: function(obj, tagName) { | |
104 if (obj) { | |
105 var result = obj.previousSibling; | |
106 if (tagName) { | |
107 var tn = tagName.toUpperCase(); | |
108 while (result && result.tagName != tn) result = result.previousSibling; | |
109 } else { | |
110 while (result && result.nodeType != 1) result = result.previousSibling; | |
111 } | |
112 return result; | |
113 } | |
114 return null; | |
115 }, | |
116 | |
117 removeChildren: function(oObj) { | |
118 if (!oObj || typeof oObj != "object") return; | |
119 while(oObj.hasChildNodes()) oObj.removeChild(oObj.firstChild) | |
120 }, | |
121 | |
122 insertAfter: function(parent, node, referenceNode) { | |
123 parent.insertBefore(node, referenceNode.nextSibling); | |
124 }, | |
125 | |
126 nextItem: function(item, nodeName) { | |
127 if (item == null) return; | |
128 var next = item.nextSibling; | |
129 while (next != null) { | |
130 if (next.nodeName == nodeName) return next; | |
131 next = next.nextSibling; | |
132 } | |
133 return null; | |
134 }, | |
135 | |
136 previousItem: function(item, nodeName) { | |
137 var previous = item.previousSibling; | |
138 while (previous != null) { | |
139 if (previous.nodeName == nodeName) return previous; | |
140 previous = previous.previousSibling; | |
141 } | |
142 return null | |
143 }, | |
144 | |
145 moveBefore: function(item1, item2) { | |
146 var parent = item1.parentNode; | |
147 parent.removeChild(item1); | |
148 parent.insertBefore(item1, item2); | |
149 }, | |
150 | |
151 moveAfter: function(item1, item2) { | |
152 var parent = item1.parentNode; | |
153 parent.removeChild(item1); | |
154 parent.insertBefore(item1, item2 ? item2.nextSibling : null); | |
155 }, | |
156 | |
157 | |
158 | |
159 | |
160 createCookie: function(name, value, days) { | |
161 if (days) { | |
162 var date = new Date(); | |
163 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
164 var expires = '; expires=' + date.toGMTString(); | |
165 } else expires = ''; | |
166 document.cookie = name + '=' + value + expires + '; path=/'; | |
167 }, | |
168 | |
169 readCookie: function(name) { | |
170 var nameEQ = name + '='; | |
171 var ca = document.cookie.split(';'); | |
172 for (var i = 0; i < ca.length; i++) { | |
173 var c = ca[i]; | |
174 while (c.charAt(0)==' ') c = c.substring(1, c.length); | |
175 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); | |
176 } | |
177 return ""; | |
178 }, | |
179 | |
180 eraseCookie: function(name) { | |
181 document.cookie = name + "=null; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=nih.gov; path=/"; | |
182 document.cookie = name + "; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=nih.gov; path=/"; | |
183 }, | |
184 | |
185 addClass: function(element, className) { | |
186 if (!this.hasClass(element, className)) { | |
187 if (element.className) element.className += " " + className; | |
188 else element.className = className; | |
189 } | |
190 }, | |
191 | |
192 removeClass: function(element, className) { | |
193 var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)"); | |
194 element.className = element.className.replace(regexp, "$2"); | |
195 }, | |
196 | |
197 hasClass: function(element, className) { | |
198 var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)"); | |
199 return regexp.test(element.className); | |
200 }, | |
201 | |
202 | |
203 getXY: function (obj){ | |
204 /* | |
205 +------------- w ---- | |
206 | (x,y) | |
207 | | |
208 h | |
209 | | |
210 */ | |
211 var b={x:0, y:0, w:obj.offsetWidth, h:obj.offsetHeight}; | |
212 | |
213 if (obj.offsetParent) { | |
214 while(obj) { | |
215 b.x += obj.offsetLeft; | |
216 b.y += obj.offsetTop; | |
217 obj = obj.offsetParent; | |
218 } | |
219 } else if (obj.x) { | |
220 b.x = obj.x; | |
221 b.y = obj.y; | |
222 } | |
223 return b; | |
224 }, | |
225 | |
226 /* Based on ppk (untested) */ | |
227 getEventXY: function(e) { | |
228 var xpos = 0; | |
229 var ypos = 0; | |
230 e = e || window.event; | |
231 xpos = e.pageX || (e.clientX + document.body.scrollLeft + | |
232 document.documentElement.scrollLeft); | |
233 ypos = e.pageY || (e.clientY + document.body.scrollTop + | |
234 document.documentElement.scrollTop); | |
235 return {x: xpos, y: ypos}; | |
236 }, | |
237 | |
238 drawText: function (sText, sId, add) { | |
239 if (!sId) sId = "debug"; | |
240 var obj = document.getElementById(sId); | |
241 if (obj) { | |
242 if (add) | |
243 this.insertInHtml("<br/>" + sText, obj); | |
244 else | |
245 this.replaceInHtml(sText, obj); | |
246 } | |
247 }, | |
248 | |
249 | |
250 selectRange: function (oObj /*:object*/, iStart /*:int*/, iLength /*:int*/) { | |
251 if (!(oObj && oObj.value)) return; | |
252 | |
253 if (oObj.createTextRange) { | |
254 //use text ranges for Internet Explorer | |
255 var oRange = oObj.createTextRange(); | |
256 oRange.moveStart("character", iStart); | |
257 oRange.moveEnd("character", iLength - oObj.value.length); | |
258 oRange.select(); | |
259 } else if (oObj.setSelectionRange) { | |
260 //use setSelectionRange() for Mozilla | |
261 oObj.setSelectionRange(iStart, iLength); | |
262 } | |
263 //set focus back to the textbox | |
264 oObj.focus(); | |
265 }, | |
266 | |
267 getSelection: function() { | |
268 var text = ""; | |
269 if (window.getSelection) { | |
270 text += window.getSelection(); | |
271 } else if (document.getSelection) { | |
272 text += document.getSelection(); | |
273 } else if (document.selection){ //IE | |
274 text += document.selection.createRange().text; | |
275 } | |
276 return text; | |
277 }, | |
278 | |
279 | |
280 | |
281 | |
282 // http://ejohn.org/apps/jselect/event.html | |
283 addEvent: function(obj, type, fn, b) { | |
284 if (obj.attachEvent) { | |
285 var name = "" + type + fn; | |
286 // name = name.substring(0, name.indexOf("\n")); // IE This doesn't work | |
287 obj["e" + name] = fn; | |
288 obj[name] = function(){ obj["e" + name](window.event);} | |
289 obj.attachEvent("on" + type, obj[name]); | |
290 } else { | |
291 obj.addEventListener(type, fn, b); | |
292 return true; | |
293 } | |
294 }, | |
295 | |
296 | |
297 removeEvent: function(obj, type, fn, b) { | |
298 if (obj.detachEvent) { | |
299 var name = "" + type + fn; | |
300 // name = name.substring(0, name.indexOf("\n")); //IE This doesn't work | |
301 if ("function" == typeof obj[name]) { | |
302 obj.detachEvent("on" + type, obj[name]); | |
303 obj[name] = null; | |
304 obj["e" + name] = null; | |
305 } | |
306 } else { | |
307 obj.removeEventListener(type, fn, b); | |
308 return true; | |
309 } | |
310 }, | |
311 | |
312 noBubbleEvent: function(e) { | |
313 if (e && e.stopPropagation) e.stopPropagation(); | |
314 else window.event.cancelBubble = true; | |
315 }, | |
316 | |
317 targetEvent: function(e) { | |
318 if (e.srcElement == null) { | |
319 return e.target; | |
320 } else { | |
321 return window.event.srcElement; | |
322 } | |
323 }, | |
324 | |
325 preventDefault: function(e) { | |
326 if (e.preventDefault) e.preventDefault(); | |
327 else window.event.returnValue = false; | |
328 }, | |
329 | |
330 relatedTarget: function(e) { | |
331 if (!e) var e = window.event; | |
332 if (e.relatedTarget) return e.relatedTarget; | |
333 else if (e.toElement) return e.toElement; | |
334 else if (e.fromElement) return e.fromElement; | |
335 }, | |
336 | |
337 readStyle: function(element, property) { | |
338 if (element.style[property]) { | |
339 return element.style[property]; | |
340 } else if (element.currentStyle) { | |
341 return element.currentStyle[property]; | |
342 } else if (document.defaultView && document.defaultView.getComputedStyle) { | |
343 var style = document.defaultView.getComputedStyle(element, null); | |
344 if (style) return style.getPropertyValue(property); | |
345 } | |
346 return ""; | |
347 }, | |
348 | |
349 | |
350 printObj: function (oObj, iLevel) { | |
351 var s = ""; | |
352 var sIdent = ""; | |
353 if (!iLevel) iLevel = 0; | |
354 for (var i = 0; i < iLevel; i++) { | |
355 sIdent += "__"; | |
356 } | |
357 for (var i in oObj) { | |
358 var ss = []; | |
359 if ("string" == typeof oObj[i]) { | |
360 ss = oObj[i].split("<"); | |
361 } | |
362 s += sIdent + " " + i + " : [" + (typeof oObj[i]) + "] : " + ss.join("<") + "<br/>"; | |
363 // if (oObj[i] && "object" == typeof oObj[i] && iLevel < 2) { | |
364 // s+= "<br/>-----" + typeof oObj[i] + " --- " + iLevel + "</br>"; | |
365 // s += this.printObj(oObj[i], iLevel + 1); | |
366 // } | |
367 } | |
368 return s; | |
369 }, | |
370 | |
371 jsLoader: { | |
372 sBase: "", /* Base is this directory */ | |
373 oLoaded: [], | |
374 load: function (aScripts) { | |
375 | |
376 var oS = document.getElementsByTagName("script"); | |
377 var k = 0; | |
378 for (var j = 0; j < oS.length; j++) { | |
379 if (oS[j].src == "") continue; | |
380 this.oLoaded[k++] = oS[j].src; | |
381 } | |
382 | |
383 var oHead = document.getElementsByTagName("head")[0]; | |
384 | |
385 for (var i = 0; i < aScripts.length; i++) { | |
386 var sNewSrc = this.sBase + aScripts[i]; | |
387 var oS = document.getElementsByTagName("script"); | |
388 var b = true; | |
389 for (var j = 0; j < this.oLoaded.length; j++) { | |
390 if (sNewSrc == this.oLoaded[j]) { | |
391 // alert(sNewSrc + " : already loaded"); | |
392 b = false; | |
393 } | |
394 } | |
395 | |
396 if (b) { | |
397 var oScript = document.createElement("script"); | |
398 oScript.src = sNewSrc; | |
399 oScript.setAttribute("type", "text/javascript"); | |
400 oHead.appendChild(oScript); | |
401 this.oLoaded[this.oLoaded.length] = sNewSrc; | |
402 } | |
403 } | |
404 } | |
405 }, | |
406 | |
407 // Create an id that doesn't exist in this document | |
408 createNewId: function() | |
409 { | |
410 var newid = null | |
411 | |
412 while (!newid || document.getElementById(newid)) { | |
413 newid = "XID" + Math.round(Math.random() * 65536).toString(16) | |
414 } | |
415 return newid | |
416 } | |
417 | |
418 }; | |
419 | |
420 | |
421 String.prototype.trimSpaces = function(trimMode) { | |
422 // 0 = trim begin and end | |
423 // 1 = trim begin only | |
424 // 2 = trim after only | |
425 | |
426 var targetString = this; | |
427 var iPos = 0; | |
428 if (!trimMode) trimMode = 0; | |
429 | |
430 if (trimMode==0 || trimMode==1) { | |
431 if (targetString.charAt(iPos)==" ") { | |
432 while(targetString.charAt(iPos)==" ") iPos++; | |
433 targetString = targetString.substr(iPos); | |
434 } | |
435 } | |
436 | |
437 iPos = targetString.length-1; | |
438 if (trimMode==0 || trimMode==2) { | |
439 if (targetString.charAt(iPos) == " ") { | |
440 while(targetString.charAt(iPos) == " ") iPos--; | |
441 targetString = targetString.substr(0, iPos + 1); | |
442 } | |
443 } | |
444 return targetString; | |
445 } | |
446 | |
447 | |
448 | |
449 | |
450 /* Shortcuts */ | |
451 | |
452 // Get elements by Id's | |
453 function $() { | |
454 var elements = new Array(); | |
455 | |
456 for (var i = 0; i < arguments.length; i++) { | |
457 var element = arguments[i]; | |
458 if (typeof element == 'string') | |
459 element = document.getElementById(element); | |
460 | |
461 if (arguments.length == 1) | |
462 return element; | |
463 | |
464 elements.push(element); | |
465 } | |
466 | |
467 return elements; | |
468 } | |
469 | |
470 // Get elements by AttributeValue for Attributename | |
471 // http://www.dustindiaz.com/top-ten-javascript/ (but has some errors) | |
472 function $C(attrValue, attrName, node, tag) { | |
473 //alert([attrValue, attrName, node, tag]) | |
474 if ("*" == attrValue) { | |
475 return $AN(attrName, node, tag); | |
476 } | |
477 var oElements = new Array(); | |
478 if (!node) node = document; | |
479 if (!tag) tag = '*'; | |
480 if (!attrName) attrName = 'class'; | |
481 | |
482 var els = node.getElementsByTagName(tag); | |
483 var elsLen = els.length; | |
484 var pattern = new RegExp("(^|\\s)" + attrValue + "(\\s|$)"); | |
485 var j = 0; | |
486 for (i = 0; i < elsLen; i++) { | |
487 if (attrName == "class" && pattern.test(els[i].className)) { | |
488 // IE behavior | |
489 // oElements.push(els[i]); | |
490 oElements[j++] = els[i]; | |
491 } else if (pattern.test(els[i].getAttribute(attrName))) { | |
492 oElements[j++] = els[i]; | |
493 // oElements.push(els[i]); | |
494 } | |
495 } | |
496 return oElements; | |
497 } | |
498 | |
499 | |
500 function $AN(attrName, node, tag) { | |
501 var oElements = new Array(); | |
502 if (node == null) node = document; | |
503 if (tag == null)tag = '*'; | |
504 var els = node.getElementsByTagName(tag); | |
505 for (i = 0; i < els.length; i++) { | |
506 if (els[i].getAttribute(attrName) != null) { | |
507 oElements.push(els[i]); | |
508 } | |
509 } | |
510 return oElements; | |
511 } | |
512 | |
513 function dump(aMessage) { | |
514 var consoleService = Components.classes["@mozilla.org/consoleservice;1"] | |
515 .getService(Components.interfaces.nsIConsoleService); | |
516 consoleService.logStringMessage(aMessage); | |
517 } | |
518 | |
519 | |
520 // forEach iterators from Dean Edwards: http://dean.edwards.name/weblog/2006/07/enum/ | |
521 // generic enumeration | |
522 Function.prototype.forEach = function(object, block, context) { | |
523 for (var key in object) { | |
524 if (typeof this.prototype[key] == "undefined") { | |
525 block.call(context, object[key], key, object); | |
526 } | |
527 } | |
528 }; | |
529 | |
530 // globally resolve forEach enumeration | |
531 var forEach = function(object, block, context) { | |
532 if (object) { | |
533 var resolve = Object; // default | |
534 if (object instanceof Function) { | |
535 // functions have a "length" property | |
536 resolve = Function; | |
537 } else if (object.forEach instanceof Function) { | |
538 // the object implements a custom forEach method so use that | |
539 object.forEach(block, context); | |
540 return; | |
541 } else if (typeof object.length == "number") { | |
542 // the object is array-like | |
543 resolve = Array; | |
544 } | |
545 resolve.forEach(object, block, context); | |
546 } | |
547 }; | |
548 | |
549 // | |
550 // Update Array class to JS 1.5 if not yet there. | |
551 // | |
552 | |
553 // array-like enumeration | |
554 if (!Array.forEach) { // mozilla already supports this | |
555 Array.forEach = function(object, block, context) { | |
556 for (var i = 0; i < object.length; i++) { | |
557 block.call(context, object[i], i, object); | |
558 } | |
559 }; | |
560 } | |
561 | |
562 if (!Array.prototype.indexOf) | |
563 Array.prototype.indexOf = function(item, startIndex) { | |
564 var len = this.length; | |
565 if (startIndex == null) | |
566 startIndex = 0; | |
567 else if (startIndex < 0) { | |
568 startIndex += len; | |
569 if (startIndex < 0) | |
570 startIndex = 0; | |
571 } | |
572 for (var i = startIndex; i < len; i++) { | |
573 var val = this[i] || this.charAt && this.charAt(i); | |
574 if (val == item) | |
575 return i; | |
576 } | |
577 return -1; | |
578 }; | |
579 | |
580 if (!Array.prototype.lastIndexOf) | |
581 Array.prototype.lastIndexOf = function(item, startIndex) { | |
582 var len = this.length; | |
583 if (startIndex == null || startIndex >= len) | |
584 startIndex = len - 1; | |
585 else if (startIndex < 0) | |
586 startIndex += len; | |
587 for (var i = startIndex; i >= 0; i--) { | |
588 var val = this[i] || this.charAt && this.charAt(i); | |
589 if (val == item) | |
590 return i; | |
591 } | |
592 return -1; | |
593 }; | |
594 | |
595 if (!Array.prototype.map) | |
596 Array.prototype.map = function(func, thisVal) { | |
597 var len = this.length; | |
598 var ret = new Array(len); | |
599 for (var i = 0; i < len; i++) | |
600 ret[i] = func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this); | |
601 return ret; | |
602 }; | |
603 | |
604 if (!Array.prototype.filter) | |
605 Array.prototype.filter = function(func, thisVal) { | |
606 var len = this.length; | |
607 var ret = new Array(); | |
608 for (var i = 0; i < len; i++) { | |
609 var val = this[i] || this.charAt && this.charAt(i); | |
610 if(func.call(thisVal, val, i, this)) | |
611 ret[ret.length] = val; | |
612 } | |
613 return ret; | |
614 }; | |
615 | |
616 if (!Array.prototype.every) | |
617 Array.prototype.every = function(func, thisVal) { | |
618 var len = this.length; | |
619 for (var i = 0; i < len; i++) | |
620 if (!func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this)) | |
621 return false; | |
622 return true; | |
623 }; | |
624 | |
625 if (!Array.prototype.some) | |
626 Array.prototype.some = function(func, thisVal) { | |
627 var len = this.length; | |
628 for (var i = 0; i < len; i++) | |
629 if (func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this)) | |
630 return true; | |
631 return false; | |
632 }; | |
633 |