Add UI element to display messages of the day

This change adds an UI element that displays messages registered with
the MessageOfTheDay extension point. The messages will be displayed
below the main navigation header. This functionality was already part
of the GWT-UI, but was never migrated to the new UI.

The messages can be dismissed by a button click, that will create a
cookie that will prevent the message from being displayed again until
the redisplay time configured with the message.

The content of the message can be HTML. Depending on where the message-
content is coming from, this might be harmful. In case of the
messageoftheday plugin, the message is added as a config from the Gerrit
server site, which can probably be considered save. However, in a future
change rendering HTML should be made configurable and switched off by
default. Text should then be rendered using `gr-formatted-text`.

Change-Id: I2cbbcbcd49734b645701b9fac62a016a269d37f2
This commit is contained in:
Thomas Draebing
2020-01-27 17:55:55 +01:00
parent c77032cc74
commit 1fbf53df60
5 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
<!--
@license
Copyright (C) 2020 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-button/gr-button.html">
<dom-module id="gr-message-header">
<template>
<style include="shared-styles">
#container {
background-color: lightyellow;
display: flex;
height: fit-content;
justify-content: space-between;
padding: 1em;
}
</style>
<div id="container" hidden$="[[_hidden]]">
<div id="message"></div>
<gr-button id="dismissMessageBtn"
link
on-tap="_handleDismissMessage">Dismiss</gr-button>
</div>
<gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
</template>
<script src="../../../scripts/util.js"></script>
<script src="gr-message-header.js"></script>
</dom-module>

View File

@@ -0,0 +1,52 @@
/**
* @license
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
Polymer({
is: 'gr-message-header',
properties: {
message: {
type: Object,
reflectToAttribute: true,
},
_hidden: {
type: Boolean,
value: true,
},
},
attached() {
if (!this.message || !this.message.html) {
return;
}
this._isHidden();
this.$.message.innerHTML = this.message.html;
},
_handleDismissMessage() {
document.cookie =
`msg-${this.message.id}=1; expires=${this.message.redisplay}`;
this._hidden = true;
},
_isHidden() {
this._hidden = window.util.getCookie(`msg-${this.message.id}`) === '1';
},
});
})();

View File

@@ -0,0 +1,60 @@
<!DOCTYPE html>
<!--
@license
Copyright (C) 2020 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-message-header</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-message-header.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-message-header></gr-message-header>
</template>
</test-fixture>
<script>
suite('gr-message-header tests', () => {
let element;
setup(() => {
element = fixture('basic');
});
test('show message', () => {
element.message = {html: 'This is a test message.'};
element.attached();
assert.equal(element.$.message.innerHTML, element.message.html);
});
test('hide message on dismiss', () => {
element.message = {html: 'This is a test message.', id: 'test'};
element.attached();
MockInteractions.tap(element.$.dismissMessageBtn);
assert.isTrue(element.$.container.hidden);
assert.isTrue(document.cookie.includes('msg-test=1'));
element.attached();
assert.isTrue(element.$.container.hidden);
});
});
</script>

View File

@@ -55,6 +55,7 @@ limitations under the License.
<link rel="import" href="./core/gr-error-manager/gr-error-manager.html">
<link rel="import" href="./core/gr-keyboard-shortcuts-dialog/gr-keyboard-shortcuts-dialog.html">
<link rel="import" href="./core/gr-main-header/gr-main-header.html">
<link rel="import" href="./core/gr-message-header/gr-message-header.html">
<link rel="import" href="./core/gr-navigation/gr-navigation.html">
<link rel="import" href="./core/gr-reporting/gr-reporting.html">
<link rel="import" href="./core/gr-router/gr-router.html">
@@ -158,6 +159,12 @@ limitations under the License.
class$="[[_computeShadowClass(_isShadowDom)]]">
</gr-main-header>
</gr-fixed-panel>
<template
is="dom-repeat"
items="[[_getMessages(_serverConfig)]]"
as="message">
<gr-message-header message="{{message}}"></gr-message-header>
</template>
<main>
<template is="dom-if" if="[[_showChangeListView]]" restamp="true">
<gr-change-list-view

View File

@@ -339,6 +339,10 @@
config.gerrit.web_uis && config.gerrit.web_uis.includes('GWT');
},
_getMessages(config) {
return config.messages ? config.messages : [];
},
_handlePageError(e) {
const props = [
'_showChangeListView',