Skip to content

Commit 36036c5

Browse files
author
Vladimir Enchev
committed
support of [on]event/gesture added + tests
1 parent 24e959c commit 36036c5

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

apps/tests/xml-declaration/xml-declaration-tests.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ export function test_parse_ShouldFindEventHandlersInExports() {
245245
TKUnit.assert(loaded, "Parse should find event handlers in exports.");
246246
};
247247

248+
export function test_parse_ShouldFindEventHandlersWithOnInExports() {
249+
var loaded;
250+
var page = builder.parse("<Page onloaded='myLoaded'></Page>", {
251+
myLoaded: args => {
252+
loaded = true;
253+
}
254+
});
255+
page._emit("loaded");
256+
257+
TKUnit.assert(loaded, "Parse should find event handlers in exports.");
258+
};
259+
248260
export function test_parse_ShouldSetGridAttachedProperties() {
249261
var p = <Page>builder.parse("<Page><GridLayout><Label row='1' col='2' rowSpan='3' colSpan='4' /></GridLayout></Page>");
250262
var grid = <gridLayoutModule.GridLayout>p.content;
@@ -368,6 +380,18 @@ export function test_parse_ShouldParseBindingsToEvents() {
368380
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
369381
};
370382

383+
export function test_parse_ShouldParseBindingsToEventsWithOn() {
384+
var p = <Page>builder.parse("<Page><Button ontap='{{ myTap }}' /></Page>");
385+
p.bindingContext = {
386+
myTap: function (args) {
387+
//
388+
}
389+
};
390+
var btn = <buttonModule.Button>p.content;
391+
392+
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
393+
};
394+
371395
export function test_parse_ShouldParseBindingsToGestures() {
372396
var p = <Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
373397
var context = {
@@ -385,6 +409,23 @@ export function test_parse_ShouldParseBindingsToGestures() {
385409
TKUnit.assert(observer.context === context, "Context should be equal to binding context. Actual result: " + observer.context);
386410
};
387411

412+
export function test_parse_ShouldParseBindingsToGesturesWithOn() {
413+
var p = <Page>builder.parse("<Page><Label ontap='{{ myTap }}' /></Page>");
414+
var context = {
415+
myTap: function (args) {
416+
//
417+
}
418+
};
419+
420+
p.bindingContext = context;
421+
var lbl = <Label>p.content;
422+
423+
var observer = (<view.View>lbl).getGestureObservers(gesturesModule.GestureTypes.tap)[0];
424+
425+
TKUnit.assert(observer !== undefined, "Expected result: true.");
426+
TKUnit.assert(observer.context === context, "Context should be equal to binding context. Actual result: " + observer.context);
427+
};
428+
388429
export function test_parse_ShouldParseSubProperties() {
389430
var p = <Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
390431
var obj = new observable.Observable();

ui/core/view-common.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ registerSpecialProperty("class", (instance: definition.View, propertyValue: stri
2020
instance.className = propertyValue;
2121
});
2222

23+
function getEventOrGestureName(name: string) : string {
24+
return name.indexOf("on") === 0 ? name.substr(2, name.length - 2) : name;
25+
}
26+
2327
export function isEventOrGesture(name: string, view: View): boolean {
2428
if (types.isString(name)) {
25-
var evt = `${name}Event`;
29+
var eventOrGestureName = getEventOrGestureName(name);
30+
var evt = `${eventOrGestureName}Event`;
2631

2732
return view.constructor && evt in view.constructor ||
28-
gestures.fromString(name.toLowerCase()) !== undefined;
33+
gestures.fromString(eventOrGestureName.toLowerCase()) !== undefined;
2934
}
3035

3136
return false;
@@ -230,6 +235,9 @@ export class View extends proxy.ProxyObject implements definition.View {
230235

231236
public addEventListener(arg: string | gestures.GestureTypes, callback: (data: observable.EventData) => void, thisArg?: any) {
232237
if (types.isString(arg)) {
238+
239+
arg = getEventOrGestureName(<string>arg);
240+
233241
var gesture = gestures.fromString(<string>arg);
234242
if (gesture && !this._isEvent(<string>arg)) {
235243
this.observe(gesture, callback, thisArg);

0 commit comments

Comments
 (0)