-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage-store.phtml
More file actions
105 lines (98 loc) · 3.81 KB
/
Copy pathmessage-store.phtml
File metadata and controls
105 lines (98 loc) · 3.81 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
<?php
declare(strict_types=1);
use Loki\Base\ViewModel\ModuleConfig;
use Magento\Framework\View\Element\Template;
/** @version 1.1.21 */
/** @var Template $block */
/** @var ModuleConfig $moduleConfig */
$moduleConfig = $block->getModuleConfig();
if ($moduleConfig->isEnabled('MageOS_AlpineMessages')) {
return;
}
$fetchLocalStorage = !(bool)$block->getSkipLocalStorage();
$fetchCookies = !(bool)$block->getSkipCookies();
?>
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('Message', {
messages: [],
fetchLocalStorage: <?= /* @noEscape */ $fetchLocalStorage ? 'true' : 'false' ?>,
fetchCookies: <?= /* @noEscape */ $fetchLocalStorage ? 'true' : 'false' ?>,
init() {
if (this.fetchLocalStorage) {
// Listen for LocalStorage initialization and load messages once
document.addEventListener('loki:init:localstorage-store', async () => {
const messageSection = await Alpine.store('LocalStorage').get('messages');
if (messageSection && messageSection.messages) {
this.messages = [...this.messages, ...messageSection.messages];
this.reset();
}
}, { once: true });
}
},
getMessages() {
const cookieMessages = this.getCookieMessages();
if (cookieMessages.length > 0) {
this.messages = [...this.messages, ...cookieMessages];
MageCookies.remove('mage-messages');
}
return this.messages;
},
getCookieMessages() {
if (!this.fetchCookies) {
return [];
}
const cookieMessages = MageCookies.get('mage-messages') || [];
if (!cookieMessages instanceof Array) {
MageCookies.remove('mage-messages');
return [];
}
return cookieMessages;
},
addMessage(type, text) {
this.messages.push({type, text});
},
removeMessage(type, text) {
this.messages = this.messages.filter(message => {
return !(message.type === type && message.text === text);
});
},
async saveMessage(type, text) {
const messages = await this.getMessagesFromStore();
messages.push({type, text});
this.setMessageInStore(messages);
},
addErrorMessage(text) {
this.addMessage('error', text);
},
addWarningMessage(text) {
this.addMessage('warning', text);
},
addSuccessMessage(text) {
this.addMessage('success', text);
},
addNoticeMessage(text) {
this.addMessage('notice', text);
},
reset() {
this.getStore().set('messages', {
data_id: this.getStore().getNewSectionLifetime(),
messages: []
});
},
async getMessagesFromStore() {
const messagesSection = await this.getStore().get('messages');
return messagesSection && messagesSection.messages ? messagesSection.messages : [];
},
setMessageInStore(messages) {
this.getStore().set('messages', {
data_id: this.getStore().getNewSectionLifetime(),
messages
});
},
getStore() {
return Alpine.store('LocalStorage');
}
});
});
</script>