From 7f5aab96f08bc2e1d02297946faf4d0f542e302b Mon Sep 17 00:00:00 2001 From: Piotr Gawron Date: Fri, 14 Jul 2017 15:25:47 +0200 Subject: [PATCH 1/4] allow to create DualListbox from DOM element --- src/dual-listbox.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/dual-listbox.js b/src/dual-listbox.js index f1bdbba..f6f4e8a 100644 --- a/src/dual-listbox.js +++ b/src/dual-listbox.js @@ -19,7 +19,11 @@ const SELECTED_MODIFIER = 'dual-listbox__item--selected'; class DualListbox { constructor(selector, options={}) { this.setDefaults(); - this.select = document.querySelector(selector); + if (this.isElement(selector)) { + this.select = selector; + } else { + this.select = document.querySelector(selector); + } this.selected = []; this.available = []; @@ -439,6 +443,28 @@ class DualListbox { } } } + + /** + * @Private + * Returns true if argument is a DOM node + */ + isNode(o) { + return ( + typeof Node === "object" ? o instanceof Node : + o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string" + ); + } + + /** + * @Private + * Returns true if argument is a DOM element + */ + isElement(o) { + return ( + typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2 + o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string" + ); + } } export default DualListbox; From fc68c2e79b5f71e298c3de514e4bde06e2432734 Mon Sep 17 00:00:00 2001 From: Piotr Gawron Date: Fri, 14 Jul 2017 15:39:39 +0200 Subject: [PATCH 2/4] function renamed to reflect functionality --- src/dual-listbox.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/dual-listbox.js b/src/dual-listbox.js index f6f4e8a..05eb5d1 100644 --- a/src/dual-listbox.js +++ b/src/dual-listbox.js @@ -19,7 +19,7 @@ const SELECTED_MODIFIER = 'dual-listbox__item--selected'; class DualListbox { constructor(selector, options={}) { this.setDefaults(); - if (this.isElement(selector)) { + if (this.isDomElement(selector)) { this.select = selector; } else { this.select = document.querySelector(selector); @@ -444,22 +444,11 @@ class DualListbox { } } - /** - * @Private - * Returns true if argument is a DOM node - */ - isNode(o) { - return ( - typeof Node === "object" ? o instanceof Node : - o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string" - ); - } - /** * @Private * Returns true if argument is a DOM element */ - isElement(o) { + isDomElement(o) { return ( typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2 o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string" From 148be23b584d85ffa74d37de8989d102038fc916 Mon Sep 17 00:00:00 2001 From: Piotr Gawron Date: Fri, 14 Jul 2017 15:40:05 +0200 Subject: [PATCH 3/4] unit test for creation from DOM element added --- test/dual-listbox.spec.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/dual-listbox.spec.js b/test/dual-listbox.spec.js index 759d2e4..915a217 100644 --- a/test/dual-listbox.spec.js +++ b/test/dual-listbox.spec.js @@ -289,4 +289,13 @@ describe('Duallistbox', function() { expect(dlb.available.length).toBe(10); expect(dlb.selected.length).toBe(0); }); + + it('should be able to create object from DOM element', () => { + let domParent = document.createElement("div"); + domParent.innerHTML = FIXTURE_FILLED_SELECT; + let dlb = new DualListbox(domParent.getElementsByTagName('select')[0]); + + expect(dlb.available.length).toBe(10); + expect(dlb.selected.length).toBe(0); + }); }); From 22aba6c969cec687a595e95820dc6ef69eb06705 Mon Sep 17 00:00:00 2001 From: Jorik Kraaikamp Date: Mon, 17 Jul 2017 10:09:26 +0200 Subject: [PATCH 4/4] Moved the empty variable declarations to the top. --- src/dual-listbox.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dual-listbox.js b/src/dual-listbox.js index 05eb5d1..3c5738e 100644 --- a/src/dual-listbox.js +++ b/src/dual-listbox.js @@ -19,15 +19,15 @@ const SELECTED_MODIFIER = 'dual-listbox__item--selected'; class DualListbox { constructor(selector, options={}) { this.setDefaults(); + this.selected = []; + this.available = []; + if (this.isDomElement(selector)) { this.select = selector; } else { this.select = document.querySelector(selector); } - this.selected = []; - this.available = []; - this._initOptions(options); this._initReusableElements(); this._splitSelectOptions(this.select);