OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @constructor | 8 * @constructor |
9 */ | 9 */ |
10 function MessageWindowImpl() { | 10 function MessageWindowImpl() { |
(...skipping 26 matching lines...) Expand all Loading... | |
37 }; | 37 }; |
38 parentWindow.postMessage(message, '*'); | 38 parentWindow.postMessage(message, '*'); |
39 this.sentReply_ = true; | 39 this.sentReply_ = true; |
40 } else { | 40 } else { |
41 // Make sure that the reply we're ignoring is from the window close. | 41 // Make sure that the reply we're ignoring is from the window close. |
42 base.debug.assert(result == 0); | 42 base.debug.assert(result == 0); |
43 } | 43 } |
44 }; | 44 }; |
45 | 45 |
46 /** | 46 /** |
47 * Initializes the button with the label and the click handler. | 47 * Updates the button label text. |
48 * Hides the button if the label is null or undefined. | 48 * Hides the button if the label is null or undefined. |
49 * | 49 * |
50 * @param{HTMLElement} button | 50 * @param{HTMLElement} button |
51 * @param{?string} label | 51 * @param{?string} label |
52 * @param{Function} clickHandler | |
53 * @private | 52 * @private |
54 */ | 53 */ |
55 MessageWindowImpl.prototype.initButton_ = | 54 MessageWindowImpl.prototype.updateButton_ = function(button, label) { |
56 function(button, label, clickHandler) { | |
57 if (label) { | 55 if (label) { |
58 button.innerText = label; | 56 button.innerText = label; |
59 button.addEventListener('click', clickHandler, false); | |
60 } | 57 } |
61 button.hidden = !Boolean(label); | 58 button.hidden = !Boolean(label); |
62 }; | 59 }; |
63 | 60 |
64 /** | 61 /** |
65 * Event-handler callback, invoked when the parent window supplies the | 62 * Event-handler callback, invoked when the parent window supplies the |
66 * message content. | 63 * message content. |
67 * | 64 * |
68 * @param{Event} event | 65 * @param{Event} event |
69 * @private | 66 * @private |
70 */ | 67 */ |
71 MessageWindowImpl.prototype.onMessage_ = function(event) { | 68 MessageWindowImpl.prototype.onMessage_ = function(event) { |
72 switch (event.data['command']) { | 69 var command = /** @type {string} */ (event.data['command']); |
73 case 'show': | 70 if (command !== 'show' && command !== 'update') { |
74 // Validate the message. | 71 console.error('Unexpected message: ' + command); |
75 var messageId = /** @type {number} */ (event.data['id']); | 72 return; |
76 var title = /** @type {string} */ (event.data['title']); | 73 } |
77 var message = /** @type {string} */ (event.data['message']); | |
78 var infobox = /** @type {string} */ (event.data['infobox']); | |
79 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']); | |
80 /** @type {string} */ | |
81 var cancelButtonLabel = (event.data['cancelButtonLabel']); | |
82 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']); | |
83 if (typeof(messageId) != 'number' || | |
84 typeof(title) != 'string' || | |
85 typeof(message) != 'string' || | |
86 typeof(infobox) != 'string' || | |
87 typeof(buttonLabel) != 'string' || | |
88 typeof(showSpinner) != 'boolean') { | |
89 console.log('Bad show message:', event.data); | |
90 break; | |
91 } | |
92 | 74 |
93 // Set the dialog text. | 75 // Validate the message. |
94 var button = document.getElementById('button-primary'); | 76 var messageId = /** @type {number} */ (event.data['id']); |
95 var cancelButton = document.getElementById('button-secondary'); | 77 var title = /** @type {string} */ (event.data['title']); |
96 var messageDiv = document.getElementById('message'); | 78 var message = /** @type {string} */ (event.data['message']); |
97 var infoboxDiv = document.getElementById('infobox'); | 79 var infobox = /** @type {string} */ (event.data['infobox']); |
80 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']); | |
81 var cancelButtonLabel = /** @type {string} */ | |
82 (event.data['cancelButtonLabel']); | |
83 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']); | |
98 | 84 |
99 document.getElementById('title').innerText = title; | 85 // For the 'show' command, these values are required; whereas they are |
100 document.querySelector('title').innerText = title; | 86 // all optional for the 'update' command. |
Jamie
2015/05/11 22:54:53
I think we should do the type check regardless. Fo
garykac
2015/05/13 22:05:27
The same needs to be done for cancelButtonLabel (w
| |
101 messageDiv.innerHTML = message; | 87 if (command === 'show') { |
88 if (typeof(messageId) !== 'number' || | |
89 typeof(title) !== 'string' || | |
90 typeof(message) !== 'string' || | |
91 typeof(infobox) !== 'string' || | |
92 typeof(buttonLabel) !== 'string' || | |
93 typeof(showSpinner) !== 'boolean') { | |
94 console.log('Bad show message: ' + event.data); | |
95 return; | |
96 } | |
97 } | |
102 | 98 |
103 if (showSpinner) { | 99 var button = document.getElementById('button-primary'); |
104 messageDiv.classList.add('waiting'); | 100 var cancelButton = document.getElementById('button-secondary'); |
105 messageDiv.classList.add('prominent'); | 101 var messageDiv = document.getElementById('message'); |
106 } | 102 var infoboxDiv = document.getElementById('infobox'); |
107 if (infobox != '') { | |
108 infoboxDiv.innerText = infobox; | |
109 } else { | |
110 infoboxDiv.hidden = true; | |
111 } | |
112 | 103 |
113 this.initButton_( | 104 if (typeof(title) === 'string') { |
114 button, | 105 document.getElementById('title').innerText = title; |
115 buttonLabel, | 106 document.querySelector('title').innerText = title; |
116 this.sendReply_.bind(this, event.source, messageId, 1)); | 107 } |
108 if (typeof(message) === 'string') { | |
109 messageDiv.innerText = message; | |
110 } | |
111 if (typeof(infobox) === 'string') { | |
112 if (infobox != '') { | |
113 infoboxDiv.innerText = infobox; | |
114 } else { | |
115 infoboxDiv.hidden = true; | |
116 } | |
117 } | |
118 if (typeof(showSpinner) === 'boolean') { | |
119 if (showSpinner) { | |
120 messageDiv.classList.add('waiting'); | |
121 messageDiv.classList.add('prominent'); | |
122 } else { | |
123 messageDiv.classList.remove('waiting'); | |
124 messageDiv.classList.remove('prominent'); | |
125 } | |
126 } | |
127 this.updateButton_(button, buttonLabel); | |
128 this.updateButton_(cancelButton, cancelButtonLabel); | |
117 | 129 |
118 this.initButton_( | 130 base.resizeWindowToContent(); |
119 cancelButton, | |
120 cancelButtonLabel, | |
121 this.sendReply_.bind(this, event.source, messageId, 0)); | |
122 | 131 |
123 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; | 132 if (command === 'show') { |
124 buttonToFocus.focus(); | 133 // Set up clickhandlers for the buttons. |
Jamie
2015/05/11 22:54:53
s/clickhandlers/click-handlers/
garykac
2015/05/13 22:05:27
Done.
| |
134 button.addEventListener( | |
135 'click', this.sendReply_.bind(this, event.source, messageId, 1), false); | |
136 cancelButton.addEventListener( | |
137 'click', this.sendReply_.bind(this, event.source, messageId, 0), false); | |
Jamie
2015/05/11 22:54:53
Optional: Consider moving the event handlers to th
garykac
2015/05/13 22:05:27
That doesn't feel like it belongs in this cl.
It
Jamie
2015/05/13 23:43:17
Acknowledged.
| |
125 | 138 |
126 // Add a close handler in case the window is closed without clicking one | 139 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; |
127 // of the buttons. This will send a 0 as the result. | 140 buttonToFocus.focus(); |
128 // Note that when a button is pressed, this will result in sendReply_ | |
129 // being called multiple times (once for the button, once for close). | |
130 chrome.app.window.current().onClosed.addListener( | |
131 this.sendReply_.bind(this, event.source, messageId, 0)); | |
132 | 141 |
133 base.resizeWindowToContent(); | 142 // Add a close handler in case the window is closed without clicking one |
134 chrome.app.window.current().show(); | 143 // of the buttons. This will send a 0 as the result. |
135 break; | 144 // Note that when a button is pressed, this will result in sendReply_ |
145 // being called multiple times (once for the button, once for close). | |
146 chrome.app.window.current().onClosed.addListener( | |
147 this.sendReply_.bind(this, event.source, messageId, 0)); | |
136 | 148 |
137 case 'update_message': | 149 chrome.app.window.current().show(); |
138 var message = /** @type {string} */ (event.data['message']); | |
139 if (typeof(message) != 'string') { | |
140 console.log('Bad update_message message:', event.data); | |
141 break; | |
142 } | |
143 | |
144 var messageDiv = document.getElementById('message'); | |
145 messageDiv.innerText = message; | |
146 | |
147 base.resizeWindowToContent(true); | |
148 break; | |
149 | |
150 default: | |
151 console.error('Unexpected message:', event.data); | |
152 } | 150 } |
153 }; | 151 }; |
154 | 152 |
155 var messageWindow = new MessageWindowImpl(); | 153 var messageWindow = new MessageWindowImpl(); |
OLD | NEW |