summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/input_window_dialog.js
blob: 5f8c1da5130561d0552bc5cc991a58e4a8feee7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('inputWindowDialog', function() {
  'use strict';

  /**
   * Disables the controls while the dialog is busy.
   */
  function disableControls() {
    $('cancel').disabled = true;
    $('ok').disabled = true;
  }

  /**
   * Returns true if URL area is shown.
   */
  function isURLAreaShown() {
    return !$('url-area').classList.contains('area-hidden');
  }

  /**
   * Close the dialog and pass a result value to the dialog close handler.
   * @param {boolean} result The value to pass to the dialog close handler.
   */
  function closeWithResult(result) {
    disableControls();
    var values = [result];
    if (result) {
      values.push($('name').value);
      if (isURLAreaShown()) {
        values.push($('url').value);
      }
    }
    var json = JSON.stringify(values);
    chrome.send('DialogClose', [json]);
  }

  /**
   * Inserts translated strings on loading.
   */
  function initialize() {
    var args = JSON.parse(chrome.dialogArguments);
    $('name-label').textContent = args.nameLabel;
    $('name').value = args.name;
    $('ok').textContent = args.ok_button_title;

    if (args.urlLabel && args.url) {
      $('url-label').textContent = args.urlLabel;
      $('url').value = args.url;
    } else {
      $('url-area').classList.add('area-hidden');
    }

    $('cancel').onclick = function() {
      closeWithResult(false);
    }

    $('ok').onclick = function() {
      if (!$('ok').disabled) {
        closeWithResult(true);
      }
    }

    function validate() {
      var values = [$('name').value];
      if (isURLAreaShown()) {
        values.push($('url').value);
      }
      chrome.send('validate', values);
    }

    $('name').oninput = validate;
    $('url').oninput = isURLAreaShown() ? validate : null;

    document.body.onkeydown = function(evt) {
      if (evt.keyCode == 13) {  // Enter key
        $('ok').onclick();
      } else if (evt.keyCode == 27) {  // Escape key
        $('cancel').onclick();
      }
    }
  }

  /**
   * Called in response to validate request.
   * @param {boolean} valid The result of validate request.
   */
  function ackValidation(valid) {
    $('ok').disabled = !valid;
  }

  return {
    initialize: initialize,
    ackValidation: ackValidation,
  };
});

document.addEventListener('DOMContentLoaded', inputWindowDialog.initialize);