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

Commit

Permalink
upgrade d_js
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 5, 2017
1 parent 6ec33bb commit 151111d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"webpack": "^3.0.0"
},
"dependencies": {
"d_js": "^1.10.0"
"d_js": "^2.0.0"
}
}
1 change: 0 additions & 1 deletion src/AjaxSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import d from 'd_js';
import Source from './Source.js';

export default class AjaxSource extends Source {
Expand Down
9 changes: 7 additions & 2 deletions src/DatalistSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class DatalistSource extends Source {
parent.element.appendChild(suggestion.element);
this.result.push(suggestion);
} else if (suggestion.element.parentElement === parent.element) {
parent.element.removeChild(suggestion.element);
suggestion.element.remove();
}
});

Expand All @@ -70,9 +70,14 @@ function getAvailableOptions(element) {
const data = [];

d.getAll({ optgroup: element }).forEach(optgroup => {
const options = [];
d
.getAll({ option: optgroup })
.forEach(option => options.push(createItem(option)));

data.push({
label: optgroup.label,
options: d.getAll({ option: optgroup }).map(createItem)
options: options
});
});

Expand Down
17 changes: 7 additions & 10 deletions src/Group.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import d from 'd_js';

export default class Group {
constructor(data, settings = {}) {
this.data = data;
this.label = settings.label ? data[settings.label] : data.label;

this.element = d.parse('<ul></ul>');
this.wrapperElement = d.parse(
`<li><strong>${this.label}</strong></li>`
);
this.wrapperElement.appendChild(this.element);
this.element = document.createElement('ul');
this.wrapperElement = document.createElement('li');
this.wrapperElement.innerHTML = `<strong>${this.label}</strong>`;
this.wrapperElement.append(this.element);
}

load(data) {
Expand All @@ -18,7 +15,7 @@ export default class Group {
}

append(child) {
this.element.appendChild(child);
this.element.append(child);
}

refresh(parent, query, selected) {
Expand All @@ -30,10 +27,10 @@ export default class Group {

if (ul.childElementCount) {
if (element.parentElement !== parent) {
parent.appendChild(element);
parent.append(element);
}
} else if (element.parentElement === parent) {
parent.removeChild(element);
element.remove();
}
}
}
14 changes: 6 additions & 8 deletions src/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export default class Source {
this.result = [];
this.current = 0;

this.element = d.parse('<ul></ul>');
(this.settings.parent || document.body).appendChild(this.element);
this.element = document.createElement('ul');
(this.settings.parent || document.body).append(this.element);

var self = this;

d.delegate('mouseenter', this.element, 'li', function(e, target) {
self.selectByElement(target);
d.delegate('mouseenter', this.element, 'li', (e, target) => {
this.selectByElement(target);
});
}

Expand Down Expand Up @@ -142,7 +140,7 @@ export default class Source {
} else if (
suggestion.wrapperElement.parentElement === this.element
) {
this.element.removeChild(suggestion.wrapperElement);
suggestion.wrapperElement.remove();
}
} else {
callback(suggestion, this);
Expand All @@ -151,6 +149,6 @@ export default class Source {
}

destroy() {
d.remove(this.element);
this.element.remove();
}
}
16 changes: 7 additions & 9 deletions src/Suggestion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import d from 'd_js';

export default class Suggestion {
constructor(data, settings = {}, group) {
this.data = data;
Expand All @@ -10,29 +8,29 @@ export default class Suggestion {
: data.label || this.value;

//Render
this.element = document.createElement('li');

if (typeof settings.render === 'function') {
this.element = d.parse(`<li>${settings.render(this)}</li>`);
this.element.innerHTML = settings.render(this);
} else {
this.element = d.parse(`<li>${this.data.label || this.value}</li>`);
this.element.innerHTML = this.data.label || this.value;
}
}

refresh(parent, query, selected) {
if (this.match(query)) {
parent.appendChild(this.element);
parent.append(this.element);
this.unselect();
selected.push(this);
} else {
if (this.element.parentElement === parent) {
parent.removeChild(this.element);
this.element.remove();
}
}
}

detach() {
if (this.element.parentElement) {
this.element.parentElement.removeChild(this.element);
}
this.element.remove();
}

scroll(parent, scrollGroup) {
Expand Down

0 comments on commit 151111d

Please sign in to comment.