Changeset 505

Show
Ignore:
Timestamp:
10/17/07 02:42:47 (6 years ago)
Author:
gyuque
Message:

lang/javascript/userscripts/syobocalplus.user.js: implemented incremental search

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userscripts/syobocalplus.user.js

    r504 r505  
    133133                        if (!tobj) 
    134134                        { 
    135                                 tobj = {count: 0, name: name}; 
     135                                tobj = {count: 0, name: name, tid: tid, box_class: "tid-"+tid}; 
    136136                                this.namedMap[tid] = tobj; 
    137137 
     
    143143 
    144144                        tobj.count++; 
     145                        this.count++; 
     146 
     147                        return tobj; 
    145148                }, 
    146149 
     
    158161                searchTitle: function(kw) { 
    159162                        var matchedTitles = []; 
    160                          
     163                        var count = 0; 
     164 
    161165                        for(var s = 0;;) 
    162166                        { 
     
    168172                                var t = this.namedMap[ this.idxDB.charCodeAt(idx) ]; 
    169173                                if (t) 
     174                                { 
    170175                                        matchedTitles.push(t); 
     176                                        count += t.count; 
     177                                } 
    171178                        } 
    172179 
     
    174181                                return null; 
    175182 
    176                         return matchedTitles; 
     183                        return {count_all: count, titles: matchedTitles}; 
     184                }, 
     185 
     186                map: function(proc) { 
     187                        for (var t in this.namedMap) 
     188                                proc(this.namedMap[t], t); 
     189                }, 
     190 
     191                commitTitleFilterStyles: function() { 
     192                        var rules = []; 
     193                        this.map(function(t) { 
     194                                 rules.push(".plus-unvail-t"+t.tid+" .v3box."+t.box_class+"{-moz-opacity:1;}"); 
     195                        }); 
     196                        GM_addStyle(rules.join(' ')); 
     197                } 
     198        } 
     199// ----------------------------------- 
     200        var FilterBox = function() { 
     201                var x = document.evaluate("//div[@class='main']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
     202                if (x.snapshotLength < 1) 
     203                        throw "div.main not found"; 
     204 
     205                this.element = x.snapshotItem(0); 
     206                this.originalClass = this.element.className; 
     207        } 
     208 
     209        FilterBox.prototype = { 
     210                clear: function() { 
     211                        this.element.className = this.originalClass; 
     212                }, 
     213 
     214                setFilterClasses: function(c) { 
     215                        this.element.className = this.originalClass+" "+c; 
    177216                } 
    178217        } 
     
    223262// ----------------------------------- 
    224263 
    225         var ChannelSelector = function(chman) { 
     264        var ChannelSelector = function(chman, tman, fbox) { 
    226265                this.chManager = chman; 
     266                this.titleManager = tman; 
     267                this.titleFilterBox = fbox; 
    227268 
    228269                var _this = this; 
     
    231272                var ul = document.createElement("ul"); 
    232273                document.body.appendChild(ul); 
    233                 ul.innerHTML = "\u5168\u3066"; 
     274 
     275 
     276                ul.innerHTML = "\uff80\uff72\uff84\uff99\u691c\u7d22<input type=\"text\"><span> </span> &nbsp; \u5c40 \u5168\u3066"; 
     277 
     278                // search box 
     279                var txtbox = ul.childNodes[1]; 
     280                var resarea = ul.childNodes[2]; 
     281                var sbox = new SearchBox(txtbox, resarea); 
     282                sbox.search = function(k){return _this.searchTitle(k);} 
    234283                 
    235284                var btnAllHide = document.createElement("button"); 
     
    251300 
    252301        ChannelSelector.prototype = { 
     302                searchTitle: function(key) { 
     303                        if (!key.match(/[^\s]/) || key == "") 
     304                        { 
     305                                this.titleFilterBox.clear(); 
     306                                removeClass(document.body, "plus-filtered"); 
     307                                return {message: ""}; 
     308                        } 
     309                        var sres = this.titleManager.searchTitle(key); 
     310 
     311                        addClass(document.body, "plus-filtered"); 
     312 
     313                        if (!sres) 
     314                                return {message: "(0)"}; 
     315 
     316                        var len = sres.titles.length; 
     317                        var unvail_classes = [] 
     318                        for (var i = 0;i < len;i++) 
     319                                unvail_classes.push("plus-unvail-t"+sres.titles[i].tid); 
     320                        this.titleFilterBox.setFilterClasses(unvail_classes.join(" ")); 
     321 
     322                        return {message: '('+sres.count_all+')'}; 
     323                }, 
     324 
    253325                addChannel: function(chname, chobj) { 
    254326                        var li = document.createElement("li"); 
     
    314386                applyChannelClass: function(chobj) { 
    315387                        this.box.className += " "+chobj.chclass; 
     388                } 
     389        } 
     390// ----------------------------------- 
     391        var SearchBox = function(elem, msgbdy) { 
     392                this.textBox = elem; 
     393                elem.className = "plus-searchbox"; 
     394                this.messageBox = msgbdy; 
     395                 
     396                var _this = this; 
     397                this.oldValue = elem.value; 
     398                elem.addEventListener("keyup", function(e){_this.onKey(e)}, false); 
     399        } 
     400 
     401        SearchBox.prototype = { 
     402                onKey: function(e) { 
     403                        var v = this.textBox.value; 
     404                        if (this.oldValue != v) 
     405                        { 
     406                                if (this.search) 
     407                                { 
     408                                        var res = this.search(v); 
     409                                        if (res && res.message != undefined) 
     410                                                this.outMessage(res.message); 
     411                                } 
     412                                this.oldValue = v; 
     413                        } 
     414                }, 
     415 
     416                outMessage: function(msg) { 
     417                        this.messageBox.innerHTML = msg; 
    316418                } 
    317419        } 
     
    340442                finish: function() { 
    341443                        this.chManager.commitChannelFilterStyles(); 
     444                        this.titleManager.commitTitleFilterStyles(); 
    342445                }, 
    343446 
     
    373476                                        As[i].href.match(/tid\/([0-9]+)/); 
    374477                                        var tid = RegExp["$1"] - 0; 
    375                                         this.titleManager.put(tid, title); 
     478                                        var tobj = this.titleManager.put(tid, title); 
     479                                        box.className += " "+tobj.box_class; 
    376480                                } 
    377481                        } 
     
    386490        } 
    387491// ----------------------------------- 
    388         GM_addStyle(".chselector{z-index: 100; font-size: 80%; width: 100%; position: fixed; bottom: 0; margin: 0; padding: 2px; background: #eee; border-top: 1px solid #ddd;} .chselector li{display: inline; margin: 0 2px 0 2px;}"); 
     492 
     493        GM_addStyle(".chselector{z-index: 100; font-size: 80%; width: 100%; position: fixed; bottom: 0; margin: 0; padding: 2px 2px 2px 4px; background: ThreeDFace; border-top: 1px solid #ThreeDShadow;} .chselector li{display: inline; margin: 0 2px 0 2px;} .plus-filtered .v3box{-moz-opacity:"+HiddenAlpha+"}"); 
    389494 
    390495        document.body.style.paddingBottom = "2em"; 
    391496        var divs = document.evaluate("//div[@class='v3box']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
     497        var filter = new FilterBox(); 
    392498 
    393499        var chman = new ChannelManager(); 
    394500        var tman = new TitleManager(); 
    395         new ChannelSelector(chman); 
     501        new ChannelSelector(chman, tman, filter); 
    396502 
    397503        for (var fx in FixedChannels)