diff --git a/bower.json b/bower.json index b1533b4..aa2cf29 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "angular-shims-placeholder", "description": "Angular directive to emulate the `placeholder` attribute on text and password input fields for old browsers, such as IE9, IE8, and below", - "version": "0.4.3", + "version": "0.4.4", "homepage": "https://github.com/cvn/angular-shims-placeholder", "authors": [ { diff --git a/dist/angular-shims-placeholder.js b/dist/angular-shims-placeholder.js index 4840a26..4d1e2c8 100644 --- a/dist/angular-shims-placeholder.js +++ b/dist/angular-shims-placeholder.js @@ -1,19 +1,19 @@ -/*! angular-shims-placeholder - v0.4.3 - 2015-06-14 +/*! angular-shims-placeholder - v0.4.4 - 2015-06-18 * https://github.com/cvn/angular-shims-placeholder * Copyright (c) 2015 Chad von Nau; Licensed MIT */ (function(angular, document, undefined) { 'use strict'; angular.module('ng.shims.placeholder', []) -.service('placeholderSniffer', ["$document", function($document){ +.service('placeholderSniffer', ['$document', function($document){ this.emptyClassName = 'empty', this.hasPlaceholder = function() { // test for native placeholder support - var test = $document[0].createElement("input"); + var test = $document[0].createElement('input'); return (test.placeholder !== void 0); }; }]) -.directive('placeholder', ["$timeout", "$document", "$interpolate", "$injector", "placeholderSniffer", function($timeout, $document, $interpolate, $injector, placeholderSniffer) { +.directive('placeholder', ['$timeout', '$document', '$interpolate', '$injector', 'placeholderSniffer', function($timeout, $document, $interpolate, $injector, placeholderSniffer) { if (placeholderSniffer.hasPlaceholder()) return {}; var documentListenersApplied = false, @@ -67,8 +67,8 @@ angular.module('ng.shims.placeholder', []) // handler for model-less inputs to interact with non-angular code if (!ngModel) { - elem.bind('change', function () { - changePlaceholder($interpolate(elem.attr('placeholder') || '')(scope)); + elem.bind('change', function (event) { + changePlaceholder($interpolate(elem.attr('placeholder') || '')(scope), event); }); } @@ -82,7 +82,7 @@ angular.module('ng.shims.placeholder', []) // // TODO: remove when tab key behavior is fixed in // angular core - if (domElem === document.activeElement && !elem.val()) { + if (isActiveElement(domElem) && !elem.val()) { domElem.select(); } }; @@ -101,16 +101,16 @@ angular.module('ng.shims.placeholder', []) documentListenersApplied = true; } - function updateValue(e) { + function updateValue(event) { var val = elem.val(); // don't update from placeholder, helps debounce if (elem.hasClass(emptyClassName) && val && val === text) { return; } - conditionalDefer(function(){ setValue(val); }); + conditionalDefer(function(){ setValue(val); }, event); } - function conditionalDefer(callback) { + function conditionalDefer(callback, event) { // IE8/9: ngModel uses a keydown handler with deferrered // execution to check for changes to the input. this $timeout // prevents callback from firing before the keydown handler, @@ -119,7 +119,7 @@ angular.module('ng.shims.placeholder', []) // // TODO: remove this function when tab key behavior is fixed in // angular core - if (document.documentMode <= 11) { + if (document.documentMode <= 11 && event) { $timeout(callback, 0); } else { callback(); @@ -127,7 +127,7 @@ angular.module('ng.shims.placeholder', []) } function setValue(val) { - if (!val && val !== 0 && domElem !== document.activeElement) { + if (!val && val !== 0 && !isActiveElement(domElem)) { // show placeholder when necessary elem.addClass(emptyClassName); elem.val(!is_pwd ? text : ''); @@ -166,12 +166,22 @@ angular.module('ng.shims.placeholder', []) return val; } - function changePlaceholder(value) { + function changePlaceholder(value, event) { if (elem.hasClass(emptyClassName) && elem.val() === text) { elem.val(''); } text = value; - updateValue(); + updateValue(event); + } + + // IE9: getting activeElement in an iframe raises error + // http://tjvantoll.com/2013/08/30/bugs-with-document-activeelement-in-internet-explorer/ + function isActiveElement(elmn) { + var result = false; + try { + result = elmn === document.activeElement; + } catch (error) {} + return result; } function setAttrUnselectable(elmn, enable) { diff --git a/dist/angular-shims-placeholder.min.js b/dist/angular-shims-placeholder.min.js index dcb4664..05e103c 100644 --- a/dist/angular-shims-placeholder.min.js +++ b/dist/angular-shims-placeholder.min.js @@ -1,4 +1,4 @@ -/*! angular-shims-placeholder - v0.4.3 - 2015-06-14 +/*! angular-shims-placeholder - v0.4.4 - 2015-06-18 * https://github.com/cvn/angular-shims-placeholder * Copyright (c) 2015 Chad von Nau; Licensed MIT */ -!function(a,b,c){"use strict";a.module("ng.shims.placeholder",[]).service("placeholderSniffer",["$document",function(a){this.emptyClassName="empty",this.hasPlaceholder=function(){var b=a[0].createElement("input");return void 0!==b.placeholder}}]).directive("placeholder",["$timeout","$document","$interpolate","$injector","placeholderSniffer",function(c,d,e,f,g){if(g.hasPlaceholder())return{};var h=!1,i=parseFloat(a.version.full);try{var j=f.get("$animate")}catch(k){}return{restrict:"A",require:"?ngModel",priority:i>=1.2?110:-10,link:function(f,k,l,m){function n(a){var b=k.val();k.hasClass(M)&&b&&b===L||o(function(){p(b)})}function o(a){b.documentMode<=11?c(a,0):a()}function p(a){a||0===a||H===b.activeElement?(k.removeClass(M),k.val(a)):(k.addClass(M),k.val(K?"":L)),K&&(v(),j&&w())}function q(){return m?f.$eval(l.ngModel)||"":r()||""}function r(){var a=k.val();return a===l.placeholder&&(a=""),a}function s(a){k.hasClass(M)&&k.val()===L&&k.val(""),L=a,n()}function t(a,b){b?a.attr("unselectable","on"):a.removeAttr("unselectable")}function u(){F=a.element(''),y(),A(F),F.addClass(M).bind("focus",D),H.parentNode.insertBefore(F[0],H);for(var b=[l.ngDisabled,l.ngReadonly,l.ngRequired,l.ngShow,l.ngHide],c=0;c=1.3?j.addClass(k,"").then(v):j.addClass(k,"",v)}function x(){j?w():v()}function y(){F.val(L),F.attr("class",k.attr("class")||"").attr("style",k.attr("style")||"").prop("disabled",k.prop("disabled")).prop("readOnly",k.prop("readOnly")).prop("required",k.prop("required")),t(F,"on"===k.attr("unselectable"))}function z(a){i>=1.2?a.removeClass(N):a.css("display","")}function A(a){i>=1.2?a.addClass(N):a.css("display","none")}function B(){A(k),z(F)}function C(){A(F),z(k)}function D(){C(),H.focus()}function E(){var a="undefined"!=typeof l.ngShow,b="undefined"!=typeof l.ngHide;return a||b?a&&!f.$eval(l.ngShow)||b&&f.$eval(l.ngHide):!1}var F,G=q(),H=k[0],I=H.nodeName.toLowerCase(),J="input"===I||"textarea"===I,K="password"===l.type,L=l.placeholder||"",M=g.emptyClassName,N="ng-hide";J&&(l.$observe("placeholder",function(a){s(a)}),K&&u(),p(G),k.bind("focus",function(){k.hasClass(M)&&(k.val(""),k.removeClass(M),H.select())}),k.bind("blur",n),m||k.bind("change",function(){s(e(k.attr("placeholder")||"")(f))}),m&&(m.$render=function(){p(m.$viewValue),H!==b.activeElement||k.val()||H.select()}),h||(d.bind("selectstart",function(b){var c=a.element(b.target);c.hasClass(M)&&c.prop("disabled")&&b.preventDefault()}),h=!0))}}}])}(window.angular,window.document); \ No newline at end of file +!function(a,b,c){"use strict";a.module("ng.shims.placeholder",[]).service("placeholderSniffer",["$document",function(a){this.emptyClassName="empty",this.hasPlaceholder=function(){var b=a[0].createElement("input");return void 0!==b.placeholder}}]).directive("placeholder",["$timeout","$document","$interpolate","$injector","placeholderSniffer",function(c,d,e,f,g){if(g.hasPlaceholder())return{};var h=!1,i=parseFloat(a.version.full);try{var j=f.get("$animate")}catch(k){}return{restrict:"A",require:"?ngModel",priority:i>=1.2?110:-10,link:function(f,k,l,m){function n(a){var b=k.val();k.hasClass(N)&&b&&b===M||o(function(){p(b)},a)}function o(a,d){b.documentMode<=11&&d?c(a,0):a()}function p(a){a||0===a||t(I)?(k.removeClass(N),k.val(a)):(k.addClass(N),k.val(L?"":M)),L&&(w(),j&&x())}function q(){return m?f.$eval(l.ngModel)||"":r()||""}function r(){var a=k.val();return a===l.placeholder&&(a=""),a}function s(a,b){k.hasClass(N)&&k.val()===M&&k.val(""),M=a,n(b)}function t(a){var c=!1;try{c=a===b.activeElement}catch(d){}return c}function u(a,b){b?a.attr("unselectable","on"):a.removeAttr("unselectable")}function v(){G=a.element(''),z(),B(G),G.addClass(N).bind("focus",E),I.parentNode.insertBefore(G[0],I);for(var b=[l.ngDisabled,l.ngReadonly,l.ngRequired,l.ngShow,l.ngHide],c=0;c=1.3?j.addClass(k,"").then(w):j.addClass(k,"",w)}function y(){j?x():w()}function z(){G.val(M),G.attr("class",k.attr("class")||"").attr("style",k.attr("style")||"").prop("disabled",k.prop("disabled")).prop("readOnly",k.prop("readOnly")).prop("required",k.prop("required")),u(G,"on"===k.attr("unselectable"))}function A(a){i>=1.2?a.removeClass(O):a.css("display","")}function B(a){i>=1.2?a.addClass(O):a.css("display","none")}function C(){B(k),A(G)}function D(){B(G),A(k)}function E(){D(),I.focus()}function F(){var a="undefined"!=typeof l.ngShow,b="undefined"!=typeof l.ngHide;return a||b?a&&!f.$eval(l.ngShow)||b&&f.$eval(l.ngHide):!1}var G,H=q(),I=k[0],J=I.nodeName.toLowerCase(),K="input"===J||"textarea"===J,L="password"===l.type,M=l.placeholder||"",N=g.emptyClassName,O="ng-hide";K&&(l.$observe("placeholder",function(a){s(a)}),L&&v(),p(H),k.bind("focus",function(){k.hasClass(N)&&(k.val(""),k.removeClass(N),I.select())}),k.bind("blur",n),m||k.bind("change",function(a){s(e(k.attr("placeholder")||"")(f),a)}),m&&(m.$render=function(){p(m.$viewValue),t(I)&&!k.val()&&I.select()}),h||(d.bind("selectstart",function(b){var c=a.element(b.target);c.hasClass(N)&&c.prop("disabled")&&b.preventDefault()}),h=!0))}}}])}(window.angular,window.document); \ No newline at end of file diff --git a/package.json b/package.json index 12fd5b7..a7afd17 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "angular-shims-placeholder", "description": "Angular directive to emulate the `placeholder` attribute on text and password input fields for old browsers, such as IE9, IE8, and below", - "version": "0.4.3", + "version": "0.4.4", "homepage": "https://github.com/cvn/angular-shims-placeholder", "author": { "name": "Chad von Nau",