Skip to content

Commit 0886143

Browse files
committed
fix(pfToastNotificationList): HTML Content for Toast Notifications
Allow HTML content for Toast Notifications
1 parent 919418a commit 0886143

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

src/notification/toast-notification-list.component.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* @param {Array} notifications The list of current notifications to display. Each notification should have the following (see pfToastNotification):
88
* <ul style='list-style-type: none'>
99
* <li>.type - (String) The type of the notification message. Allowed value is one of these: 'success','info','danger', 'warning'
10-
* <li>.header - (String) The header to display for the notification (optional)
11-
* <li>.message - (String) The main text message of the notification.
10+
* <li>.header - (String) The header to display for the notification, accepts HTML content. (optional)
11+
* <li>.message - (String) The main text message of the notification. Accepts HTML content.
1212
* <li>.actionTitle Text to show for the primary action, optional.
1313
* <li>.actionCallback (function(this notification)) Function to invoke when primary action is selected, optional
1414
* <li>.menuActions Optional list of actions to place in the kebab menu:<br/>
@@ -118,7 +118,7 @@
118118
119119
$scope.type = $scope.types[0];
120120
$scope.header = 'Default header.';
121-
$scope.message = 'Default notification message.';
121+
$scope.message = 'Default <strong>notification</strong> message.';
122122
$scope.showClose = false;
123123
$scope.persistent = false;
124124
@@ -193,7 +193,7 @@
193193
$scope.handleAction,
194194
($scope.showMenu ? $scope.menuActions : undefined)
195195
);
196-
}
196+
};
197197
198198
$scope.notifications = Notifications.data;
199199
});
@@ -225,3 +225,4 @@ angular.module('patternfly.notification').component('pfToastNotificationList', {
225225
};
226226
}
227227
});
228+

src/notification/toast-notification.component.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* @scope
66
*
77
* @param {string} notificationType The type of the notification message. Allowed value is one of these: 'success','info','danger', 'warning'
8-
* @param {string} header The header text of the notification.
9-
* @param {string} message The main text message of the notification.
8+
* @param {string} header The header text of the notification. Accepts HTML content.
9+
* @param {string} message The main text message of the notification. Accepts HTML content.
1010
* @param {boolean} showClose Flag to show the close button, default: true
1111
* @param {function} closeCallback (function(data)) Function to invoke when close action is selected, optional
1212
* @param {string} actionTitle Text to show for the primary action, optional.
@@ -107,7 +107,7 @@
107107
$scope.showClose = false;
108108
109109
$scope.header = 'Default Header.';
110-
$scope.message = 'Default Message.';
110+
$scope.message = 'Default <strong>notification</strong> message.';
111111
$scope.primaryAction = '';
112112
113113
$scope.updateType = function(item) {
@@ -190,7 +190,7 @@ angular.module( 'patternfly.notification' ).component('pfToastNotification', {
190190
'data': '=?'
191191
},
192192
templateUrl: 'notification/toast-notification.html',
193-
controller: function () {
193+
controller: function ($sce) {
194194
'use strict';
195195
var ctrl = this,
196196
_showClose;
@@ -249,5 +249,10 @@ angular.module( 'patternfly.notification' ).component('pfToastNotification', {
249249
ctrl.updateShowClose();
250250
}
251251
};
252+
253+
ctrl.trustAsHtml = function (html) {
254+
return $sce.trustAsHtml(html);
255+
};
252256
}
253257
});
258+

src/notification/toast-notification.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
<span class="pficon pficon-info" ng-if="$ctrl.notificationType === 'info'"></span>
2828
<span class="pficon pficon-error-circle-o" ng-if="$ctrl.notificationType === 'danger'"></span>
2929
<span class="pficon pficon-warning-triangle-o" ng-if="$ctrl.notificationType === 'warning'"></span>
30-
<span ng-if="$ctrl.header">
31-
<strong>{{$ctrl.header}}</strong> {{$ctrl.message}}
32-
</span>
33-
<span ng-if="!$ctrl.header">
34-
{{$ctrl.message}}
30+
<span>
31+
<strong ng-if="$ctrl.header" ng-bind-html="$ctrl.trustAsHtml($ctrl.header)"></strong>
32+
<span ng-bind-html="$ctrl.trustAsHtml($ctrl.message)"></span>
3533
</span>
3634
</div>
35+

test/notification/toast-notification-list.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,13 @@ describe('Component: pfToastNotificationList', function () {
157157
// No Menu Actions
158158
$scope.notifications.forEach(function(nextItem) {
159159
nextItem.menuActions = undefined;
160-
})
160+
});
161+
161162
var htmlTmp = '<pf-toast-notification-list notifications="notifications" show-close="false" close-callback="handleClose"></pf-toast-notification-list>';
162163

163164
compileHTML(htmlTmp, $scope);
164165

165-
var closeButton = element.find('.toast-notifications-list-pf .toast-pf button.close');
166+
closeButton = element.find('.toast-notifications-list-pf .toast-pf button.close');
166167
expect(closeButton.length).toBe(3);
167168

168169
expect($scope.closeData).toBeUndefined();
@@ -208,3 +209,4 @@ describe('Component: pfToastNotificationList', function () {
208209
expect($scope.menuData.header).toBe("Header 1");
209210
});
210211
});
212+

