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 * Updates the button label text. | 47 * Initializes the button with the label and the click handler. |
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 |
52 * @private | 53 * @private |
53 */ | 54 */ |
54 MessageWindowImpl.prototype.updateButton_ = function(button, label) { | 55 MessageWindowImpl.prototype.initButton_ = |
| 56 function(button, label, clickHandler) { |
55 if (label) { | 57 if (label) { |
56 button.innerText = label; | 58 button.innerText = label; |
| 59 button.addEventListener('click', clickHandler, false); |
57 } | 60 } |
58 button.hidden = !Boolean(label); | 61 button.hidden = !Boolean(label); |
59 }; | 62 }; |
60 | 63 |
61 /** | 64 /** |
62 * Event-handler callback, invoked when the parent window supplies the | 65 * Event-handler callback, invoked when the parent window supplies the |
63 * message content. | 66 * message content. |
64 * | 67 * |
65 * @param{Event} event | 68 * @param{Event} event |
66 * @private | 69 * @private |
67 */ | 70 */ |
68 MessageWindowImpl.prototype.onMessage_ = function(event) { | 71 MessageWindowImpl.prototype.onMessage_ = function(event) { |
69 var command = /** @type {string} */ (event.data['command']); | 72 switch (event.data['command']) { |
70 if (command !== 'show' && command !== 'update') { | 73 case 'show': |
71 console.error('Unexpected message: ' + command); | 74 // Validate the message. |
72 return; | 75 var messageId = /** @type {number} */ (event.data['id']); |
73 } | 76 var title = /** @type {string} */ (event.data['title']); |
| 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 } |
74 | 92 |
75 // Validate the message. | 93 // Set the dialog text. |
76 var messageId = /** @type {number} */ (event.data['id']); | 94 var button = document.getElementById('button-primary'); |
77 var title = /** @type {string} */ (event.data['title']); | 95 var cancelButton = document.getElementById('button-secondary'); |
78 var message = /** @type {string} */ (event.data['message']); | 96 var messageDiv = document.getElementById('message'); |
79 var infobox = /** @type {string} */ (event.data['infobox']); | 97 var infoboxDiv = document.getElementById('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']); | |
84 | 98 |
85 // Many of these fields are optional for either the 'show' or 'update' | 99 document.getElementById('title').innerText = title; |
86 // message. These vars are used to mark the optional fields to allow | 100 document.querySelector('title').innerText = title; |
87 // them to be undefined. | 101 messageDiv.innerHTML = message; |
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) { | |
97 console.log('Bad ' + command + ' message: ' + event.data); | |
98 return; | |
99 } | |
100 | 102 |
101 var button = document.getElementById('button-primary'); | 103 if (showSpinner) { |
102 var cancelButton = document.getElementById('button-secondary'); | 104 messageDiv.classList.add('waiting'); |
103 var messageDiv = document.getElementById('message'); | 105 messageDiv.classList.add('prominent'); |
104 var infoboxDiv = document.getElementById('infobox'); | 106 } |
| 107 if (infobox != '') { |
| 108 infoboxDiv.innerText = infobox; |
| 109 } else { |
| 110 infoboxDiv.hidden = true; |
| 111 } |
105 | 112 |
106 if (isString(title)) { | 113 this.initButton_( |
107 document.getElementById('title').innerText = title; | 114 button, |
108 document.querySelector('title').innerText = title; | 115 buttonLabel, |
109 } | 116 this.sendReply_.bind(this, event.source, messageId, 1)); |
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); | |
131 | 117 |
132 base.resizeWindowToContent(true); | 118 this.initButton_( |
| 119 cancelButton, |
| 120 cancelButtonLabel, |
| 121 this.sendReply_.bind(this, event.source, messageId, 0)); |
133 | 122 |
134 if (command === 'show') { | 123 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; |
135 // Set up click-handlers for the buttons. | 124 buttonToFocus.focus(); |
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); | |
140 | 125 |
141 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button; | 126 // Add a close handler in case the window is closed without clicking one |
142 buttonToFocus.focus(); | 127 // of the buttons. This will send a 0 as the result. |
| 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)); |
143 | 132 |
144 // Add a close handler in case the window is closed without clicking one | 133 base.resizeWindowToContent(true); |
145 // of the buttons. This will send a 0 as the result. | 134 chrome.app.window.current().show(); |
146 // Note that when a button is pressed, this will result in sendReply_ | 135 break; |
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)); | |
150 | 136 |
151 chrome.app.window.current().show(); | 137 case 'update_message': |
| 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 |