Skip to content

Commit 80e3363

Browse files
authored
Merge pull request NativeScript#3297 from NativeScript/fixes
Fixed: getViewById, TextDecoration
2 parents 8d573e0 + 51e448a commit 80e3363

File tree

16 files changed

+170
-214
lines changed

16 files changed

+170
-214
lines changed
Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
1-
import view = require("ui/core/view");
2-
import label = require("ui/label");
3-
import button = require("ui/button");
4-
import textField = require("ui/text-field");
5-
import textView = require("ui/text-view");
1+
import { EventData, TextBase, TextDecoration } from "ui/text-base";
2+
import { Page } from "ui/page";
63

7-
export function changeTextButonTap(args) {
8-
var btnChange = <button.Button>args.object;
9-
var lbl = <label.Label>btnChange.parent.getViewById("Label");
10-
var btn = <button.Button>btnChange.parent.getViewById("Button");
11-
var textField = <textField.TextField>btnChange.parent.getViewById("TextField");
12-
var textView = <textView.TextView>btnChange.parent.getViewById("TextView");
13-
14-
if(lbl.text === "Change text") {
15-
lbl.text = btn.text = textField.text = textView.text = "Text changed";
16-
} else {
17-
lbl.text = btn.text = textField.text = textView.text = "Change text";
18-
}
19-
}
20-
21-
export function butonTap(args) {
22-
var btnChange = <view.View>args.object;
23-
var lbl = <label.Label>btnChange.parent.getViewById("Label");
24-
var btn = <button.Button>btnChange.parent.getViewById("Button");
25-
var textField = <textField.TextField>btnChange.parent.getViewById("TextField");
26-
var textView = <textView.TextView>btnChange.parent.getViewById("TextView");
4+
const possibleValues = [
5+
TextDecoration.NONE,
6+
TextDecoration.UNDERLINE,
7+
TextDecoration.LINE_THROUGH,
8+
TextDecoration.UNDERLINE_LINE_THROUGH
9+
];
2710

28-
if (lbl.style.textDecoration === "none") {
29-
lbl.style.textDecoration = "underline";
30-
btn.style.textDecoration = "underline";
31-
textField.style.textDecoration = "underline";
32-
textView.style.textDecoration = "underline";
33-
} else if (lbl.style.textDecoration === "underline") {
34-
lbl.style.textDecoration = "line-through";
35-
btn.style.textDecoration = "line-through";
36-
textField.style.textDecoration = "line-through";
37-
textView.style.textDecoration = "line-through";
38-
} else if (lbl.style.textDecoration === "line-through") {
39-
lbl.style.textDecoration = "line-through underline";
40-
btn.style.textDecoration = "line-through underline";
41-
textField.style.textDecoration = "line-through underline";
42-
textView.style.textDecoration = "line-through underline";
43-
} else if (lbl.style.textDecoration === "line-through underline") {
44-
lbl.style.textDecoration = "line-through underline none";
45-
btn.style.textDecoration = "line-through underline none";
46-
textField.style.textDecoration = "line-through underline none";
47-
textView.style.textDecoration = "line-through underline none";
48-
} else if (lbl.style.textDecoration === "line-through underline none") {
49-
lbl.style.textDecoration = "none";
50-
btn.style.textDecoration = "none";
51-
textField.style.textDecoration = "none";
52-
textView.style.textDecoration = "none";
53-
54-
if(lbl.text === "Change text") {
55-
lbl.text = btn.text = textField.text = textView.text = "Text changed";
56-
} else {
57-
lbl.text = btn.text = textField.text = textView.text = "Change text";
58-
}
59-
}
11+
export function butonTap(args: EventData) {
12+
let page = (<TextBase>args.object).page;
13+
let lbl = <TextBase>page.getViewById("Label");
14+
let btn = <TextBase>page.getViewById("Button");
15+
let textField = <TextBase>page.getViewById("TextField");
16+
let textView = <TextBase>page.getViewById("TextView");
17+
18+
let currentIndex = possibleValues.indexOf(lbl.textDecoration);
19+
let newIndex = (currentIndex + 1) % possibleValues.length;
20+
let newValue = <TextDecoration>possibleValues[newIndex];
21+
22+
lbl.textDecoration = newValue;
23+
btn.textDecoration = newValue;
24+
textField.textDecoration = newValue;
25+
textView.textDecoration = newValue;
6026
}
Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
1-
<Page >
1+
<Page>
22
<StackLayout>
3-
<!--<Button id="ChangeText" automationText="Change text" text="Change text" tap="changeTextButonTap" />-->
43
<Button id="Change" automationText="Change" text="Change" tap="butonTap" />
5-
<Label id="Label" automationText="Label" text="Label" style.textDecoration="none" />
6-
<Button id="Button" automationText="Button" text="Button" style.textDecoration="none" />
7-
<TextField id="TextField" automationText="TextField" text="TextField" style.textDecoration="none" />
8-
<TextView id="TextView" automationText="TextView" text="TextView" style.textDecoration="none" />
9-
10-
<!--
11-
<Label text="Label" style="text-decoration:none" />
12-
<Label text="Label" style="text-decoration:underline" />
13-
<Label text="Label" style="text-decoration:line-through" />
14-
<Label text="Label" style="text-decoration:line-through underline" />
15-
<Label text="Label" style="text-decoration:line-through underline none" />
16-
17-
<TextField text="TextField" style="text-decoration:none" />
18-
<TextField text="TextField" style="text-decoration:underline" />
19-
<TextField text="TextField" style="text-decoration:line-through" />
20-
<TextField text="TextField" style="text-decoration:underline line-through" />
21-
<TextField text="TextField" style="text-decoration:underline line-through none" />
22-
23-
<TextView text="TextView" style="text-decoration:none" />
24-
<TextView text="TextView" style="text-decoration:underline" />
25-
<TextView text="TextView" style="text-decoration:line-through" />
26-
<TextView text="TextView" style="text-decoration:line-through underline" />
27-
<TextView text="TextView" style="text-decoration:line-through underline none" />
28-
29-
<Button text="Button" style="text-decoration:none" />
30-
<Button text="Button" style="text-decoration:underline" />
31-
<Button text="Button" style="text-decoration:line-through" />
32-
<Button text="Button" style="text-decoration:underline line-through" />
33-
<Button text="Button" style="text-decoration:underline line-through none" />
34-
-->
4+
<Label id="Label" automationText="Label" text="Label" textDecoration="none" />
5+
<Button id="Button" automationText="Button" text="Button" textDecoration="none" />
6+
<TextField id="TextField" automationText="TextField" text="TextField" textDecoration="none" />
7+
<TextView id="TextView" automationText="TextView" text="TextView" textDecoration="none" />
358
</StackLayout>
369
</Page>

apps/app/ui-tests-app/css/text-transform.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,15 @@ export function butonTap(args) {
3535
btn.style.textTransform = "uppercase";
3636
textField.style.textTransform = "uppercase";
3737
textView.style.textTransform = "uppercase";
38-
} else if (lbl.style.textTransform === "uppercase" && lbl.style.textDecoration !== "line-through underline") {
38+
} else if (lbl.style.textTransform === "uppercase") {
3939
lbl.style.textTransform = "lowercase";
4040
btn.style.textTransform = "lowercase";
4141
textField.style.textTransform = "lowercase";
4242
textView.style.textTransform = "lowercase";
4343
} else if (lbl.style.textTransform === "lowercase") {
44-
// lbl.style.textTransform = "uppercase";
45-
// btn.style.textTransform = "uppercase";
46-
// textField.style.textTransform = "uppercase";
47-
// textView.style.textTransform = "uppercase";
48-
49-
// lbl.style.textDecoration = "line-through underline";
50-
// btn.style.textDecoration = "line-through underline";
51-
// textField.style.textDecoration = "line-through underline";
52-
// textView.style.textDecoration = "line-through underline";
53-
//} else if (lbl.style.textTransform === "uppercase" && lbl.style.textDecoration === "line-through underline") {
5444
lbl.style.textTransform = "none";
5545
btn.style.textTransform = "none";
5646
textField.style.textTransform = "none";
5747
textView.style.textTransform = "none";
58-
59-
//lbl.style.textDecoration = "none";
60-
//btn.style.textDecoration = "none";
61-
//textField.style.textDecoration = "none";
62-
//textView.style.textDecoration = "none";
6348
}
6449
}

