1 | define(["exports", "./_base/kernel", "./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 | |
---|
8 | // TODOC: summary not showing up in output, see https://github.com/csnover/js-doc-parse/issues/42 |
---|
9 | |
---|
10 | // ============================= |
---|
11 | // Element properties Functions |
---|
12 | // ============================= |
---|
13 | |
---|
14 | // helper to connect events |
---|
15 | var _evtHdlrMap = {}, _ctr = 0, _attrId = dojo._scopeName + "attrid"; |
---|
16 | |
---|
17 | exports.names = { |
---|
18 | // properties renamed to avoid clashes with reserved words |
---|
19 | "class": "className", |
---|
20 | "for": "htmlFor", |
---|
21 | // properties written as camelCase |
---|
22 | tabindex: "tabIndex", |
---|
23 | readonly: "readOnly", |
---|
24 | colspan: "colSpan", |
---|
25 | frameborder: "frameBorder", |
---|
26 | rowspan: "rowSpan", |
---|
27 | valuetype: "valueType" |
---|
28 | }; |
---|
29 | |
---|
30 | exports.get = function getProp(/*DOMNode|String*/ node, /*String*/ name){ |
---|
31 | // summary: |
---|
32 | // Gets a property on an HTML element. |
---|
33 | // description: |
---|
34 | // Handles normalized getting of properties on DOM nodes. |
---|
35 | // |
---|
36 | // node: DOMNode|String |
---|
37 | // id or reference to the element to get the property on |
---|
38 | // name: String |
---|
39 | // the name of the property to get. |
---|
40 | // returns: |
---|
41 | // the value of the requested property or its default value |
---|
42 | // |
---|
43 | // example: |
---|
44 | // | // get the current value of the "foo" property on a node |
---|
45 | // | require(["dojo/dom-prop", "dojo/dom"], function(domProp, dom){ |
---|
46 | // | domProp.get(dom.byId("nodeId"), "foo"); |
---|
47 | // | // or we can just pass the id: |
---|
48 | // | domProp.get("nodeId", "foo"); |
---|
49 | // | }); |
---|
50 | |
---|
51 | node = dom.byId(node); |
---|
52 | var lc = name.toLowerCase(), propName = exports.names[lc] || name; |
---|
53 | return node[propName]; // Anything |
---|
54 | }; |
---|
55 | |
---|
56 | exports.set = function setProp(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){ |
---|
57 | // summary: |
---|
58 | // Sets a property on an HTML element. |
---|
59 | // description: |
---|
60 | // Handles normalized setting of properties on DOM nodes. |
---|
61 | // |
---|
62 | // When passing functions as values, note that they will not be |
---|
63 | // directly assigned to slots on the node, but rather the default |
---|
64 | // behavior will be removed and the new behavior will be added |
---|
65 | // using `dojo.connect()`, meaning that event handler properties |
---|
66 | // will be normalized and that some caveats with regards to |
---|
67 | // non-standard behaviors for onsubmit apply. Namely that you |
---|
68 | // should cancel form submission using `dojo.stopEvent()` on the |
---|
69 | // passed event object instead of returning a boolean value from |
---|
70 | // the handler itself. |
---|
71 | // node: DOMNode|String |
---|
72 | // id or reference to the element to set the property on |
---|
73 | // name: String|Object |
---|
74 | // the name of the property to set, or a hash object to set |
---|
75 | // multiple properties at once. |
---|
76 | // value: String? |
---|
77 | // The value to set for the property |
---|
78 | // returns: |
---|
79 | // the DOM node |
---|
80 | // |
---|
81 | // example: |
---|
82 | // | // use prop() to set the tab index |
---|
83 | // | require(["dojo/dom-prop"], function(domProp){ |
---|
84 | // | domProp.set("nodeId", "tabIndex", 3); |
---|
85 | // | }); |
---|
86 | // |
---|
87 | // example: |
---|
88 | // Set multiple values at once, including event handlers: |
---|
89 | // | require(["dojo/dom-prop"], function(domProp){ |
---|
90 | // | domProp.set("formId", { |
---|
91 | // | "foo": "bar", |
---|
92 | // | "tabIndex": -1, |
---|
93 | // | "method": "POST", |
---|
94 | // | }); |
---|
95 | // | }); |
---|
96 | |
---|
97 | node = dom.byId(node); |
---|
98 | var l = arguments.length; |
---|
99 | if(l == 2 && typeof name != "string"){ // inline'd type check |
---|
100 | // the object form of setter: the 2nd argument is a dictionary |
---|
101 | for(var x in name){ |
---|
102 | exports.set(node, x, name[x]); |
---|
103 | } |
---|
104 | return node; // DomNode |
---|
105 | } |
---|
106 | var lc = name.toLowerCase(), propName = exports.names[lc] || name; |
---|
107 | if(propName == "style" && typeof value != "string"){ // inline'd type check |
---|
108 | // special case: setting a style |
---|
109 | style.set(node, value); |
---|
110 | return node; // DomNode |
---|
111 | } |
---|
112 | if(propName == "innerHTML"){ |
---|
113 | // special case: assigning HTML |
---|
114 | // the hash lists elements with read-only innerHTML on IE |
---|
115 | if(has("ie") && node.tagName.toLowerCase() in {col: 1, colgroup: 1, |
---|
116 | table: 1, tbody: 1, tfoot: 1, thead: 1, tr: 1, title: 1}){ |
---|
117 | ctr.empty(node); |
---|
118 | node.appendChild(ctr.toDom(value, node.ownerDocument)); |
---|
119 | }else{ |
---|
120 | node[propName] = value; |
---|
121 | } |
---|
122 | return node; // DomNode |
---|
123 | } |
---|
124 | if(lang.isFunction(value)){ |
---|
125 | // special case: assigning an event handler |
---|
126 | // clobber if we can |
---|
127 | var attrId = node[_attrId]; |
---|
128 | if(!attrId){ |
---|
129 | attrId = _ctr++; |
---|
130 | node[_attrId] = attrId; |
---|
131 | } |
---|
132 | if(!_evtHdlrMap[attrId]){ |
---|
133 | _evtHdlrMap[attrId] = {}; |
---|
134 | } |
---|
135 | var h = _evtHdlrMap[attrId][propName]; |
---|
136 | if(h){ |
---|
137 | //h.remove(); |
---|
138 | conn.disconnect(h); |
---|
139 | }else{ |
---|
140 | try{ |
---|
141 | delete node[propName]; |
---|
142 | }catch(e){} |
---|
143 | } |
---|
144 | // ensure that event objects are normalized, etc. |
---|
145 | if(value){ |
---|
146 | //_evtHdlrMap[attrId][propName] = on(node, propName, value); |
---|
147 | _evtHdlrMap[attrId][propName] = conn.connect(node, propName, value); |
---|
148 | }else{ |
---|
149 | node[propName] = null; |
---|
150 | } |
---|
151 | return node; // DomNode |
---|
152 | } |
---|
153 | node[propName] = value; |
---|
154 | return node; // DomNode |
---|
155 | }; |
---|
156 | }); |
---|