inputhandler.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.events.InputHandler</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.debug.DivConsole');
  13. goog.require('goog.dom');
  14. goog.require('goog.events');
  15. goog.require('goog.events.InputHandler');
  16. goog.require('goog.log');
  17. </script>
  18. <link rel="stylesheet" href="css/demo.css">
  19. </head>
  20. <body>
  21. <h1>goog.events.InputHandler</h1>
  22. <p><button onclick="addSome('text')">Add Some</button>
  23. <input type="text" id="text"><span></span>
  24. <p><button onclick="addSome('password')">Add Some</button>
  25. <input type="password" id="password"><span></span>
  26. <p><button onclick="addSome('textarea')">Add Some</button>
  27. <textarea id="textarea"></textarea><span></span>
  28. <br><br>
  29. <fieldset class="goog-debug-panel">
  30. <legend>Event Log</legend>
  31. <div id="log" style="height: 400px;"></div>
  32. </fieldset>
  33. <script>
  34. var $ = goog.dom.getElement;
  35. var logger = goog.log.getLogger('demo');
  36. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  37. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.FINER);
  38. logconsole.setCapturing(true);
  39. var DOM_EVENTS = ['keydown', 'keyup', 'keypress', 'change', 'cut', 'paste',
  40. 'drop', 'input'];
  41. var INPUTHANDLER_EVENTS =
  42. goog.object.getValues(goog.events.InputHandler.EventType);
  43. function logEvent(src, e) {
  44. goog.log.info(logger, '[Event from ' + src + ']: ' + e.type);
  45. }
  46. function addSome(id) {
  47. var el = $(id);
  48. el.value += '.';
  49. }
  50. function updateText(e) {
  51. var inputEl = e.target;
  52. var outputEl = inputEl.nextSibling;
  53. while (outputEl.hasChildNodes()) {
  54. outputEl.removeChild(outputEl.lastChild);
  55. }
  56. outputEl.appendChild(document.createTextNode(inputEl.value));
  57. }
  58. goog.object.forEach(['text', 'password', 'textarea'], function(id) {
  59. var el = $(id);
  60. var ih = new goog.events.InputHandler(el);
  61. goog.events.listen(ih, goog.events.InputHandler.EventType.INPUT, updateText);
  62. goog.events.listen(ih, INPUTHANDLER_EVENTS,
  63. goog.partial(logEvent, 'InputHandler'));
  64. goog.events.listen(el, DOM_EVENTS,
  65. goog.partial(logEvent, 'DOM'));
  66. });
  67. </script>
  68. </body>
  69. </html>