在 GWT 中处理 iron-list 的键盘事件?

Handle Keyboard events for iron-list in GWT?

我使用 google Polymer 中的 iron-list

<iron-list items="[[data]]" as="item">
  <template>
    <div tabindex$="[[tabIndex]]">
      Name: [[item.name]]
    </div>
  </template>
</iron-list>

我知道你可以使用 Polymer.IronA11yKeysBehavior but even with the example 我不知道如何将它添加到 JavaScript 到我的铁名单中。

使用Vaadin Polymer GWT lib。在这个库中你有

IronList list; 
list.setKeyBindings(???);  // don't know how to use this function
list.setKeyEventTarget(????);  // don't know how to use this function

当我检查键绑定的当前值时,我定义了一个打印函数来将变量记录到控制台:

public native void print(JavaScriptObject obj) /-{ console.log(对象); }-/;

然后我打印当前值:

print(list.getKeyBindings());

结果是:

Object {up: "_didMoveUp", down: "_didMoveDown", enter: "_didEnter"}

似乎已经定义了一些键绑定,但我不知道在哪里可以找到函数 _didMoveUp_didMoveDown_didEnter

当我做的时候

print(list.getKeyEventTarget());

我得到:

<iron-list class="fit x-scope iron-list-0" tabindex="1" style="overflow: auto;">
</iron-list>

如何使用 Vaadin Polymer GWT 库设置用于捕获键盘事件的处理程序?当按下 enter 等键时,如何接收事件?

如果要添加按键事件自定义元素(不知道我理解对不对) 您必须实施 Polymer.IronA11yKeysBehavior 行为,然后使用 keyBindings 原型 属性 来表达哪些键组合将触发事件。

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <base href="https://polygit.org/components/">
  <script src="webcomponentsjs/webcomponents.js"></script>
  
  <link rel="import" href="iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
  <link rel="import" href="iron-list/iron-list.html">
<body>
  
 <dom-module id="x-elem">

  <template>  
       <iron-list items="[[data]]" as="item">
    <template>
      <div>
        pressed: [[item]]
      </div>
    </template>
  </template>
</dom-module>

<script>
  Polymer({
    is: 'x-elem',
    behaviors: [
      Polymer.IronA11yKeysBehavior
    ],
    properties: {
      preventDefault:{type:Boolean,value:true},
      data:{value:[]},
      keyEventTarget: {
        type: Object,
        value: function() {
          return document.body;
        }
      }
    },
    
    keyBindings: {
      '* pageup pagedown left right down up home end space enter @ ~ " $ ? ! \ + : # backspace': '_handler',
      'a': '_handler',
      'shift+a alt+a': '_handler',
      'shift+tab shift+space': '_handler'
    },
    _handler: function(event) {
      if (this.preventDefault) {   // try to set true/false and press shit+a and press up or down
        event.preventDefault();
      }
      console.log(event.detail.combo);
      this.push('data',event.detail.combo);
      
    }
  });
  </script>
  <x-elem></x-elem>
</body>

</html>

我希望能回答你如何听琴键的问题。 _handler 函数接收一个事件,因此您可以查看事件的详细信息并获取目标(如果有焦点)。

event.detail.target 

回答这个问题

list.setKeyBindings(???); // don't know how to use this function

根据com/vaadin/polymer/vaadin-gwt-polymer-elements/1.2.3.1-SNAPSHOT/vaadin-gwt-polymer-elements-1.2.3.1-20160201.114641-2.jar!/com/vaadin/polymer/public/bower_components/iron-list/iron-list.html:292

keyBindings 应该有 object 这样的结构:

    {
      'up': '_didMoveUp',
      'down': '_didMoveDown',
      'enter': '_didEnter'
    }

要构建这样的对象,您可以使用以下方法:

        new JSONObject() {{
            put("up", new JSONString("_didMoveUp"));
            put("down", new JSONString("_didMoveDown"));
            put("enter", new JSONString("_didEnter"));
        }}.getJavaScriptObject();

I have no idea where I find the functions _didMoveUp, _didMoveDown and _didEnter

它们可以在这里找到:com/vaadin/polymer/vaadin-gwt-polymer-elements/1.2.3.1-SNAPSHOT/vaadin-gwt-polymer-elements-1.2.3.1-20160201.114641-2.jar!/com/vaadin/polymer/public/bower_components/iron-list/iron-list.html:1504

这是摘录

    _didMoveUp: function() {
      this._focusPhysicalItem(Math.max(0, this._focusedIndex - 1));
    },

    _didMoveDown: function() {
      this._focusPhysicalItem(Math.min(this._virtualCount, this._focusedIndex + 1));
    },

    _didEnter: function(e) {
      // focus the currently focused physical item
      this._focusPhysicalItem(this._focusedIndex);
      // toggle selection
      this._selectionHandler(e.detail.keyboardEvent);
    }

How can I set up a handler for capturing keyboard events using Vaadin Polymer GWT lib?

How can I receive an event when keys like enter are pressed?

我可以找到这个 Polymer convention:不供外部使用的属性应使用下划线作为前缀。

这就是为什么它们没有在 JsType IronListElement 中公开的原因。 您可以使用 JSNI 更改此功能。我认为是这样的:

    private native static void changeDidMoveUp(IronListElement ironList) /*-{
        var _old = ironList._didMoveUp;
        ironList._didMoveUp = function() {
            console.log('tracking');
            _old();
        }
    }-*/;

或添加一个新的

    IronListElement element ...

    com.vaadin.polymer.elemental.Function<Void, Event> func = event -> {
        logger.debug(event.detail);
        ...
        return null;
    };

    private native static void addUpdatePressed(IronListElement ironList, Function func) /*-{
        ironList._updatePressed = func;
    }-*/;

    {
        addUpdatePressed(element, func);
        element.addOwnKeyBinding("a", "_updatePressed");
        element.addOwnKeyBinding("shift+a alt+a", "_updatePressed");
        element.addOwnKeyBinding("shift+tab shift+space", "_updatePressed");
    }

应该可以。您可以从 IronList#getPolymerElement().

获取元素

请记住,我还没有测试过这段代码:)