diff --git a/massautocomplete.js b/massautocomplete.js
index 4ea2627..025e63c 100644
--- a/massautocomplete.js
+++ b/massautocomplete.js
@@ -34,12 +34,10 @@ angular.module('MassAutoComplete', [])
// Position ac container given a target element
config.position_autocomplete = function(container, target) {
- var rect = target[0].getBoundingClientRect(),
- scrollTop = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset,
- scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft || window.pageXOffset;
+ var rect = target[0].getBoundingClientRect();
- container[0].style.top = rect.top + rect.height + scrollTop + 'px';
- container[0].style.left = rect.left + scrollLeft + 'px';
+ container[0].style.left = 0;
+ container[0].style.top = rect.height + 'px';
container[0].style.width = rect.width + 'px';
};
@@ -80,7 +78,7 @@ angular.module('MassAutoComplete', [])
scope.container = angular.element(element[0].getElementsByClassName('ac-container')[0]);
},
- controller: ['$scope', function($scope) {
+ controller: ['$scope', '$element', function($scope, $element) {
var that = this;
var bound_events = {};
@@ -427,6 +425,12 @@ angular.module('MassAutoComplete', [])
that.detach();
$scope.container.remove();
});
+
+ // Make element positionned if not already
+ if (['fixed', 'absolute', 'sticky', 'relative'].indexOf($element.css('position')) === -1) {
+ $element.css('position', 'relative');
+ }
+
}]
};
}])
diff --git a/massautocomplete.min.js b/massautocomplete.min.js
index 702b8d9..bb440ea 100644
--- a/massautocomplete.min.js
+++ b/massautocomplete.min.js
@@ -1 +1 @@
-!function(){"use strict";angular.module("MassAutoComplete",[]).provider("massAutocompleteConfig",function(){var e=this;e.KEYS={TAB:9,ESC:27,ENTER:13,UP:38,DOWN:40},e.EVENTS={KEYDOWN:"keydown",RESIZE:"resize",BLUR:"blur"},e.DEBOUNCE={position:150,attach:300,suggest:200,blur:150},e.generate_random_id=function(e){return e+"_"+Math.random().toString().substring(2)},e.position_autocomplete=function(e,t){var n=t[0].getBoundingClientRect(),o=document.body.scrollTop||document.documentElement.scrollTop||window.pageYOffset,i=document.body.scrollLeft||document.documentElement.scrollLeft||window.pageXOffset;e[0].style.top=n.top+n.height+o+"px",e[0].style.left=n.left+i+"px",e[0].style.width=n.width+"px"},this.$get=function(){return e}}).directive("massAutocomplete",["massAutocompleteConfig","$timeout","$window","$document","$q",function(e,t,n,o,i){return{restrict:"A",scope:{options:"&massAutocomplete"},transclude:!0,template:'
',link:function(e,t){e.container=angular.element(t[0].getElementsByClassName("ac-container")[0])},controller:["$scope",function(a){function c(){a.show_autocomplete=!0}function l(){a.show_autocomplete=!1,m()}function s(e,t,n){var o;return function(){var i=this,a=arguments,c=function(){o=null,n||e.apply(i,a)},l=n&&!o;clearTimeout(o),o=setTimeout(c,t),l&&e.apply(i,a)}}function u(t){return(!t.id||""===t.id)&&(t.id=e.generate_random_id("ac_element"),!0)}function r(){e.position_autocomplete(a.container,b)}function d(t,n){a.selected_index=0,a.waiting_for_suggestion=!0,"string"==typeof t&&t.length>0?i.when(N.suggest(t),function(o){b&&b===n&&(o&&o.length>0?(o.forEach(function(t){t.id||(t.id=e.generate_random_id("ac_item"))}),a.results=[{value:t,label:"",id:""}].concat(o),c(),N.auto_select_first&&f(1)):(a.results=[],l()))},function(e){l(),N.on_error&&N.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,l(),a.$apply())}function E(e,t,n){b!==t&&(b&&g.detach(),t[0]===o[0].activeElement&&(n.on_attach&&n.on_attach(),b=t,v=e,N=n,w=e.$viewValue,T=u(t),a.container[0].setAttribute("aria-labelledby",b.id),a.results=[],a.selected_index=-1,_(),S=a.$watch(function(){return e.$modelValue},function(e){e!==y&&(r(),A(e,b))})))}function p(e){v.$modelValue!==e&&(v.$setViewValue(e),v.$render())}function m(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant")}function f(e){var t=a.results[e];return b.val(t.value),a.selected_index=e,a.container[0].setAttribute("aria-activedescendant",t.id),t}function _(){angular.element(n).bind(e.EVENTS.RESIZE,x),h[e.EVENTS.BLUR]=function(){t(function(){b&&b[0]===o[0].activeElement||g.detach()},V.debounce_blur)},b.bind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),h[e.EVENTS.KEYDOWN]=function(t){if(!t.shiftKey)switch(t.keyCode){case e.KEYS.ESC:a.show_autocomplete?(l(),a.$apply()):b.val(w);break;case e.KEYS.ENTER:a.show_autocomplete&&a.selected_index>0&&!a.waiting_for_suggestion&&(a.apply_selection(a.selected_index),t.stopPropagation(),t.preventDefault()),l(),a.$apply();break;case e.KEYS.TAB:if(!a.show_autocomplete)break;t.preventDefault();case e.KEYS.DOWN:a.results.length>0&&(a.show_autocomplete?f(a.selected_index+1>a.results.length-1?0:a.selected_index+1):(c(),f(0)),a.$apply());break;case e.KEYS.UP:a.show_autocomplete&&(t.preventDefault(),f(a.selected_index-1>=0?a.selected_index-1:a.results.length-1),a.$apply())}},b.bind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN])}var g=this,h={};h[e.EVENTS.BLUR]=null,h[e.EVENTS.KEYDOWN]=null,h[e.EVENTS.RESIZE]=null;var b,v,N,w,S,y,T,$=a.options()||{},V={debounce_position:$.debounce_position||e.DEBOUNCE.position,debounce_attach:$.debounce_attach||e.DEBOUNCE.attach,debounce_suggest:$.debounce_suggest||e.DEBOUNCE.suggest,debounce_blur:$.debounce_blur||e.DEBOUNCE.blur};a.show_autocomplete=!1;var x=s(r,V.debounce_position),A=s(d,V.debounce_suggest);g.attach=s(E,V.debounce_attach),g.detach=function(){if(b){var t=b.val();p(t),N.on_detach&&N.on_detach(t),b.unbind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN]),b.unbind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),T&&b[0].removeAttribute("id")}l(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,h[e.EVENTS.RESIZE]),S&&S(),a.selected_index=a.results=void 0,v=b=w=void 0},a.apply_selection=function(e){if(b[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=f(e);y=t.value,p(t.value),l(),N.on_select&&N.on_select(t)}},a.$on("$destroy",function(){g.detach(),a.container.remove()})}]}}]).directive("massAutocompleteItem",function(){return{restrict:"A",require:["^massAutocomplete","ngModel"],scope:{massAutocompleteItem:"&"},link:function(e,t,n,o){n.$set("autocomplete","off");var i=o[0],a=o[1];t.bind("focus",function(){var n=e.massAutocompleteItem();if(!n)throw new Error("Invalid options");i.attach(a,t,n)})}}})}();
\ No newline at end of file
+!function(){"use strict";angular.module("MassAutoComplete",[]).provider("massAutocompleteConfig",function(){var e=this;e.KEYS={TAB:9,ESC:27,ENTER:13,UP:38,DOWN:40},e.EVENTS={KEYDOWN:"keydown",RESIZE:"resize",BLUR:"blur"},e.DEBOUNCE={position:150,attach:300,suggest:200,blur:150},e.generate_random_id=function(e){return e+"_"+Math.random().toString().substring(2)},e.position_autocomplete=function(e,t){var n=t[0].getBoundingClientRect();e[0].style.left=0,e[0].style.top=n.height+"px",e[0].style.width=n.width+"px"},this.$get=function(){return e}}).directive("massAutocomplete",["massAutocompleteConfig","$timeout","$window","$document","$q",function(e,t,n,o,i){return{restrict:"A",scope:{options:"&massAutocomplete"},transclude:!0,template:'',link:function(e,t){e.container=angular.element(t[0].getElementsByClassName("ac-container")[0])},controller:["$scope","$element",function(a,s){function c(){a.show_autocomplete=!0}function l(){a.show_autocomplete=!1,f()}function u(e,t,n){var o;return function(){var i=this,a=arguments,s=function(){o=null,n||e.apply(i,a)},c=n&&!o;clearTimeout(o),o=setTimeout(s,t),c&&e.apply(i,a)}}function r(t){return(!t.id||""===t.id)&&(t.id=e.generate_random_id("ac_element"),!0)}function d(){e.position_autocomplete(a.container,v)}function E(t,n){a.selected_index=0,a.waiting_for_suggestion=!0,"string"==typeof t&&t.length>0?i.when(S.suggest(t),function(o){v&&v===n&&(o&&o.length>0?(o.forEach(function(t){t.id||(t.id=e.generate_random_id("ac_item"))}),a.results=[{value:t,label:"",id:""}].concat(o),c(),S.auto_select_first&&m(1)):(a.results=[],l()))},function(e){l(),S.on_error&&S.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,l(),a.$apply())}function p(e,t,n){v!==t&&(v&&h.detach(),t[0]===o[0].activeElement&&(n.on_attach&&n.on_attach(),v=t,N=e,S=n,w=e.$viewValue,T=r(t),a.container[0].setAttribute("aria-labelledby",v.id),a.results=[],a.selected_index=-1,g(),y=a.$watch(function(){return e.$modelValue},function(e){e!==$&&(d(),D(e,v))})))}function _(e){N.$modelValue!==e&&(N.$setViewValue(e),N.$render())}function f(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant")}function m(e){var t=a.results[e];return v.val(t.value),a.selected_index=e,a.container[0].setAttribute("aria-activedescendant",t.id),t}function g(){angular.element(n).bind(e.EVENTS.RESIZE,A),b[e.EVENTS.BLUR]=function(){t(function(){v&&v[0]===o[0].activeElement||h.detach()},x.debounce_blur)},v.bind(e.EVENTS.BLUR,b[e.EVENTS.BLUR]),b[e.EVENTS.KEYDOWN]=function(t){if(!t.shiftKey)switch(t.keyCode){case e.KEYS.ESC:a.show_autocomplete?(l(),a.$apply()):v.val(w);break;case e.KEYS.ENTER:a.show_autocomplete&&a.selected_index>0&&!a.waiting_for_suggestion&&(a.apply_selection(a.selected_index),t.stopPropagation(),t.preventDefault()),l(),a.$apply();break;case e.KEYS.TAB:if(!a.show_autocomplete)break;t.preventDefault();case e.KEYS.DOWN:a.results.length>0&&(a.show_autocomplete?m(a.selected_index+1>a.results.length-1?0:a.selected_index+1):(c(),m(0)),a.$apply());break;case e.KEYS.UP:a.show_autocomplete&&(t.preventDefault(),m(a.selected_index-1>=0?a.selected_index-1:a.results.length-1),a.$apply())}},v.bind(e.EVENTS.KEYDOWN,b[e.EVENTS.KEYDOWN])}var h=this,b={};b[e.EVENTS.BLUR]=null,b[e.EVENTS.KEYDOWN]=null,b[e.EVENTS.RESIZE]=null;var v,N,S,w,y,$,T,V=a.options()||{},x={debounce_position:V.debounce_position||e.DEBOUNCE.position,debounce_attach:V.debounce_attach||e.DEBOUNCE.attach,debounce_suggest:V.debounce_suggest||e.DEBOUNCE.suggest,debounce_blur:V.debounce_blur||e.DEBOUNCE.blur};a.show_autocomplete=!1;var A=u(d,x.debounce_position),D=u(E,x.debounce_suggest);h.attach=u(p,x.debounce_attach),h.detach=function(){if(v){var t=v.val();_(t),S.on_detach&&S.on_detach(t),v.unbind(e.EVENTS.KEYDOWN,b[e.EVENTS.KEYDOWN]),v.unbind(e.EVENTS.BLUR,b[e.EVENTS.BLUR]),T&&v[0].removeAttribute("id")}l(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,b[e.EVENTS.RESIZE]),y&&y(),a.selected_index=a.results=void 0,N=v=w=void 0},a.apply_selection=function(e){if(v[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=m(e);$=t.value,_(t.value),l(),S.on_select&&S.on_select(t)}},a.$on("$destroy",function(){h.detach(),a.container.remove()}),["fixed","absolute","sticky","relative"].indexOf(s.css("position"))===-1&&s.css("position","relative")}]}}]).directive("massAutocompleteItem",function(){return{restrict:"A",require:["^massAutocomplete","ngModel"],scope:{massAutocompleteItem:"&"},link:function(e,t,n,o){n.$set("autocomplete","off");var i=o[0],a=o[1];t.bind("focus",function(){var n=e.massAutocompleteItem();if(!n)throw new Error("Invalid options");i.attach(a,t,n)})}}})}();
\ No newline at end of file