OSDN Git Service

Version 0.6.125, bugfix X.Node.
[pettanr/clientJs.git] / 0.6.x / js / 20_ui / 08_Box.js
1 \r
2 X.UI.Layout.Canvas = X.UI.Layout.create( {\r
3         name : 'CanvasLayout',\r
4         \r
5         overrideAttrsForSelf : {\r
6                 selectable  : false,\r
7                 role        : [ 0, X.UI.Dirty.CLEAN, X.UI.Attr.USER.UINODE, X.UI.Attr.Type.INIT_ONLY | X.UI.Attr.Type.LIST, 'none,chrome,container' ],\r
8                 \r
9                 width       : [ '100%',         X.UI.Dirty.LAYOUT, X.UI.Attr.USER.LAYOUT, X.UI.Attr.Type.LENGTH | X.UI.Attr.Type.PERCENT ],\r
10                 maxWidth    : [ X.UI.Attr.AUTO, X.UI.Dirty.LAYOUT, X.UI.Attr.USER.LAYOUT, X.UI.Attr.Type.LENGTH | X.UI.Attr.Type.PERCENT ],\r
11                 height      : [ 0,              X.UI.Dirty.LAYOUT, X.UI.Attr.USER.LAYOUT, X.UI.Attr.Type.LENGTH | X.UI.Attr.Type.PERCENT ],\r
12                 maxHeight   : [ X.UI.Attr.AUTO, X.UI.Dirty.LAYOUT, X.UI.Attr.USER.LAYOUT, X.UI.Attr.Type.LENGTH | X.UI.Attr.Type.PERCENT ]\r
13         },\r
14         \r
15         overrideAttrsForChild : {\r
16                 left   : true,\r
17                 top    : true,\r
18                 bottom : true,\r
19                 right  : true\r
20         },\r
21         \r
22         calculate : function( data, isNeedsDetection, x, y, w, h ){\r
23                 var uinodes, l, i, _x, _y, _w, _h, node;\r
24 \r
25                 data.preMesure( w, h );\r
26                 \r
27                 if( isNeedsDetection && ( data.boxWidth === X.UI.Attr.AUTO || data.boxHeight === X.UI.Attr.AUTO ) ) return false;\r
28                 \r
29                 _x = data.contentL;\r
30                 _y = data.contentT;\r
31                 _w = data.contentWidth;\r
32                 _h = data.contentHeight;\r
33 \r
34                 if( ( uinodes = data.uinodes ) && ( l = uinodes.length ) ){\r
35                         for( i = 0; i < l; ++i ){\r
36                                 node = uinodes[ i ];\r
37                                 node.calculate( false, _x, _y, _w, _h );\r
38                         };\r
39                 } else\r
40                 if( data.contentHeight === X.UI.Attr.AUTO ){\r
41                         data.contentHeight = data.minContentHeight !== X.UI.Attr.AUTO ? data.minContentHeight : 0;\r
42                 };\r
43                 \r
44                 data.postMesure();\r
45                 data.updateLayout( x + data.boxX, y + data.boxY );\r
46                 \r
47                 return true;\r
48         }\r
49 });\r
50 \r
51 \r
52 X.UI._Box = X.UI._AbstractUINode.inherits(\r
53         'X.UI._Box',\r
54         X.Class.PRIVATE_DATA | X.Class.SUPER_ACCESS, // 現状 super 指定がないとconstructor未定擬時に親のconstructor が使われない\r
55         {\r
56                 layout          : null,\r
57                 uinodes         : null,\r
58                 xnodes          : null,\r
59                 \r
60                 hitChildData    : null,\r
61                 pointerChildren : true,\r
62                 through         : false,\r
63 \r
64                 scrollingX      : 0,\r
65                 scrollingY      : 0,\r
66 \r
67                 Constructor : function( layout, args ){\r
68                         var i = 0,\r
69                                 l = args.length,\r
70                                 j = -1,\r
71                                 uinodes, arg, _data, attrs, support;\r
72                         \r
73                         if( !this.User.instanceOf( X.UI.Box ) ){\r
74                                 //throw new Error( 'Box を継承したインスタンスだけが _Box のオーナーになれます' );\r
75                         };                      \r
76                         \r
77                         this.xnode  = X_Doc_create( 'div' );\r
78                         \r
79                         // すでに定義されていればそちらを採用\r
80                         // supportAttrs や attrClass が、layout を元に上書きされているため\r
81                         this.layout = this.layout || layout;\r
82                         \r
83                         for( ; i < l; ++i ){\r
84                                 arg = args[ i ];\r
85                                 if( arg.instanceOf && arg.instanceOf( X.UI.AbstractUINode ) ){\r
86                                         _data = X_Class_getPrivate( arg );\r
87                                         if( !uinodes ) this.uinodes = uinodes = [];\r
88                                         uinodes[ ++j ] = _data;\r
89                                         if( _data.parent ){\r
90                                                 //throw new Error( 'インスタンスはすでに親に追加されています ' + arg );\r
91                                         };\r
92                                 } else\r
93                                 if( arg.instanceOf && arg.instanceOf( X.UI.Layout.Base ) ){\r
94                                         //this.layout = arg;\r
95                                 } else\r
96                                 if( X.Type.isObject( arg ) ){\r
97                                         if( attrs ){\r
98                                                 attrs = X_Class_override( attrs, arg );\r
99                                         } else {\r
100                                                 attrs = arg;\r
101                                         };\r
102                                 } else {\r
103                                         //throw new Error( 'AbstractUINode を継承したインスタンスを渡してください ' + arg );\r
104                                 };\r
105                         };\r
106                         \r
107                         for( p in attrs ){\r
108                                 if( X_EMPTY_OBJECT[ p ] ) continue;\r
109                                 ( support = this.supportAttrs[ p ] ) && this.setAttr( p, support, attrs[ p ] );\r
110                         };\r
111                 },\r
112         /* Rellay */\r
113                 initialize : function( root, rootData, parent, parentData ){\r
114                         var uinodes = this.uinodes,\r
115                                 i       = uinodes && uinodes.length;\r
116                         this.root       = root;\r
117                         this.rootData   = rootData;\r
118                         this.parent     = parent;\r
119                         this.parentData = parentData;\r
120                         //this.xnode      = X_Doc_create( 'div' );\r
121                         \r
122                         if( i ){\r
123                                 for( ; i; ){\r
124                                         uinodes[ --i ].initialize( root, rootData, this.User, this );\r
125                                 };                              \r
126                         };\r
127                         \r
128                         this.phase = 1;\r
129                         this.User.dispatch( { type : X.UI.Event.INIT } );\r
130                 },\r
131                 \r
132                 addToParent : function( parentXNode ){\r
133                         var uinodes = this.uinodes,\r
134                                 l       = uinodes && uinodes.length,\r
135                                 i;\r
136 \r
137                         parentXNode && parentXNode.append( this.xnode );\r
138                         \r
139                         if( l ){\r
140                                 for( i = 0; i < l; ++i ){\r
141                                         uinodes[ i ].addToParent( this.xnode );\r
142                                 };\r
143                         };\r
144                         \r
145                         this.phase = 2;\r
146                         this.User.dispatch( { type : X.UI.Event.ADDED } );\r
147                 },\r
148                 \r
149         /* Rellay */\r
150                 creationComplete : function(){\r
151                         var uinodes = this.uinodes,\r
152                                 i       = uinodes && uinodes.length;\r
153                         if( i ){\r
154                                 for( ; i; ){\r
155                                         uinodes[ --i ].creationComplete();\r
156                                 };                              \r
157                         };\r
158                         \r
159                         X.UI._AbstractUINode.prototype.creationComplete.apply( this, arguments );\r
160                 },\r
161                 \r
162                 calculate : function( isNeedsDetection, x, y, w, h ){\r
163                         var ret = this.layout.calculate( this, isNeedsDetection, x, y, w, h );                  \r
164                         this.phase = 4;\r
165                         return ret;\r
166                 },\r
167                 \r
168                 capcher : function( x, y ){\r
169                         var uinodes, child, _x, _y, hit, i;\r
170                         if( this.pointerDisabled ) return false;\r
171                         delete this.hitChildData;\r
172                         x -= this.boxX;\r
173                         y -= this.boxY;\r
174                         if( this.pointerChildren && ( uinodes = this.uinodes ) && ( i = uinodes.length ) ){\r
175                                 _x = x - this.scrollingX;\r
176                                 _y = y - this.scrollingY;\r
177                                 for( ; i; ){\r
178                                         child = uinodes[ --i ];\r
179                                         if( !child.pointerDisabled && child.boxX <= _x && _x < child.boxX + child.boxWidth && child.boxY <= _y && _y < child.boxY + child.boxHeight && child.capcher( _x, _y ) ){\r
180                                                 //console.log( 'hit child ' + _x + ' ' + _y + ' boxX:' + child.boxX + ' boxY:' + child.boxY );\r
181                                                 this.hitChildData = child;\r
182                                                 break;\r
183                                         };\r
184                                 };\r
185                         };\r
186                         if( this.through ){\r
187                                 this.hitChildData && !this.hovering && ( this.rootData.hoverList[ this.rootData.hoverList.length ] = this );\r
188                                 return !!this.hitChildData;\r
189                         };\r
190                         hit = 0 <= x && x < this.boxWidth && 0 <= y && y < this.boxHeight;\r
191                         ( this.hitChildData || hit ) && !this.hovering && ( this.rootData.hoverList[ this.rootData.hoverList.length ] = this );\r
192                         if( hit && this.hitChildData === null ) this.rootData.targetNodeData = this;\r
193                         return hit || !!this.hitChildData;\r
194                 },\r
195                 \r
196                 addAt : function( index, _uinodes ){\r
197                         //console.log( '# AddAt ' + this.phase )\r
198                         var uinodes = this.uinodes,\r
199                                 num     = uinodes.length,\r
200                                 p1      = 1 <= this.phase,\r
201                                 p2      = 2 <= this.phase,\r
202                                 p3      = 3 <= this.phase,\r
203                                 i       = 0,\r
204                                 _p1, _p2,\r
205                                 l, data;\r
206 \r
207                         //console.log( '### AddAt ' + this.phase )\r
208                         for( l = _uinodes.length; i < l; ++i ){\r
209                                 data = X_Class_getPrivate( _uinodes[ i ] );\r
210                                 _p1  = p1 && data.phase < 1;\r
211                                 _p2  = p2 && data.phase < 2;\r
212                                 _p1 && data.initialize( this.root, this.rootData, this.User, this );\r
213                                 if( index <= num ){\r
214                                         // _p2 && this.xnode.insertBefore( data.xnode, uinodes[ index + i ].xnode );\r
215                                         _p2 && uinodes[ index + i ].xnode.before( data.xnode );\r
216                                         _p2 && data.addToParent( this.xnode );\r
217                                         uinodes.splice( index + i, 0, data );\r
218                                 } else {\r
219                                         _p2 && data.addToParent( this.xnode );\r
220                                         uinodes[ uinodes.length ] = data;       \r
221                                 };\r
222                                 p3 && data.phase < 3 && data.creationComplete();\r
223                         };\r
224                         4 <= this.phase && this.rootData.reserveCalc();\r
225                 },\r
226                 \r
227                 remove : function( _uinodes ){\r
228                         //console.log( '# AddAt ' + this.phase )\r
229                         var uinodes = this.uinodes,\r
230                                 i       = _uinodes.length,\r
231                                 n, data;\r
232 \r
233                         //console.log( '### AddAt ' + this.phase )\r
234                         for( ; i; ){\r
235                                 data = X_Class_getPrivate( _uinodes[ --i ] );\r
236                                 if( ( n = uinodes.indexOf( data ) ) !== -1 ){\r
237                                         uinodes.splice( n, 1 );\r
238                                         data._remove();\r
239                                 };\r
240                         };\r
241                         4 <= this.phase && this.rootData.reserveCalc();\r
242                 },\r
243                 \r
244                 removeAt : function( from, length ){\r
245                         var uinodes = this.uinodes,\r
246                                 i       = uinodes.length,\r
247                                 to      = from + ( X.Type.isNumber( length ) && 1 <= length ? length : 1 ),\r
248                                 node;\r
249                         for( ; i; ){\r
250                                 node = uinodes[ --i ];\r
251                                 if( from <= i && i < to ){\r
252                                         uinodes.splice( i, 1 );\r
253                                         node._remove();\r
254                                 };\r
255                         };\r
256                         4 <= this.phase && this.rootData.reserveCalc();\r
257                 },\r
258                 \r
259                 _remove : function(){\r
260                         var uinodes = this.uinodes,\r
261                                 i       = uinodes.length;\r
262                         for( ; i; ){ uinodes[ --i ]._remove(); };\r
263                                         \r
264                         switch( this.phase ){\r
265                                 case 4:\r
266                                 case 3:\r
267                                 case 2:\r
268                                         this.xnode.remove();\r
269                                 case 1:\r
270                                         delete this.root;\r
271                                         delete this.rootData;\r
272                                         delete this.parent;\r
273                                         delete this.parentData;\r
274                                         delete this.xnode;\r
275                         };\r
276                         delete this.phase;\r
277                 }\r
278         }\r
279 );\r
280 \r
281 X.UI.Box = X.UI.AbstractUINode.inherits(\r
282         'Box',\r
283         X.Class.SUPER_ACCESS,\r
284         X.UI._Box,\r
285         {\r
286                 Constructor : function(){\r
287                         X_Class_newPrivate( this, X.UI.Layout.Canvas, arguments );\r
288                 },\r
289                 \r
290                 add : function( node /* , node, node ... */ ){\r
291                         X_Class_getPrivate( this ).addAt( this.numNodes() + 1, Array.prototype.slice.call( arguments ) );\r
292                         return this;\r
293                 },\r
294                 addAt : function( index, node /* , node, node ... */ ){\r
295                         if( index < 0 ) index = 0;\r
296                         X_Class_getPrivate( this ).addAt( arguments[ 0 ], Array.prototype.slice.call( arguments, 1 ) );\r
297                         return this;\r
298                 },\r
299                 remove : function( node /* , node, node ... */ ){\r
300                         X_Class_getPrivate( this ).remove( Array.prototype.slice.call( arguments ) );\r
301                         return this;\r
302                 },\r
303                 removeAt : function( from, length ){\r
304                         X_Class_getPrivate( this ).removeAt( from, length );\r
305                         return this;\r
306                 },\r
307                 getNodesByClass : function( klass ){\r
308                         var ret     = [],\r
309                                 uinodes = X_Class_getPrivate( this ).uinodes,\r
310                                 i, l, node;\r
311                         if( !uinodes || uinodes.length === 0 ) return ret;\r
312                         for( i = 0, l = uinodes.length; i < l; ++i ){\r
313                                 node = uinodes[ i ].User;\r
314                                 if( node.instanceOf( klass ) ) ret[ ret.length ] = node;\r
315                         };\r
316                         return ret;\r
317                 },\r
318                 getFirstChild : function(){\r
319                         return this.getNodeAt( 0 );\r
320                 },\r
321                 getLastChild : function(){\r
322                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
323                         return uinodes && uinodes.length && uinodes[ uinodes.length - 1 ].User || null;\r
324                 },\r
325                 getNodeAt : function( index ){\r
326                         if( index < 0 ) return null;\r
327                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
328                         return uinodes && uinodes[ index ].User || null;\r
329                 },\r
330                 numNodes : function(){\r
331                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
332                         return uinodes && uinodes.length || 0;\r
333                 }\r
334         }\r
335 );\r
336 \r
337 /*\r
338  * layout が設定されている Box のサブクラスに対して、layout を指定できない.\r
339  * \r
340  */\r
341 X.UI.Box.presets = function(){\r
342         var args   = arguments,\r
343                 i      = 0,\r
344                 l      = args.length,   \r
345                 shadow = X_Class_getClassDef( this ).privateClass,\r
346                 layout = shadow.prototype.layout,\r
347                 arg, attrs, supports, klass, klassDef, privateKlass, boxName;\r
348                         \r
349         for( ; i < l; ++i ){\r
350                 arg = args[ i ];\r
351                 if( !arg ) continue;\r
352                 // レイアウトの preset ができるのは layout が未定義な Box だけ\r
353                 if( !shadow.prototype.layout && arg.instanceOf && arg.instanceOf( X.UI.Layout.Base ) ){\r
354                         layout = arg;\r
355                 } else\r
356                 if( ( klassDef = X_Class_getClassDef( arg ) ) && klassDef.isPrivate ){\r
357                         privateKlass = arg;\r
358                         layout = privateKlass.prototype.layout;\r
359                 } else\r
360                 if( X.Type.isObject( arg ) ){\r
361                         if( attrs ){\r
362                                 X_Class_override( attrs, arg, true );\r
363                         } else {\r
364                                 attrs = arg;\r
365                         };\r
366                 } else\r
367                 if( X.Type.isString( arg ) ){\r
368                         boxName = arg;\r
369                 };\r
370         };\r
371         \r
372         if( privateKlass ){\r
373                 supports = X.UI.Attr.createAttrDef( privateKlass.prototype.supportAttrs, layout.overrideAttrsForSelf );\r
374                 \r
375                 klass = this.inherits( privateKlass );\r
376                 privateKlass.prototype.supportAttrs = supports,\r
377                 privateKlass.prototype.attrClass    = X.UI.Attr.preset( privateKlass.prototype.attrClass, supports, attrs );\r
378         } else {\r
379                 supports = X.UI.Attr.createAttrDef( shadow.prototype.supportAttrs, layout.overrideAttrsForSelf );\r
380                 \r
381                 klass = this.inherits(\r
382                         boxName,\r
383                         shadow.inherits(\r
384                                 {\r
385                                         layout       : layout,\r
386                                         supportAttrs : supports,\r
387                                         attrClass    : X.UI.Attr.preset( shadow.prototype.attrClass, supports, attrs )\r
388                                 }\r
389                         )\r
390                 );              \r
391         };\r
392 \r
393         klass.presets = this.presets || X.UI.Box.presets;\r
394         \r
395         return klass;\r
396 };\r