forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactionStack.js
151 lines (140 loc) · 2.77 KB
/
actionStack.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
import confirm from "dialogs/confirm";
import appSettings from "lib/settings";
const stack = [];
let mark = null;
let onCloseAppCallback;
let freeze = false;
export default {
/**
* Length of stack
* @returns {number}
*/
get length() {
return stack.length;
},
/**
* Function to be called when app is closed
* @returns {Function}
*/
get onCloseApp() {
return onCloseAppCallback;
},
/**
* Function to be called when app is closed
* @param {Function} cb
*/
set onCloseApp(cb) {
onCloseAppCallback = cb;
},
/**
* Copy of actionStack for window
* @deprecated
*/
windowCopy() {
const copyStack = { ...this };
delete copyStack.windowCopy;
copyStack.pop = (repeat) => {
window.log(
"error",
"Deprecated: `window.actionStack` is deprecated, import `actionStack` instead",
);
this.pop(repeat);
};
return copyStack;
},
/**
* Push action to stack
* @param {object} fun
* @param {string} fun.id
* @param {Function} fun.action
*/
push(fun) {
stack.push(fun);
},
/**
* Pop action from stack
* @param {number} repeat pop action multiple times
* @returns
*/
async pop(repeat) {
if (freeze) return;
let confirmation = true;
if (typeof repeat === "number" && repeat > 1) {
for (let i = 0; i < repeat; ++i) {
this.pop();
}
return;
}
const fun = stack.pop();
if (fun) {
fun.action();
return;
}
if (appSettings.value.confirmOnExit) {
let closeMessage =
acode.exitAppMessage || strings["close app"].capitalize(0);
confirmation = await confirm(strings.warning.toUpperCase(), closeMessage);
}
if (confirmation) {
const { exitApp } = navigator.app;
if (typeof onCloseAppCallback === "function") {
const res = onCloseAppCallback();
if (res instanceof Promise) {
res.finally(exitApp);
return;
}
}
if (IS_FREE_VERSION && window.iad?.isLoaded()) {
window.iad.show();
}
exitApp();
}
},
get(id) {
return stack.find((act) => act.id === id);
},
/**
* Remove action with given id from stack
* @param {String} id
* @returns {Boolean}
*/
remove(id) {
for (let i = 0; i < stack.length; ++i) {
let action = stack[i];
if (action.id === id) {
stack.splice(i, 1);
return true;
}
}
return false;
},
/**
* Check if action with given id exists in stack
* @param {String} id
* @returns {Boolean}
*/
has(id) {
for (let act of stack) if (act.id === id) return true;
return false;
},
/**
* Sets a mark to recently pushed action
*/
setMark() {
mark = stack.length;
},
/**
* Remove all actions that are pushed after marked positions (using `setMark()`)
*/
clearFromMark() {
if (mark === null) return;
stack.splice(mark);
mark = null;
},
freeze() {
freeze = true;
},
unfreeze() {
freeze = false;
},
};