Skip to content

Commit f56260e

Browse files
authored
Non angular events (#51)
Implements #20
1 parent 0ced04c commit f56260e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+602
-67
lines changed

README.md

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ You need to add `xmlns:dd="nativescript-drop-down"` to your page tag, and then s
2121

2222
## API
2323

24+
### Events
25+
* **opened**
26+
Triggered when the DropDown is opened.
27+
28+
* **selectedIndexChanged**
29+
Triggered when the user changes the selection in the DropDown
30+
2431
### Static Properties
32+
* **openedEvent** - *String*
33+
String value used when hooking to opened event.
34+
35+
* **selectedIndexChangedEvent** - *String*
36+
String value used when hooking to selectedIndexChanged event.
37+
2538
* **itemsProperty** - *[Property](http://docs.nativescript.org/api-reference/classes/_ui_core_dependency_observable_.property.html)*
2639
Represents the observable property backing the items property of each DropDown instance.
2740

@@ -59,7 +72,9 @@ Opens the drop down.
5972
<!-- test-page.xml -->
6073
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" xmlns:dd="nativescript-drop-down">
6174
<GridLayout rows="auto, auto, *" columns="auto, *">
62-
<dd:DropDown items="{{ items }}" selectedIndex="{{ selectedIndex }}" row="0" colSpan="2" />
75+
<dd:DropDown items="{{ items }}" selectedIndex="{{ selectedIndex }}"
76+
opened="dropDownOpened" selectedIndexChanged="dropDownSelectedIndexChanged"
77+
row="0" colSpan="2" />
6378
<Label text="Selected Index:" row="1" col="0" fontSize="18" verticalAlignment="bottom"/>
6479
<TextField text="{{ selectedIndex }}" row="1" col="1" />
6580
</GridLayout>
@@ -71,6 +86,7 @@ Opens the drop down.
7186
import observable = require("data/observable");
7287
import observableArray = require("data/observable-array");
7388
import pages = require("ui/page");
89+
import { SelectedIndexChangedEventData } from "nativescript-drop-down";
7490

7591
var viewModel: observable.Observable;
7692

@@ -89,24 +105,45 @@ export function pageLoaded(args: observable.EventData) {
89105

90106
page.bindingContext = viewModel;
91107
}
108+
109+
export function dropDownOpened(args: EventData) {
110+
console.log("Drop Down opened");
111+
}
112+
113+
export function dropDownSelectedIndexChanged(args: SelectedIndexChangedEventData) {
114+
console.log(`Drop Down selected index changed from ${args.oldIndex} to ${args.newIndex}`);
115+
}
92116
```
93117

94118
## Angular 2 Example
95119

96120
```TypeScript
97121
// main.ts
98-
import {nativeScriptBootstrap} from "nativescript-angular/application";
99-
import {AppComponent} from "./app.component";
100-
import {registerElement} from "nativescript-angular/element-registry";
122+
import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
123+
import { NgModule } from "@angular/core";
124+
import { AppComponent } from "./app.component";
125+
import { registerElement } from "nativescript-angular/element-registry";
126+
101127
registerElement("DropDown", () => require("nativescript-drop-down/drop-down").DropDown);
102-
nativeScriptBootstrap(AppComponent);
128+
129+
@NgModule({
130+
declarations: [AppComponent],
131+
bootstrap: [AppComponent],
132+
imports: [NativeScriptModule],
133+
})
134+
class AppComponentModule {}
135+
136+
platformNativeScriptDynamic().bootstrapModule(AppComponentModule);
103137
```
104138

105139
```HTML
106140
<!-- app.component.html -->
107141
<StackLayout>
108142
<GridLayout rows="auto, auto, *" columns="auto, *">
109-
<DropDown #dd backroundColor="red" [items]="items" [selectedIndex]="selectedIndex" (selectedIndexChange)="onchange(dd.selectedIndex)" row="0" colSpan="2"></DropDown>
143+
<DropDown #dd backroundColor="red" [items]="items" [selectedIndex]="selectedIndex"
144+
(selectedIndexChanged)="onchange($event)" (opened)="onopen()"
145+
row="0" colSpan="2">
146+
</DropDown>
110147
<Label text="Selected Index:" row="1" col="0" fontSize="18" verticalAlignment="bottom"></Label>
111148
<TextField [text]="selected" row="1" col="1" ></TextField>
112149
</GridLayout>
@@ -115,8 +152,8 @@ nativeScriptBootstrap(AppComponent);
115152

116153
```TypeScript
117154
// app.component.ts
118-
import {Component, OnInit, OnChanges} from "@angular/core";
119-
import {ObservableArray} from "data/observable-array";
155+
import { Component } from "@angular/core";
156+
import { SelectedIndexChangedEventData } from "nativescript-drop-down";
120157

121158
@Component({
122159
selector: "my-app",
@@ -133,8 +170,12 @@ export class AppComponent {
133170
}
134171
}
135172

136-
public onchange(selectedi){
137-
console.log("selected index " + selectedi);
173+
public onchange(args: SelectedIndexChangedEventData) {
174+
console.log(`Drop Down selected index changed from ${args.oldIndex} to ${args.newIndex}`);
175+
}
176+
177+
public onopen() {
178+
console.log("Drop Down opened.");
138179
}
139180
}
140181
```

drop-down-common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ function onHintPropertyChanged(data: PropertyChangeData) {
3737
picker._onHintPropertyChanged(data);
3838
}
3939

40-
4140
export abstract class DropDown extends View implements definition.DropDown {
41+
public static openedEvent = "opened";
42+
public static selectedIndexChangedEvent = "selectedIndexChanged";
43+
4244
public static itemsProperty = new Property(
4345
"items",
4446
DROPDOWN,

drop-down.android.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Label } from "ui/label";
2121
import { StackLayout } from "ui/layouts/stack-layout";
2222
import { Color } from "color";
2323
import * as types from "utils/types";
24+
import { SelectedIndexChangedEventData } from "nativescript-drop-down";
2425

2526
global.moduleMerge(common, exports);
2627

@@ -50,11 +51,37 @@ export class DropDown extends common.DropDown {
5051
let that = new WeakRef(this);
5152
this.android.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener({
5253
onItemSelected(parent: any, convertView: android.view.View, index: number, id: number) {
53-
let owner = that.get();
54+
let owner = that.get(),
55+
oldIndex = owner.selectedIndex,
56+
newIndex = (index === 0 ? undefined : index - 1);
57+
5458
owner._selectedIndexInternal = index;
59+
60+
if (newIndex !== oldIndex) {
61+
owner.notify(<SelectedIndexChangedEventData>{
62+
eventName: common.DropDown.selectedIndexChangedEvent,
63+
object: owner,
64+
oldIndex: oldIndex,
65+
newIndex: newIndex
66+
});
67+
}
5568
},
5669
onNothingSelected() { /* Currently Not Needed */ }
5770
}));
71+
72+
this.android.setOnTouchListener(new android.view.View.OnTouchListener({
73+
onTouch(v: android.view.View, event: android.view.MotionEvent) {
74+
if (event.getAction() === android.view.MotionEvent.ACTION_DOWN) {
75+
let owner = that.get();
76+
77+
owner.notify({
78+
eventName: common.DropDown.openedEvent,
79+
object: owner
80+
});
81+
}
82+
return false;
83+
}
84+
}));
5885

5986
// When used in templates the selectedIndex changed event is fired before the native widget is init.
6087
// So here we must set the inital value (if any)

drop-down.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ limitations under the License.
1717
declare module "nativescript-drop-down" {
1818
import { View } from "ui/core/view";
1919
import { Property } from "ui/core/dependency-observable";
20+
import { EventData } from "data/observable";
21+
22+
export interface SelectedIndexChangedEventData extends EventData {
23+
oldIndex: number;
24+
newIndex: number;
25+
}
2026

2127
export class DropDown extends View {
28+
public static openedEvent: string;
29+
public static selectedIndexChangedEvent: string;
30+
2231
public static itemsProperty: Property;
2332
public static selectedIndexProperty: Property;
2433
public static hintProperty: Property;
@@ -31,6 +40,10 @@ declare module "nativescript-drop-down" {
3140
ios: UITextField;
3241
android: android.widget.Spinner;
3342

43+
public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
44+
public on(event: "opened", callback: (args: EventData) => void, thisArg?: any);
45+
public on(event: "selectedIndexChanged", callback: (args: SelectedIndexChangedEventData) => void, thisArg?: any);
46+
3447
public open();
3548
}
3649
}

drop-down.ios.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Font } from "ui/styling/font";
2525
import { Span } from "text/span";
2626
import { FormattedString } from "text/formatted-string";
2727
import * as enums from "ui/enums";
28+
import { SelectedIndexChangedEventData } from "nativescript-drop-down";
2829

2930
global.moduleMerge(common, exports);
3031

@@ -36,7 +37,7 @@ export class DropDown extends common.DropDown {
3637
private _doneButton: UIBarButtonItem;
3738
private _doneTapDelegate: TapHandler;
3839
private _accessoryViewVisible: boolean;
39-
40+
4041
public _textField: TextField;
4142
public _listPicker: ListPicker;
4243

@@ -49,6 +50,7 @@ export class DropDown extends common.DropDown {
4950
this._listPicker = new ListPicker();
5051

5152
(this._listPicker as any)._delegate = DropDownListPickerDelegateImpl.initWithOwner(this);
53+
(this._textField as any)._delegate = DropDownTextFieldDelegateImpl.initWithOwner(this);
5254
this._flexToolbarSpace = UIBarButtonItem.alloc().initWithBarButtonSystemItemTargetAction(UIBarButtonSystemItem.FlexibleSpace, null, null);
5355
this._doneTapDelegate = TapHandler.initWithOwner(new WeakRef(this));
5456
this._doneButton = UIBarButtonItem.alloc().initWithBarButtonSystemItemTargetAction(UIBarButtonSystemItem.Done, this._doneTapDelegate, "tap");
@@ -92,6 +94,7 @@ export class DropDown extends common.DropDown {
9294
});
9395
this.ios.inputView = this._listPicker.ios;
9496
this._showHideAccessoryView();
97+
9598
}
9699

97100
public onUnloaded() {
@@ -144,6 +147,27 @@ class TapHandler extends NSObject {
144147
}
145148
}
146149

150+
class DropDownTextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
151+
public static ObjCProtocols = [UITextFieldDelegate];
152+
153+
private _owner: WeakRef<DropDown>;
154+
155+
public static initWithOwner(owner: DropDown): DropDownTextFieldDelegateImpl {
156+
let delegate = <DropDownTextFieldDelegateImpl>DropDownTextFieldDelegateImpl.new();
157+
delegate._owner = new WeakRef(owner);
158+
return delegate;
159+
}
160+
161+
public textFieldDidBeginEditing(textField: UITextField) {
162+
let owner = this._owner.get();
163+
164+
owner.notify({
165+
eventName: common.DropDown.openedEvent,
166+
object: owner
167+
});
168+
}
169+
}
170+
147171
class DropDownListPickerDelegateImpl extends NSObject implements UIPickerViewDelegate {
148172
public static ObjCProtocols = [UIPickerViewDelegate];
149173

@@ -181,7 +205,18 @@ class DropDownListPickerDelegateImpl extends NSObject implements UIPickerViewDel
181205
public pickerViewDidSelectRowInComponent(pickerView: UIPickerView, row: number, component: number): void {
182206
let owner = this._owner.get();
183207
if (owner) {
208+
let oldIndex = owner.selectedIndex;
209+
184210
owner._listPicker._onPropertyChangedFromNative(ListPicker.selectedIndexProperty, row);
211+
212+
if (row !== oldIndex) {
213+
owner.notify(<SelectedIndexChangedEventData>{
214+
eventName: common.DropDown.selectedIndexChangedEvent,
215+
object: owner,
216+
oldIndex: oldIndex,
217+
newIndex: row
218+
});
219+
}
185220
}
186221
}
187222
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-drop-down",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A NativeScript DropDown widget.",
55
"main": "drop-down.js",
66
"nativescript": {

0 commit comments

Comments
 (0)