]>
Commit | Line | Data |
---|---|---|
1354d172 AD |
1 | define("dojo/dom", ["./_base/sniff", "./_base/lang", "./_base/window"], |
2 | function(has, lang, win){ | |
3 | // module: | |
4 | // dojo/dom | |
5 | // summary: | |
6 | // This module defines the core dojo DOM API. | |
7 | ||
8 | // FIXME: need to add unit tests for all the semi-public methods | |
9 | ||
10 | try{ | |
11 | document.execCommand("BackgroundImageCache", false, true); | |
12 | }catch(e){ | |
13 | // sane browsers don't have cache "issues" | |
14 | } | |
15 | ||
16 | // ============================= | |
17 | // DOM Functions | |
18 | // ============================= | |
19 | ||
20 | /*===== | |
21 | dojo.byId = function(id, doc){ | |
22 | // summary: | |
23 | // Returns DOM node with matching `id` attribute or `null` | |
24 | // if not found. If `id` is a DomNode, this function is a no-op. | |
25 | // | |
26 | // id: String|DOMNode | |
27 | // A string to match an HTML id attribute or a reference to a DOM Node | |
28 | // | |
29 | // doc: Document? | |
30 | // Document to work in. Defaults to the current value of | |
31 | // dojo.doc. Can be used to retrieve | |
32 | // node references from other documents. | |
33 | // | |
34 | // example: | |
35 | // Look up a node by ID: | |
36 | // | var n = dojo.byId("foo"); | |
37 | // | |
38 | // example: | |
39 | // Check if a node exists, and use it. | |
40 | // | var n = dojo.byId("bar"); | |
41 | // | if(n){ doStuff() ... } | |
42 | // | |
43 | // example: | |
44 | // Allow string or DomNode references to be passed to a custom function: | |
45 | // | var foo = function(nodeOrId){ | |
46 | // | nodeOrId = dojo.byId(nodeOrId); | |
47 | // | // ... more stuff | |
48 | // | } | |
49 | =====*/ | |
50 | ||
51 | /*===== | |
52 | dojo.isDescendant = function(node, ancestor){ | |
53 | // summary: | |
54 | // Returns true if node is a descendant of ancestor | |
55 | // node: DOMNode|String | |
56 | // string id or node reference to test | |
57 | // ancestor: DOMNode|String | |
58 | // string id or node reference of potential parent to test against | |
59 | // | |
60 | // example: | |
61 | // Test is node id="bar" is a descendant of node id="foo" | |
62 | // | if(dojo.isDescendant("bar", "foo")){ ... } | |
63 | }; | |
64 | =====*/ | |
65 | ||
66 | // TODO: do we need this function in the base? | |
67 | ||
68 | /*===== | |
69 | dojo.setSelectable = function(node, selectable){ | |
70 | // summary: | |
71 | // Enable or disable selection on a node | |
72 | // node: DOMNode|String | |
73 | // id or reference to node | |
74 | // selectable: Boolean | |
75 | // state to put the node in. false indicates unselectable, true | |
76 | // allows selection. | |
77 | // example: | |
78 | // Make the node id="bar" unselectable | |
79 | // | dojo.setSelectable("bar"); | |
80 | // example: | |
81 | // Make the node id="bar" selectable | |
82 | // | dojo.setSelectable("bar", true); | |
83 | }; | |
84 | =====*/ | |
85 | ||
86 | var dom = {}; // the result object | |
87 | ||
88 | if(has("ie")){ | |
89 | dom.byId = function(id, doc){ | |
90 | if(typeof id != "string"){ | |
91 | return id; | |
92 | } | |
93 | var _d = doc || win.doc, te = id && _d.getElementById(id); | |
94 | // attributes.id.value is better than just id in case the | |
95 | // user has a name=id inside a form | |
96 | if(te && (te.attributes.id.value == id || te.id == id)){ | |
97 | return te; | |
98 | }else{ | |
99 | var eles = _d.all[id]; | |
100 | if(!eles || eles.nodeName){ | |
101 | eles = [eles]; | |
102 | } | |
103 | // if more than 1, choose first with the correct id | |
104 | var i = 0; | |
105 | while((te = eles[i++])){ | |
106 | if((te.attributes && te.attributes.id && te.attributes.id.value == id) || te.id == id){ | |
107 | return te; | |
108 | } | |
109 | } | |
110 | } | |
111 | }; | |
112 | }else{ | |
113 | dom.byId = function(id, doc){ | |
114 | // inline'd type check. | |
115 | // be sure to return null per documentation, to match IE branch. | |
116 | return ((typeof id == "string") ? (doc || win.doc).getElementById(id) : id) || null; // DOMNode | |
117 | }; | |
118 | } | |
119 | /*===== | |
120 | }; | |
121 | =====*/ | |
122 | ||
123 | dom.isDescendant = function(/*DOMNode|String*/node, /*DOMNode|String*/ancestor){ | |
124 | try{ | |
125 | node = dom.byId(node); | |
126 | ancestor = dom.byId(ancestor); | |
127 | while(node){ | |
128 | if(node == ancestor){ | |
129 | return true; // Boolean | |
130 | } | |
131 | node = node.parentNode; | |
132 | } | |
133 | }catch(e){ /* squelch, return false */ } | |
134 | return false; // Boolean | |
135 | }; | |
136 | ||
137 | // TODO: do we need this function in the base? | |
138 | ||
139 | dom.setSelectable = function(/*DOMNode|String*/node, /*Boolean*/selectable){ | |
140 | node = dom.byId(node); | |
141 | if(has("mozilla")){ | |
142 | node.style.MozUserSelect = selectable ? "" : "none"; | |
143 | }else if(has("khtml") || has("webkit")){ | |
144 | node.style.KhtmlUserSelect = selectable ? "auto" : "none"; | |
145 | }else if(has("ie")){ | |
146 | var v = (node.unselectable = selectable ? "" : "on"), | |
147 | cs = node.getElementsByTagName("*"), i = 0, l = cs.length; | |
148 | for(; i < l; ++i){ | |
149 | cs.item(i).unselectable = v; | |
150 | } | |
151 | } | |
152 | //FIXME: else? Opera? | |
153 | }; | |
154 | ||
155 | return dom; | |
156 | }); |