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