| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 cr.define('inputWindowDialog', function() { | 5 cr.define('inputWindowDialog', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Disables the controls while the dialog is busy. | 9 * Disables the controls while the dialog is busy. |
| 10 */ | 10 */ |
| 11 function disableControls() { | 11 function disableControls() { |
| 12 $('cancel').disabled = true; | 12 $('cancel').disabled = true; |
| 13 $('ok').disabled = true; | 13 $('ok').disabled = true; |
| 14 } | 14 } |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Returns true if URL area is shown. |
| 18 */ |
| 19 function isURLAreaShown() { |
| 20 return !$('url-area').classList.contains('area-hidden'); |
| 21 } |
| 22 |
| 23 /** |
| 17 * Close the dialog and pass a result value to the dialog close handler. | 24 * Close the dialog and pass a result value to the dialog close handler. |
| 18 * @param {boolean} result The value to pass to the dialog close handler. | 25 * @param {boolean} result The value to pass to the dialog close handler. |
| 19 */ | 26 */ |
| 20 function closeWithResult(result) { | 27 function closeWithResult(result) { |
| 21 disableControls(); | 28 disableControls(); |
| 22 var value = [result]; | 29 var values = [result]; |
| 23 if (result) { | 30 if (result) { |
| 24 value.push($('name').value); | 31 values.push($('name').value); |
| 32 if (isURLAreaShown()) { |
| 33 values.push($('url').value); |
| 34 } |
| 25 } | 35 } |
| 26 var json = JSON.stringify(value); | 36 var json = JSON.stringify(values); |
| 27 chrome.send('DialogClose', [json]); | 37 chrome.send('DialogClose', [json]); |
| 28 } | 38 } |
| 29 | 39 |
| 30 /** | 40 /** |
| 31 * Inserts translated strings on loading. | 41 * Inserts translated strings on loading. |
| 32 */ | 42 */ |
| 33 function initialize() { | 43 function initialize() { |
| 34 i18nTemplate.process(document, templateData); | 44 i18nTemplate.process(document, templateData); |
| 35 | 45 |
| 36 var args = JSON.parse(chrome.dialogArguments); | 46 var args = JSON.parse(chrome.dialogArguments); |
| 37 $('name-label').textContent = args.label; | 47 $('name-label').textContent = args.nameLabel; |
| 38 $('name').value = args.contents; | 48 $('name').value = args.name; |
| 39 $('ok').textContent = args.ok_button_title; | 49 $('ok').textContent = args.ok_button_title; |
| 40 | 50 |
| 51 if (args.urlLabel && args.url) { |
| 52 $('url-label').textContent = args.urlLabel; |
| 53 $('url').value = args.url; |
| 54 } else { |
| 55 $('url-area').classList.add('area-hidden'); |
| 56 } |
| 57 |
| 41 $('cancel').onclick = function() { | 58 $('cancel').onclick = function() { |
| 42 closeWithResult(false); | 59 closeWithResult(false); |
| 43 } | 60 } |
| 44 | 61 |
| 45 $('ok').onclick = function() { | 62 $('ok').onclick = function() { |
| 46 if (!$('ok').disabled) { | 63 if (!$('ok').disabled) { |
| 47 closeWithResult(true); | 64 closeWithResult(true); |
| 48 } | 65 } |
| 49 } | 66 } |
| 50 | 67 |
| 51 $('name').oninput = function() { | 68 function validate() { |
| 52 var name = $('name').value; | 69 var values = [$('name').value]; |
| 53 chrome.send('validate', [name]); | 70 if (isURLAreaShown()) { |
| 71 values.push($('url').value); |
| 72 } |
| 73 chrome.send('validate', values); |
| 54 } | 74 } |
| 55 | 75 |
| 76 $('name').oninput = validate; |
| 77 $('url').oninput = isURLAreaShown() ? validate : null; |
| 78 |
| 56 document.body.onkeydown = function(evt) { | 79 document.body.onkeydown = function(evt) { |
| 57 if (evt.keyCode == 13) { // Enter key | 80 if (evt.keyCode == 13) { // Enter key |
| 58 $('ok').onclick(); | 81 $('ok').onclick(); |
| 59 } else if (evt.keyCode == 27) { // Escape key | 82 } else if (evt.keyCode == 27) { // Escape key |
| 60 $('cancel').onclick(); | 83 $('cancel').onclick(); |
| 61 } | 84 } |
| 62 } | 85 } |
| 63 } | 86 } |
| 64 | 87 |
| 65 /** | 88 /** |
| 66 * Called in response to validate request. | 89 * Called in response to validate request. |
| 67 * @param {boolean} valid The result of validate request. | 90 * @param {boolean} valid The result of validate request. |
| 68 */ | 91 */ |
| 69 function ackValidation(valid) { | 92 function ackValidation(valid) { |
| 70 $('ok').disabled = !valid; | 93 $('ok').disabled = !valid; |
| 71 } | 94 } |
| 72 | 95 |
| 73 return { | 96 return { |
| 74 initialize: initialize, | 97 initialize: initialize, |
| 75 ackValidation: ackValidation, | 98 ackValidation: ackValidation, |
| 76 }; | 99 }; |
| 77 }); | 100 }); |
| 78 | 101 |
| 79 document.addEventListener('DOMContentLoaded', inputWindowDialog.initialize); | 102 document.addEventListener('DOMContentLoaded', inputWindowDialog.initialize); |
| OLD | NEW |