-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.shared.js
More file actions
187 lines (163 loc) · 4.94 KB
/
app.shared.js
File metadata and controls
187 lines (163 loc) · 4.94 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Shared utilities + global state (kept global: plain <script>, no bundler)
// Placeholder images via dummyimage.com
const DUMMY_IMAGE_BASE = "https://dummyimage.com/600x900/{{color}}/000000.png";
const INAPP_IMAGE_COLORS = [
"007FFF",
"2DBE8D",
"7C5CFF",
"38BDF8",
"22C55E",
"A78BFA",
"24B3A8",
"8B6EFF",
"4FD1C5",
"F472B6",
];
const DEFAULT_SETTINGS = {
operations: {
viewProduct: { systemName: "app.viewProduct" },
viewCategory: { systemName: "app.viewCategory" },
},
ttl: { inapps: "1.00:00:00" },
slidingExpiration: {
config: "00:30:00",
pushTokenKeepalive: "14.00:00:00",
},
inapp: {
maxInappsPerSession: 100,
maxInappsPerDay: 100,
minIntervalBetweenShows: "00:00:01",
},
featureToggles: {
MobileSdkShouldSendInAppShowError: true,
},
};
const el = (id) => document.getElementById(id);
// Editor state
let outputDirty = false;
let lastGeneratedText = "";
// Guards CodeMirror "change" handler during programmatic setValue()
let suppressOutputInput = false;
// Reset button "double click" arm state
let resetArmedUntil = 0;
let resetArmTimer = null;
// CodeMirror + JSON helpers
let cmEditor = null;
let jsonNavTimer = null;
let errorMarker = null;
let editorHistoryPrimed = false;
function clamp(n, min, max) {
return Math.max(min, Math.min(max, n));
}
function pad2(x) {
return String(x).padStart(2, "0");
}
// Parse "d.HH:MM:SS" format (Mindbox duration)
function parseMindboxDurationToSeconds(raw) {
const s = String(raw || "").trim();
const m = /^(\d+)\.(\d{2}):(\d{2}):(\d{2})$/.exec(s);
if (!m) return null;
const days = Number(m[1]);
const hh = Number(m[2]);
const mm = Number(m[3]);
const ss = Number(m[4]);
if (![days, hh, mm, ss].every(Number.isFinite)) return null;
return days * 86400 + hh * 3600 + mm * 60 + ss;
}
// Parse "HH:MM:SS" format (no days prefix)
function parseHHMMSSNoDaysToSeconds(raw) {
const s = String(raw || "").trim();
const m = /^(\d+):(\d{2}):(\d{2})$/.exec(s);
if (!m) return null;
const hh = Number(m[1]);
const mm = Number(m[2]);
const ss = Number(m[3]);
if (![hh, mm, ss].every(Number.isFinite)) return null;
return hh * 3600 + mm * 60 + ss;
}
// Accept both "HH:MM:SS" and "d.HH:MM:SS"
function parseTimespanToSeconds(raw) {
const s = String(raw || "").trim();
return parseMindboxDurationToSeconds(s) ?? parseHHMMSSNoDaysToSeconds(s);
}
function secondsToMindboxDuration(sec) {
const s = Math.max(0, Math.floor(sec));
const days = Math.floor(s / 86400);
const rem = s % 86400;
const hh = Math.floor(rem / 3600);
const mm = Math.floor((rem % 3600) / 60);
const ss = rem % 60;
return `${days}.${pad2(hh)}:${pad2(mm)}:${pad2(ss)}`;
}
function secondsToHHMMSSNoDays(sec) {
const s = Math.max(0, Math.floor(sec));
const hh = Math.floor(s / 3600);
const mm = Math.floor((s % 3600) / 60);
const ss = s % 60;
return `${pad2(hh)}:${pad2(mm)}:${pad2(ss)}`;
}
// Output "HH:MM:SS" for < 24h, otherwise "d.HH:MM:SS"
function secondsToTimespan(sec) {
const s = Math.max(0, Math.floor(sec));
const days = Math.floor(s / 86400);
const rem = s % 86400;
const hh = Math.floor(rem / 3600);
const mm = Math.floor((rem % 3600) / 60);
const ss = rem % 60;
if (days > 0) return `${days}.${pad2(hh)}:${pad2(mm)}:${pad2(ss)}`;
return `${pad2(hh)}:${pad2(mm)}:${pad2(ss)}`;
}
function pickImageColor(id) {
const n = Number(String(id || "").trim());
if (!Number.isFinite(n) || n <= 0) return INAPP_IMAGE_COLORS[0];
return INAPP_IMAGE_COLORS[(n - 1) % INAPP_IMAGE_COLORS.length];
}
function pickImage(id, { targetingLabel, formLabel } = {}) {
const parts = [
`ID ${id}`,
formLabel || "modal",
targetingLabel || "true",
];
const text = parts.join(" · ");
const color = pickImageColor(id);
const base = DUMMY_IMAGE_BASE.replace("{{color}}", color);
return `${base}&text=${encodeURIComponent(text)}`;
}
function pickRedirect(id) {
return `https://ru.wikipedia.org/wiki/${id}`;
}
function randomPromoCode(id) {
const n = Math.floor(100 + Math.random() * 900);
return `PROMO-${id}-${n}`;
}
function defaultPayloadJson() {
return JSON.stringify({
feature_type: "catalog",
expand_data: { name: "", section: "female__trends" },
});
}
function flashButton(btn, { error = false } = {}) {
if (!btn) return;
const cls = error ? "flashError" : "flash";
btn.classList.add(cls);
window.setTimeout(() => btn.classList.remove(cls), 450);
}
function openUrlMaybe(raw) {
const url = String(raw || "").trim();
if (!url) return false;
if (!/^https?:\/\//i.test(url)) return false;
window.open(url, "_blank", "noopener,noreferrer");
return true;
}
function randInt(min, max) {
return Math.floor(min + Math.random() * (max - min + 1));
}
function randFloat(min, max, decimals = 3) {
const n = min + Math.random() * (max - min);
const p = Math.pow(10, decimals);
return Math.round(n * p) / p;
}
function randHexColor() {
const n = randInt(0, 0xffffff);
return `#${n.toString(16).padStart(6, "0").toUpperCase()}`;
}