diff --git a/CHANGELOG.md b/CHANGELOG.md index 0948a8b..282230a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 0.3.0 - 2017-07-22 + +### Added + +* New method `.destroy()` to unbind all events and restore the input to the previous state. + +### Changed + +* The escape key not only closes the list of suggestions but also restore the previous value +* Selected any element on mouseover (#1) + +### Fixed + +* Fixed scroll behaviour (#2) +* Switch from `fetch` to `XMLHttpRequest` to fix cors problems in firefox + browsersync (#3) + ## 0.2.0 - 2017-07-03 ### Added diff --git a/demo/script.js b/demo/script.js index 6d2915e..21e8f5f 100644 --- a/demo/script.js +++ b/demo/script.js @@ -1,6 +1,5 @@ -import { Suggestions, Source, AjaxSource, DatalistSource } from '../src'; +import { Suggestions, DatalistSource } from '../src'; -//Datalist const datalistInput = document.getElementById('input-datalist'); const suggestions = new Suggestions( @@ -17,3 +16,5 @@ const suggestions = new Suggestions( suggestions.on('select', value => { console.log(value); }); + +setTimeout(() => suggestions.destroy(), 5000); \ No newline at end of file diff --git a/src/DatalistSource.js b/src/DatalistSource.js index 77c7b0c..2a849a4 100644 --- a/src/DatalistSource.js +++ b/src/DatalistSource.js @@ -4,11 +4,10 @@ import Suggestion from './Suggestion.js'; import Group from './Group.js'; export default class DatalistSource extends Source { - constructor(element, settings = {}) { - const listElement = element.ownerDocument.getElementById( - element.getAttribute('list') + constructor(input, settings = {}) { + const listElement = input.ownerDocument.getElementById( + input.getAttribute('list') ); - element.removeAttribute('list'); if (!settings.parent) { settings.parent = listElement.parentElement; @@ -16,6 +15,11 @@ export default class DatalistSource extends Source { super(settings); + this.input = input; + this.listId = input.getAttribute('list'); + + input.removeAttribute('list'); + this.load(getAvailableOptions(listElement)); } @@ -55,6 +59,11 @@ export default class DatalistSource extends Source { this.close(); } } + + destroy() { + this.input.setAttribute('list', this.listId); + super.destroy(); + } } function getAvailableOptions(element) { diff --git a/src/Source.js b/src/Source.js index 1d85d34..ed679df 100644 --- a/src/Source.js +++ b/src/Source.js @@ -149,4 +149,8 @@ export default class Source { } }); } + + destroy() { + d.remove(this.element); + } } diff --git a/src/index.js b/src/index.js index 695f4e3..4081175 100644 --- a/src/index.js +++ b/src/index.js @@ -104,4 +104,9 @@ export class Suggestions { this.events[event].forEach(callback => callback.apply(this, args)); } } + + destroy() { + d.off('input focus keydown', this.element); + this.source.destroy(); + } }