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 // Many of these fields are optional for either the 'show' or 'update' |
100 document.querySelector('title').innerText = title; | 86 // message. These vars are used to mark the optional fields to allow |
101 messageDiv.innerHTML = message; | 87 // them to be undefined. |
88 var optionalFieldShow = command === 'show'; | |
89 var optionalFieldUpdate = command === 'update'; | |
90 if (isNumber(messageId) || | |
91 isString(title, optionalFieldUpdate) || | |
92 isString(message, optionalFieldUpdate) || | |
93 isString(infobox, optionalFieldUpdate) || | |
94 isString(buttonLabel, optionalFieldUpdate) || | |
95 isString(cancelButtonLabel, optionalFieldShow || optionalFieldUpdate) || | |
96 isBoolean(showSpinner, optionalFieldUpdate) { | |
Dan Beam
2015/05/15 06:15:01
you're missing a closing ) here
http://build.chrom
| |
97 console.log('Bad ' + command + ' message: ' + event.data); | |
98 return; | |
99 } | |
102 | 100 |
103 if (showSpinner) { | 101 var button = document.getElementById('button-primary'); |
104 messageDiv.classList.add('waiting'); | 102 var cancelButton = document.getElementById('button-secondary'); |
105 messageDiv.classList.add('prominent'); | 103 var messageDiv = document.getElementById('message'); |
106 } | 104 var infoboxDiv = document.getElementById('infobox'); |
107 if (infobox != '') { | |
108 infoboxDiv.innerText = infobox; | |
109 } else { | |
110 infoboxDiv.hidden = true; | |
111 } | |
112 | 105 |
113 this.initButton_( | 106 if (isString(title)) { |
114 button, | 107 document.getElementById('title').innerText = title; |
115 buttonLabel, | 108 document.querySelector('title').innerText = title; |
116 this.sendReply_.bind(this, event.source, messageId, 1)); | 109 } |
110 if (isString(message) { | |
111 messageDiv.innerText = message; | |
112 } | |
113 if (isString(infobox)) { | |
114 if (infobox != '') { | |
115 infoboxDiv.innerText = infobox; | |
116 } else { | |
117 infoboxDiv.hidden = true; | |
118 } | |
119 } | |
120 if (isBoolean(showSpinner)) { | |
121 if (showSpinner) { | |
122 messageDiv.classList.add('waiting'); | |
123 messageDiv.classList.add('prominent'); | |
124 } else { | |
125 messageDiv.classList.remove('waiting'); | |
126 messageDiv.classList.remove('prominent'); | |
127 } | |
128 } | |
129 this.updateButton_(button, buttonLabel); | |
130 this.updateButton_(cancelButton, cancelButtonLabel); | |
117 | 131 |
118 this.initButton_( | 132 base.resizeWindowToContent(true); |
119 cancelButton, | |
120 cancelButtonLabel, | |
121 this.sendReply_.bind(this, event.source, messageId, 0)); | |
122 | 133 |
123 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; | 134 if (command === 'show') { |
124 buttonToFocus.focus(); | 135 // Set up click-handlers for the buttons. |
136 button.addEventListener( | |
137 'click', this.sendReply_.bind(this, event.source, messageId, 1), false); | |
138 cancelButton.addEventListener( | |
139 'click', this.sendReply_.bind(this, event.source, messageId, 0), false); | |
125 | 140 |
126 // Add a close handler in case the window is closed without clicking one | 141 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; |
127 // of the buttons. This will send a 0 as the result. | 142 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 | 143 |
133 base.resizeWindowToContent(true); | 144 // Add a close handler in case the window is closed without clicking one |
134 chrome.app.window.current().show(); | 145 // of the buttons. This will send a 0 as the result. |
135 break; | 146 // Note that when a button is pressed, this will result in sendReply_ |
147 // being called multiple times (once for the button, once for close). | |
148 chrome.app.window.current().onClosed.addListener( | |
149 this.sendReply_.bind(this, event.source, messageId, 0)); | |
136 | 150 |
137 case 'update_message': | 151 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 } | 152 } |
153 }; | 153 }; |
154 | 154 |
155 var messageWindow = new MessageWindowImpl(); | 155 var messageWindow = new MessageWindowImpl(); |
OLD | NEW |