|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <a-affix v-if="showBanner" class="announcement-banner-container"> |
| 20 | + <a-alert |
| 21 | + :type="bannerConfig.type || 'default'" |
| 22 | + :show-icon="bannerConfig.showIcon !== false" |
| 23 | + :closable="bannerConfig.closable !== false" |
| 24 | + :banner="true" |
| 25 | + @close="handleClose" |
| 26 | + :style="[ { border: borderColor }]" |
| 27 | + > |
| 28 | + <template #message> |
| 29 | + <div class="banner-content" v-html="sanitizedMessage" :style="[$store.getters.darkMode ? { color: 'rgba(255, 255, 255, 0.65)' } : { color: '#888' }]" /> |
| 30 | + </template> |
| 31 | + </a-alert> |
| 32 | + </a-affix> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script> |
| 36 | +import DOMPurify from 'dompurify' |
| 37 | +
|
| 38 | +export default { |
| 39 | + name: 'AnnouncementBanner', |
| 40 | + data () { |
| 41 | + return { |
| 42 | + showBanner: false, |
| 43 | + bannerConfig: {}, |
| 44 | + dismissed: false |
| 45 | + } |
| 46 | + }, |
| 47 | + computed: { |
| 48 | + sanitizedMessage () { |
| 49 | + if (!this.bannerConfig.message) return '' |
| 50 | + const cleanHTML = DOMPurify.sanitize(this.bannerConfig.message, { |
| 51 | + ALLOWED_TAGS: [ |
| 52 | + 'p', 'div', 'span', 'br', 'strong', 'b', 'em', 'i', 'u', |
| 53 | + 'a', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', |
| 54 | + 'small', 'mark', 'del', 'ins', 'sub', 'sup' |
| 55 | + ], |
| 56 | + ALLOWED_ATTR: ['href', 'target', 'rel', 'class', 'id', 'style'], |
| 57 | + ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp|xxx):|[^a-z]|[a-z+.-]+(?:[^a-z+.\-:]|$))/i, |
| 58 | + FORBID_TAGS: ['script', 'object', 'embed', 'form', 'input', 'textarea', 'select', 'button'], |
| 59 | + FORBID_ATTR: ['onclick', 'onload', 'onerror', 'onmouseover', 'onfocus', 'onblur'] |
| 60 | + }) |
| 61 | + return cleanHTML |
| 62 | + }, |
| 63 | + borderColor () { |
| 64 | + const colorMap = { |
| 65 | + error: '#ffa39e', |
| 66 | + warning: '#ffe58f', |
| 67 | + success: '#b7eb8f', |
| 68 | + info: '#b3cde3' |
| 69 | + } |
| 70 | + const color = colorMap[this.bannerConfig.type] |
| 71 | + return color ? `1px solid ${color}` : '0px' |
| 72 | + } |
| 73 | + }, |
| 74 | + mounted () { |
| 75 | + this.loadBannerConfig() |
| 76 | + }, |
| 77 | + methods: { |
| 78 | + loadBannerConfig () { |
| 79 | + const config = this.$config?.announcementBanner || {} |
| 80 | + if (config && config.enabled && config.message) { |
| 81 | + this.bannerConfig = config |
| 82 | + if (config.persistDismissal) { |
| 83 | + const dismissedKey = `cs-banner-dismissed-${this.getBannerHash()}` |
| 84 | + this.dismissed = this.$localStorage.get(dismissedKey) === 'true' |
| 85 | + } |
| 86 | + if (!this.dismissed && this.isWithinDisplayPeriod()) { |
| 87 | + this.showBanner = true |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + isWithinDisplayPeriod () { |
| 92 | + const config = this.bannerConfig |
| 93 | + const now = new Date() |
| 94 | +
|
| 95 | + if (config.startDate) { |
| 96 | + const startDate = new Date(config.startDate) |
| 97 | + if (now < startDate) return false |
| 98 | + } |
| 99 | +
|
| 100 | + if (config.endDate) { |
| 101 | + const endDate = new Date(config.endDate) |
| 102 | + if (now > endDate) return false |
| 103 | + } |
| 104 | + return true |
| 105 | + }, |
| 106 | + handleClose () { |
| 107 | + this.showBanner = false |
| 108 | + if (this.bannerConfig.persistDismissal) { |
| 109 | + const dismissedKey = `cs-banner-dismissed-${this.getBannerHash()}` |
| 110 | + this.$localStorage.set(dismissedKey, 'true') |
| 111 | + } |
| 112 | + if (this.bannerConfig.onClose) { |
| 113 | + this.bannerConfig.onClose() |
| 114 | + } |
| 115 | + }, |
| 116 | + getBannerHash () { |
| 117 | + // Create a simple hash of the message content for dismissal tracking |
| 118 | + let hash = 0 |
| 119 | + const str = this.bannerConfig.message || '' |
| 120 | + for (let i = 0; i < str.length; i++) { |
| 121 | + const char = str.charCodeAt(i) |
| 122 | + hash = ((hash << 5) - hash) + char |
| 123 | + hash = hash & hash // Convert to 32bit integer |
| 124 | + } |
| 125 | + return Math.abs(hash).toString() |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +</script> |
| 130 | +
|
| 131 | +<style scoped> |
| 132 | +.announcement-banner-container { |
| 133 | + z-index: 1000; |
| 134 | + top: 0; |
| 135 | + margin: 0; |
| 136 | + width: 100%; |
| 137 | + justify-content: center; |
| 138 | + align-items: center; |
| 139 | +} |
| 140 | +
|
| 141 | +.banner-content { |
| 142 | + line-height: 1.7; |
| 143 | + text-align: center |
| 144 | +} |
| 145 | +</style> |
0 commit comments