mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
support of [on]event/gesture added + tests
This commit is contained in:
@ -245,6 +245,18 @@ export function test_parse_ShouldFindEventHandlersInExports() {
|
||||
TKUnit.assert(loaded, "Parse should find event handlers in exports.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldFindEventHandlersWithOnInExports() {
|
||||
var loaded;
|
||||
var page = builder.parse("<Page onloaded='myLoaded'></Page>", {
|
||||
myLoaded: args => {
|
||||
loaded = true;
|
||||
}
|
||||
});
|
||||
page._emit("loaded");
|
||||
|
||||
TKUnit.assert(loaded, "Parse should find event handlers in exports.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldSetGridAttachedProperties() {
|
||||
var p = <Page>builder.parse("<Page><GridLayout><Label row='1' col='2' rowSpan='3' colSpan='4' /></GridLayout></Page>");
|
||||
var grid = <gridLayoutModule.GridLayout>p.content;
|
||||
@ -368,6 +380,18 @@ export function test_parse_ShouldParseBindingsToEvents() {
|
||||
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToEventsWithOn() {
|
||||
var p = <Page>builder.parse("<Page><Button ontap='{{ myTap }}' /></Page>");
|
||||
p.bindingContext = {
|
||||
myTap: function (args) {
|
||||
//
|
||||
}
|
||||
};
|
||||
var btn = <buttonModule.Button>p.content;
|
||||
|
||||
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToGestures() {
|
||||
var p = <Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
|
||||
var context = {
|
||||
@ -385,6 +409,23 @@ export function test_parse_ShouldParseBindingsToGestures() {
|
||||
TKUnit.assert(observer.context === context, "Context should be equal to binding context. Actual result: " + observer.context);
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToGesturesWithOn() {
|
||||
var p = <Page>builder.parse("<Page><Label ontap='{{ myTap }}' /></Page>");
|
||||
var context = {
|
||||
myTap: function (args) {
|
||||
//
|
||||
}
|
||||
};
|
||||
|
||||
p.bindingContext = context;
|
||||
var lbl = <Label>p.content;
|
||||
|
||||
var observer = (<view.View>lbl).getGestureObservers(gesturesModule.GestureTypes.tap)[0];
|
||||
|
||||
TKUnit.assert(observer !== undefined, "Expected result: true.");
|
||||
TKUnit.assert(observer.context === context, "Context should be equal to binding context. Actual result: " + observer.context);
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseSubProperties() {
|
||||
var p = <Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
|
||||
var obj = new observable.Observable();
|
||||
|
@ -20,12 +20,17 @@ registerSpecialProperty("class", (instance: definition.View, propertyValue: stri
|
||||
instance.className = propertyValue;
|
||||
});
|
||||
|
||||
function getEventOrGestureName(name: string) : string {
|
||||
return name.indexOf("on") === 0 ? name.substr(2, name.length - 2) : name;
|
||||
}
|
||||
|
||||
export function isEventOrGesture(name: string, view: View): boolean {
|
||||
if (types.isString(name)) {
|
||||
var evt = `${name}Event`;
|
||||
var eventOrGestureName = getEventOrGestureName(name);
|
||||
var evt = `${eventOrGestureName}Event`;
|
||||
|
||||
return view.constructor && evt in view.constructor ||
|
||||
gestures.fromString(name.toLowerCase()) !== undefined;
|
||||
gestures.fromString(eventOrGestureName.toLowerCase()) !== undefined;
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -230,6 +235,9 @@ export class View extends proxy.ProxyObject implements definition.View {
|
||||
|
||||
public addEventListener(arg: string | gestures.GestureTypes, callback: (data: observable.EventData) => void, thisArg?: any) {
|
||||
if (types.isString(arg)) {
|
||||
|
||||
arg = getEventOrGestureName(<string>arg);
|
||||
|
||||
var gesture = gestures.fromString(<string>arg);
|
||||
if (gesture && !this._isEvent(<string>arg)) {
|
||||
this.observe(gesture, callback, thisArg);
|
||||
|
Reference in New Issue
Block a user