Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
new method .destroy(), updated changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jul 22, 2017
1 parent 2e7841d commit 96843e8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions demo/script.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -17,3 +16,5 @@ const suggestions = new Suggestions(
suggestions.on('select', value => {
console.log(value);
});

setTimeout(() => suggestions.destroy(), 5000);
17 changes: 13 additions & 4 deletions src/DatalistSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ 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;
}

super(settings);

this.input = input;
this.listId = input.getAttribute('list');

input.removeAttribute('list');

this.load(getAvailableOptions(listElement));
}

Expand Down Expand Up @@ -55,6 +59,11 @@ export default class DatalistSource extends Source {
this.close();
}
}

destroy() {
this.input.setAttribute('list', this.listId);
super.destroy();
}
}

function getAvailableOptions(element) {
Expand Down
4 changes: 4 additions & 0 deletions src/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ export default class Source {
}
});
}

destroy() {
d.remove(this.element);
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 96843e8

Please sign in to comment.