Commit c8d73e1a authored by Randall Leeds's avatar Randall Leeds

Vendorize autofill-event with npm

parent 8d585aec
require('autofill-event')
baseURI = require('base-url')() baseURI = require('base-url')()
angular = require('angular') angular = require('angular')
require('angular-jwt') require('angular-jwt')
......
...@@ -34,7 +34,6 @@ module.exports = function(config) { ...@@ -34,7 +34,6 @@ module.exports = function(config) {
'vendor/annotator.js', 'vendor/annotator.js',
'vendor/katex.js', 'vendor/katex.js',
'vendor/moment-timezone-data.js', 'vendor/moment-timezone-data.js',
'vendor/polyfills/autofill-event.js',
'vendor/polyfills/bind.js', 'vendor/polyfills/bind.js',
'vendor/polyfills/url.js', 'vendor/polyfills/url.js',
......
/**
* Autofill event polyfill ##version:1.0.0##
* (c) 2014 Google, Inc.
* License: MIT
*/
(function(window) {
var $ = window.jQuery || window.angular.element;
var rootElement = window.document.documentElement,
$rootElement = $(rootElement);
addGlobalEventListener('change', markValue);
addValueChangeByJsListener(markValue);
$.prototype.checkAndTriggerAutoFillEvent = jqCheckAndTriggerAutoFillEvent;
// Need to use blur and not change event
// as Chrome does not fire change events in all cases an input is changed
// (e.g. when starting to type and then finish the input by auto filling a username)
addGlobalEventListener('blur', function(target) {
// setTimeout needed for Chrome as it fills other
// form fields a little later...
window.setTimeout(function() {
findParentForm(target).find('input').checkAndTriggerAutoFillEvent();
}, 20);
});
window.document.addEventListener('DOMContentLoaded', function() {
// The timeout is needed for Chrome as it auto fills
// login forms some time after DOMContentLoaded!
window.setTimeout(function() {
$rootElement.find('input').checkAndTriggerAutoFillEvent();
}, 200);
}, false);
return;
// ----------
function jqCheckAndTriggerAutoFillEvent() {
var i, el;
for (i=0; i<this.length; i++) {
el = this[i];
if (!valueMarked(el)) {
markValue(el);
triggerChangeEvent(el);
}
}
}
function valueMarked(el) {
var val = el.value,
$$currentValue = el.$$currentValue;
if (!val && !$$currentValue) {
return true;
}
return val === $$currentValue;
}
function markValue(el) {
el.$$currentValue = el.value;
}
function addValueChangeByJsListener(listener) {
var jq = window.jQuery || window.angular.element,
jqProto = jq.prototype;
var _val = jqProto.val;
jqProto.val = function(newValue) {
var res = _val.apply(this, arguments);
if (arguments.length > 0) {
forEach(this, function(el) {
listener(el, newValue);
});
}
return res;
}
}
function addGlobalEventListener(eventName, listener) {
// Use a capturing event listener so that
// we also get the event when it's stopped!
// Also, the blur event does not bubble.
rootElement.addEventListener(eventName, onEvent, true);
function onEvent(event) {
var target = event.target;
listener(target);
}
}
function findParentForm(el) {
while (el) {
if (el.nodeName === 'FORM') {
return $(el);
}
el = el.parentNode;
}
return $();
}
function forEach(arr, listener) {
if (arr.forEach) {
return arr.forEach(listener);
}
var i;
for (i=0; i<arr.length; i++) {
listener(arr[i]);
}
}
function triggerChangeEvent(element) {
var doc = window.document;
var event = doc.createEvent("HTMLEvents");
event.initEvent("change", true, true);
element.dispatchEvent(event);
}
})(window);
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"angular-route": "1.2.28", "angular-route": "1.2.28",
"angular-sanitize": "1.2.28", "angular-sanitize": "1.2.28",
"angulartics": "0.17.2", "angulartics": "0.17.2",
"autofill-event": "0.0.1",
"babelify": "^6.1.3", "babelify": "^6.1.3",
"base-url": "^1.0.0", "base-url": "^1.0.0",
"bootstrap": "3.3.5", "bootstrap": "3.3.5",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment