Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Fixes #10 and resolves #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobius1 committed Mar 30, 2017
1 parent 70168f2 commit 2a25cf4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ Supports most modern browsers including IE9 and above.

## Change Log

### v2.1.2

* New option `closeOnScroll` ([#11](https://github.com/Mobius1/Selectr/issues/11)) (see [docs](http://mobius.ovh/docs/selectr/pages/options))
* Fixed bug with checking dropdown is on-screen ([#10](https://github.com/Mobius1/Selectr/issues/10))
* Fixed navigation bug ([70168f2](/Mobius1/Selectr/commit/70168f2df967881818116a81ac4edbaa98588381))

### v2.1.1

* new option `allowDeselect` (see [docs](http://mobius.ovh/docs/selectr/pages/options))
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobius1-selectr",
"version": "2.1.1",
"version": "2.1.2",
"ignore": [
".gitattributes",
"README.md"
Expand Down
4 changes: 2 additions & 2 deletions dist/selectr.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobius1-selectr",
"version": "2.1.1",
"version": "2.1.2",
"description": "A lightweight dependency-free javascript select box replacement.",
"main": "dist/selectr.min.js",
"scripts": {
Expand Down
60 changes: 34 additions & 26 deletions src/selectr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Selectr 2.1.1
* Selectr 2.1.2
* http://mobius.ovh/docs/selectr
*
* Released under the MIT license
Expand Down Expand Up @@ -99,13 +99,11 @@
if (h){ a.apply(e, f); }
};
},
getBoundingRect: function(el) {
getBoundingRect: function(el, abs) {
var win = window;
var doc = document;
var body = doc.body;
var rect = el.getBoundingClientRect();
var offsetX = win.pageXOffset !== undefined ? win.pageXOffset : (doc.documentElement || body.parentNode || body).scrollLeft;
var offsetY = win.pageYOffset !== undefined ? win.pageYOffset : (doc.documentElement || body.parentNode || body).scrollTop;
var offsetX = abs ? win.pageXOffset : 0;
var offsetY = abs ? win.pageYOffset : 0;

return {
bottom: rect.bottom + offsetY,
Expand Down Expand Up @@ -463,12 +461,14 @@
util.on(document, "keyup", this.events.keyup);

_.update = util.debounce(function() {
if (_.opened) {
// Optionally close dropdown on scroll / resize (#11)
if (_.opened && _.settings.closeOnScroll) {
_.close();
}
if ( this.width ) {
this.container.style.width = this.width;
}
checkInvert.call(_);
}, 50);

util.on(window, "resize", _.update);
Expand Down Expand Up @@ -796,7 +796,7 @@
} else {
var nextBottom = nextRect.top + nextRect.height;
currentOffset = offset + _.optsRect.height;
nextOffset = optsTop + nextBottom - currentOffset;
nextOffset = optsTop + (nextBottom - currentOffset);

if (_.activeIdx === 0) {
_.optsOptions.scrollTop = 0;
Expand Down Expand Up @@ -845,6 +845,24 @@
}
};

// Make sure dropdown stays on screen
var checkInvert = function() {
var s = util.getBoundingRect(this.selected);
var o = this.optsOptions.parentNode.offsetHeight;
var wh = window.innerHeight;
var doInvert = s.top + s.height + o > wh;

if (doInvert) {
util.addClass(this.container, "inverted");
this.isInverted = true;
} else {
util.removeClass(this.container, "inverted");
this.isInverted = false;
}

this.optsRect = util.getBoundingRect(this.optsOptions);
};

var dismiss = function(e) {
var target = e.target;
if (!this.container.contains(target) && (this.opened || this.container.classList.contains("notice"))) {
Expand Down Expand Up @@ -899,7 +917,8 @@
searchable: true,
clearable: false,
sortSelected: false,
allowDeselect: false
allowDeselect: false,
closeOnScroll: false
};

/**
Expand Down Expand Up @@ -968,14 +987,15 @@

this.activeIdx = 0;
this.navigating = false;
this.elRect = this.el.getBoundingClientRect();

Emitter.mixin(this);

build.call(this);

this.update();

this.optsRect = util.getBoundingRect(this.optsOptions);

this.rendered = true;

var _ = this;
Expand Down Expand Up @@ -1033,9 +1053,9 @@
value = value.toString().trim();
}

// Can"t pass array to select-one
// Can't pass array to select-one
if ( !_.el.multiple && isArray ) {
return;
return false;
}

util.each(this.el.options, function(i, opt) {
Expand Down Expand Up @@ -1231,26 +1251,16 @@

util.addClass(_.container, "open");

_.optsRect = util.getBoundingRect(_.optsOptions);
checkInvert.call(_);

var wh = window.innerHeight;
var scrollHeight = _.optsOptions.scrollHeight;
var doInvert = _.elRect.top + _.elRect.height + _.optsRect.height > wh;
var scrollHeight = this.optsOptions.scrollHeight;

if ( scrollHeight <= _.optsRect.height ) {
if ( _.requiresPagination ) {
paginate.call(_);
}
}

if (doInvert) {
util.addClass(_.container, "inverted");
this.isInverted = true;
} else {
util.removeClass(_.container, "inverted");
this.isInverted = false;
}

util.removeClass(_.container, "notice");

if (_.settings.searchable && !_.settings.taggable) {
Expand All @@ -1261,8 +1271,6 @@
}, 10);
}

_.optsRect = util.getBoundingRect(_.optsOptions);

if ( !this.opened ) {
_.emit("selectr.open");
}
Expand Down

0 comments on commit 2a25cf4

Please sign in to comment.