-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathxp.ui.js
170 lines (137 loc) · 4.15 KB
/
xp.ui.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class NavigationWindow {
constructor(args) {
this.args = args;
}
open(options = {}) {
options.displayHomeAsUp = false;
return this.openWindow(this.args.window, options);
}
close(options = {}) {
return this.closeWindow(this.args.window, options);
}
openWindow(window, options = {}) {
options.swipeBack = (typeof options.swipeBack === 'boolean') ? options.swipeBack : this.args.swipeBack;
options.displayHomeAsUp = (typeof options.displayHomeAsUp === 'boolean') ? options.displayHomeAsUp : this.args.displayHomeAsUp;
if (OS_ANDROID && options.animated !== false) {
options.activityEnterAnimation = Ti.Android.R.anim.slide_in_left;
options.activityExitAnimation = Ti.Android.R.anim.slide_out_right;
}
if (options.swipeBack !== false) {
window.addEventListener('swipe', () => {
if (e.direction === 'right') {
this.closeWindow(window, options);
}
});
}
if (OS_ANDROID && options.displayHomeAsUp !== false && !window.navBarHidden) {
window.addEventListener('open', () => {
const activity = window.getActivity();
if (activity) {
const actionBar = activity.actionBar;
if (actionBar) {
actionBar.displayHomeAsUp = true;
actionBar.onHomeIconItemSelected = () => {
this.closeWindow(window, options);
};
}
}
});
}
return window.open(options);
}
closeWindow(window, options = {}) {
if (OS_ANDROID && options.animated !== false) {
options.activityEnterAnimation = Ti.Android.R.anim.slide_in_left;
options.activityExitAnimation = Ti.Android.R.anim.slide_out_right;
}
return window.close(options);
}
};
const createNavigationWindow = (args) => {
const navWin = OS_IOS ? Ti.UI.iOS.createNavigationWindow(args) : new NavigationWindow(args);
if (args && args.id) {
Alloy.Globals[args.id] = navWin;
}
return navWin;
}
const createWindow = (args) => {
if (OS_IOS) {
return Ti.UI.createWindow(args);
} else {
return Ti.UI.createView(args);
}
};
const createTextArea = (args) => {
const $textArea = Ti.UI.createTextArea(args);
if (args.hintText) {
$textArea.originalColor = $textArea.color || '#000';
if (!$textArea.value) {
$textArea.applyProperties({
value: $textArea.hintText,
color: '#ccc'
});
}
$textArea.addEventListener('focus', (e) => {
if (e.source.value == e.source.hintText) {
e.source.applyProperties({
value: '',
color: e.source.originalColor
});
}
});
$textArea.addEventListener('blur', (e) => {
if (!e.source.value) {
e.source.applyProperties({
value: e.source.hintText,
color: '#ccc'
});
}
});
}
return $textArea;
};
const createLabel = (args) => {
if (OS_IOS && args.html) {
const html = args.html;
delete args.text;
delete args.html;
const label = Ti.UI.createLabel(args);
const ref = label;
const html2as = require('nl.fokkezb.html2as');
html2as(html, function (err, attr) {
if (err) {
console.error(err);
} else {
ref.attributedString = attr;
}
ref = null;
});
return label;
} else {
return Ti.UI.createLabel(args);
}
};
/**
* Fixes the auto focus on textfield on android
*/
const createTextField = (args) => {
const isAndroid = Ti.Platform.osname === 'android';
if (isAndroid) {
const view = Ti.UI.createTextField(args);
// fix auto focus
view.addEventListener('focus', function focusFix(e) {
e.source.blur();
e.source.removeEventListener('focus', focusFix);
});
return view;
} else {
return Ti.UI.createTextField(args);
}
};
export {
createNavigationWindow,
createWindow,
createTextArea,
createLabel,
createTextField
};