Changeset 12818

Show
Ignore:
Timestamp:
03/02/08 19:52:30 (9 months ago)
Author:
alex
Message:

shorten NodeList? code, add toggleClass(), and update documentation. Refs #4205. Refs #3961. !strict

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/_base/NodeList.js

    r12676 r12818  
    88 
    99        var tnl = function(arr){ 
     10                // decorate an array to make it look like a NodeList 
    1011                arr.constructor = dojo.NodeList; 
    1112                dojo._mixin(arr, dojo.NodeList.prototype); 
     
    1314        } 
    1415 
    15         var _mapIntoDojo = function(func){ 
     16        var _mapIntoDojo = function(func, alwaysThis){ 
     17                // returns a function which, when executed in the scope of its caller, 
     18                // applies the passed arguments to a particular dojo.* function (named 
     19                // in func) and aggregates the returns. if alwaysThis is true, it 
     20                // always returns the scope object and not the collected returns from 
     21                // the Dojo method 
    1622                return function(){ 
    1723                        var _a = arguments; 
     
    2127                                return d[func].apply(d, aa); 
    2228                        }); 
    23                         return ( (_a.length > 1) || !d.isString(_a[0]) ) ? this : s; // String||dojo.NodeList 
     29                        return (alwaysThis || ( (_a.length > 1) || !d.isString(_a[0]) )) ? this : s; // String||dojo.NodeList 
    2430                } 
    2531        }; 
     
    8288                        //              with the caveat that it returns a dojo.NodeList and not a 
    8389                        //              raw Array. For more details, see: 
    84                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice 
     90                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice> 
    8591                        // index: Integer 
    8692                        //              begin can be a positive or negative integer, with positive 
     
    109115                        //              with the caveat that it returns a dojo.NodeList and not a 
    110116                        //              raw Array. For more details, see: 
    111                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:concat 
     117                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:concat> 
    112118                        // item: Object...? 
    113119                        //              Any number of optional parameters may be passed in to be 
     
    129135                        //      description: 
    130136                        //              For more details on the behavior of indexOf, see: 
    131                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf 
     137                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf> 
    132138                        //      returns: 
    133139                        //              Positive Integer or 0 for a match, -1 of not found. 
     
    141147                        //      description: 
    142148                        //              For more details on the behavior of lastIndexOf, see: 
    143                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf 
     149                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf> 
    144150                        // value: Object 
    145151                        //              The value to search for. 
     
    153159                every: function(/*Function*/callback, /*Object?*/thisObject){ 
    154160                        //      summary: 
    155                         //              see dojo.every() and: 
    156                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every 
     161                        //              see `dojo.every()` and: 
     162                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every> 
    157163                        //              Takes the same structure of arguments and returns as 
    158164                        //              dojo.every() with the caveat that the passed array is 
     
    186192 
    187193                        d.forEach(this, callback, thisObj); 
    188                         return this; // dojo.NodeList non-standard return to allow easier chaining 
     194                        // non-standard return to allow easier chaining 
     195                        return this; // dojo.NodeList  
    189196                }, 
    190197 
     
    225232                        //              If a value is passed, the return is this NodeList 
    226233                }, 
    227                 =====*/ 
    228                 attr: _mapIntoDojo("attr"), 
    229                 style: _mapIntoDojo("style"), 
    230  
    231                 addClass: function(/*String*/ className){ 
    232                         // summary: 
     234 
     235                addClass: function(className){ 
     236                        //      summary: 
    233237                        //              adds the specified class to every node in the list 
    234                         // 
    235                         this.forEach(function(i){ d.addClass(i, className); }); 
    236                         return this; 
    237                 }, 
    238  
    239                 removeClass: function(/*String*/ className){ 
    240                         this.forEach(function(i){ d.removeClass(i, className); }); 
    241                         return this; 
    242                 }, 
    243  
    244                 // FIXME: toggleClass()? connectPublisher()? connectRunOnce()? 
    245  
    246                 place: function(/*String||Node*/ queryOrNode, /*String*/ position){ 
    247                         //      summary: 
    248                         //              places elements of this node list relative to the first element matched 
    249                         //              by queryOrNode. Returns the original NodeList. 
    250                         //      queryOrNode: 
    251                         //              may be a string representing any valid CSS3 selector or a DOM node. 
    252                         //              In the selector case, only the first matching element will be used  
    253                         //              for relative positioning. 
    254                         //      position: 
    255                         //              can be one of: 
    256                         //                      "last"||"end" (default) 
    257                         //                      "first||"start" 
    258                         //                      "before" 
    259                         //                      "after" 
    260                         //              or an offset in the childNodes property 
    261                         var item = d.query(queryOrNode)[0]; 
    262                         position = position || "last"; 
    263  
    264                         for(var x=0; x<this.length; x++){ 
    265                                 d.place(this[x], item, position); 
    266                         } 
    267                         return this; // dojo.NodeList 
    268                 }, 
    269  
    270                 connect: function(/*String*/ methodName, /*Object||Function||String*/ objOrFunc, /*String?*/ funcName){ 
     238                        //      className: String 
     239                        //              the CSS class to add 
     240                        //      return: 
     241                        //              dojo.NodeList, this list 
     242                }, 
     243 
     244                removeClass: function(className){ 
     245                        //      summary: 
     246                        //              removes the specified class from every node in the list 
     247                        //      className: String 
     248                        //              the CSS class to add 
     249                        //      return: 
     250                        //              dojo.NodeList, this list 
     251                }, 
     252 
     253                toggleClass: function(className, condition){ 
     254                        //      summary: 
     255                        //              Adds a class to node if not present, or removes if present. 
     256                        //              Pass a boolean condition if you want to explicitly add or remove. 
     257                        //      condition: Boolean? 
     258                        //              If passed, true means to add the class, false means to remove. 
     259                        //      className: String 
     260                        //              the CSS class to add 
     261                        //      return: dojo.NodeList 
     262                        //              this list 
     263                }, 
     264 
     265                connect: function(methodName, objOrFunc, funcName){ 
    271266                        //      summary: 
    272267                        //              attach event handlers to every item of the NodeList. Uses dojo.connect() 
    273268                        //              so event properties are normalized 
    274                         //      methodName: 
     269                        //      methodName: String 
    275270                        //              the name of the method to attach to. For DOM events, this should be 
    276271                        //              the lower-case name of the event 
    277                         //      objOrFunc: 
     272                        //      objOrFunc: Object|Function|String 
    278273                        //              if 2 arguments are passed (methodName, objOrFunc), objOrFunc should 
    279274                        //              reference a function or be the name of the function in the global 
     
    281276                        //              (methodName, objOrFunc, funcName), objOrFunc must be the scope to  
    282277                        //              locate the bound function in 
    283                         //      funcName: 
     278                        //      funcName: String? 
    284279                        //              optional. A string naming the function in objOrFunc to bind to the 
    285280                        //              event. May also be a function reference. 
     
    291286                        // example: 
    292287                        //              attach foo.bar() to every odd div's onmouseover 
    293                         //              |       dojo.query("div:nth-child(odd)").onclick("onmouseover", foo, "bar"); 
    294  
    295                         // FIXME: if we only get a function, should the scope be the node? 
    296                         this.forEach(function(item){ 
    297                                 d.connect(item, methodName, objOrFunc, funcName); 
    298                         }); 
    299                         return this; // dojo.NodeList 
     288                        //              |       dojo.query("div:nth-child(odd)").connect("onmouseover", foo, "bar"); 
     289                }, 
     290                =====*/ 
     291                attr: _mapIntoDojo("attr"), 
     292                style: _mapIntoDojo("style"), 
     293                addClass: _mapIntoDojo("addClass", true), 
     294                removeClass: _mapIntoDojo("removeClass", true), 
     295                toggleClass: _mapIntoDojo("toggleClass", true), 
     296                connect: _mapIntoDojo("connect", true), 
     297 
     298                // FIXME: connectPublisher()? connectRunOnce()? 
     299 
     300                place: function(/*String||Node*/ queryOrNode, /*String*/ position){ 
     301                        //      summary: 
     302                        //              places elements of this node list relative to the first element matched 
     303                        //              by queryOrNode. Returns the original NodeList. 
     304                        //      queryOrNode: 
     305                        //              may be a string representing any valid CSS3 selector or a DOM node. 
     306                        //              In the selector case, only the first matching element will be used  
     307                        //              for relative positioning. 
     308                        //      position: 
     309                        //              can be one of: 
     310                        //                      * "last"||"end" (default) 
     311                        //                      * "first||"start" 
     312                        //                      * "before" 
     313                        //                      * "after" 
     314                        //              or an offset in the childNodes property 
     315                        var item = d.query(queryOrNode)[0]; 
     316                        return this.forEach(function(i){ d.place(i, item, (position||"last")); }); // dojo.NodeList 
    300317                }, 
    301318 
     
    305322                        //              filter from their parents and returns them as a new 
    306323                        //              NodeList. 
    307                         //      simpleFilter: single-expression CSS filter 
    308                         //      return: a dojo.NodeList of all of the elements orpahned 
     324                        //      simpleFilter: 
     325                        //              single-expression CSS filter 
     326                        //      return: 
     327                        //              `dojo.NodeList` the orpahned elements  
    309328                        var orphans = simpleFilter ? d._filterQueryResult(this, simpleFilter) : this; 
    310329                        orphans.forEach(function(item){ 
     
    326345                        //              first element of this NodeList. 
    327346                        //      position: 
    328                         //              optional. One of: 
    329                         //                      "last"||"end" (default) 
    330                         //                      "first||"start" 
    331                         //                      "before" 
    332                         //                      "after" 
     347                        //              can be one of: 
     348                        //                      * "last"||"end" (default) 
     349                        //                      * "first||"start" 
     350                        //                      * "before" 
     351                        //                      * "after" 
    333352                        //              or an offset in the childNodes property 
    334353                        var item = this[0]; 
     
    413432                        //              argument is provided, the content is appended to the end of 
    414433                        //              each item. 
     434                        //      content: 
     435                        //              the HTML in string format to add at position to every item 
     436                        //      position: 
     437                        //              can be one of: 
     438                        //                      * "last"||"end" (default) 
     439                        //                      * "first||"start" 
     440                        //                      * "before" 
     441                        //                      * "after" 
     442                        //              or an offset in the childNodes property 
    415443                        //      example: 
    416444                        //              appends content to the end if the position is ommitted 
     
    422450                        //              adds a header before each element of the list 
    423451                        //      |       dojo.query(".note").addContent("<h4>NOTE:</h4>", "before"); 
    424                         //      content: 
    425                         //              the HTML in string format to add at position to every item 
    426                         //      position: 
    427                         //              One of: 
    428                         //                      "last"||"end" (default) 
    429                         //                      "first||"start" 
    430                         //                      "before" 
    431                         //                      "after" 
    432                         //              or an integer offset in the childNodes property 
    433452                        var ta = d.doc.createElement("span"); 
    434453                        if(d.isString(content)){ 
     
    449468                        return this; // dojo.NodeList 
    450469                }, 
     470 
     471                empty: function(){ 
     472                        //      summary: 
     473                        //              clears all content from each node in the list 
     474                        return this.forEach("item.innerHTML='';"); // dojo.NodeList 
     475 
     476                        // FIXME: should we be checking for and/or disposing of widgets below these nodes? 
     477                }, 
    451478                 
    452479                instantiate: function(/*String|Object*/ declaredClass, /*Object?*/ properties){ 
    453                         // summary: 
    454                         //              Create a new instance of a specified class, using the specified properties 
    455                         //              and each node in the nodeList as a srcNodeRef 
     480                        //      summary: 
     481                        //              Create a new instance of a specified class, using the 
     482                        //              specified properties and each node in the nodeList as a 
     483                        //              srcNodeRef 
    456484                        // 
    457485                        var c = d.isFunction(declaredClass) ? declaredClass : d.getObject(declaredClass); 
    458                         this.forEach(function(i){ 
     486                        return this.forEach(function(i){ 
    459487                                new c(properties||{},i); 
    460                         }) 
    461                         return this; // dojo.NodeList 
     488                        }) // dojo.NodeList 
    462489                } 
    463490