forked from jasonkneen/UTiL
/
xp.ui.js
171 lines (135 loc) · 4.64 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
171
if (!OS_IOS) {
var NavigationWindow = function (args) {
this.args = args;
};
NavigationWindow.prototype.open = function (params) {
params = params || {};
params.displayHomeAsUp = false;
return this.openWindow(this.args.window, params);
};
NavigationWindow.prototype.close = function (params) {
return this.closeWindow(this.args.window, params);
};
NavigationWindow.prototype.openWindow = function (window, options) {
var that = this;
options = options || {};
options.swipeBack = (typeof options.swipeBack === 'boolean') ? options.swipeBack : that.args.swipeBack;
options.displayHomeAsUp = (typeof options.displayHomeAsUp === 'boolean') ? options.displayHomeAsUp : that.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', function (e) {
if (e.direction === 'right') {
that.closeWindow(window, options);
}
});
}
if (OS_ANDROID && options.displayHomeAsUp !== false && !window.navBarHidden) {
window.addEventListener('open', function () {
var activity = window.getActivity();
if (activity) {
var actionBar = activity.actionBar;
if (actionBar) {
actionBar.displayHomeAsUp = true;
actionBar.onHomeIconItemSelected = function () {
that.closeWindow(window, options);
};
}
}
});
}
return window.open(options);
};
NavigationWindow.prototype.closeWindow = function (window, options) {
options = 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);
};
}
exports.createNavigationWindow = function (args) {
var navWin = OS_IOS ? Ti.UI.iOS.createNavigationWindow(args) : new NavigationWindow(args);
if (args && args.id) {
Alloy.Globals[args.id] = navWin;
}
return navWin;
};
exports.createWindow = function (args) {
if (OS_IOS) {
return Ti.UI.createWindow(args);
} else {
return Ti.UI.createView(args);
}
};
exports.createTextArea = function (args) {
var $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', function (e) {
if (e.source.value == e.source.hintText) {
e.source.applyProperties({
value: '',
color: e.source.originalColor
});
}
});
$textArea.addEventListener('blur', function (e) {
if (!e.source.value) {
e.source.applyProperties({
value: e.source.hintText,
color: '#ccc'
});
}
});
}
return $textArea;
};
exports.createLabel = function createLabel(args) {
if (OS_IOS && args.html) {
var html = args.html;
delete args.text;
delete args.html;
var label = Ti.UI.createLabel(args);
var ref = label;
var 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);
}
};
// helper
var isAndroid = Ti.Platform.osname == "android";
/**
* Fixes the auto focus on textfield on android
*/
exports.createTextField = function (args) {
if (isAndroid) {
var 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);
}
};