Wikipedia/utils.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // Symbian Foundation Example Code
       
     3 //
       
     4 // This software is in the public domain. No copyright is claimed, and you 
       
     5 // may use it for any purpose without license from the Symbian Foundation.
       
     6 // No warranty for any purpose is expressed or implied by the authors or
       
     7 // the Symbian Foundation. 
       
     8 // ////////////////////////////////////////////////////////////////////////////
       
     9 
       
    10 var logEnabled = true;
       
    11 
       
    12 // Opens a URL in a separate browser window
       
    13 function openURL(url) {
       
    14     if (window.widget) {
       
    15         // in WRT
       
    16         widget.openURL(url);
       
    17     } else {
       
    18         // outside WRT
       
    19         window.open(url, "NewWindow");
       
    20     }
       
    21 }
       
    22 
       
    23 function increaseFontSize(){
       
    24     if (window.widget) {
       
    25 		setCssBodyFontSize(currentFontSize + 2);
       
    26 	}
       
    27 }
       
    28 
       
    29 function decreaseFontSize(){
       
    30     if (window.widget) {
       
    31 		if (currentFontSize > 4) {
       
    32 			setCssBodyFontSize(currentFontSize - 2);
       
    33 		}
       
    34 	}
       
    35 }
       
    36 
       
    37 function setCssBodyFontSize(size){
       
    38     if (window.widget) {
       
    39 		currentFontSize = size;
       
    40 		var sizestring = "" + size;
       
    41 		document.body.style.fontSize = sizestring + "px";
       
    42 		widget.setPreferenceForKey(sizestring, "fontsize");
       
    43 	}
       
    44 }
       
    45 
       
    46 
       
    47 function log(txt) {
       
    48 	if (logEnabled) {
       
    49 //		txt = txt.replace(/</g, unescape("%26lt%3B")).replace(/>/g, unescape("%26gt%3B")); 
       
    50 //		txt = txt.replace(/</g, "&lt");
       
    51 //		txt = txt.replace(/\>/g, "&gt");
       
    52 		document.getElementById("debugdiv").innerHTML = txt + "<br> " + document.getElementById("debugdiv").innerHTML;
       
    53 	} 
       
    54 }
       
    55 
       
    56 function setupLog() {
       
    57 	if (logEnabled) {
       
    58 		if (window.widget) {
       
    59 			var logMenuItem = new MenuItem("Toggle log", 99);
       
    60 			logMenuItem.onSelect = function() {
       
    61 				var div = document.getElementById("debugdiv");
       
    62 				var main = document.getElementById("uimandiv");
       
    63 				if ( div.style.display == "none" ) {
       
    64 					div.style.display = "";
       
    65 					main.style.display = "none";
       
    66 				} else {
       
    67 					div.style.display = "none";
       
    68 					main.style.display = "";
       
    69 				}
       
    70 			}
       
    71 			menu.append(logMenuItem);
       
    72 		}
       
    73 	}
       
    74 }
       
    75 
       
    76 function shorten(text, len) {
       
    77 	if ( text.length < len + 3 ) return text;
       
    78 	return text.substring(0,len) + "...";
       
    79 }
       
    80 
       
    81 function uniDecode(text) {
       
    82 	var ptr = 0;
       
    83 	var buf = "";
       
    84 	while ( ptr < text.length ){
       
    85 		if ( text.charAt(ptr) == '%' ) {
       
    86 			// read next two chars and interpret as hex UTF-8 char code
       
    87 			var hex = "";
       
    88 			ptr ++;
       
    89 			hex += text.charAt(ptr) ;
       
    90 			ptr ++;
       
    91 			hex += text.charAt(ptr) ;
       
    92 			var ccode = unhex(hex);
       
    93 			// decode utf-8
       
    94 			if (ccode < 128) { // 1 byte char
       
    95 				buf += String.fromCharCode(ccode);
       
    96 				ptr++;
       
    97 			}
       
    98 			else if((ccode > 191) && (ccode < 224)) { // 2 byte char 
       
    99 				var hex2 = "";
       
   100 				ptr ++; // move to % 
       
   101 				ptr ++; // move to first hex digit 
       
   102 				hex2 += text.charAt(ptr) ;
       
   103 				ptr ++;
       
   104 				hex2 += text.charAt(ptr) ;
       
   105 				var ccode2 = unhex(hex2);
       
   106 				// need more stuff to get char
       
   107 				buf += String.fromCharCode(((ccode & 31) << 6) | (ccode2 & 63));
       
   108 				ptr ++;
       
   109 			}
       
   110 			else {  // 3 byte char
       
   111 				var hex2 = "";
       
   112 				ptr ++; // move to % 
       
   113 				ptr ++; // move to first hex digit 
       
   114 				hex2 += text.charAt(ptr) ;
       
   115 				ptr ++;
       
   116 				hex2 += text.charAt(ptr) ;
       
   117 				var ccode2 = unhex(hex2);
       
   118 				var hex3 = "";
       
   119 				ptr ++; // move to % 
       
   120 				ptr ++; // move to first hex digit 
       
   121 				hex3 += text.charAt(ptr) ;
       
   122 				ptr ++;
       
   123 				hex3 += text.charAt(ptr) ;
       
   124 				var ccode3 = unhex(hex2);
       
   125 				buf += String.fromCharCode(((ccode & 15) << 12) | ((ccode2 & 63) << 6) | (ccode3 & 63));
       
   126 				ptr++;
       
   127 			}
       
   128 		} else {
       
   129 			buf += text.charAt(ptr);
       
   130 			ptr ++;
       
   131 		}
       
   132 	}
       
   133 	return buf;
       
   134 }
       
   135 
       
   136 function unhex(hx) {
       
   137 	var val = 0;
       
   138 	for ( var i = 0 ; i < hx.length ; i ++ ) {
       
   139 		val = val * 16;
       
   140 		switch(hx.charAt(i)) {
       
   141 			case '0': continue; 
       
   142 			case '1':
       
   143 			case '2':
       
   144 			case '3':
       
   145 			case '4':
       
   146 			case '5':
       
   147 			case '6':
       
   148 			case '7':
       
   149 			case '8':
       
   150 			case '9': {
       
   151 				val += parseInt(hx.charAt(i));
       
   152 				break;
       
   153 			}
       
   154 			case 'A':
       
   155 			case 'a':{
       
   156 				val += 10;
       
   157 				break;
       
   158 			}
       
   159 			case 'B':
       
   160 			case 'b':{
       
   161 				val += 11;
       
   162 				break;
       
   163 			}
       
   164 			case 'C':
       
   165 			case 'c':{
       
   166 				val += 12;
       
   167 				break;
       
   168 			}
       
   169 			case 'D':
       
   170 			case 'd':{
       
   171 				val += 13;
       
   172 				break;
       
   173 			}
       
   174 			case 'E':
       
   175 			case 'e':{
       
   176 				val += 14;
       
   177 				break;
       
   178 			}
       
   179 			case 'F':
       
   180 			case 'f':{
       
   181 				val += 15;
       
   182 				break;
       
   183 			}
       
   184 		}
       
   185 	}
       
   186 	return val;
       
   187 }
       
   188 
       
   189 function shortFormatTime(ts) {
       
   190 	var date = new Date(ts);
       
   191 	var ret = "";
       
   192 	ret += pad(2,"0", date.getDate());
       
   193 	ret += "/";
       
   194 	ret += pad(2,"0",(1+date.getMonth()));
       
   195 	ret += " ";
       
   196 	ret += pad(2,"0", date.getHours());
       
   197 	ret += ":";
       
   198 	ret += pad(2,"0",date.getMinutes());
       
   199 	return ret;
       
   200 }
       
   201 
       
   202 
       
   203 function nocache(url) {
       
   204     if (url.indexOf("?") == -1) {
       
   205         url += "?";
       
   206     } else {
       
   207         url += "&";
       
   208     }
       
   209     url += "xnocache=" + (new Date().getTime());
       
   210 	return url;
       
   211 }
       
   212 
       
   213 function pad(_num, _char, _val) {
       
   214 	var buf = ""+ _val;
       
   215 	while(buf.length < _num){
       
   216 		buf = _char + "" + buf;
       
   217 	}
       
   218 //	alert("pad: num=" +num + ", char=" + char + ", val=" + val +", result=" +buf );
       
   219 	return buf;
       
   220 }
       
   221 
       
   222 function getViewRep(view) {
       
   223 	if ( view == null ) {
       
   224 		return "null";
       
   225 	} else if (view) {
       
   226 		return view.caption;
       
   227 	} else {
       
   228 		return "undefined";
       
   229 	}
       
   230 }
       
   231 
       
   232 
       
   233 function ViewCache(size) {
       
   234 	this.size = size;
       
   235 	this.names = new Array();
       
   236 	this.views = new Array();
       
   237 }
       
   238 
       
   239 ViewCache.prototype.addView = function(name,view) {
       
   240 	this.names.splice(0,0,name);
       
   241 	this.views.splice(0,0,view);
       
   242 	
       
   243 	if (this.names.length > this.size) {
       
   244 		this.names.splice(this.size, 1);
       
   245 		this.views.splice(this.size, 1);
       
   246 	}
       
   247 }
       
   248 
       
   249 ViewCache.prototype.getView = function(name){
       
   250 	for ( var i = 0 ; i < this.names.length ; i++ ) {
       
   251 		if ( name == this.names[i] ) {
       
   252 			return this.views[i];
       
   253 		}
       
   254 	}
       
   255 	return null;
       
   256 }