OSDN Git Service

Version 0.6.134, add comments for closure compiler.
[pettanr/clientJs.git] / 0.6.x / js / 02_dom / 05_XNodeAttr.js
1 var X_Node_Attr_noValue = {\r
2                 'checked'  : 1,\r
3                 'compact'  : 1,\r
4                 'declare'  : 1,\r
5                 'defer'    : 1,\r
6                 'disabled' : 1,\r
7                 'ismap'    : 1,\r
8                 'multiple' : 1,\r
9                 'nohref'   : 1,\r
10                 'noresize' : 1,\r
11                 'noshade'  : 1,\r
12                 'nowrap'   : 1,\r
13                 'readonly' : 1,\r
14                 'selected' : 1\r
15 },\r
16 X_Node_Attr_renameForDOM = {\r
17                 'class'            : 'className',\r
18                 'accesskey'        : 'accessKey',\r
19                 'accept-charset'   : 'acceptCharset',\r
20                 'bgcolor'          : 'bgColor',\r
21                 'cellpadding'      : 'cellPadding',\r
22                 'cellspacing'      : 'cellSpacing',\r
23                 'char'             : 'ch',\r
24                 'charoff'          : 'chOff',\r
25                 'codebase'         : 'codeBase',\r
26                 'codetype'         : 'codeType',\r
27                 'colspan'          : 'colSpan',\r
28                 'datetime'         : 'dateTime',\r
29                 'for'              : 'htmlFor',\r
30                 'frameborder'      : 'frameBorder',\r
31                 'http-equiv'       : 'httpEquiv',\r
32                 'ismap'            : 'isMap',\r
33                 'longdesc'         : 'longDesc',\r
34                 'maxlength'        : 'maxLength',\r
35                 'nohref'           : 'noHref',\r
36                 'readonly'         : 'readOnly',\r
37                 'rowspan'          : 'rowSpan',\r
38                 'tabindex'         : 'tabIndex',\r
39                 'usemap'           : 'useMap',\r
40                 'valuetype'        : 'valueType',\r
41                 'checked'          : 'defaultChecked'\r
42 },\r
43 \r
44 X_Node_Attr_HAS_VALUE = {\r
45                 'INPUT'    : true,\r
46                 'TEXTAREA' : true,\r
47                 'SELECT'   : true,\r
48                 'BUTTON'   : true,\r
49                 'OBJECT'   : true,\r
50                 'PARAM'    : true // FlashVars が flash 側から書き換えられるケースがある??\r
51 },\r
52 \r
53 // <input type=button,hidden,submit,reset,radio,checkbox> の場合、value の値はユーザーで変えることはない\r
54 // <input type=text,password,file> はユーザーによって常に変更される HTML5 ではこれにさらにいろいろ加わる\r
55 X_Node_Attr_STATIC_VALUE_TYPES = {\r
56         'button'   : true,\r
57         'hidden'   : true,\r
58         'submit'   : true,\r
59         'reset'    : true,\r
60         'radio'    : true,\r
61         'checkbox' : true\r
62 },\r
63 \r
64 // 自由な内容が入るため、参照文字への変換が必要\r
65 X_Node_Attr_toChrReferance = {\r
66         'value' : true,\r
67         'title' : true,\r
68         'alt'   : true\r
69 },\r
70 \r
71 X_Node_Attr_renameForTag = {};\r
72         // http://nanto.asablo.jp/blog/2005/10/29/123294\r
73         // checked -> defaultChecked\r
74         // 動的に生成した input 要素を文書ツリーに挿入する前に設定した checked 属性は反映されず、defaultChecked だと反映される\r
75         // ロードイベントを拾うために、要素生成時にネットワーク関連の属性を設定しない。\r
76         //  -> src (img, iframe, ), link の href, <param name="movie" src=>\r
77         // \r
78 function X_Node_Attr_objToAttrText( that, skipNetworkForElmCreation ){\r
79         var obj     = that[ '_attrs' ],\r
80                 noValue = X_Node_Attr_noValue,\r
81                 attrs   = [ '' ], // 先頭にスペース\r
82                 plain   = X_EMPTY_OBJECT,\r
83                 n = 0, k, check;\r
84 \r
85         if( skipNetworkForElmCreation ){\r
86                 delete that[ '_newAttrs' ];\r
87                 // このあとで _newAttr にネットワーク系の属性を控える, attrText には加えない\r
88         } else {\r
89                 that[ '_flags' ] &= ~X_Node_State.OLD_ATTRTEXT;\r
90                 // 完全な attrText\r
91         };\r
92         \r
93         if( !obj ){ // Opera7\r
94                 delete that[ '_attrText' ];\r
95                 return '';\r
96         };\r
97         \r
98         for( k in obj ){\r
99                 if( plain[ k ] ) continue;\r
100                 \r
101                 if( skipNetworkForElmCreation ){\r
102                         check = false;\r
103                         switch( that[ '_tag' ] + k ){\r
104                                 case 'PARAMvalue' :\r
105                                         check = obj[ 'name' ] !== 'movie';\r
106                                 case 'INPUTsrc'  :\r
107                                         check = check || ( obj[ 'type' ] !== 'image' );\r
108                                 case 'LINKhref' :\r
109                                         check = check || ( obj[ 'rel' ] !== 'stylesheet' );\r
110                                         \r
111                                         if( !check ) break;\r
112 \r
113                                 case 'IMGsrc'     :\r
114                                 case 'IFRAMEsrc'  :\r
115                                 case 'FRAMEsrc'   :\r
116                                 case 'SCRIPTsrc'  :\r
117                                 case 'EMBEDsrc'   :\r
118                                 case 'OBJECTdata' :\r
119                                 case 'BGSOUNDsrc' :\r
120                                 case 'APPLETcode' :\r
121                                 //case 'AUDIOsrc' :\r
122                                 //case 'VIDEOsrc' :\r
123                                         if( !that[ '_newAttrs' ] ) that[ '_newAttrs' ] = {};\r
124                                         that[ '_newAttrs' ][ k ] = obj[ k ];\r
125                                         continue;\r
126                         };\r
127                 };\r
128 \r
129                 attrs[ ++n ] = noValue[ k ] ? k : [\r
130                         k, '="',\r
131                         X_Node_Attr_toChrReferance[ k ] ? X_String_toChrReferance( obj[ k ] ) : obj[ k ],\r
132                         '"' ].join( '' );\r
133         };\r
134         \r
135         if( 0 < n ){\r
136                 return that[ '_attrText' ] = attrs.join( ' ' );\r
137         };\r
138         delete that[ '_attrText' ];\r
139         return '';\r
140 };\r
141 \r
142 (function( renameForDOM, renameForTag ){\r
143         var k;\r
144         for( k in renameForDOM ){\r
145                 //if( X_EMPTY_OBJECT[ k ] ) continue;\r
146                 renameForTag[ renameForDOM[ k ] ] = k;\r
147         };\r
148 })( X_Node_Attr_renameForDOM, X_Node_Attr_renameForTag );\r
149 \r
150 \r
151 \r
152 /* --------------------------------------\r
153  *  attribute\r
154  * \r
155  * http://nanto.asablo.jp/blog/2005/10/29/123294\r
156  * className, onclick等 はここで設定しない\r
157  * \r
158  */\r
159 Node.prototype[ 'attr' ] = function( nameOrObj /* v */ ){\r
160         var attrs = this[ '_attrs' ], newAttrs, f, k, elm, v;\r
161         \r
162         if( !this[ '_tag' ] ) return this;\r
163         \r
164         if( nameOrObj && X_Type_isObject( nameOrObj ) ){\r
165                 attrs || ( attrs = this[ '_attrs' ] = {} );\r
166                 newAttrs = this[ '_newAttrs' ] || ( this[ '_newAttrs' ] = {} );\r
167                 \r
168                 for( k in nameOrObj ){\r
169                         //if( X_EMPTY_OBJECT[ k ] ) continue;\r
170                         if( X_Node_Attr_setAttr( this, attrs, newAttrs, k, nameOrObj[ k ] ) === true ) f = true;\r
171                 };\r
172                 if( f ){\r
173                         delete this[ '_attrText' ];\r
174                         this[ '_flags' ] |= X_Node_State.DIRTY_ATTR | X_Node_State.OLD_ATTRTEXT;\r
175                         this[ '_flags' ] & X_Node_State.IN_TREE && X_Node_reserveUpdate();\r
176                 };\r
177                 return this;\r
178         } else\r
179         if( 1 < arguments.length ){\r
180                 // setter\r
181                 if( X_Node_Attr_setAttr( this, attrs || ( this[ '_attrs' ] = {} ), this[ '_newAttrs' ] || ( this[ '_newAttrs' ] = {} ), nameOrObj, arguments[ 1 ] ) === true ){\r
182                         delete this[ '_attrText' ];\r
183                         this[ '_flags' ] |= X_Node_State.DIRTY_ATTR | X_Node_State.OLD_ATTRTEXT;\r
184                         this[ '_flags' ] & X_Node_State.IN_TREE && X_Node_reserveUpdate();\r
185                 };\r
186                 return this;\r
187         } else\r
188         if( typeof nameOrObj === 'string' ){\r
189                 // getter\r
190                 switch( nameOrObj ){\r
191                         case 'id' :\r
192                                 return this[ '_id' ];\r
193                         case 'class' :\r
194                         case 'className' :\r
195                                 return this[ '_className' ];\r
196                         case 'tag' :\r
197                         case 'tagName' :\r
198                                 return this[ '_tag' ];\r
199                         case 'style' :\r
200                         case 'cssText' :\r
201                                 return this[ 'cssText' ]();\r
202 \r
203                         case 'src' : // src は遷移して変化する, name も?\r
204                                 if( this[ '_tag' ] !== 'IFRAME' ) break;\r
205                                 if( this[ '_newAttrs' ] && X_Object_inObject( nameOrObj, this[ '_newAttrs' ] ) ) return this[ '_newAttrs' ][ nameOrObj ];\r
206                                 if( elm = X_UA_DOM.IE4 ? this[ '_rawObject' ] || X_Node__ie4getRawNode( this ) : this[ '_rawObject' ] ){\r
207                                         if( !attrs ) attrs = this[ '_attrs' ] = {};\r
208                                         return attrs[ nameOrObj ] = elm[ nameOrObj ]; // getAttribute( nameOrObj )?\r
209                                 };\r
210                                 break;\r
211                                 \r
212                         case 'selected' :\r
213                                 // kquery.js : safariのバグ対策\r
214                                 // if ($.browser.safari && key === "selected" && tmp) tmp.selectedIndex;\r
215                                 // 親ノードの selectedIndex の getter を呼んでおくと値が正しくなる、ということ?( by itozyun )\r
216                                 if( X_UA[ 'WebKit' ] ) this[ '_rawObject' ].parentNode && this[ '_rawObject' ].parentNode.selectedIndex;\r
217                         case 'value' :\r
218                                 if( this[ '_tag' ] === 'INPUT' && X_Node_Attr_STATIC_VALUE_TYPES[ attrs[ 'type' ] ] ) break;\r
219                         case 'checked' :\r
220                         case 'disabled' :                       \r
221                         case 'selectedIndex' :\r
222                                 if( X_Node_Attr_HAS_VALUE[ this[ '_tag' ] ] ){\r
223                                         if( this[ '_newAttrs' ] && X_Object_inObject( nameOrObj, this[ '_newAttrs' ] ) ) return this[ '_newAttrs' ][ nameOrObj ];\r
224                                         if( elm = X_UA_DOM.IE4 ? this[ '_rawObject' ] || X_Node__ie4getRawNode( this ) : this[ '_rawObject' ] ){\r
225                                                 if( !attrs ) attrs = this[ '_attrs' ] = {};\r
226                                                 return attrs[ nameOrObj ] = elm[ nameOrObj ]; // getAttribute( nameOrObj )?\r
227                                         };\r
228                                 };\r
229                                 break;\r
230                 };\r
231                 return attrs && attrs[ X_Node_Attr_renameForTag[ nameOrObj ] || nameOrObj ];\r
232         };\r
233 };\r
234 function X_Node_Attr_setAttr( that, attrs, newAttrs, name, v ){\r
235         switch( name ){\r
236                 // case 'type' : TODO IE は input, button, object に対して type の再設定が出来ない _state が要素生成済なら不可\r
237                 case 'UID' :\r
238                 case 'tag' :\r
239                 case 'tagName' :\r
240                         return;\r
241                 case 'id' :\r
242                         v = ( v !== 'ie4uid' + that[ '_uid' ] ) ? v : undefined;\r
243                         // TODO unique の check\r
244                         if( v !== that[ '_id' ] ){\r
245                                 that[ '_id' ] = v;\r
246                                 that[ '_flags' ] |= X_Node_State.DIRTY_ID;\r
247                                 that[ '_flags' ] & X_Node_State.IN_TREE && X_Node_reserveUpdate();\r
248                         };\r
249                         return; \r
250                 case 'class' :\r
251                 case 'className' :\r
252                         return that[ 'className' ]( v );\r
253                 case 'style' :\r
254                 case 'cssText' :\r
255                         return that[ 'cssText' ]( v );\r
256                 case 'text' :\r
257                         return that[ 'text' ]( v );\r
258                 case 'html' :\r
259                         return that[ 'html' ]( v );\r
260         };\r
261         // debug\r
262         if( name.indexOf( 'on' ) === 0 ){\r
263                 X.Logger.warn( 'xnode.attr("' + name + '") is wrong, xnode.listen() & xnode.unlisten().' );\r
264                 return;\r
265         };\r
266         \r
267         name = X_Node_Attr_renameForTag[ name ] || name;\r
268         if( attrs[ name ] === v ) return;\r
269         \r
270         if( v == null ){\r
271                 newAttrs[ name ] = undefined;\r
272                 if( X_Object_inObject( name, attrs ) ) delete attrs[ name ];\r
273         } else {\r
274                 newAttrs[ name ] = attrs[ name ] = v;\r
275         };\r
276         return true;\r
277 };\r
278 \r