1 | define(["./sniff", "./_base/window"], |
---|
2 | function(has, win){ |
---|
3 | // module: |
---|
4 | // dojo/dom |
---|
5 | |
---|
6 | // FIXME: need to add unit tests for all the semi-public methods |
---|
7 | |
---|
8 | if(has("ie") <= 7){ |
---|
9 | try{ |
---|
10 | document.execCommand("BackgroundImageCache", false, true); |
---|
11 | }catch(e){ |
---|
12 | // sane browsers don't have cache "issues" |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | // ============================= |
---|
17 | // DOM Functions |
---|
18 | // ============================= |
---|
19 | |
---|
20 | // the result object |
---|
21 | var dom = { |
---|
22 | // summary: |
---|
23 | // This module defines the core dojo DOM API. |
---|
24 | }; |
---|
25 | |
---|
26 | if(has("ie")){ |
---|
27 | dom.byId = function(id, doc){ |
---|
28 | if(typeof id != "string"){ |
---|
29 | return id; |
---|
30 | } |
---|
31 | var _d = doc || win.doc, te = id && _d.getElementById(id); |
---|
32 | // attributes.id.value is better than just id in case the |
---|
33 | // user has a name=id inside a form |
---|
34 | if(te && (te.attributes.id.value == id || te.id == id)){ |
---|
35 | return te; |
---|
36 | }else{ |
---|
37 | var eles = _d.all[id]; |
---|
38 | if(!eles || eles.nodeName){ |
---|
39 | eles = [eles]; |
---|
40 | } |
---|
41 | // if more than 1, choose first with the correct id |
---|
42 | var i = 0; |
---|
43 | while((te = eles[i++])){ |
---|
44 | if((te.attributes && te.attributes.id && te.attributes.id.value == id) || te.id == id){ |
---|
45 | return te; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | }; |
---|
50 | }else{ |
---|
51 | dom.byId = function(id, doc){ |
---|
52 | // inline'd type check. |
---|
53 | // be sure to return null per documentation, to match IE branch. |
---|
54 | return ((typeof id == "string") ? (doc || win.doc).getElementById(id) : id) || null; // DOMNode |
---|
55 | }; |
---|
56 | } |
---|
57 | /*===== |
---|
58 | dom.byId = function(id, doc){ |
---|
59 | // summary: |
---|
60 | // Returns DOM node with matching `id` attribute or falsy value (ex: null or undefined) |
---|
61 | // if not found. If `id` is a DomNode, this function is a no-op. |
---|
62 | // |
---|
63 | // id: String|DOMNode |
---|
64 | // A string to match an HTML id attribute or a reference to a DOM Node |
---|
65 | // |
---|
66 | // doc: Document? |
---|
67 | // Document to work in. Defaults to the current value of |
---|
68 | // dojo/_base/window.doc. Can be used to retrieve |
---|
69 | // node references from other documents. |
---|
70 | // |
---|
71 | // example: |
---|
72 | // Look up a node by ID: |
---|
73 | // | require(["dojo/dom"], function(dom){ |
---|
74 | // | var n = dom.byId("foo"); |
---|
75 | // | }); |
---|
76 | // |
---|
77 | // example: |
---|
78 | // Check if a node exists, and use it. |
---|
79 | // | require(["dojo/dom"], function(dom){ |
---|
80 | // | var n = dom.byId("bar"); |
---|
81 | // | if(n){ doStuff() ... } |
---|
82 | // | }); |
---|
83 | // |
---|
84 | // example: |
---|
85 | // Allow string or DomNode references to be passed to a custom function: |
---|
86 | // | require(["dojo/dom"], function(dom){ |
---|
87 | // | var foo = function(nodeOrId){ |
---|
88 | // | nodeOrId = dom.byId(nodeOrId); |
---|
89 | // | // ... more stuff |
---|
90 | // | } |
---|
91 | // | }); |
---|
92 | }; |
---|
93 | =====*/ |
---|
94 | |
---|
95 | dom.isDescendant = function(/*DOMNode|String*/ node, /*DOMNode|String*/ ancestor){ |
---|
96 | // summary: |
---|
97 | // Returns true if node is a descendant of ancestor |
---|
98 | // node: DOMNode|String |
---|
99 | // string id or node reference to test |
---|
100 | // ancestor: DOMNode|String |
---|
101 | // string id or node reference of potential parent to test against |
---|
102 | // |
---|
103 | // example: |
---|
104 | // Test is node id="bar" is a descendant of node id="foo" |
---|
105 | // | require(["dojo/dom"], function(dom){ |
---|
106 | // | if(dom.isDescendant("bar", "foo")){ ... } |
---|
107 | // | }); |
---|
108 | |
---|
109 | try{ |
---|
110 | node = dom.byId(node); |
---|
111 | ancestor = dom.byId(ancestor); |
---|
112 | while(node){ |
---|
113 | if(node == ancestor){ |
---|
114 | return true; // Boolean |
---|
115 | } |
---|
116 | node = node.parentNode; |
---|
117 | } |
---|
118 | }catch(e){ /* squelch, return false */ } |
---|
119 | return false; // Boolean |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | // TODO: do we need setSelectable in the base? |
---|
124 | |
---|
125 | // Add feature test for user-select CSS property |
---|
126 | // (currently known to work in all but IE < 10 and Opera) |
---|
127 | has.add("css-user-select", function(global, doc, element){ |
---|
128 | // Avoid exception when dom.js is loaded in non-browser environments |
---|
129 | if(!element){ return false; } |
---|
130 | |
---|
131 | var style = element.style; |
---|
132 | var prefixes = ["Khtml", "O", "ms", "Moz", "Webkit"], |
---|
133 | i = prefixes.length, |
---|
134 | name = "userSelect", |
---|
135 | prefix; |
---|
136 | |
---|
137 | // Iterate prefixes from most to least likely |
---|
138 | do{ |
---|
139 | if(typeof style[name] !== "undefined"){ |
---|
140 | // Supported; return property name |
---|
141 | return name; |
---|
142 | } |
---|
143 | }while(i-- && (name = prefixes[i] + "UserSelect")); |
---|
144 | |
---|
145 | // Not supported if we didn't return before now |
---|
146 | return false; |
---|
147 | }); |
---|
148 | |
---|
149 | /*===== |
---|
150 | dom.setSelectable = function(node, selectable){ |
---|
151 | // summary: |
---|
152 | // Enable or disable selection on a node |
---|
153 | // node: DOMNode|String |
---|
154 | // id or reference to node |
---|
155 | // selectable: Boolean |
---|
156 | // state to put the node in. false indicates unselectable, true |
---|
157 | // allows selection. |
---|
158 | // example: |
---|
159 | // Make the node id="bar" unselectable |
---|
160 | // | require(["dojo/dom"], function(dom){ |
---|
161 | // | dom.setSelectable("bar"); |
---|
162 | // | }); |
---|
163 | // example: |
---|
164 | // Make the node id="bar" selectable |
---|
165 | // | require(["dojo/dom"], function(dom){ |
---|
166 | // | dom.setSelectable("bar", true); |
---|
167 | // | }); |
---|
168 | }; |
---|
169 | =====*/ |
---|
170 | |
---|
171 | var cssUserSelect = has("css-user-select"); |
---|
172 | dom.setSelectable = cssUserSelect ? function(node, selectable){ |
---|
173 | // css-user-select returns a (possibly vendor-prefixed) CSS property name |
---|
174 | dom.byId(node).style[cssUserSelect] = selectable ? "" : "none"; |
---|
175 | } : function(node, selectable){ |
---|
176 | node = dom.byId(node); |
---|
177 | |
---|
178 | // (IE < 10 / Opera) Fall back to setting/removing the |
---|
179 | // unselectable attribute on the element and all its children |
---|
180 | var nodes = node.getElementsByTagName("*"), |
---|
181 | i = nodes.length; |
---|
182 | |
---|
183 | if(selectable){ |
---|
184 | node.removeAttribute("unselectable"); |
---|
185 | while(i--){ |
---|
186 | nodes[i].removeAttribute("unselectable"); |
---|
187 | } |
---|
188 | }else{ |
---|
189 | node.setAttribute("unselectable", "on"); |
---|
190 | while(i--){ |
---|
191 | nodes[i].setAttribute("unselectable", "on"); |
---|
192 | } |
---|
193 | } |
---|
194 | }; |
---|
195 | |
---|
196 | return dom; |
---|
197 | }); |
---|