1 | define(["exports", "./_base/kernel", "./_base/sniff", "./_base/lang", "./dom", "./dom-style", "./dom-construct", "./_base/connect"], |
---|
2 | function(exports, dojo, has, lang, dom, style, ctr, conn){ |
---|
3 | // module: |
---|
4 | // dojo/dom-prop |
---|
5 | // summary: |
---|
6 | // This module defines the core dojo DOM properties API. |
---|
7 | // Indirectly depends on dojo.empty() and dojo.toDom(). |
---|
8 | |
---|
9 | // ============================= |
---|
10 | // Element properties Functions |
---|
11 | // ============================= |
---|
12 | |
---|
13 | /*===== |
---|
14 | prop.get = function(node, name){ |
---|
15 | // summary: |
---|
16 | // Gets a property on an HTML element. |
---|
17 | // description: |
---|
18 | // Handles normalized getting of properties on DOM nodes. |
---|
19 | // |
---|
20 | // node: DOMNode|String |
---|
21 | // id or reference to the element to get the property on |
---|
22 | // name: String |
---|
23 | // the name of the property to get. |
---|
24 | // returns: |
---|
25 | // the value of the requested property or its default value |
---|
26 | // |
---|
27 | // example: |
---|
28 | // | // get the current value of the "foo" property on a node |
---|
29 | // | dojo.getProp(dojo.byId("nodeId"), "foo"); |
---|
30 | // | // or we can just pass the id: |
---|
31 | // | dojo.getProp("nodeId", "foo"); |
---|
32 | }; |
---|
33 | =====*/ |
---|
34 | |
---|
35 | /*===== |
---|
36 | prop.set = function(node, name, value){ |
---|
37 | // summary: |
---|
38 | // Sets a property on an HTML element. |
---|
39 | // description: |
---|
40 | // Handles normalized setting of properties on DOM nodes. |
---|
41 | // |
---|
42 | // When passing functions as values, note that they will not be |
---|
43 | // directly assigned to slots on the node, but rather the default |
---|
44 | // behavior will be removed and the new behavior will be added |
---|
45 | // using `dojo.connect()`, meaning that event handler properties |
---|
46 | // will be normalized and that some caveats with regards to |
---|
47 | // non-standard behaviors for onsubmit apply. Namely that you |
---|
48 | // should cancel form submission using `dojo.stopEvent()` on the |
---|
49 | // passed event object instead of returning a boolean value from |
---|
50 | // the handler itself. |
---|
51 | // node: DOMNode|String |
---|
52 | // id or reference to the element to set the property on |
---|
53 | // name: String|Object |
---|
54 | // the name of the property to set, or a hash object to set |
---|
55 | // multiple properties at once. |
---|
56 | // value: String? |
---|
57 | // The value to set for the property |
---|
58 | // returns: |
---|
59 | // the DOM node |
---|
60 | // |
---|
61 | // example: |
---|
62 | // | // use prop() to set the tab index |
---|
63 | // | dojo.setProp("nodeId", "tabIndex", 3); |
---|
64 | // | |
---|
65 | // |
---|
66 | // example: |
---|
67 | // Set multiple values at once, including event handlers: |
---|
68 | // | dojo.setProp("formId", { |
---|
69 | // | "foo": "bar", |
---|
70 | // | "tabIndex": -1, |
---|
71 | // | "method": "POST", |
---|
72 | // | "onsubmit": function(e){ |
---|
73 | // | // stop submitting the form. Note that the IE behavior |
---|
74 | // | // of returning true or false will have no effect here |
---|
75 | // | // since our handler is connect()ed to the built-in |
---|
76 | // | // onsubmit behavior and so we need to use |
---|
77 | // | // dojo.stopEvent() to ensure that the submission |
---|
78 | // | // doesn't proceed. |
---|
79 | // | dojo.stopEvent(e); |
---|
80 | // | |
---|
81 | // | // submit the form with Ajax |
---|
82 | // | dojo.xhrPost({ form: "formId" }); |
---|
83 | // | } |
---|
84 | // | }); |
---|
85 | // |
---|
86 | // example: |
---|
87 | // Style is s special case: Only set with an object hash of styles |
---|
88 | // | dojo.setProp("someNode",{ |
---|
89 | // | id:"bar", |
---|
90 | // | style:{ |
---|
91 | // | width:"200px", height:"100px", color:"#000" |
---|
92 | // | } |
---|
93 | // | }); |
---|
94 | // |
---|
95 | // example: |
---|
96 | // Again, only set style as an object hash of styles: |
---|
97 | // | var obj = { color:"#fff", backgroundColor:"#000" }; |
---|
98 | // | dojo.setProp("someNode", "style", obj); |
---|
99 | // | |
---|
100 | // | // though shorter to use `dojo.style()` in this case: |
---|
101 | // | dojo.style("someNode", obj); |
---|
102 | }; |
---|
103 | =====*/ |
---|
104 | |
---|
105 | // helper to connect events |
---|
106 | var _evtHdlrMap = {}, _ctr = 0, _attrId = dojo._scopeName + "attrid"; |
---|
107 | |
---|
108 | //>>excludeStart("webkitMobile", kwArgs.webkitMobile); |
---|
109 | // the next dictionary lists elements with read-only innerHTML on IE |
---|
110 | var _roInnerHtml = {col: 1, colgroup: 1, |
---|
111 | // frameset: 1, head: 1, html: 1, style: 1, |
---|
112 | table: 1, tbody: 1, tfoot: 1, thead: 1, tr: 1, title: 1}; |
---|
113 | //>>excludeEnd("webkitMobile"); |
---|
114 | |
---|
115 | exports.names = { |
---|
116 | // properties renamed to avoid clashes with reserved words |
---|
117 | "class": "className", |
---|
118 | "for": "htmlFor", |
---|
119 | // properties written as camelCase |
---|
120 | tabindex: "tabIndex", |
---|
121 | readonly: "readOnly", |
---|
122 | colspan: "colSpan", |
---|
123 | frameborder: "frameBorder", |
---|
124 | rowspan: "rowSpan", |
---|
125 | valuetype: "valueType" |
---|
126 | }; |
---|
127 | |
---|
128 | exports.get = function getProp(/*DOMNode|String*/node, /*String*/name){ |
---|
129 | node = dom.byId(node); |
---|
130 | var lc = name.toLowerCase(), propName = exports.names[lc] || name; |
---|
131 | return node[propName]; // Anything |
---|
132 | }; |
---|
133 | |
---|
134 | exports.set = function setProp(/*DOMNode|String*/node, /*String|Object*/name, /*String?*/value){ |
---|
135 | node = dom.byId(node); |
---|
136 | var l = arguments.length; |
---|
137 | if(l == 2 && typeof name != "string"){ // inline'd type check |
---|
138 | // the object form of setter: the 2nd argument is a dictionary |
---|
139 | for(var x in name){ |
---|
140 | exports.set(node, x, name[x]); |
---|
141 | } |
---|
142 | return node; // DomNode |
---|
143 | } |
---|
144 | var lc = name.toLowerCase(), propName = exports.names[lc] || name; |
---|
145 | if(propName == "style" && typeof value != "string"){ // inline'd type check |
---|
146 | // special case: setting a style |
---|
147 | style.style(node, value); |
---|
148 | return node; // DomNode |
---|
149 | } |
---|
150 | if(propName == "innerHTML"){ |
---|
151 | // special case: assigning HTML |
---|
152 | //>>excludeStart("webkitMobile", kwArgs.webkitMobile); |
---|
153 | if(has("ie") && node.tagName.toLowerCase() in _roInnerHtml){ |
---|
154 | ctr.empty(node); |
---|
155 | node.appendChild(ctr.toDom(value, node.ownerDocument)); |
---|
156 | }else{ |
---|
157 | //>>excludeEnd("webkitMobile"); |
---|
158 | node[propName] = value; |
---|
159 | //>>excludeStart("webkitMobile", kwArgs.webkitMobile); |
---|
160 | } |
---|
161 | //>>excludeEnd("webkitMobile"); |
---|
162 | return node; // DomNode |
---|
163 | } |
---|
164 | if(lang.isFunction(value)){ |
---|
165 | // special case: assigning an event handler |
---|
166 | // clobber if we can |
---|
167 | var attrId = node[_attrId]; |
---|
168 | if(!attrId){ |
---|
169 | attrId = _ctr++; |
---|
170 | node[_attrId] = attrId; |
---|
171 | } |
---|
172 | if(!_evtHdlrMap[attrId]){ |
---|
173 | _evtHdlrMap[attrId] = {}; |
---|
174 | } |
---|
175 | var h = _evtHdlrMap[attrId][propName]; |
---|
176 | if(h){ |
---|
177 | //h.remove(); |
---|
178 | conn.disconnect(h); |
---|
179 | }else{ |
---|
180 | try{ |
---|
181 | delete node[propName]; |
---|
182 | }catch(e){} |
---|
183 | } |
---|
184 | // ensure that event objects are normalized, etc. |
---|
185 | if(value){ |
---|
186 | //_evtHdlrMap[attrId][propName] = on(node, propName, value); |
---|
187 | _evtHdlrMap[attrId][propName] = conn.connect(node, propName, value); |
---|
188 | }else{ |
---|
189 | node[propName] = null; |
---|
190 | } |
---|
191 | return node; // DomNode |
---|
192 | } |
---|
193 | node[propName] = value; |
---|
194 | return node; // DomNode |
---|
195 | }; |
---|
196 | }); |
---|