-
Notifications
You must be signed in to change notification settings - Fork 0
/
cordova-app.js
140 lines (132 loc) · 4.49 KB
/
cordova-app.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
var cordovaApp = {
f7: null,
/*
This method hides splashscreen after 2 seconds
*/
handleSplashscreen: function() {
var f7 = cordovaApp.f7;
if (!window.navigator.splashscreen || f7.device.electron) return;
setTimeout(() => {
window.navigator.splashscreen.hide();
}, 2000);
},
/*
This method prevents back button tap to exit from app on android.
In case there is an opened modal it will close that modal instead.
In case there is a current view with navigation history, it will go back instead.
*/
handleAndroidBackButton: function () {
var f7 = cordovaApp.f7;
const $ = f7.$;
if (f7.device.electron) return;
document.addEventListener('backbutton', function (e) {
if ($('.actions-modal.modal-in').length) {
f7.actions.close('.actions-modal.modal-in');
e.preventDefault();
return false;
}
if ($('.dialog.modal-in').length) {
f7.dialog.close('.dialog.modal-in');
e.preventDefault();
return false;
}
if ($('.sheet-modal.modal-in').length) {
f7.sheet.close('.sheet-modal.modal-in');
e.preventDefault();
return false;
}
if ($('.popover.modal-in').length) {
f7.popover.close('.popover.modal-in');
e.preventDefault();
return false;
}
if ($('.popup.modal-in').length) {
if ($('.popup.modal-in>.view').length) {
const currentView = f7.views.get('.popup.modal-in>.view');
if (currentView && currentView.router && currentView.router.history.length > 1) {
currentView.router.back();
e.preventDefault();
return false;
}
}
f7.popup.close('.popup.modal-in');
e.preventDefault();
return false;
}
if ($('.login-screen.modal-in').length) {
f7.loginScreen.close('.login-screen.modal-in');
e.preventDefault();
return false;
}
const currentView = f7.views.current;
if (currentView && currentView.router && currentView.router.history.length > 1) {
currentView.router.back();
e.preventDefault();
return false;
}
if ($('.panel.panel-in').length) {
f7.panel.close('.panel.panel-in');
e.preventDefault();
return false;
}
}, false);
},
/*
This method does the following:
- provides cross-platform view "shrinking" on keyboard open/close
- hides keyboard accessory bar for all inputs except where it required
*/
handleKeyboard: function () {
var f7 = cordovaApp.f7;
if (!window.Keyboard || !window.Keyboard.shrinkView || f7.device.electron) return;
var $ = f7.$;
window.Keyboard.shrinkView(false);
window.Keyboard.disableScrollingInShrinkView(true);
window.Keyboard.hideFormAccessoryBar(true);
window.addEventListener('keyboardWillShow', () => {
f7.input.scrollIntoView(document.activeElement, 0, true, true);
});
window.addEventListener('keyboardDidShow', () => {
f7.input.scrollIntoView(document.activeElement, 0, true, true);
});
window.addEventListener('keyboardDidHide', () => {
if (document.activeElement && $(document.activeElement).parents('.messagebar').length) {
return;
}
window.Keyboard.hideFormAccessoryBar(false);
});
window.addEventListener('keyboardHeightWillChange', (event) => {
var keyboardHeight = event.keyboardHeight;
if (keyboardHeight > 0) {
// Keyboard is going to be opened
document.body.style.height = `calc(100% - ${keyboardHeight}px)`;
$('html').addClass('device-with-keyboard');
} else {
// Keyboard is going to be closed
document.body.style.height = '';
$('html').removeClass('device-with-keyboard');
}
});
$(document).on('touchstart', 'input, textarea, select', function (e) {
var nodeName = e.target.nodeName.toLowerCase();
var type = e.target.type;
var showForTypes = ['datetime-local', 'time', 'date', 'datetime'];
if (nodeName === 'select' || showForTypes.indexOf(type) >= 0) {
window.Keyboard.hideFormAccessoryBar(false);
} else {
window.Keyboard.hideFormAccessoryBar(true);
}
}, true);
},
init: function (f7) {
// Save f7 instance
cordovaApp.f7 = f7;
// Handle Android back button
cordovaApp.handleAndroidBackButton();
// Handle Splash Screen
cordovaApp.handleSplashscreen();
// Handle Keyboard
cordovaApp.handleKeyboard();
},
};
export default cordovaApp;