OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('inputWindowDialog', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * Disables the controls while the dialog is busy. |
| 10 */ |
| 11 function disableControls() { |
| 12 $('cancel').disabled = true; |
| 13 $('ok').disabled = true; |
| 14 } |
| 15 |
| 16 /** |
| 17 * 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. |
| 19 */ |
| 20 function closeWithResult(result) { |
| 21 disableControls(); |
| 22 var value = [result]; |
| 23 if (result) { |
| 24 value.push($('name').value); |
| 25 } |
| 26 var json = JSON.stringify(value); |
| 27 chrome.send('DialogClose', [json]); |
| 28 } |
| 29 |
| 30 /** |
| 31 * Inserts translated strings on loading. |
| 32 */ |
| 33 function initialize() { |
| 34 i18nTemplate.process(document, templateData); |
| 35 |
| 36 var args = JSON.parse(chrome.dialogArguments); |
| 37 $('name-label').textContent = args.label; |
| 38 $('name').value = args.contents; |
| 39 |
| 40 $('cancel').onclick = function() { |
| 41 closeWithResult(false); |
| 42 } |
| 43 |
| 44 $('ok').onclick = function() { |
| 45 if (!$('ok').disabled) { |
| 46 closeWithResult(true); |
| 47 } |
| 48 } |
| 49 |
| 50 $('name').oninput = function() { |
| 51 var name = $('name').value; |
| 52 chrome.send('validate', [name]); |
| 53 } |
| 54 |
| 55 document.body.onkeydown = function(evt) { |
| 56 if (evt.keyCode == 13) { // Enter key |
| 57 $('ok').onclick(); |
| 58 } else if (evt.keyCode == 27) { // Escape key |
| 59 $('cancel').onclick(); |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 /** |
| 65 * Called in response to validate request. |
| 66 * @param {boolean} valid The result of validate request. |
| 67 */ |
| 68 function ackValidation(valid) { |
| 69 $('ok').disabled = !valid; |
| 70 } |
| 71 |
| 72 return { |
| 73 initialize: initialize, |
| 74 ackValidation: ackValidation, |
| 75 }; |
| 76 }); |
| 77 |
| 78 document.addEventListener('DOMContentLoaded', inputWindowDialog.initialize); |
OLD | NEW |