📄 nodelist.js
字号:
// summary: // attach event handlers to every item of the NodeList. Uses dojo.connect() // so event properties are normalized // methodName: String // the name of the method to attach to. For DOM events, this should be // the lower-case name of the event // objOrFunc: Object|Function|String // if 2 arguments are passed (methodName, objOrFunc), objOrFunc should // reference a function or be the name of the function in the global // namespace to attach. If 3 arguments are provided // (methodName, objOrFunc, funcName), objOrFunc must be the scope to // locate the bound function in // funcName: String? // optional. A string naming the function in objOrFunc to bind to the // event. May also be a function reference. // example: // add an onclick handler to every button on the page // | dojo.query("div:nth-child(odd)").connect("onclick", function(e){ // | console.debug("clicked!"); // | }); // example: // attach foo.bar() to every odd div's onmouseover // | dojo.query("div:nth-child(odd)").connect("onmouseover", foo, "bar"); }, =====*/ attr: _mapIntoDojo("attr"), style: _mapIntoDojo("style"), addClass: _mapIntoDojo("addClass", true), removeClass: _mapIntoDojo("removeClass", true), toggleClass: _mapIntoDojo("toggleClass", true), connect: _mapIntoDojo("connect", true), // FIXME: connectPublisher()? connectRunOnce()? place: function(/*String||Node*/ queryOrNode, /*String*/ position){ // summary: // places elements of this node list relative to the first element matched // by queryOrNode. Returns the original NodeList. // queryOrNode: // may be a string representing any valid CSS3 selector or a DOM node. // In the selector case, only the first matching element will be used // for relative positioning. // position: // can be one of: // * "last"||"end" (default) // * "first||"start" // * "before" // * "after" // or an offset in the childNodes property var item = d.query(queryOrNode)[0]; return this.forEach(function(i){ d.place(i, item, (position||"last")); }); // dojo.NodeList }, orphan: function(/*String?*/ simpleFilter){ // summary: // removes elements in this list that match the simple // filter from their parents and returns them as a new // NodeList. // simpleFilter: // single-expression CSS filter // return: // `dojo.NodeList` the orpahned elements var orphans = simpleFilter ? d._filterQueryResult(this, simpleFilter) : this; orphans.forEach(function(item){ if(item.parentNode){ item.parentNode.removeChild(item); } }); return orphans; // dojo.NodeList }, adopt: function(/*String||Array||DomNode*/ queryOrListOrNode, /*String?*/ position){ // summary: // places any/all elements in queryOrListOrNode at a // position relative to the first element in this list. // Returns a dojo.NodeList of the adopted elements. // queryOrListOrNode: // a DOM node or a query string or a query result. // Represents the nodes to be adopted relative to the // first element of this NodeList. // position: // can be one of: // * "last"||"end" (default) // * "first||"start" // * "before" // * "after" // or an offset in the childNodes property var item = this[0]; return d.query(queryOrListOrNode).forEach(function(ai){ d.place(ai, item, position || "last"); }); // dojo.NodeList }, // FIXME: do we need this? query: function(/*String*/ queryStr){ // summary: // Returns a new, flattened NodeList. Elements of the new list // satisfy the passed query but use elements of the // current NodeList as query roots. if(!queryStr){ return this; } // FIXME: probably slow // FIXME: use map? var ret = d.NodeList(); this.forEach(function(item){ d.query(queryStr, item).forEach(function(subItem){ if(subItem !== undefined){ ret.push(subItem); } }); }); return ret; // dojo.NodeList }, filter: function(/*String*/ simpleQuery){ // summary: // "masks" the built-in javascript filter() method to support // passing a simple string filter in addition to supporting // filtering function objects. // example: // "regular" JS filter syntax as exposed in dojo.filter: // | dojo.query("*").filter(function(item){ // | // highlight every paragraph // | return (item.nodeName == "p"); // | }).styles("backgroundColor", "yellow"); // example: // the same filtering using a CSS selector // | dojo.query("*").filter("p").styles("backgroundColor", "yellow"); var items = this; var _a = arguments; var r = d.NodeList(); var rp = function(t){ if(t !== undefined){ r.push(t); } } if(d.isString(simpleQuery)){ items = d._filterQueryResult(this, _a[0]); if(_a.length == 1){ // if we only got a string query, pass back the filtered results return items; // dojo.NodeList } // if we got a callback, run it over the filtered items _a.shift(); } // handle the (callback, [thisObject]) case d.forEach(d.filter(items, _a[0], _a[1]), rp); return r; // dojo.NodeList }, /* // FIXME: should this be "copyTo" and include parenting info? clone: function(){ // summary: // creates node clones of each element of this list // and returns a new list containing the clones }, */ addContent: function(/*String*/ content, /*String||Integer?*/ position){ // summary: // add a node or some HTML as a string to every item in the list. // Returns the original list. // description: // a copy of the HTML content is added to each item in the // list, with an optional position argument. If no position // argument is provided, the content is appended to the end of // each item. // content: // the HTML in string format to add at position to every item // position: // can be one of: // * "last"||"end" (default) // * "first||"start" // * "before" // * "after" // or an offset in the childNodes property // example: // appends content to the end if the position is ommitted // | dojo.query("h3 > p").addContent("hey there!"); // example: // add something to the front of each element that has a "thinger" property: // | dojo.query("[thinger]").addContent("...", "first"); // example: // adds a header before each element of the list // | dojo.query(".note").addContent("<h4>NOTE:</h4>", "before"); var ta = d.doc.createElement("span"); if(d.isString(content)){ ta.innerHTML = content; }else{ ta.appendChild(content); } if(position === undefined){ position = "last"; } var ct = (position == "first" || position == "after") ? "lastChild" : "firstChild"; this.forEach(function(item){ var tn = ta.cloneNode(true); while(tn[ct]){ d.place(tn[ct], item, position); } }); return this; // dojo.NodeList }, empty: function(){ // summary: // clears all content from each node in the list return this.forEach("item.innerHTML='';"); // dojo.NodeList // FIXME: should we be checking for and/or disposing of widgets below these nodes? }, instantiate: function(/*String|Object*/ declaredClass, /*Object?*/ properties){ // summary: // Create a new instance of a specified class, using the // specified properties and each node in the nodeList as a // srcNodeRef // var c = d.isFunction(declaredClass) ? declaredClass : d.getObject(declaredClass); return this.forEach(function(i){ new c(properties||{},i); }) // dojo.NodeList } }); // syntactic sugar for DOM events d.forEach([ "blur", "focus", "click", "keydown", "keypress", "keyup", "mousedown", "mouseenter", "mouseleave", "mousemove", "mouseout", "mouseover", "mouseup" ], function(evt){ var _oe = "on"+evt; dojo.NodeList.prototype[_oe] = function(a, b){ return this.connect(_oe, a, b); } // FIXME: should these events trigger publishes? /* return (a ? this.connect(_oe, a, b) : this.forEach(function(n){ // FIXME: // listeners get buried by // addEventListener and can't be dug back // out to be triggered externally. // see: // http://developer.mozilla.org/en/docs/DOM:element console.debug(n, evt, _oe); // FIXME: need synthetic event support! var _e = { target: n, faux: true, type: evt }; // dojo._event_listener._synthesizeEvent({}, { target: n, faux: true, type: evt }); try{ n[evt](_e); }catch(e){ console.debug(e); } try{ n[_oe](_e); }catch(e){ console.debug(e); } }) ); } */ } );})();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -