OSDN Git Service

94342542d6c73f9b11b09b2f46977b33677a26d8
[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( Node ) ){\r
94                                         //this.layout = arg;\r
95                                 } else\r
96                                 if( arg.instanceOf && arg.instanceOf( X.UI.Layout.Base ) ){\r
97                                         //this.layout = arg;\r
98                                 } else\r
99                                 if( X_Type_isObject( arg ) ){\r
100                                         if( attrs ){\r
101                                                 attrs = X_Class_override( attrs, arg );\r
102                                         } else {\r
103                                                 attrs = arg;\r
104                                         };\r
105                                 } else {\r
106                                         //throw new Error( 'AbstractUINode を継承したインスタンスを渡してください ' + arg );\r
107                                 };\r
108                         };\r
109                         \r
110                         for( p in attrs ){\r
111                                 if( X_EMPTY_OBJECT[ p ] ) continue;\r
112                                 ( support = this.supportAttrs[ p ] ) && this.setAttr( p, support, attrs[ p ] );\r
113                         };\r
114                 },\r
115         /* Rellay */\r
116                 initialize : function( root, rootData, parent, parentData ){\r
117                         var uinodes = this.uinodes,\r
118                                 i       = uinodes && uinodes.length;\r
119                         this.root       = root;\r
120                         this.rootData   = rootData;\r
121                         this.parent     = parent;\r
122                         this.parentData = parentData;\r
123                         //this.xnode      = X_Doc_create( 'div' );\r
124                         \r
125                         if( i ){\r
126                                 for( ; i; ){\r
127                                         uinodes[ --i ].initialize( root, rootData, this.User, this );\r
128                                 };                              \r
129                         };\r
130                         \r
131                         this.phase = 1;\r
132                         this.User.dispatch( { type : X.UI.Event.INIT } );\r
133                 },\r
134                 \r
135                 addToParent : function( parentXNode ){\r
136                         var uinodes = this.uinodes,\r
137                                 l       = uinodes && uinodes.length,\r
138                                 i;\r
139 \r
140                         parentXNode && parentXNode.append( this.xnode );\r
141                         \r
142                         if( l ){\r
143                                 for( i = 0; i < l; ++i ){\r
144                                         uinodes[ i ].addToParent( this.xnode );\r
145                                 };\r
146                         };\r
147                         \r
148                         this.phase = 2;\r
149                         this.User.dispatch( { type : X.UI.Event.ADDED } );\r
150                 },\r
151                 \r
152         /* Rellay */\r
153                 creationComplete : function(){\r
154                         var uinodes = this.uinodes,\r
155                                 i       = uinodes && uinodes.length;\r
156                         if( i ){\r
157                                 for( ; i; ){\r
158                                         uinodes[ --i ].creationComplete();\r
159                                 };                              \r
160                         };\r
161                         \r
162                         X.UI._AbstractUINode.prototype.creationComplete.apply( this, arguments );\r
163                 },\r
164                 \r
165                 calculate : function( isNeedsDetection, x, y, w, h ){\r
166                         var ret = this.layout.calculate( this, isNeedsDetection, x, y, w, h );                  \r
167                         this.phase = 4;\r
168                         return ret;\r
169                 },\r
170                 \r
171                 capcher : function( x, y ){\r
172                         var uinodes, child, _x, _y, hit, i;\r
173                         if( this.pointerDisabled ) return false;\r
174                         delete this.hitChildData;\r
175                         x -= this.boxX;\r
176                         y -= this.boxY;\r
177                         if( this.pointerChildren && ( uinodes = this.uinodes ) && ( i = uinodes.length ) ){\r
178                                 _x = x - this.scrollingX;\r
179                                 _y = y - this.scrollingY;\r
180                                 for( ; i; ){\r
181                                         child = uinodes[ --i ];\r
182                                         if( !child.pointerDisabled && child.boxX <= _x && _x < child.boxX + child.boxWidth && child.boxY <= _y && _y < child.boxY + child.boxHeight && child.capcher( _x, _y ) ){\r
183                                                 //console.log( 'hit child ' + _x + ' ' + _y + ' boxX:' + child.boxX + ' boxY:' + child.boxY );\r
184                                                 this.hitChildData = child;\r
185                                                 break;\r
186                                         };\r
187                                 };\r
188                         };\r
189                         if( this.through ){\r
190                                 this.hitChildData && !this.hovering && ( this.rootData.hoverList[ this.rootData.hoverList.length ] = this );\r
191                                 return !!this.hitChildData;\r
192                         };\r
193                         hit = 0 <= x && x < this.boxWidth && 0 <= y && y < this.boxHeight;\r
194                         ( this.hitChildData || hit ) && !this.hovering && ( this.rootData.hoverList[ this.rootData.hoverList.length ] = this );\r
195                         if( hit && this.hitChildData === null ) this.rootData.targetNodeData = this;\r
196                         return hit || !!this.hitChildData;\r
197                 },\r
198                 \r
199                 addAt : function( index, _uinodes ){\r
200                         //console.log( '# AddAt ' + this.phase )\r
201                         var uinodes = this.uinodes,\r
202                                 num     = uinodes.length,\r
203                                 p1      = 1 <= this.phase,\r
204                                 p2      = 2 <= this.phase,\r
205                                 p3      = 3 <= this.phase,\r
206                                 i       = 0,\r
207                                 _p1, _p2,\r
208                                 l, data;\r
209 \r
210                         //console.log( '### AddAt ' + this.phase )\r
211                         for( l = _uinodes.length; i < l; ++i ){\r
212                                 data = X_Class_getPrivate( _uinodes[ i ] );\r
213                                 _p1  = p1 && data.phase < 1;\r
214                                 _p2  = p2 && data.phase < 2;\r
215                                 _p1 && data.initialize( this.root, this.rootData, this.User, this );\r
216                                 if( index <= num ){\r
217                                         // _p2 && this.xnode.insertBefore( data.xnode, uinodes[ index + i ].xnode );\r
218                                         _p2 && uinodes[ index + i ].xnode.before( data.xnode );\r
219                                         _p2 && data.addToParent( this.xnode );\r
220                                         uinodes.splice( index + i, 0, data );\r
221                                 } else {\r
222                                         _p2 && data.addToParent( this.xnode );\r
223                                         uinodes[ uinodes.length ] = data;       \r
224                                 };\r
225                                 p3 && data.phase < 3 && data.creationComplete();\r
226                         };\r
227                         4 <= this.phase && this.rootData.reserveCalc();\r
228                 },\r
229                 \r
230                 remove : function( _uinodes ){\r
231                         //console.log( '# AddAt ' + this.phase )\r
232                         var uinodes = this.uinodes,\r
233                                 i       = _uinodes.length,\r
234                                 n, data;\r
235 \r
236                         //console.log( '### AddAt ' + this.phase )\r
237                         for( ; i; ){\r
238                                 data = X_Class_getPrivate( _uinodes[ --i ] );\r
239                                 if( ( n = uinodes.indexOf( data ) ) !== -1 ){\r
240                                         uinodes.splice( n, 1 );\r
241                                         data._remove();\r
242                                 };\r
243                         };\r
244                         4 <= this.phase && this.rootData.reserveCalc();\r
245                 },\r
246                 \r
247                 removeAt : function( from, length ){\r
248                         var uinodes = this.uinodes,\r
249                                 i       = uinodes.length,\r
250                                 to      = from + ( X_Type_isNumber( length ) && 1 <= length ? length : 1 ),\r
251                                 node;\r
252                         for( ; i; ){\r
253                                 node = uinodes[ --i ];\r
254                                 if( from <= i && i < to ){\r
255                                         uinodes.splice( i, 1 );\r
256                                         node._remove();\r
257                                 };\r
258                         };\r
259                         4 <= this.phase && this.rootData.reserveCalc();\r
260                 },\r
261                 \r
262                 _remove : function(){\r
263                         var uinodes = this.uinodes,\r
264                                 i       = uinodes.length;\r
265                         for( ; i; ){ uinodes[ --i ]._remove(); };\r
266                                         \r
267                         switch( this.phase ){\r
268                                 case 4:\r
269                                 case 3:\r
270                                 case 2:\r
271                                         this.xnode.remove();\r
272                                 case 1:\r
273                                         delete this.root;\r
274                                         delete this.rootData;\r
275                                         delete this.parent;\r
276                                         delete this.parentData;\r
277                                         delete this.xnode;\r
278                         };\r
279                         delete this.phase;\r
280                 }\r
281         }\r
282 );\r
283 \r
284 X.UI.Box = X.UI.AbstractUINode.inherits(\r
285         'Box',\r
286         X.Class.SUPER_ACCESS,\r
287         X.UI._Box,\r
288         {\r
289                 Constructor : function(){\r
290                         X_Class_newPrivate( this, X.UI.Layout.Canvas, arguments );\r
291                 },\r
292                 \r
293                 add : function( node /* , node, node ... */ ){\r
294                         X_Class_getPrivate( this ).addAt( this.numNodes() + 1, Array.prototype.slice.call( arguments ) );\r
295                         return this;\r
296                 },\r
297                 addAt : function( index, node /* , node, node ... */ ){\r
298                         if( index < 0 ) index = 0;\r
299                         X_Class_getPrivate( this ).addAt( arguments[ 0 ], Array.prototype.slice.call( arguments, 1 ) );\r
300                         return this;\r
301                 },\r
302                 remove : function( node /* , node, node ... */ ){\r
303                         X_Class_getPrivate( this ).remove( Array.prototype.slice.call( arguments ) );\r
304                         return this;\r
305                 },\r
306                 removeAt : function( from, length ){\r
307                         X_Class_getPrivate( this ).removeAt( from, length );\r
308                         return this;\r
309                 },\r
310                 getNodesByClass : function( klass ){\r
311                         var ret     = [],\r
312                                 uinodes = X_Class_getPrivate( this ).uinodes,\r
313                                 i, l, node;\r
314                         if( !uinodes || uinodes.length === 0 ) return ret;\r
315                         for( i = 0, l = uinodes.length; i < l; ++i ){\r
316                                 node = uinodes[ i ].User;\r
317                                 if( node.instanceOf( klass ) ) ret[ ret.length ] = node;\r
318                         };\r
319                         return ret;\r
320                 },\r
321                 getFirstChild : function(){\r
322                         return this.getNodeAt( 0 );\r
323                 },\r
324                 getLastChild : function(){\r
325                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
326                         return uinodes && uinodes.length && uinodes[ uinodes.length - 1 ].User || null;\r
327                 },\r
328                 getNodeAt : function( index ){\r
329                         if( index < 0 ) return null;\r
330                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
331                         return uinodes && uinodes[ index ].User || null;\r
332                 },\r
333                 numNodes : function(){\r
334                         var uinodes = X_Class_getPrivate( this ).uinodes;\r
335                         return uinodes && uinodes.length || 0;\r
336                 }\r
337         }\r
338 );\r
339 \r
340 /*\r
341  * layout が設定されている Box のサブクラスに対して、layout を指定できない.\r
342  * \r
343  */\r
344 X.UI.Box.presets = function(){\r
345         var args   = arguments,\r
346                 i      = 0,\r
347                 l      = args.length,   \r
348                 shadow = X_Class_getClassDef( this ).privateClass,\r
349                 layout = shadow.prototype.layout,\r
350                 arg, attrs, supports, klass, klassDef, privateKlass, boxName;\r
351                         \r
352         for( ; i < l; ++i ){\r
353                 arg = args[ i ];\r
354                 if( !arg ) continue;\r
355                 // レイアウトの preset ができるのは layout が未定義な Box だけ\r
356                 if( !shadow.prototype.layout && arg.instanceOf && arg.instanceOf( X.UI.Layout.Base ) ){\r
357                         layout = arg;\r
358                 } else\r
359                 if( ( klassDef = X_Class_getClassDef( arg ) ) && klassDef.isPrivate ){\r
360                         privateKlass = arg;\r
361                         layout = privateKlass.prototype.layout;\r
362                 } else\r
363                 if( X_Type_isObject( arg ) ){\r
364                         if( attrs ){\r
365                                 X_Class_override( attrs, arg, true );\r
366                         } else {\r
367                                 attrs = arg;\r
368                         };\r
369                 } else\r
370                 if( X_Type_isString( arg ) ){\r
371                         boxName = arg;\r
372                 };\r
373         };\r
374         \r
375         if( privateKlass ){\r
376                 supports = X.UI.Attr.createAttrDef( privateKlass.prototype.supportAttrs, layout.overrideAttrsForSelf );\r
377                 \r
378                 klass = this.inherits( privateKlass );\r
379                 privateKlass.prototype.supportAttrs = supports,\r
380                 privateKlass.prototype.attrClass    = X.UI.Attr.preset( privateKlass.prototype.attrClass, supports, attrs );\r
381         } else {\r
382                 supports = X.UI.Attr.createAttrDef( shadow.prototype.supportAttrs, layout.overrideAttrsForSelf );\r
383                 \r
384                 klass = this.inherits(\r
385                         boxName,\r
386                         shadow.inherits(\r
387                                 {\r
388                                         layout       : layout,\r
389                                         supportAttrs : supports,\r
390                                         attrClass    : X.UI.Attr.preset( shadow.prototype.attrClass, supports, attrs )\r
391                                 }\r
392                         )\r
393                 );              \r
394         };\r
395 \r
396         klass.presets = this.presets || X.UI.Box.presets;\r
397         \r
398         return klass;\r
399 };\r