OpenLayers OpenLayers

Changeset 7579

Show
Ignore:
Timestamp:
07/28/08 17:16:39 (4 months ago)
Author:
tschaub
Message:

Adding functions to manage dom element class names. Use OpenLayers.Element.hasClass, addClass, removeClass, toggleClass for css class name management. r=pagameba,me (closes #1607)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/BaseTypes/Element.js

    r6131 r7579  
    125125 
    126126    /** 
     127     * Function: hasClass 
     128     * Tests if an element has the given CSS class name. 
     129     * 
     130     * Parameters: 
     131     * element - {DOMElement} A DOM element node. 
     132     * name - {String} The CSS class name to search for. 
     133     * 
     134     * Returns: 
     135     * {Boolean} The element has the given class name. 
     136     */ 
     137    hasClass: function(element, name) { 
     138        var names = element.className; 
     139        return (!!names && new RegExp("(^|\\s)" + name + "(\\s|$)").test(names)); 
     140    }, 
     141     
     142    /** 
     143     * Function: addClass 
     144     * Add a CSS class name to an element.  Safe where element already has 
     145     *     the class name. 
     146     * 
     147     * Parameters: 
     148     * element - {DOMElement} A DOM element node. 
     149     * name - {String} The CSS class name to add. 
     150     * 
     151     * Returns: 
     152     * {DOMElement} The element. 
     153     */ 
     154    addClass: function(element, name) { 
     155        if(!OpenLayers.Element.hasClass(element, name)) { 
     156            element.className += (element.className ? " " : "") + name; 
     157        } 
     158        return element; 
     159    }, 
     160 
     161    /** 
     162     * Function: removeClass 
     163     * Remove a CSS class name from an element.  Safe where element does not 
     164     *     have the class name. 
     165     * 
     166     * Parameters: 
     167     * element - {DOMElement} A DOM element node. 
     168     * name - {String} The CSS class name to remove. 
     169     * 
     170     * Returns: 
     171     * {DOMElement} The element. 
     172     */ 
     173    removeClass: function(element, name) { 
     174        var names = element.className; 
     175        if(names) { 
     176            element.className = OpenLayers.String.trim( 
     177                names.replace( 
     178                    new RegExp("(^|\\s+)" + name + "(\\s+|$)"), " " 
     179                ) 
     180            ); 
     181        } 
     182        return element; 
     183    }, 
     184 
     185    /** 
     186     * Function: toggleClass 
     187     * Remove a CSS class name from an element if it exists.  Add the class name 
     188     *     if it doesn't exist. 
     189     * 
     190     * Parameters: 
     191     * element - {DOMElement} A DOM element node. 
     192     * name - {String} The CSS class name to toggle. 
     193     * 
     194     * Returns: 
     195     * {DOMElement} The element. 
     196     */ 
     197    toggleClass: function(element, name) { 
     198        if(OpenLayers.Element.hasClass(element, name)) { 
     199            OpenLayers.Element.removeClass(element, name); 
     200        } else { 
     201            OpenLayers.Element.addClass(element, name); 
     202        } 
     203        return element; 
     204    }, 
     205 
     206    /** 
    127207     * APIFunction: getStyle 
    128208     *  
  • trunk/openlayers/tests/BaseTypes/Element.html

    r6724 r7579  
    137137    }     
    138138 
     139    function test_hasClass(t) {         
     140        t.plan(14); 
     141        var has = OpenLayers.Element.hasClass; 
     142 
     143        var element = document.createElement("div"); 
     144        element.className = "fe fi fo fum one.part two-part three:part four"; 
     145         
     146        t.ok(has(element, "fe"), "has fe"); 
     147        t.ok(has(element, "fi"), "has fi"); 
     148        t.ok(has(element, "fo"), "has fo"); 
     149        t.ok(!has(element, "f"), "hasn't f"); 
     150        t.ok(!has(element, "o"), "hasn't o"); 
     151        t.ok(!has(element, "fumb"), "hasn't fumb"); 
     152        t.ok(!has(element, "one"), "hasn't one"); 
     153        t.ok(has(element, "one.part"), "has one.part"); 
     154        t.ok(!has(element, "two"), "hasn't two"); 
     155        t.ok(has(element, "two-part"), "has two-part"); 
     156        t.ok(!has(element, "three"), "hasn't three"); 
     157        t.ok(has(element, "three:part"), "has three:part"); 
     158        t.ok(has(element, "four"), "has four"); 
     159         
     160        element.className = ""; 
     161        t.ok(!has(element, "nada"), "hasn't nada"); 
     162    } 
     163 
     164    function test_addClass(t) {         
     165        t.plan(6); 
     166        var add = OpenLayers.Element.addClass; 
     167         
     168        var element = document.createElement("div"); 
     169        element.id = "foo"; 
     170        t.eq(element.className, "", "starts with no class name"); 
     171         
     172        var mod = add(element, "first"); 
     173        t.eq(mod.id, element.id, "returns the same element"); 
     174        t.eq(element.className, "first", "properly adds first class name");         
     175        t.eq(add(element, "second").className, "first second", 
     176             "properly adds second class name"); 
     177        t.eq(add(element, "second").className, "first second", 
     178             "doesn't do anything for duplicated names"); 
     179        t.eq(add(element, "third").className, "first second third", 
     180             "properly adds third class name"); 
     181    } 
     182     
     183    function test_removeClass(t) { 
     184        t.plan(5); 
     185        var remove = OpenLayers.Element.removeClass; 
     186         
     187        var element = document.createElement("div"); 
     188        element.id = "foo"; 
     189        element.className = "first second middle fourth last"; 
     190         
     191        var mod = remove(element, "last"); 
     192        t.eq(mod.id, element.id, "returns the same element"); 
     193        t.eq(element.className, "first second middle fourth", 
     194             "properly removes last class name"); 
     195        t.eq(remove(element, "first").className, "second middle fourth", 
     196             "properly removes first class name"); 
     197        t.eq(remove(element, "middle").className, "second fourth", 
     198             "properly removes middle class name"); 
     199        t.eq(remove(element, "nada").className, "second fourth", 
     200             "doesn't do anything for bogus class name"); 
     201    } 
     202 
     203    function test_toggleClass(t) { 
     204        t.plan(5); 
     205        var toggle = OpenLayers.Element.toggleClass; 
     206         
     207        var element = document.createElement("div"); 
     208        element.id = "foo"; 
     209         
     210        var mod = toggle(element, "first"); 
     211        t.eq(mod.id, element.id, "returns the same element"); 
     212        t.eq(element.className, "first", "adds first new class name"); 
     213        t.eq(toggle(element, "second").className, "first second", 
     214             "adds second new class name"); 
     215        t.eq(toggle(element, "first").className, "second", 
     216             "removes first existing class name"); 
     217        t.eq(toggle(element, "second").className, "", 
     218             "removes second existing class name"); 
     219    } 
     220 
    139221    function test_Element_getStyle(t) { 
    140222        t.plan(4);