test/notification/toast-notification.spec.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ describe('Component: pfToastNotification', function () {
2121
scope.$digest();
2222
};
2323

24-
var setupHTML = function (notificationType, header, showClose, primaryAction, showMenu, data) {
24+
var setupHTML = function (notificationType, header, message, showClose, primaryAction, showMenu, data) {
2525
$scope.type = notificationType;
2626
$scope.header = header;
27-
$scope.message = "Test Toast Notification Message";
27+
$scope.message = message || "Test Toast Notification Message";
2828
$scope.showClose = showClose;
2929
$scope.primaryAction = primaryAction;
30-
$scope.data = data
30+
$scope.data = data;
3131

3232
$scope.closeData = undefined;
3333
$scope.closeCallback = function (data) {
@@ -100,26 +100,38 @@ describe('Component: pfToastNotification', function () {
100100
};
101101

102102
it('should have the correct header and message', function () {
103-
setupHTML ("info", "Test Header", false, '', false);
103+
setupHTML ("info", "Test Header", null, false, '', false);
104104
header = element.find('.toast-pf span strong');
105105
expect(header.length).toBe(1);
106106
expect(header.text()).toBe("Test Header");
107-
message = element.find('.toast-pf span');
107+
message = element.find('.toast-pf > span');
108108
expect(message.length).toBe(2);
109109
expect(angular.element(message[1]).text()).toContain("Test Toast Notification Message");
110110
});
111111

112112
it('should have the correct message when no header is given', function () {
113-
setupHTML ("info", "", false, '', false);
113+
setupHTML ("info", "", null, false, '', false);
114114
var header = element.find('.toast-pf span strong');
115115
expect(header.length).toBe(0);
116-
var message = element.find('.toast-pf span');
116+
var message = element.find('.toast-pf > span');
117117
expect(message.length).toBe(2);
118118
expect(angular.element(message[1]).text()).toContain("Test Toast Notification Message");
119119
});
120120

121+
it('should allow HTML content within the header and message', function () {
122+
setupHTML ("info", "<em>Test Header</em>", null, false, '', false);
123+
var header = element.find('.toast-pf span strong em');
124+
expect(header.length).toBe(1);
125+
expect(header.text()).toContain("Test Header");
126+
127+
setupHTML ("info", "", "<em>Test Notification Message</em>", false, '', false);
128+
var message = element.find('.toast-pf > span em');
129+
expect(message.length).toBe(1);
130+
expect(message.text()).toContain("Test Notification Message");
131+
});
132+
121133
it('should have the correct status icon', function () {
122-
setupHTML ("success", "Test Header", false, '', false);
134+
setupHTML ("success", "Test Header", null, false, '', false);
123135
var okIcon = element.find('.pficon.pficon-ok');
124136
var infoIcon = element.find('.pficon.pficon-info');
125137
var errorIcon = element.find('.pficon.pficon-error-circle-o');
@@ -129,7 +141,7 @@ describe('Component: pfToastNotification', function () {
129141
expect(errorIcon.length).toBe(0);
130142
expect(warnIcon.length).toBe(0);
131143

132-
setupHTML ("info", "Test Header", false, '', false);
144+
setupHTML ("info", "Test Header", null, false, '', false);
133145
okIcon = element.find('.pficon.pficon-ok');
134146
infoIcon = element.find('.pficon.pficon-info');
135147
errorIcon = element.find('.pficon.pficon-error-circle-o');
@@ -139,7 +151,7 @@ describe('Component: pfToastNotification', function () {
139151
expect(errorIcon.length).toBe(0);
140152
expect(warnIcon.length).toBe(0);
141153

142-
setupHTML ("danger", "Test Header", false, '', false);
154+
setupHTML ("danger", "Test Header", null, false, '', false);
143155
okIcon = element.find('.pficon.pficon-ok');
144156
infoIcon = element.find('.pficon.pficon-info');
145157
errorIcon = element.find('.pficon.pficon-error-circle-o');
@@ -149,7 +161,7 @@ describe('Component: pfToastNotification', function () {
149161
expect(errorIcon.length).toBe(1);
150162
expect(warnIcon.length).toBe(0);
151163

152-
setupHTML ("warning", "Test Header", false, '', false);
164+
setupHTML ("warning", "Test Header", null, false, '', false);
153165
okIcon = element.find('.pficon.pficon-ok');
154166
infoIcon = element.find('.pficon.pficon-info');
155167
errorIcon = element.find('.pficon.pficon-error-circle-o');
@@ -162,11 +174,11 @@ describe('Component: pfToastNotification', function () {
162174
});
163175

164176
it('should have the close button when specified', function () {
165-
setupHTML ("success", "Test Header", false, 'Test Action', true);
177+
setupHTML ("success", "Test Header", null, false, 'Test Action', true);
166178
var closeButton = element.find('button.close');
167179
expect(closeButton.length).toBe(0);
168180

169-
setupHTML ("success", "Test Header", true, 'Test Action', false);
181+
setupHTML ("success", "Test Header", null, true, 'Test Action', false);
170182
closeButton = element.find('button.close');
171183
expect(closeButton.length).toBe(1);
172184

@@ -179,13 +191,13 @@ describe('Component: pfToastNotification', function () {
179191
expect($scope.closeData.title).toBe("Test Notification");
180192

181193
// No close button even if specified when menu actions exist
182-
setupHTML ("success", "Test Header", true, 'Test Action', true);
194+
setupHTML ("success", "Test Header", null, true, 'Test Action', true);
183195
closeButton = element.find('button.close');
184196
expect(closeButton.length).toBe(0);
185197
});
186198

187199
it('should have the correct primary action and call the correct callback when clicked', function () {
188-
setupHTML ("success", "Test Header", false, 'Test Action', false);
200+
setupHTML ("success", "Test Header", null, false, 'Test Action', false);
189201
var actionButton = element.find('.toast-pf-action > a');
190202
expect(actionButton.length).toBe(1);
191203
expect($scope.actionData).toBeUndefined();
@@ -198,7 +210,7 @@ describe('Component: pfToastNotification', function () {
198210
});
199211

200212
it('should have the correct kebab menu and call the correct callback when items are clicked', function () {
201-
setupHTML ("success", "Test Header", false, 'Test Action', true);
213+
setupHTML ("success", "Test Header", null, false, 'Test Action', true);
202214
var menuIndicator = element.find('.dropdown-kebab-pf');
203215
expect(menuIndicator.length).toBe(1);
204216
var menuItems = element.find('.dropdown-kebab-pf .dropdown-menu li');
@@ -219,13 +231,13 @@ describe('Component: pfToastNotification', function () {
219231
});
220232

221233
it('should have correct number of separators', function () {
222-
setupHTML ("success", "Test Header", false, 'Test Action', true);
234+
setupHTML ("success", "Test Header", null, false, 'Test Action', true);
223235
var fields = element.find('.dropdown-kebab-pf .dropdown-menu .divider');
224236
expect(fields.length).toBe(1);
225237
});
226238

227239
it('should correctly disable actions and not call the callback if clicked', function () {
228-
setupHTML ("success", "Test Header", false, 'Test Action', true);
240+
setupHTML ("success", "Test Header", null, false, 'Test Action', true);
229241
var fields = element.find('.dropdown-kebab-pf .dropdown-menu .disabled > a');
230242
expect(fields.length).toBe(1);
231243

@@ -239,3 +251,4 @@ describe('Component: pfToastNotification', function () {
239251
expect($scope.menuData).toBeUndefined();
240252
});
241253
});
254+

0 commit comments

Comments
 (0)