-
-
Notifications
You must be signed in to change notification settings - Fork 626
Expand file tree
/
Copy pathstatamic.js
More file actions
351 lines (294 loc) · 9.31 KB
/
statamic.js
File metadata and controls
351 lines (294 loc) · 9.31 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { createApp, h } from 'vue';
import App from './App.vue';
import { createPinia, defineStore } from 'pinia';
import axios from 'axios';
import registerGlobalComponents from './components.js';
import registerGlobalCommandPalette from './commands.js';
import registerUiComponents from './ui.js';
import registerFieldtypes from './fieldtypes.js';
import VueClickAway from 'vue3-click-away';
import 'floating-vue/dist/style.css';
import tooltipDirective from '@/directives/tooltip.js';
import { createInertiaApp } from '@inertiajs/vue3';
import { router } from '@inertiajs/vue3';
import PortalVue from 'portal-vue';
import autosize from 'autosize';
import wait from '@/util/wait.js';
import markdown from '@/util/markdown.js';
import VueComponentDebug from 'vue-component-debug';
import { registerIconSetFromStrings } from '@ui';
import Layout from '@/pages/layout/Layout.vue';
import { setTranslations, setLocale } from '@/translations/translator.js';
import { setDefaultLocale as setFormattingLocale } from '@/components/FormattingLocale.js';
import {
keys,
components,
events,
progress,
fieldActions,
conditions,
callbacks,
dirty,
slug,
hooks,
bard,
reveal,
echo,
permissions,
dateFormatter,
numberFormatter,
commandPalette,
colorMode,
contrast,
config,
preferences,
toast,
portals,
stacks,
inertia,
} from '@api';
let bootingCallbacks = [];
let configuringCallbacks = [];
let bootedCallbacks = [];
export default {
booting(callback) {
bootingCallbacks.push(callback);
},
configuring(callback) {
configuringCallbacks.push(callback);
},
booted(callback) {
bootedCallbacks.push(callback);
},
get $config() {
return config;
},
get $preferences() {
return preferences;
},
get $callbacks() {
return callbacks;
},
get $hooks() {
return hooks;
},
get $toast() {
return toast;
},
get $conditions() {
return conditions;
},
get $slug() {
return slug;
},
get $bard() {
return bard;
},
get $echo() {
return echo;
},
get $pinia() {
return { defineStore };
},
get $keys() {
return keys;
},
get $permissions() {
return permissions;
},
get $components() {
return components;
},
get $inertia() {
return inertia;
},
get $date() {
return dateFormatter;
},
get $number() {
return numberFormatter;
},
get $progress() {
return progress;
},
get $colorMode() {
return colorMode;
},
get $contrast() {
return contrast;
},
get $fieldActions() {
return fieldActions;
},
get $dirty() {
return dirty;
},
get $events() {
return events;
},
get $reveal() {
return reveal;
},
get $commandPalette() {
return commandPalette;
},
get user() {
return this.$config.get('user');
},
config(config) {
this.initialConfig = config;
},
component(name, component) {
this.$components.register(name, component);
},
async start() {
setTranslations(this.initialConfig.translations);
setLocale(this.initialConfig.translationLocale);
config.initialize(this.initialConfig);
colorMode.initialize(this.initialConfig.user?.color_mode);
contrast.initialize(this.initialConfig.user?.preferences?.strict_accessibility);
preferences.initialize(this.initialConfig.user?.preferences, this.initialConfig.defaultPreferences);
const formattingLocale = this.initialConfig.user?.preferences?.formatting_locale;
if (formattingLocale === 'language') {
setFormattingLocale(this.initialConfig.translationLocale);
} else if (formattingLocale) {
setFormattingLocale(formattingLocale);
}
bootingCallbacks.forEach((callback) => callback(this));
bootingCallbacks = [];
const el = document.getElementById('statamic');
const titleEl = document.getElementById('blade-title');
const bladeTitle = titleEl?.dataset.title;
const bladeContent = el?.innerHTML || '';
const _this = this;
const corePages = import.meta.glob('../pages/**/*.vue');
await createInertiaApp({
id: 'statamic',
resolve: async name => {
if (name === 'NonInertiaPage') {
return {
default: {
layout: Layout,
template: `<div>${bladeContent}</div>`,
}
}
}
// Resolve core pages
const pageImport = corePages[`../pages/${name}.vue`];
let page = pageImport ? await pageImport() : null;
// Resolve addon pages
if (!page) {
const addonPage = inertia.get(name);
if (addonPage) page = { default: addonPage };
}
if (!page) {
let message = `Couldn't find Inertia component for the [${name}] page. `;
message += name.endsWith('.vue')
? 'You do not need to include the .vue extension when referencing a page.'
: 'Did you you register a [Pages/${name}] component?';
throw new Error(message);
}
page.default.layout = page.default.layout || Layout;
return page;
},
async setup({ el, App: InertiaApp, props, plugin }) {
const app = await _this.configureApp(InertiaApp, props);
app.use(plugin).mount(el);
},
title: (title) => title || bladeTitle
})
// Handle non-Inertia responses with full page reload
router.on('invalid', (event) => {
if (event.detail.response.status === 200) {
event.preventDefault();
window.location.href = event.detail.response.request.responseURL;
}
});
bootedCallbacks.forEach((callback) => callback(this));
bootedCallbacks = [];
},
async configureApp(InertiaApp, props, el) {
this.$app = createApp({
...App,
render: () => h(InertiaApp, props),
});
this.$app.config.silent = false;
this.$app.config.devtools = true;
this.$app.use(createPinia());
this.$app.use(PortalVue, { portalName: 'v-portal' });
this.$app.use(VueClickAway);
this.$app.directive('tooltip', tooltipDirective);
this.$app.use(VueComponentDebug, { enabled: import.meta.env.VITE_VUE_COMPONENT_DEBUG === 'true' });
toast.initialize(this.$app);
Object.assign(this.$app.config.globalProperties, {
$config: config,
$axios: axios,
$events: events,
$preferences: preferences,
$progress: progress,
$keys: keys,
$fieldActions: fieldActions,
$conditions: conditions,
$callbacks: callbacks,
$dirty: dirty,
$slug: slug,
$portals: portals,
$stacks: stacks,
$hooks: hooks,
$toast: toast,
$bard: bard,
$reveal: reveal,
$echo: echo,
$permissions: permissions,
$date: dateFormatter,
$number: numberFormatter,
$commandPalette: commandPalette,
$colorMode: colorMode,
$contrast: contrast,
});
Object.assign(this.$app.config.globalProperties, {
__(key, replacements) {
return __(key, replacements);
},
__n(key, number, replacements) {
return __n(key, number, replacements);
},
$markdown(value, options = {}) {
return markdown(value, options);
},
cp_url(url) {
return cp_url(url);
},
docs_url(url) {
return docs_url(url);
},
can(permission) {
const permissions = JSON.parse(atob(Statamic.$config.get('permissions')));
return permissions.includes('super') || permissions.includes(permission);
},
$wait(ms) {
return wait(ms);
},
});
this.$app.directive('elastic', {
mounted: (el) => autosize(el),
});
await registerUiComponents(this.$app);
registerGlobalComponents(this.$app);
registerGlobalCommandPalette();
registerFieldtypes(this.$app);
registerIconSets(this.initialConfig);
components.boot(this.$app);
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.headers.common['X-CSRF-TOKEN'] = Statamic.$config.get('csrfToken');
configuringCallbacks.forEach((callback) => callback(this));
configuringCallbacks = [];
return this.$app;
},
};
function registerIconSets(config) {
const sets = config.customSvgIcons;
for (const name in sets) {
registerIconSetFromStrings(name, sets[name]);
}
}