UNPKG

3.15 kBJavaScriptView Raw
1import { View, CustomLayoutView } from '../core/view';
2import { layout } from '../../utils';
3/**
4 * Represents a View that has a single child - content.
5 * The View itself does not have visual representation and serves as a placeholder for its content in the logical tree.
6 */
7export class ContentView extends CustomLayoutView {
8 /**
9 * Gets or sets the single child of the view.
10 */
11 get content() {
12 return this._content;
13 }
14 set content(value) {
15 const oldView = this._content;
16 if (this._content) {
17 this._removeView(this._content);
18 }
19 this._content = value;
20 if (this._content) {
21 this._addView(this._content);
22 }
23 this._onContentChanged(oldView, value);
24 if (global.isIOS && oldView !== value) {
25 this.requestLayout();
26 }
27 }
28 get layoutView() {
29 let result;
30 if (this._content) {
31 let first = true;
32 this._content._eachLayoutView((child) => {
33 if (first) {
34 first = false;
35 result = child;
36 }
37 else {
38 throw new Error('More than one layout child inside a ContentView');
39 }
40 });
41 }
42 return result;
43 }
44 get _childrenCount() {
45 return this._content ? 1 : 0;
46 }
47 //@private
48 /**
49 * Called when the content property has changed.
50 * @private
51 * @param oldView The previous content.
52 * @param newView The new content.
53 */
54 _onContentChanged(oldView, newView) {
55 //
56 }
57 _addChildFromBuilder(name, value) {
58 if (value instanceof View) {
59 this.content = value;
60 }
61 }
62 eachChildView(callback) {
63 const content = this._content;
64 if (content) {
65 callback(content);
66 }
67 }
68 // This method won't be called in Android because we use the native android layout.
69 onMeasure(widthMeasureSpec, heightMeasureSpec) {
70 const result = View.measureChild(this, this.layoutView, widthMeasureSpec, heightMeasureSpec);
71 const width = layout.getMeasureSpecSize(widthMeasureSpec);
72 const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
73 const height = layout.getMeasureSpecSize(heightMeasureSpec);
74 const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
75 const measureWidth = Math.max(result.measuredWidth, this.effectiveMinWidth);
76 const measureHeight = Math.max(result.measuredHeight, this.effectiveMinHeight);
77 const widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
78 const heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
79 this.setMeasuredDimension(widthAndState, heightAndState);
80 }
81 // This method won't be called in Android because we use the native android layout.
82 onLayout(left, top, right, bottom) {
83 View.layoutChild(this, this.layoutView, 0, 0, right - left, bottom - top);
84 }
85}
86ContentView.prototype.recycleNativeView = 'auto';
87//# sourceMappingURL=index.js.map
\No newline at end of file