tns-core-modules/tns-core-modules.base.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
/// <reference path="text/text.d.ts" />
3131
/// <reference path="timer/timer.d.ts" />
3232
/// <reference path="trace/trace.d.ts" />
33+
/// <reference path="ui/definitions.d.ts" />
3334
/// <reference path="ui/action-bar/action-bar.d.ts" />
3435
/// <reference path="ui/activity-indicator/activity-indicator.d.ts" />
3536
/// <reference path="ui/animation/animation.d.ts" />

tns-core-modules/ui/core/view-base.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,47 @@ export function isEventOrGesture(name: string, view: ViewBaseDefinition): boolea
5050
return false;
5151
}
5252

53+
export function getViewById(view: ViewBaseDefinition, id: string): ViewBaseDefinition {
54+
if (!view) {
55+
return undefined;
56+
}
57+
58+
if (view.id === id) {
59+
return view;
60+
}
61+
62+
let retVal: ViewBaseDefinition;
63+
const descendantsCallback = function (child: ViewBaseDefinition): boolean {
64+
if (child.id === id) {
65+
retVal = child;
66+
// break the iteration by returning false
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
73+
eachDescendant(view, descendantsCallback);
74+
return retVal;
75+
}
76+
77+
export function eachDescendant(view: ViewBaseDefinition, callback: (child: ViewBaseDefinition) => boolean) {
78+
if (!callback || !view) {
79+
return;
80+
}
81+
82+
let continueIteration: boolean;
83+
let localCallback = function (child: ViewBaseDefinition): boolean {
84+
continueIteration = callback(child);
85+
if (continueIteration) {
86+
child.eachChild(localCallback);
87+
}
88+
return continueIteration;
89+
}
90+
91+
view.eachChild(localCallback);
92+
}
93+
5394
export class ViewBase extends Observable implements ViewBaseDefinition {
5495
private _updatingJSPropertiesDict = {};
5596
private _style: Style;
@@ -87,6 +128,10 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
87128
return this._isLoaded;
88129
}
89130

131+
getViewById<T extends ViewBaseDefinition>(id: string): T {
132+
return <T>getViewById(this, id);
133+
}
134+
90135
get page(): ViewBaseDefinition {
91136
if (this.parent) {
92137
return this.parent.page;
@@ -492,4 +537,15 @@ function resetStyles(view: ViewBase): void {
492537
}
493538

494539
export const idProperty = new Property<ViewBase, string>({ name: "id", valueChanged: (view, oldValue, newValue) => resetStyles(view) });
495-
idProperty.register(ViewBase);
540+
idProperty.register(ViewBase);
541+
542+
export function makeValidator<T>(...values: T[]): (value: any) => value is T {
543+
const set = new Set(values);
544+
return (value: any): value is T => set.has(value);
545+
}
546+
export function makeParser<T>(isValid: (value: any) => boolean, def: T): (value: any) => T {
547+
return value => {
548+
const lower = value && value.toLowerCase();
549+
return isValid(lower) ? lower : def;
550+
}
551+
}

tns-core-modules/ui/core/view-common.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,6 @@ Style.prototype.effectiveBorderRightWidth = 0;
4949
Style.prototype.effectiveBorderBottomWidth = 0;
5050
Style.prototype.effectiveBorderLeftWidth = 0;
5151

52-
export function getViewById(view: ViewDefinition, id: string): ViewDefinition {
53-
if (!view) {
54-
return undefined;
55-
}
56-
57-
if (view.id === id) {
58-
return view;
59-
}
60-
61-
let retVal: ViewDefinition;
62-
const descendantsCallback = function (child: ViewDefinition): boolean {
63-
if (child.id === id) {
64-
retVal = child;
65-
// break the iteration by returning false
66-
return false;
67-
}
68-
69-
return true;
70-
}
71-
72-
eachDescendant(view, descendantsCallback);
73-
return retVal;
74-
}
75-
76-
export function eachDescendant(view: ViewDefinition, callback: (child: ViewDefinition) => boolean) {
77-
if (!callback || !view) {
78-
return;
79-
}
80-
81-
let continueIteration: boolean;
82-
let localCallback = function (child: ViewDefinition): boolean {
83-
continueIteration = callback(child);
84-
if (continueIteration) {
85-
child._eachChildView(localCallback);
86-
}
87-
return continueIteration;
88-
}
89-
90-
view._eachChildView(localCallback);
91-
}
92-
9352
export function PseudoClassHandler(...pseudoClasses: string[]): MethodDecorator {
9453
let stateEventNames = pseudoClasses.map(s => ":" + s);
9554
let listeners = Symbol("listeners");
@@ -233,10 +192,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
233192
}
234193
}
235194

236-
getViewById<T extends ViewDefinition>(id: string): T {
237-
return <T>getViewById(this, id);
238-
}
239-
240195
// START Style property shortcuts
241196
get borderColor(): string | Color {
242197
return this.style.borderColor;

tns-core-modules/ui/core/view.d.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ declare module "ui/core/view" {
4141
export const zeroLength: Length;
4242
export function getLengthEffectiveValue(param: Length): number;
4343

44-
/**
45-
* Gets a child view by id.
46-
* @param view - The parent (container) view of the view to look for.
47-
* @param id - The id of the view to look for.
48-
* Returns an instance of a view (if found), otherwise undefined.
49-
*/
50-
export function getViewById(view: View, id: string): View;
51-
5244
/**
5345
* Converts string into boolean value.
5446
* Throws error if value is not 'true' or 'false'.
@@ -350,16 +342,6 @@ declare module "ui/core/view" {
350342
*/
351343
isUserInteractionEnabled: boolean;
352344

353-
/**
354-
* Gets or sets the id for this view.
355-
*/
356-
id: string;
357-
358-
/**
359-
* Gets or sets the CSS class name for this view.
360-
*/
361-
className: string;
362-
363345
/**
364346
* Gets is layout is valid. This is a read-only property.
365347
*/
@@ -475,11 +457,6 @@ declare module "ui/core/view" {
475457

476458
public static combineMeasuredStates(curState: number, newState): number;
477459

478-
/**
479-
* Returns the child view with the specified id.
480-
*/
481-
public getViewById<T extends View>(id: string): T;
482-
483460
/**
484461
* Tries to focus the view.
485462
* Returns a value indicating whether this view or one of its descendants actually took focus.

tns-core-modules/ui/definitions.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ declare module "ui/core/view-base" {
2929
export function getAncestor(view: ViewBase, criterion: string | Function): ViewBase;
3030
export function isEventOrGesture(name: string, view: ViewBase): boolean;
3131

32+
/**
33+
* Gets a child view by id.
34+
* @param view - The parent (container) view of the view to look for.
35+
* @param id - The id of the view to look for.
36+
* Returns an instance of a view (if found), otherwise undefined.
37+
*/
38+
export function getViewById(view: ViewBase, id: string): ViewBase;
39+
3240
export class ViewBase extends Observable {
3341
/**
3442
* String value used when hooking to loaded event.
@@ -50,6 +58,21 @@ declare module "ui/core/view-base" {
5058
*/
5159
public readonly parent: ViewBase;
5260

61+
/**
62+
* Gets or sets the id for this view.
63+
*/
64+
public id: string;
65+
66+
/**
67+
* Returns the child view with the specified id.
68+
*/
69+
public getViewById<T extends ViewBase>(id: string): T;
70+
71+
/**
72+
* Gets or sets the CSS class name for this view.
73+
*/
74+
public className: string;
75+
5376
/**
5477
* Gets owner page. This is a read-only property.
5578
*/
@@ -106,6 +129,9 @@ declare module "ui/core/view-base" {
106129
export const idProperty: Property<ViewBase, string>;
107130
export const classNameProperty: Property<ViewBase, string>;
108131
export const bindingContextProperty: InheritedProperty<ViewBase, any>;
132+
133+
export function makeValidator<T>(...values: T[]): (value: any) => value is T;
134+
export function makeParser<T>(isValid: (value: any) => boolean, def: T): (value: any) => T;
109135
}
110136

111137
declare module "ui/core/properties" {

0 commit comments

Comments
 (0)