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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 MessageWindowImpl.prototype.initButton_ = | 55 MessageWindowImpl.prototype.initButton_ = |
56 function(button, label, clickHandler) { | 56 function(button, label, clickHandler) { |
57 if (label) { | 57 if (label) { |
58 button.innerText = label; | 58 button.innerText = label; |
59 button.addEventListener('click', clickHandler, false); | 59 button.addEventListener('click', clickHandler, false); |
60 } | 60 } |
61 button.hidden = !Boolean(label); | 61 button.hidden = !Boolean(label); |
62 }; | 62 }; |
63 | 63 |
64 /** | 64 /** |
65 * Updates the button label text. | |
66 * Hides the button if the label is null or undefined. | |
67 * | |
68 * @param{HTMLElement} button | |
69 * @param{?string} label | |
70 * @private | |
71 */ | |
72 MessageWindowImpl.prototype.updateButton_ = function(button, label) { | |
73 if (label) { | |
74 button.innerText = label; | |
75 } | |
76 button.hidden = !Boolean(label); | |
77 }; | |
78 | |
79 /** | |
65 * Event-handler callback, invoked when the parent window supplies the | 80 * Event-handler callback, invoked when the parent window supplies the |
66 * message content. | 81 * message content. |
67 * | 82 * |
68 * @param{Event} event | 83 * @param{Event} event |
69 * @private | 84 * @private |
70 */ | 85 */ |
71 MessageWindowImpl.prototype.onMessage_ = function(event) { | 86 MessageWindowImpl.prototype.onMessage_ = function(event) { |
72 switch (event.data['command']) { | 87 switch (event.data['command']) { |
73 case 'show': | 88 case 'show': |
74 // Validate the message. | 89 // Validate the message. |
75 var messageId = /** @type {number} */ (event.data['id']); | 90 var messageId = /** @type {number} */ (event.data['id']); |
76 var title = /** @type {string} */ (event.data['title']); | 91 var title = /** @type {string} */ (event.data['title']); |
77 var message = /** @type {string} */ (event.data['message']); | 92 var message = /** @type {string} */ (event.data['message']); |
78 var infobox = /** @type {string} */ (event.data['infobox']); | 93 var infobox = /** @type {string} */ (event.data['infobox']); |
79 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']); | 94 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']); |
80 /** @type {string} */ | 95 var cancelButtonLabel = /** @type {string} */ |
81 var cancelButtonLabel = (event.data['cancelButtonLabel']); | 96 (event.data['cancelButtonLabel']); |
82 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']); | 97 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']); |
83 if (typeof(messageId) != 'number' || | 98 if (typeof(messageId) != 'number' || |
84 typeof(title) != 'string' || | 99 typeof(title) != 'string' || |
85 typeof(message) != 'string' || | 100 typeof(message) != 'string' || |
86 typeof(infobox) != 'string' || | 101 typeof(infobox) != 'string' || |
87 typeof(buttonLabel) != 'string' || | 102 typeof(buttonLabel) != 'string' || |
88 typeof(showSpinner) != 'boolean') { | 103 typeof(showSpinner) != 'boolean') { |
89 console.log('Bad show message:', event.data); | 104 console.log('Bad show message:', event.data); |
90 break; | 105 break; |
91 } | 106 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 // of the buttons. This will send a 0 as the result. | 142 // of the buttons. This will send a 0 as the result. |
128 // Note that when a button is pressed, this will result in sendReply_ | 143 // Note that when a button is pressed, this will result in sendReply_ |
129 // being called multiple times (once for the button, once for close). | 144 // being called multiple times (once for the button, once for close). |
130 chrome.app.window.current().onClosed.addListener( | 145 chrome.app.window.current().onClosed.addListener( |
131 this.sendReply_.bind(this, event.source, messageId, 0)); | 146 this.sendReply_.bind(this, event.source, messageId, 0)); |
132 | 147 |
133 base.resizeWindowToContent(); | 148 base.resizeWindowToContent(); |
134 chrome.app.window.current().show(); | 149 chrome.app.window.current().show(); |
135 break; | 150 break; |
136 | 151 |
137 case 'update_message': | 152 case 'update_message': |
Jamie
2015/05/09 01:17:00
How about renaming this message 'update' and havin
garykac
2015/05/11 21:34:16
Done.
| |
138 var message = /** @type {string} */ (event.data['message']); | 153 var message = /** @type {string} */ (event.data['message']); |
154 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']); | |
155 var cancelButtonLabel = /** @type {string} */ | |
156 (event.data['cancelButtonLabel']); | |
157 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']); | |
158 // The update_message may leave the buttonLabel, cancelButtonLabel and | |
159 // showSpinner values undefined to have them retain their current values. | |
139 if (typeof(message) != 'string') { | 160 if (typeof(message) != 'string') { |
140 console.log('Bad update_message message:', event.data); | 161 console.log('Bad update_message message:', event.data); |
141 break; | 162 break; |
142 } | 163 } |
143 | 164 |
165 var button = document.getElementById('button-primary'); | |
166 var cancelButton = document.getElementById('button-secondary'); | |
144 var messageDiv = document.getElementById('message'); | 167 var messageDiv = document.getElementById('message'); |
168 | |
145 messageDiv.innerText = message; | 169 messageDiv.innerText = message; |
146 | 170 |
171 // If showSpinner was not specified, then don't change its state. | |
172 if (typeof(showSpinner) == 'boolean') { | |
173 if (showSpinner) { | |
174 messageDiv.classList.add('waiting'); | |
175 messageDiv.classList.add('prominent'); | |
176 } else { | |
177 messageDiv.classList.remove('waiting'); | |
178 messageDiv.classList.remove('prominent'); | |
179 } | |
180 } | |
181 | |
182 this.updateButton_(button, buttonLabel); | |
183 this.updateButton_(cancelButton, cancelButtonLabel); | |
184 | |
147 base.resizeWindowToContent(true); | 185 base.resizeWindowToContent(true); |
148 break; | 186 break; |
149 | 187 |
150 default: | 188 default: |
151 console.error('Unexpected message:', event.data); | 189 console.error('Unexpected message:', event.data); |
152 } | 190 } |
153 }; | 191 }; |
154 | 192 |
155 var messageWindow = new MessageWindowImpl(); | 193 var messageWindow = new MessageWindowImpl(); |
OLD | NEW |