Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if my Polymer.dart Component is still the document.activeElement?

I have a CustomPassword component and want to provide a method isActive that allows you to retrieve if the component is still the active element on this website.

Example Code:

custom_password.html

<polymer-element name="custom-password">
  <template>
    <div>Other things here</div>
    <input id="password-field" type="password" value="{{value}}">
  </template>
  <script type="application/dart" src="custom_password.dart"></script>
</polymer-element>

custom_password.dart

import 'package:polymer/polymer.dart';

@CustomTag('custom-password')
class CustomPassword extends PolymerElement {

  @published
  String value;

  CustomPassword.created() : super.created() {
  }

  bool isActive() {
    // TODO figure out if the CustomPassword element is still active.
    return false;
  }
}
like image 521
Nik Graf Avatar asked Dec 26 '22 06:12

Nik Graf


1 Answers

With a help from the Polymer Group I was able to come up with a solution:

For browsers with shadow DOM support it works out of the box by comparing the hashCode of the document.activeElement with the components hashCode.

For browsers without shadow DOM support the password field will be the active element. The trick here is to wrap the document.activeElement in order to compare it to the wrapped passwordField.

Example:

custom_password.dart

import 'package:polymer/polymer.dart';
import 'dart:html';
import 'dart:js' as js;

@CustomTag('custom-password')
class CustomPassword extends PolymerElement {

  @published
  String value;

  CustomPassword.created() : super.created() {
  }

  bool isActive() {
      var passwordField = $['password-field'];
      var activeElement = js.context.callMethod('wrap', [document.activeElement]);

      // For Browsers with shadow DOM support the shadowRoot.host matches while
      // for Browsers without shadow DOM support the password field match.
      if (activeElement.hashCode != hashCode &&
          activeElement.hashCode != passwordField.hashCode) {
        return false;
      } else {
        return true;
      }
  }
}
like image 176
Nik Graf Avatar answered Dec 28 '22 07:12

Nik Graf