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