Revert revert r40717 of my formatting change r40416.
[lhc/web/wiklou.git] / skins / common / mwsuggest.js
index a655080..2b2bf61 100644 (file)
@@ -25,14 +25,22 @@ var os_mouse_moved = false;
 // delay between keypress and suggestion (in ms)
 var os_search_timeout = 250;
 // these pairs of inputs/forms will be autoloaded at startup
-var os_autoload_inputs = new Array('searchInput', 'powerSearchText', 'searchText');
-var os_autoload_forms = new Array('searchform', 'powersearch', 'search' );
+var os_autoload_inputs = new Array('searchInput', 'searchInput2', 'powerSearchText', 'searchText');
+var os_autoload_forms = new Array('searchform', 'searchform2', 'powersearch', 'search' );
 // if we stopped the service
 var os_is_stopped = false;
 // max lines to show in suggest table
 var os_max_lines_per_suggest = 7;
-// if we are about to focus the searchbox for the first time
-var os_first_focus = true;
+// number of steps to animate expansion/contraction of container width
+var os_animation_steps = 6;
+// num of pixels of smallest step
+var os_animation_min_step = 2;
+// delay between steps (in ms)
+var os_animation_delay = 30;
+// max width of container in percent of normal size (1 == 100%) 
+var os_container_max_width = 2;
+// currently active animation timer
+var os_animation_timer = null;
 
 /** Timeout timer class that will fetch the results */ 
 function os_Timer(id,r,query){
@@ -41,6 +49,18 @@ function os_Timer(id,r,query){
        this.query = query;     
 }
 
+/** Timer user to animate expansion/contraction of container width */
+function os_AnimationTimer(r, target){
+       this.r = r;
+       var current = document.getElementById(r.container).offsetWidth;
+       this.inc = Math.round((target-current) / os_animation_steps);
+       if(this.inc < os_animation_min_step && this.inc >=0)
+               this.inc = os_animation_min_step; // minimal animation step
+       if(this.inc > -os_animation_min_step && this.inc <0)
+               this.inc = -os_animation_min_step;
+       this.target = target;
+}
+
 /** Property class for single search box */
 function os_Results(name, formname){   
        this.searchform = formname; // id of the searchform
@@ -86,9 +106,9 @@ function os_showResults(r){
 function os_operaWidthFix(x){
        // TODO: better css2 incompatibility detection here
        if(is_opera || is_khtml || navigator.userAgent.toLowerCase().indexOf('firefox/1')!=-1){
-               return x - 30; // opera&konqueror & old firefox don't understand overflow-x, estimate scrollbar width
+               return 30; // opera&konqueror & old firefox don't understand overflow-x, estimate scrollbar width
        }       
-       return x;
+       return 0;
 }
 
 function os_encodeQuery(value){
@@ -98,14 +118,16 @@ function os_encodeQuery(value){
   if(escape) {
     return escape(value);
   }
+  return null;
 }
 function os_decodeValue(value){
   if (decodeURIComponent) {
     return decodeURIComponent(value);
-  } 
+  }
   if(unescape){
        return unescape(value);
   }
+  return null;
 }
 
 /** Brower-dependent functions to find window inner size, and scroll status */
@@ -178,10 +200,9 @@ function os_createContainer(r){
        var pos = os_getElementPosition(r.searchbox);   
        var left = pos.left;
        var top = pos.top + s.offsetHeight;
-       var body = document.getElementById("globalWrapper");
        c.className = "os-suggest";
        c.setAttribute("id", r.container);      
-       body.appendChild(c); 
+       document.body.appendChild(c); 
        
        // dynamically generated style params   
        // IE workaround, cannot explicitely set "style" attribute
@@ -218,11 +239,41 @@ function os_fitContainer(r){
 }
 /** If some entries are longer than the box, replace text with "..." */
 function os_trimResultText(r){
+       // find max width, first see if we could expand the container to fit it
+       var maxW = 0;
+       for(var i=0;i<r.resultCount;i++){
+               var e = document.getElementById(r.resultText+i);
+               if(e.offsetWidth > maxW)
+                       maxW = e.offsetWidth;
+       }
        var w = document.getElementById(r.container).offsetWidth;
+       var fix = 0;
        if(r.containerCount < r.resultCount){           
-               w -= 20; // give 20px for scrollbar             
+               fix = 20; // give 20px for scrollbar            
        } else
-               w = os_operaWidthFix(w);
+               fix = os_operaWidthFix(w);
+       if(fix < 4)
+               fix = 4; // basic padding
+       maxW += fix;
+       
+       // resize container to fit more data if permitted       
+       var normW = document.getElementById(r.searchbox).offsetWidth;
+       var prop = maxW / normW;
+       if(prop > os_container_max_width)
+               prop = os_container_max_width;
+       else if(prop < 1)
+               prop = 1;
+       var newW = Math.round( normW * prop ); 
+       if( w != newW ){        
+               w = newW;
+               if( os_animation_timer != null )
+                       clearInterval(os_animation_timer.id)
+               os_animation_timer = new os_AnimationTimer(r,w);
+               os_animation_timer.id = setInterval("os_animateChangeWidth()",os_animation_delay);
+               w -= fix; // this much is reserved
+       }
+       
+       // trim results
        if(w < 10)
                return;
        for(var i=0;i<r.resultCount;i++){
@@ -246,6 +297,25 @@ function os_trimResultText(r){
        }
 }
 
+/** Invoked on timer to animate change in container width */
+function os_animateChangeWidth(){
+       var r = os_animation_timer.r;
+       var c = document.getElementById(r.container);
+       var w = c.offsetWidth;
+       var inc = os_animation_timer.inc;
+       var target = os_animation_timer.target;
+       var nw = w + inc;
+       if( (inc > 0 && nw >= target) || (inc <= 0 && nw <= target) ){
+               // finished !
+               c.style.width = target+"px";
+               clearInterval(os_animation_timer.id)
+               os_animation_timer = null;
+       } else{
+               // in-progress
+               c.style.width = nw+"px";
+       }
+}
+
 /** Handles data from XMLHttpRequest, and updates the suggest results */
 function os_updateResults(r, query, text, cacheKey){    
        os_cache[cacheKey] = text;
@@ -272,6 +342,7 @@ function os_updateResults(r, query, text, cacheKey){
                        var t = document.getElementById(r.resultTable);         
                        r.containerTotal = t.offsetHeight;      
                        r.containerRow = t.offsetHeight / r.resultCount;
+                       os_fitContainer(r);
                        os_trimResultText(r);                           
                        os_showResults(r);
                } catch(e){
@@ -285,7 +356,7 @@ function os_updateResults(r, query, text, cacheKey){
 /** Create the result table to be placed in the container div */
 function os_createResultTable(r, results){
        var c = document.getElementById(r.container);
-       var width = os_operaWidthFix(c.offsetWidth);    
+       var width = c.offsetWidth - os_operaWidthFix(c.offsetWidth);    
        var html = "<table class=\"os-suggest-results\" id=\""+r.resultTable+"\" style=\"width: "+width+"px;\">";
        r.results = new Array();
        r.resultCount = results.length;
@@ -412,7 +483,7 @@ function os_changeHighlight(r, cur, next, updateSearchBox){
     if(next >= 0){
        var nextRow = document.getElementById(r.resultTable + next);
        if(nextRow != null)
-               nextRow.className = "os-suggest-result-hl";
+               nextRow.className = os_HighlightClass();
        newText = r.results[next];
     } else
        newText = r.original;
@@ -434,6 +505,20 @@ function os_changeHighlight(r, cur, next, updateSearchBox){
     }
 }
 
+function os_HighlightClass() {
+       var match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
+       if (match) {
+               var webKitVersion = parseInt(match[1]);
+               if (webKitVersion < 523) {
+                       // CSS system highlight colors broken on old Safari
+                       // https://bugs.webkit.org/show_bug.cgi?id=6129
+                       // Safari 3.0.4, 3.1 known ok
+                       return "os-suggest-result-hl-webkit";
+               }
+       }
+       return "os-suggest-result-hl";
+}
+
 function os_updateSearchQuery(r,newText){
        document.getElementById(r.searchbox).value = newText;
     r.query = newText;
@@ -441,7 +526,7 @@ function os_updateSearchQuery(r,newText){
 
 /** Find event target */
 function os_getTarget(e){
-       if (!e) var e = window.event;
+       if (!e) e = window.event;
        if (e.target) return e.target;
        else if (e.srcElement) return e.srcElement;
        else return null;
@@ -514,20 +599,13 @@ function os_eventKeypress(e){
 
 /** Catch the key code (Firefox bug)  */
 function os_eventKeydown(e){
-       if (!e) var e = window.event;
-       if (e.target) targ = e.target;
-       else if (e.srcElement) targ = e.srcElement;
+       if (!e) e = window.event;
+       var targ = os_getTarget(e);
        var r = os_map[targ.id];
        if(r == null)
                return; // not our event
                        
        os_mouse_moved = false;
-               
-       if(os_first_focus){
-               // firefox bug, focus&defocus to make autocomplete=off valid
-               targ.blur(); targ.focus();
-               os_first_focus = false;
-       }
 
        os_cur_keypressed = (window.Event) ? e.which : e.keyCode;
        os_last_keypress = 0;
@@ -536,8 +614,6 @@ function os_eventKeydown(e){
 
 /** Event: loss of focus of input box */
 function os_eventBlur(e){      
-       if(os_first_focus)
-               return; // we are focusing/defocusing
        var targ = os_getTarget(e);
        var r = os_map[targ.id];
        if(r == null)
@@ -547,9 +623,8 @@ function os_eventBlur(e){
 }
 
 /** Event: focus (catch only when stopped) */
-function os_eventFocus(e){     
-       if(os_first_focus)
-               return; // we are focusing/defocusing
+function os_eventFocus(e){
+       // nothing happens here?
 }
 
 
@@ -661,18 +736,26 @@ function os_eventOnsubmit(e){
        return true;
 }
 
+function os_hookEvent(element, hookName, hookFunct) {
+       if (element.addEventListener) {
+               element.addEventListener(hookName, hookFunct, false);
+       } else if (window.attachEvent) {
+               element.attachEvent("on" + hookName, hookFunct);
+       }
+}
+
 /** Init Result objects and event handlers */
 function os_initHandlers(name, formname, element){
        var r = new os_Results(name, formname); 
        // event handler
-       element.onkeyup = function(event) { os_eventKeyup(event); };
-       element.onkeydown = function(event) { os_eventKeydown(event); };
-       element.onkeypress = function(event) { os_eventKeypress(event); };
-       element.onblur = function(event) { os_eventBlur(event); };
-       element.onfocus = function(event) { os_eventFocus(event); };
+       os_hookEvent(element, "keyup", function(event) { os_eventKeyup(event); });
+       os_hookEvent(element, "keydown", function(event) { os_eventKeydown(event); });
+       os_hookEvent(element, "keypress", function(event) { os_eventKeypress(event); });
+       os_hookEvent(element, "blur", function(event) { os_eventBlur(event); });
+       os_hookEvent(element, "focus", function(event) { os_eventFocus(event); });
        element.setAttribute("autocomplete","off");
        // stopping handler
-       document.getElementById(formname).onsubmit = function(event){ return os_eventOnsubmit(event); };
+       os_hookEvent(document.getElementById(formname), "submit", function(event){ return os_eventOnsubmit(event); });
        os_map[name] = r; 
        // toggle link
        if(document.getElementById(r.toggle) == null){