-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathNotificationListItem.js
426 lines (353 loc) · 10.1 KB
/
NotificationListItem.js
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import { fetchI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import { isIE } from "@ui5/webcomponents-base/dist/Device.js";
import Priority from "@ui5/webcomponents/dist/types/Priority.js";
import Button from "@ui5/webcomponents/dist/Button.js";
import BusyIndicator from "@ui5/webcomponents/dist/BusyIndicator.js";
import Link from "@ui5/webcomponents/dist/Link.js";
import Icon from "@ui5/webcomponents/dist/Icon.js";
import Popover from "@ui5/webcomponents/dist/Popover.js";
import NotificationListItemBase from "./NotificationListItemBase.js";
// Texts
import {
NOTIFICATION_LIST_ITEM_TXT,
NOTIFICATION_LIST_ITEM_READ,
NOTIFICATION_LIST_ITEM_UNREAD,
NOTIFICATION_LIST_ITEM_SHOW_MORE,
NOTIFICATION_LIST_ITEM_SHOW_LESS,
NOTIFICATION_LIST_ITEM_HIGH_PRIORITY_TXT,
NOTIFICATION_LIST_ITEM_MEDIUM_PRIORITY_TXT,
NOTIFICATION_LIST_ITEM_LOW_PRIORITY_TXT,
NOTIFICATION_LIST_ITEM_OVERLOW_BTN_TITLE,
NOTIFICATION_LIST_ITEM_CLOSE_BTN_TITLE,
} from "./generated/i18n/i18n-defaults.js";
// Templates
import NotificationListItemTemplate from "./generated/templates/NotificationListItemTemplate.lit.js";
// Styles
import NotificationListItemCss from "./generated/themes/NotificationListItem.css.js";
const MAX_WRAP_HEIGHT = 32; // px.
/**
* @public
*/
const metadata = {
tag: "ui5-li-notification",
languageAware: true,
managedSlots: true,
properties: /** @lends sap.ui.webcomponents.fiori.NotificationListItem.prototype */ {
/**
* Defines if the <code>heading</code> and <code>description</code> should wrap,
* they truncate by default.
*
* <br><br>
* <b>Note:</b> by default the <code>heading</code> and <code>decription</code>,
* and a <code>ShowMore/Less</code> button would be displayed.
* @type {boolean}
* @defaultvalue false
* @public
*/
wrap: {
type: Boolean,
},
/**
* Defines the state of the <code>heading</code> and <code>description</code>,
* if less or more information is displayed.
* @private
*/
_showMorePressed: {
type: Boolean,
},
/**
* Defines the visibility of the <code>showMore</code> button.
* @private
*/
_showMore: {
type: Boolean,
},
},
slots: /** @lends sap.ui.webcomponents.fiori.NotificationListItem.prototype */ {
/**
* Defines the avatar, displayed in the <code>ui5-li-notification</code>.
*
* <br><br>
* <b>Note:</b> Consider using the <code>ui5-avatar</code> to display icons, initials or images.
* <br>
* <b>Note:</b>In order to be complaint with the UX guidlines and for best experience,
* we recommend using avatars with 2rem X 2rem in size (32px X 32px). In case you are using the <code>ui5-avatar</code>
* you can set its <code>size</code><code> property to <code>XS</code> to get the required size - <code><ui5-avatar size="XS"></code>.
*
* @type {sap.ui.webcomponents.main.IAvatar}
* @slot
* @public
*/
avatar: {
type: HTMLElement,
},
/**
* Defines the elements, dipalyed in the footer of the of the <code>ui5-li-notification</code>.
* @type {HTMLElement[]}
* @slot footnotes
* @public
*/
footnotes: {
type: HTMLElement,
propertyName: "footnotes",
individualSlots: true,
},
/**
* Defines the content of the <code>ui5-li-notification</code>,
* usually a description of the notification.
*
* <br><br>
* <b>Note:</b> Although this slot accepts HTML Elements, it is strongly recommended that you only use text in order to preserve the intended design.
*
* @type {Node[]}
* @slot description
* @public
*/
"default": {
propertyName: "description",
type: Node,
},
},
events: /** @lends sap.ui.webcomponents.fiori.NotificationListItem.prototype */ {
_press: {},
},
};
/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
* The <code>ui5-li-notification</code> is a type of list item, meant to display notifications.
* <br>
*
* The component has a rich set of various properties that allows the user to set <code>avatar</code>, <code>heading</code>, descriptive <code>content</code>
* and <code>footnotes</code> to fully describe a notification.
* <br>
*
* The user can:
* <ul>
* <li>display a <code>Close</code> button</li>
* <li>can control whether the <code>heading</code> and <code>description</code> should wrap or truncate
* and display a <code>ShowMore</code> button to switch between less and more information</li>
* <li>add custom actions by using the <code>ui5-notification-action</code> component</li>
* </ul>
*
* <h3>Usage</h3>
* The component can be used in a standard <code>ui5-list</code>.
*
* <h3>ES6 Module Import</h3>
*
* <code>import @ui5/webcomponents/dist/NotificationListItem.js";</code>
* <br>
* <code>import @ui5/webcomponents/dist/NotificationAction.js";</code> (optional)
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.fiori.NotificationListItem
* @extends NotificationListItemBase
* @tagname ui5-li-notification
* @appenddocs NotificationAction
* @since 1.0.0-rc.8
* @implements sap.ui.webcomponents.fiori.INotificationListItem, sap.ui.webcomponents.main.IListItem
* @public
*/
class NotificationListItem extends NotificationListItemBase {
constructor() {
super();
// the heading overflow height
this._headingOverflowHeight = 0;
// the description overflow height
this._descOverflowHeight = 0;
// the resize handler
this.onResizeBind = this.onResize.bind(this);
}
static get metadata() {
return metadata;
}
static get styles() {
return NotificationListItemCss;
}
static get template() {
return NotificationListItemTemplate;
}
static get dependencies() {
return [
Button,
Icon,
BusyIndicator,
Link,
Popover,
];
}
static async onDefine() {
await fetchI18nBundle("@ui5/webcomponents-fiori");
}
onEnterDOM() {
ResizeHandler.register(this, this.onResizeBind);
}
onExitDOM() {
ResizeHandler.deregister(this, this.onResizeBind);
}
get hasDesc() {
return !!this.description.length;
}
get hasFootNotes() {
return !!this.footnotes.length;
}
get showMoreText() {
if (this._showMorePressed) {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_SHOW_LESS);
}
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_SHOW_MORE);
}
get overflowBtnAccessibleName() {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_OVERLOW_BTN_TITLE);
}
get closeBtnAccessibleName() {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_CLOSE_BTN_TITLE);
}
get hideShowMore() {
if (!this.wrap && this._showMore) {
return undefined;
}
return true;
}
get descriptionDOM() {
return this.shadowRoot.querySelector(".ui5-nli-description");
}
get headingDOM() {
return this.shadowRoot.querySelector(".ui5-nli-heading");
}
get headingHeight() {
return this.headingDOM.offsetHeight;
}
get descriptionHeight() {
return this.descriptionDOM.offsetHeight;
}
get headingOverflows() {
const heading = this.headingDOM;
if (!heading) {
return false;
}
if (isIE()) {
return heading.scrollHeight > MAX_WRAP_HEIGHT;
}
return heading.offsetHeight < heading.scrollHeight;
}
get descriptionOverflows() {
const description = this.descriptionDOM;
if (!description) {
return false;
}
if (isIE()) {
return description.scrollHeight > MAX_WRAP_HEIGHT;
}
return description.offsetHeight < description.scrollHeight;
}
get footerItems() {
return this.footnotes.map((el, idx, arr) => {
return {
slotName: el._individualSlot,
showDivider: idx !== arr.length - 1,
};
});
}
get ariaLabelledBy() {
const id = this._id;
const ids = [];
if (this.hasHeading) {
ids.push(`${id}-heading`);
}
if (this.hasDesc) {
ids.push(`${id}-description`);
}
if (this.hasFootNotes) {
ids.push(`${id}-footer`);
}
ids.push(`${id}-invisibleText`);
return ids.join(" ");
}
get priorityText() {
if (this.priority === Priority.High) {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_HIGH_PRIORITY_TXT);
}
if (this.priority === Priority.Medium) {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_MEDIUM_PRIORITY_TXT);
}
if (this.priority === Priority.Low) {
return this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_LOW_PRIORITY_TXT);
}
return "";
}
get accInvisibleText() {
const notifcationTxt = this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_TXT);
const readTxt = this.read ? this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_READ) : this.i18nFioriBundle.getText(NOTIFICATION_LIST_ITEM_UNREAD);
const priorityText = this.priorityText;
return `${notifcationTxt} ${readTxt} ${priorityText}`;
}
get classes() {
return {
content: {
"ui5-nli-content--ie": isIE(),
},
};
}
/**
* Event handlers
*/
_onclick(event) {
this.fireItemPress(event);
}
_onShowMoreClick(event) {
event.preventDefault();
this._showMorePressed = !this._showMorePressed;
}
_onkeydown(event) {
super._onkeydown(event);
if (isEnter(event)) {
this.fireItemPress(event);
}
}
_onkeyup(event) {
super._onkeyup(event);
const space = isSpace(event);
if (space && event.isMarked === "link") {
this._onShowMoreClick(event);
return;
}
if (space) {
this.fireItemPress(event);
}
}
/**
* Private
*/
fireItemPress(event) {
if (event.isMarked === "button" || event.isMarked === "link") {
return;
}
this.fireEvent("_press", { item: this });
}
onResize() {
if (this.wrap) {
this._showMore = false;
return;
}
const headingWouldOverflow = this.headingHeight > this._headingOverflowHeight;
const descWouldOverflow = this.hasDesc && this.descriptionHeight > this._descOverflowHeight;
const overflows = headingWouldOverflow || descWouldOverflow;
if (this._showMorePressed && overflows) {
this._showMore = true;
return;
}
if (this.headingOverflows || this.descriptionOverflows) {
this._headingOverflowHeight = this.headingHeight;
this._descOverflowHeight = this.hasDesc ? this.descriptionHeight : 0;
this._showMore = true;
return;
}
this._showMore = false;
}
}
NotificationListItem.define();
export default NotificationListItem;