Bläddra i källkod

fix: Manually trigger a change-event when autofill is used in webkit-browsers #12133

Johannes Schill 7 år sedan
förälder
incheckning
a558e76a68

+ 1 - 0
public/app/core/core.ts

@@ -1,5 +1,6 @@
 import './directives/dash_class';
 import './directives/dropdown_typeahead';
+import './directives/autofill_event_fix';
 import './directives/metric_segment';
 import './directives/misc';
 import './directives/ng_model_on_blur';

+ 35 - 0
public/app/core/directives/autofill_event_fix.ts

@@ -0,0 +1,35 @@
+import coreModule from '../core_module';
+
+/** @ngInject */
+export function autofillEventFix($compile) {
+  return {
+    link: ($scope: any, elem: any) => {
+      const input = elem[0];
+      const dispatchChangeEvent = () => {
+        const event = new Event('change');
+        return input.dispatchEvent(event);
+      };
+      const onAnimationStart = ({ animationName }: AnimationEvent) => {
+        switch (animationName) {
+          case 'onAutoFillStart':
+            return dispatchChangeEvent();
+          case 'onAutoFillCancel':
+            return dispatchChangeEvent();
+        }
+        return null;
+      };
+
+      // const onChange = (evt: Event) => console.log(evt);
+
+      input.addEventListener('animationstart', onAnimationStart);
+      // input.addEventListener('change', onChange);
+
+      $scope.$on('$destroy', () => {
+        input.removeEventListener('animationstart', onAnimationStart);
+        // input.removeEventListener('change', onChange);
+      });
+    }
+  };
+}
+
+coreModule.directive('autofillEventFix', autofillEventFix);

+ 1 - 1
public/app/partials/login.html

@@ -9,7 +9,7 @@
         <form name="loginForm" class="login-form-group gf-form-group" ng-hide="disableLoginForm">
           <div class="login-form">
             <input type="text" name="username" class="gf-form-input login-form-input" required ng-model='formModel.user' placeholder={{loginHint}}
-              autofocus>
+              autofocus autofill-event-fix>
           </div>
           <div class="login-form">
             <input type="password" name="password" class="gf-form-input login-form-input" required ng-model="formModel.password" id="inputPassword"

+ 1 - 0
public/sass/_grafana.scss

@@ -32,6 +32,7 @@
 @import 'utils/angular';
 @import 'utils/spacings';
 @import 'utils/widths';
+@import 'utils/hacks';
 
 // LAYOUTS
 @import 'layout/lists';

+ 11 - 0
public/sass/utils/_hacks.scss

@@ -0,0 +1,11 @@
+// <3: https://medium.com/@brunn/detecting-autofilled-fields-in-javascript-aed598d25da7
+// sass-lint:disable no-empty-rulesets
+@keyframes onAutoFillStart {  from {/**/}  to {/**/}}
+@keyframes onAutoFillCancel {  from {/**/}  to {/**/}}
+input:-webkit-autofill {
+  animation-name: onAutoFillStart;
+  transition: transform 1ms;
+}
+input:not(:-webkit-autofill) {
+  animation-name: onAutoFillCancel;
+}