forked from PeterStaev/NativeScript-Drop-Down
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop-down-common.ts
117 lines (96 loc) · 3.54 KB
/
drop-down-common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*! *****************************************************************************
Copyright (c) 2015 Tangra Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
import { PropertyMetadata } from "ui/core/proxy";
import { Property, PropertyChangeData, PropertyMetadataSettings } from "ui/core/dependency-observable";
import { View } from "ui/core/view";
import * as types from "utils/types";
import * as definition from "nativescript-drop-down";
export const DROPDOWN = "DropDown";
function onSelectedIndexPropertyChanged(data: PropertyChangeData) {
let picker = <DropDown>data.object;
picker._onSelectedIndexPropertyChanged(data);
}
function onItemsPropertyChanged(data: PropertyChangeData) {
let picker = <DropDown>data.object;
picker._onItemsPropertyChanged(data);
}
function onHintPropertyChanged(data: PropertyChangeData) {
let picker = <DropDown>data.object;
picker._onHintPropertyChanged(data);
}
export abstract class DropDown extends View implements definition.DropDown {
public static openedEvent = "opened";
public static selectedIndexChangedEvent = "selectedIndexChanged";
public static itemsProperty = new Property(
"items",
DROPDOWN,
new PropertyMetadata(
undefined,
PropertyMetadataSettings.AffectsLayout,
onItemsPropertyChanged
)
);
public static selectedIndexProperty = new Property(
"selectedIndex",
DROPDOWN,
new PropertyMetadata(
undefined,
PropertyMetadataSettings.AffectsLayout,
onSelectedIndexPropertyChanged
)
);
public static hintProperty = new Property(
"hint",
DROPDOWN,
new PropertyMetadata(
"",
PropertyMetadataSettings.AffectsLayout,
onHintPropertyChanged
)
);
public accessoryViewVisible;
constructor() {
super();
}
get items(): any {
return this._getValue(DropDown.itemsProperty);
}
set items(value: any) {
this._setValue(DropDown.itemsProperty, value);
}
get selectedIndex(): number {
return this._getValue(DropDown.selectedIndexProperty);
}
set selectedIndex(value: number) {
this._setValue(DropDown.selectedIndexProperty, value);
}
get hint(): string {
return this._getValue(DropDown.hintProperty);
}
set hint(value: string) {
this._setValue(DropDown.hintProperty, value);
}
public abstract open();
public abstract _onItemsPropertyChanged(data: PropertyChangeData);
public abstract _onHintPropertyChanged(data: PropertyChangeData);
public _onSelectedIndexPropertyChanged(data: PropertyChangeData) {
let index = this.selectedIndex;
if (types.isUndefined(index)) {
return;
}
if (index < 0 || index >= this.items.length) {
this.selectedIndex = undefined;
throw new Error("selectedIndex should be between [0, items.length - 1]");
}
}
}