]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/dom-construct.js.uncompressed.js
1 define("dojo/dom-construct", ["exports", "./_base/kernel", "./_base/sniff", "./_base/window", "./dom", "./dom-attr", "./on"],
2 function(exports
, dojo
, has
, win
, dom
, attr
, on
){
6 // This module defines the core dojo DOM construction API.
9 dojo.toDom = function(frag, doc){
11 // instantiates an HTML fragment returning the corresponding DOM.
15 // optional document to use when creating DOM nodes, defaults to
16 // dojo.doc if not specified.
17 // returns: DocumentFragment
20 // Create a table row:
21 // | var tr = dojo.toDom("<tr><td>First!</td></tr>");
26 dojo._toDom = function(frag, doc){
28 // Existing alias for `dojo.toDom`. Deprecated, will be removed in 2.0.
33 dojo.place = function(node, refNode, position){
35 // Attempt to insert node into the DOM, choosing from various positioning options.
36 // Returns the first argument resolved to a DOM node.
38 // node: DOMNode|String
39 // id or node reference, or HTML fragment starting with "<" to place relative to refNode
41 // refNode: DOMNode|String
42 // id or node reference to use as basis for placement
44 // position: String|Number?
45 // string noting the position of node relative to refNode or a
46 // number indicating the location in the childNodes collection of refNode.
47 // Accepted string values are:
54 // "first" and "last" indicate positions as children of refNode, "replace" replaces refNode,
55 // "only" replaces all children. position defaults to "last" if not specified
58 // Returned values is the first argument resolved to a DOM node.
60 // .place() is also a method of `dojo.NodeList`, allowing `dojo.query` node lookups.
63 // Place a node by string id as the last child of another node by string id:
64 // | dojo.place("someNode", "anotherNode");
67 // Place a node by string id before another node by string id
68 // | dojo.place("someNode", "anotherNode", "before");
71 // Create a Node, and place it in the body element (last child):
72 // | dojo.place("<div></div>", dojo.body());
75 // Put a new LI as the first child of a list by id:
76 // | dojo.place("<li></li>", "someUl", "first");
81 dojo.create = function(tag, attrs, refNode, pos){
83 // Create an element, allowing for optional attribute decoration
87 // A DOM Element creation function. A shorthand method for creating a node or
88 // a fragment, and allowing for a convenient optional attribute setting step,
89 // as well as an optional DOM placement reference.
91 // Attributes are set by passing the optional object through `dojo.setAttr`.
92 // See `dojo.setAttr` for noted caveats and nuances, and API if applicable.
94 // Placement is done via `dojo.place`, assuming the new node to be the action
95 // node, passing along the optional reference node and position.
97 // tag: DOMNode|String
98 // A string of the element to create (eg: "div", "a", "p", "li", "script", "br"),
99 // or an existing DOM node to process.
102 // An object-hash of attributes to set on the newly created node.
103 // Can be null, if you don't want to set any attributes/styles.
104 // See: `dojo.setAttr` for a description of available attributes.
106 // refNode: DOMNode?|String?
107 // Optional reference node. Used by `dojo.place` to place the newly created
108 // node somewhere in the dom relative to refNode. Can be a DomNode reference
109 // or String ID of a node.
112 // Optional positional reference. Defaults to "last" by way of `dojo.place`,
113 // though can be set to "first","after","before","last", "replace" or "only"
114 // to further control the placement of the new node relative to the refNode.
115 // 'refNode' is required if a 'pos' is specified.
121 // | var n = dojo.create("div");
124 // Create a DIV with content:
125 // | var n = dojo.create("div", { innerHTML:"<p>hi</p>" });
128 // Place a new DIV in the BODY, with no attributes set
129 // | var n = dojo.create("div", null, dojo.body());
132 // Create an UL, and populate it with LI's. Place the list as the first-child of a
133 // node with id="someId":
134 // | var ul = dojo.create("ul", null, "someId", "first");
135 // | var items = ["one", "two", "three", "four"];
136 // | dojo.forEach(items, function(data){
137 // | dojo.create("li", { innerHTML: data }, ul);
141 // Create an anchor, with an href. Place in BODY:
142 // | dojo.create("a", { href:"foo.html", title:"Goto FOO!" }, dojo.body());
145 // Create a `dojo.NodeList()` from a new element (for syntatic sugar):
146 // | dojo.query(dojo.create('div'))
147 // | .addClass("newDiv")
148 // | .onclick(function(e){ console.log('clicked', e.target) })
149 // | .place("#someNode"); // redundant, but cleaner.
154 dojo.empty = function(node){
156 // safely removes all children of the node.
157 // node: DOMNode|String
158 // a reference to a DOM node or an id.
160 // Destroy node's children byId:
161 // | dojo.empty("someId");
164 // Destroy all nodes' children in a list by reference:
165 // | dojo.query(".someNode").forEach(dojo.empty);
170 dojo.destroy = function(node){
172 // Removes a node from its parent, clobbering it and all of its
176 // Removes a node from its parent, clobbering it and all of its
177 // children. Function only works with DomNodes, and returns nothing.
179 // node: DOMNode|String
180 // A String ID or DomNode reference of the element to be destroyed
183 // Destroy a node byId:
184 // | dojo.destroy("someId");
187 // Destroy all nodes in a list by reference:
188 // | dojo.query(".someNode").forEach(dojo.destroy);
193 dojo._destroyElement = function(node){
195 // Existing alias for `dojo.destroy`. Deprecated, will be removed in 2.0.
199 // support stuff for dojo.toDom
205 tr
: ["table", "tbody"],
206 td
: ["table", "tbody", "tr"],
207 th
: ["table", "thead", "tr"],
208 legend
: ["fieldset"],
211 col
: ["table", "colgroup"],
214 reTag
= /<\s*([\w\:]+)/,
215 masterNode
= {}, masterNum
= 0,
216 masterName
= "__" + dojo
._scopeName
+ "ToDomId";
218 // generate start/end tag strings to use
219 // for the injection for each special tag wrap case.
220 for(var param
in tagWrap
){
221 if(tagWrap
.hasOwnProperty(param
)){
222 var tw
= tagWrap
[param
];
223 tw
.pre
= param
== "option" ? '<select multiple="multiple">' : "<" + tw
.join("><") + ">";
224 tw
.post
= "</" + tw
.reverse().join("></") + ">";
225 // the last line is destructive: it reverses the array,
226 // but we don't care at this point
230 function _insertBefore(/*DomNode*/node
, /*DomNode*/ref
){
231 var parent
= ref
.parentNode
;
233 parent
.insertBefore(node
, ref
);
237 function _insertAfter(/*DomNode*/node
, /*DomNode*/ref
){
239 // Try to insert node after ref
240 var parent
= ref
.parentNode
;
242 if(parent
.lastChild
== ref
){
243 parent
.appendChild(node
);
245 parent
.insertBefore(node
, ref
.nextSibling
);
250 var _destroyContainer
= null,
252 on(window
, "unload", function(){
253 _destroyContainer
= null; //prevent IE leak
256 exports
.toDom
= function toDom(frag
, doc
){
257 doc
= doc
|| win
.doc
;
258 var masterId
= doc
[masterName
];
260 doc
[masterName
] = masterId
= ++masterNum
+ "";
261 masterNode
[masterId
] = doc
.createElement("div");
264 // make sure the frag is a string.
267 // find the starting tag, and get node wrapper
268 var match
= frag
.match(reTag
),
269 tag
= match
? match
[1].toLowerCase() : "",
270 master
= masterNode
[masterId
],
272 if(match
&& tagWrap
[tag
]){
274 master
.innerHTML
= wrap
.pre
+ frag
+ wrap
.post
;
275 for(i
= wrap
.length
; i
; --i
){
276 master
= master
.firstChild
;
279 master
.innerHTML
= frag
;
282 // one node shortcut => return the node itself
283 if(master
.childNodes
.length
== 1){
284 return master
.removeChild(master
.firstChild
); // DOMNode
287 // return multiple nodes as a document fragment
288 df
= doc
.createDocumentFragment();
289 while(fc
= master
.firstChild
){ // intentional assignment
292 return df
; // DOMNode
295 exports
.place
= function place(/*DOMNode|String*/node
, /*DOMNode|String*/refNode
, /*String|Number?*/position
){
296 refNode
= dom
.byId(refNode
);
297 if(typeof node
== "string"){ // inline'd type check
298 node
= /^\s*</.test(node
) ? exports
.toDom(node
, refNode
.ownerDocument
) : dom
.byId(node
);
300 if(typeof position
== "number"){ // inline'd type check
301 var cn
= refNode
.childNodes
;
302 if(!cn
.length
|| cn
.length
<= position
){
303 refNode
.appendChild(node
);
305 _insertBefore(node
, cn
[position
< 0 ? 0 : position
]);
310 _insertBefore(node
, refNode
);
313 _insertAfter(node
, refNode
);
316 refNode
.parentNode
.replaceChild(node
, refNode
);
319 exports
.empty(refNode
);
320 refNode
.appendChild(node
);
323 if(refNode
.firstChild
){
324 _insertBefore(node
, refNode
.firstChild
);
327 // else fallthrough...
328 default: // aka: last
329 refNode
.appendChild(node
);
332 return node
; // DomNode
335 exports
.create
= function create(/*DOMNode|String*/tag
, /*Object*/attrs
, /*DOMNode?|String?*/refNode
, /*String?*/pos
){
338 refNode
= dom
.byId(refNode
);
339 doc
= refNode
.ownerDocument
;
341 if(typeof tag
== "string"){ // inline'd type check
342 tag
= doc
.createElement(tag
);
344 if(attrs
){ attr
.set(tag
, attrs
); }
345 if(refNode
){ exports
.place(tag
, refNode
, pos
); }
346 return tag
; // DomNode
350 has("ie") ? function(node
){
351 node
= dom
.byId(node
);
352 for(var c
; c
= node
.lastChild
;){ // intentional assignment
357 dom
.byId(node
).innerHTML
= "";
360 exports
.destroy
= function destroy(/*DOMNode|String*/node
){
361 node
= dom
.byId(node
);
363 var doc
= node
.ownerDocument
;
364 // cannot use _destroyContainer.ownerDocument since this can throw an exception on IE
365 if(!_destroyContainer
|| _destroyDoc
!= doc
){
366 _destroyContainer
= doc
.createElement("div");
369 _destroyContainer
.appendChild(node
.parentNode
? node
.parentNode
.removeChild(node
) : node
);
370 // NOTE: see http://trac.dojotoolkit.org/ticket/2931. This may be a bug and not a feature
371 _destroyContainer
.innerHTML
= "";