Changeset 12818
- Timestamp:
- 03/02/08 19:52:30 (9 months ago)
- Files:
-
- 1 modified
-
dojo/trunk/_base/NodeList.js (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dojo/trunk/_base/NodeList.js
r12676 r12818 8 8 9 9 var tnl = function(arr){ 10 // decorate an array to make it look like a NodeList 10 11 arr.constructor = dojo.NodeList; 11 12 dojo._mixin(arr, dojo.NodeList.prototype); … … 13 14 } 14 15 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 16 22 return function(){ 17 23 var _a = arguments; … … 21 27 return d[func].apply(d, aa); 22 28 }); 23 return ( (_a.length > 1) || !d.isString(_a[0])) ? this : s; // String||dojo.NodeList29 return (alwaysThis || ( (_a.length > 1) || !d.isString(_a[0]) )) ? this : s; // String||dojo.NodeList 24 30 } 25 31 }; … … 82 88 // with the caveat that it returns a dojo.NodeList and not a 83 89 // raw Array. For more details, see: 84 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice90 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice> 85 91 // index: Integer 86 92 // begin can be a positive or negative integer, with positive … … 109 115 // with the caveat that it returns a dojo.NodeList and not a 110 116 // raw Array. For more details, see: 111 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:concat117 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:concat> 112 118 // item: Object...? 113 119 // Any number of optional parameters may be passed in to be … … 129 135 // description: 130 136 // For more details on the behavior of indexOf, see: 131 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf137 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf> 132 138 // returns: 133 139 // Positive Integer or 0 for a match, -1 of not found. … … 141 147 // description: 142 148 // For more details on the behavior of lastIndexOf, see: 143 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf149 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf> 144 150 // value: Object 145 151 // The value to search for. … … 153 159 every: function(/*Function*/callback, /*Object?*/thisObject){ 154 160 // summary: 155 // see dojo.every()and:156 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every161 // see `dojo.every()` and: 162 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every> 157 163 // Takes the same structure of arguments and returns as 158 164 // dojo.every() with the caveat that the passed array is … … 186 192 187 193 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 189 196 }, 190 197 … … 225 232 // If a value is passed, the return is this NodeList 226 233 }, 227 =====*/ 228 attr: _mapIntoDojo("attr"), 229 style: _mapIntoDojo("style"), 230 231 addClass: function(/*String*/ className){ 232 // summary: 234 235 addClass: function(className){ 236 // summary: 233 237 // 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){ 271 266 // summary: 272 267 // attach event handlers to every item of the NodeList. Uses dojo.connect() 273 268 // so event properties are normalized 274 // methodName: 269 // methodName: String 275 270 // the name of the method to attach to. For DOM events, this should be 276 271 // the lower-case name of the event 277 // objOrFunc: 272 // objOrFunc: Object|Function|String 278 273 // if 2 arguments are passed (methodName, objOrFunc), objOrFunc should 279 274 // reference a function or be the name of the function in the global … … 281 276 // (methodName, objOrFunc, funcName), objOrFunc must be the scope to 282 277 // locate the bound function in 283 // funcName: 278 // funcName: String? 284 279 // optional. A string naming the function in objOrFunc to bind to the 285 280 // event. May also be a function reference. … … 291 286 // example: 292 287 // 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 300 317 }, 301 318 … … 305 322 // filter from their parents and returns them as a new 306 323 // 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 309 328 var orphans = simpleFilter ? d._filterQueryResult(this, simpleFilter) : this; 310 329 orphans.forEach(function(item){ … … 326 345 // first element of this NodeList. 327 346 // 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" 333 352 // or an offset in the childNodes property 334 353 var item = this[0]; … … 413 432 // argument is provided, the content is appended to the end of 414 433 // 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 415 443 // example: 416 444 // appends content to the end if the position is ommitted … … 422 450 // adds a header before each element of the list 423 451 // | dojo.query(".note").addContent("<h4>NOTE:</h4>", "before"); 424 // content:425 // the HTML in string format to add at position to every item426 // position:427 // One of:428 // "last"||"end" (default)429 // "first||"start"430 // "before"431 // "after"432 // or an integer offset in the childNodes property433 452 var ta = d.doc.createElement("span"); 434 453 if(d.isString(content)){ … … 449 468 return this; // dojo.NodeList 450 469 }, 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 }, 451 478 452 479 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 456 484 // 457 485 var c = d.isFunction(declaredClass) ? declaredClass : d.getObject(declaredClass); 458 this.forEach(function(i){486 return this.forEach(function(i){ 459 487 new c(properties||{},i); 460 }) 461 return this; // dojo.NodeList 488 }) // dojo.NodeList 462 489 } 463 490