Merge lp:~wallyworld/launchpad/improper-notification-removal into lp:launchpad

Proposed by Ian Booth
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 12779
Proposed branch: lp:~wallyworld/launchpad/improper-notification-removal
Merge into: lp:launchpad
Diff against target: 147 lines (+55/-31)
2 files modified
lib/lp/app/javascript/client.js (+42/-27)
lib/lp/app/javascript/tests/test_lp_client.js (+13/-4)
To merge this branch: bzr merge lp:~wallyworld/launchpad/improper-notification-removal
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve
Review via email: mp+56855@code.launchpad.net

Commit message

[r=lifeless][bug=754058] Removal of existing notifications after patch requests is now explicit

Description of the change

After an xhr patch request, existing page notifications were being removed so that any new ones could be displayed in a clean context. However, this broke pages which issued several xhr calls during initial loading.

== Implementation ==

Make removal of notifications via javascript explicit. There is a lp_client remove_notifications() method which can be called when required.

== Tests ==

Modify existing lp_client javascript test plus add one new test:
  test_display_notifications()
  test_remove_notifications()

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/app/javascript/client.js'
2--- lib/lp/app/javascript/client.js 2011-04-06 16:59:07 +0000
3+++ lib/lp/app/javascript/client.js 2011-04-07 22:34:57 +0000
4@@ -260,6 +260,25 @@
5 }
6 };
7
8+var NOTIFICATION_INFO = {
9+ 'level10': {
10+ 'selector': '.debug.message',
11+ 'css_class': 'debug message'
12+ },
13+ 'level20': {
14+ 'selector': '.informational.message',
15+ 'css_class': 'informational message'
16+ },
17+ 'level30': {
18+ 'selector': '.warning.message',
19+ 'css_class': 'warning message'
20+ },
21+ 'level40': {
22+ 'selector': '.error.message',
23+ 'css_class': 'error message'
24+ }
25+};
26+
27 /**
28 * Display a list of notifications - error, warning, informational or debug.
29 * @param notifications An json encoded array of (level, message) tuples.
30@@ -268,53 +287,36 @@
31 if (notifications === undefined)
32 return;
33
34- var notification_info = {
35+ var notifications_by_level = {
36 'level10': {
37- 'notifications': new Array(),
38- 'selector': '.debug.message',
39- 'css_class': 'debug message'
40+ 'notifications': new Array()
41 },
42 'level20': {
43- 'notifications': new Array(),
44- 'selector': '.informational.message',
45- 'css_class': 'informational message'
46+ 'notifications': new Array()
47 },
48 'level30': {
49- 'notifications': new Array(),
50- 'selector': '.warning.message',
51- 'css_class': 'warning message'
52+ 'notifications': new Array()
53 },
54 'level40': {
55- 'notifications': new Array(),
56- 'selector': '.error.message',
57- 'css_class': 'error message'
58+ 'notifications': new Array()
59 }
60 };
61
62- // First remove any existing notifications.
63- Y.each(notification_info, function (info) {
64- var nodes = Y.all('div#request-notifications div'+info.selector);
65- nodes.each(function(node) {
66- var parent = node.get('parentNode');
67- parent.removeChild(node);
68- });
69- });
70-
71- // Now display the new ones.
72+ // Extract the notifications from the json.
73 notifications = Y.JSON.parse(notifications);
74 Y.each(notifications, function(notification, key) {
75 var level = notification[0];
76 var message = notification[1];
77- var info = notification_info['level'+level];
78- info.notifications.push(message);
79+ notifications_by_level['level'+level].notifications.push(message);
80 });
81
82 // The place where we want to insert the notification divs.
83 var last_message = null;
84 // A mapping from the div class to notification messages.
85- Y.each(notification_info, function(info) {
86+ Y.each(notifications_by_level, function(info, key) {
87 Y.each(info.notifications, function(notification) {
88- var node = Y.Node.create("<div class='"+info.css_class+"'/>");
89+ var css_class = NOTIFICATION_INFO[key].css_class;
90+ var node = Y.Node.create("<div class='"+css_class+"'/>");
91 node.set('innerHTML', notification);
92 if (last_message==null) {
93 var div = Y.one('div#request-notifications');
94@@ -327,6 +329,19 @@
95 });
96 }
97
98+/**
99+ * Remove any notifications that are currently displayed.
100+ */
101+module.remove_notifications = function() {
102+ Y.each(NOTIFICATION_INFO, function (info) {
103+ var nodes = Y.all('div#request-notifications div'+info.selector);
104+ nodes.each(function(node) {
105+ var parent = node.get('parentNode');
106+ parent.removeChild(node);
107+ });
108+ });
109+};
110+
111 // The resources that come together to make Launchpad.
112
113 // A hosted file resource.
114
115=== modified file 'lib/lp/app/javascript/tests/test_lp_client.js'
116--- lib/lp/app/javascript/tests/test_lp_client.js 2011-04-05 12:10:01 +0000
117+++ lib/lp/app/javascript/tests/test_lp_client.js 2011-04-07 22:34:57 +0000
118@@ -233,16 +233,25 @@
119 this._checkNotificationNode('.debug.message', 'A debug');
120 this._checkNotificationNode('.informational.message', 'An info');
121
122- // Check that existing notifications are removed when a new request is
123- // received.
124+ // Any subsequent request should preserve existing notifications.
125 var new_notifications = '[ [30, "A warning"], [40, "An error"] ]';
126 this.response.setResponseHeader(
127 'X-Lazr-Notifications', new_notifications);
128 Y.lp.client.wrap_resource_on_success(null, this.response, this.args);
129+ this._checkNotificationNode('.debug.message', 'A debug');
130+ this._checkNotificationNode('.informational.message', 'An info');
131+ this._checkNotificationNode('.warning.message', 'A warning');
132+ this._checkNotificationNode('.error.message', 'An error');
133+ },
134+
135+ test_remove_notifications: function() {
136+ var notifications = '[ [10, "A debug"], [20, "An info"] ]';
137+ this.response.setResponseHeader(
138+ 'X-Lazr-Notifications', notifications);
139+ Y.lp.client.wrap_resource_on_success(null, this.response, this.args);
140+ Y.lp.client.remove_notifications();
141 this._checkNoNotificationNode('.debug.message');
142 this._checkNoNotificationNode('.informational.message');
143- this._checkNotificationNode('.warning.message', 'A warning');
144- this._checkNotificationNode('.error.message', 'An error');
145 }
146
147 }));