Commit 9081d3ef authored by Clement Ho's avatar Clement Ho

Update droplab

parent cb33856e
...@@ -51,13 +51,15 @@ Object.assign(DropDown.prototype, { ...@@ -51,13 +51,15 @@ Object.assign(DropDown.prototype, {
var self = this; var self = this;
// event delegation. // event delegation.
this.list.addEventListener('click', function(e) { this.list.addEventListener('click', function(e) {
if(e.target.tagName === 'A' || e.target.tagName === 'button') { // climb up the tree to find the LI
var selected = utils.closest(e.target, 'LI');
if(selected) {
e.preventDefault(); e.preventDefault();
self.hide(); self.hide();
var listEvent = new CustomEvent('click.dl', { var listEvent = new CustomEvent('click.dl', {
detail: { detail: {
list: self, list: self,
selected: e.target, selected: selected,
data: e.target.dataset, data: e.target.dataset,
}, },
}); });
...@@ -102,6 +104,15 @@ Object.assign(DropDown.prototype, { ...@@ -102,6 +104,15 @@ Object.assign(DropDown.prototype, {
var html = utils.t(sampleItem.outerHTML, dat); var html = utils.t(sampleItem.outerHTML, dat);
var template = document.createElement('template'); var template = document.createElement('template');
template.innerHTML = html; template.innerHTML = html;
// Help set the image src template
var imageTags = template.content.querySelectorAll('img[data-src]');
for(var i = 0; i < imageTags.length; i++) {
var imageTag = imagetags[i];
imageTag.src = imageTag.getAttribute('data-src');
imageTag.removeAttribute('data-src');
}
if(dat.hasOwnProperty('droplab_hidden') && dat.droplab_hidden){ if(dat.hasOwnProperty('droplab_hidden') && dat.droplab_hidden){
template.content.firstChild.style.display = 'none' template.content.firstChild.style.display = 'none'
}else{ }else{
...@@ -115,6 +126,9 @@ Object.assign(DropDown.prototype, { ...@@ -115,6 +126,9 @@ Object.assign(DropDown.prototype, {
} else { } else {
this.list.innerHTML = newChildren.join(''); this.list.innerHTML = newChildren.join('');
} }
// Show dropdown if there is data
data !== [] ? this.show() : this.hide();
}, },
show: function() { show: function() {
...@@ -221,6 +235,7 @@ require('./window')(function(w){ ...@@ -221,6 +235,7 @@ require('./window')(function(w){
// Restore initial State // Restore initial State
hook.list.list.innerHTML = hook.list.initialState; hook.list.list.innerHTML = hook.list.initialState;
hook.list.hide(); hook.list.hide();
hook.trigger.removeEventListener('mousedown', hook.events.mousedown); hook.trigger.removeEventListener('mousedown', hook.events.mousedown);
hook.trigger.removeEventListener('input', hook.events.input); hook.trigger.removeEventListener('input', hook.events.input);
hook.trigger.removeEventListener('keyup', hook.events.keyup); hook.trigger.removeEventListener('keyup', hook.events.keyup);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
/* global droplab */ /* global droplab */
droplab.plugin(function init(DropLab) { droplab.plugin(function init(DropLab) {
var _addData = DropLab.prototype.addData; var _addData = DropLab.prototype.addData;
var _setData = DropLab.prototype.setData;
var _loadUrlData = function(url) { var _loadUrlData = function(url) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
...@@ -24,10 +25,16 @@ droplab.plugin(function init(DropLab) { ...@@ -24,10 +25,16 @@ droplab.plugin(function init(DropLab) {
Object.assign(DropLab.prototype, { Object.assign(DropLab.prototype, {
addData: function(trigger, data) { addData: function(trigger, data) {
this.processData(trigger, data, _addData);
},
setData: function(trigger, data) {
this.processData(trigger, data, _setData);
},
processData: function(trigger, data, methodName) {
var _this = this; var _this = this;
if('string' === typeof data) { if('string' === typeof data) {
_loadUrlData(data).then(function(d) { _loadUrlData(data).then(function(d) {
_addData.call(_this, trigger, d); methodName.call(_this, trigger, d);
}).catch(function(e) { }).catch(function(e) {
if(e.message) if(e.message)
console.error(e.message, e.stack); // eslint-disable-line no-console console.error(e.message, e.stack); // eslint-disable-line no-console
...@@ -35,7 +42,7 @@ droplab.plugin(function init(DropLab) { ...@@ -35,7 +42,7 @@ droplab.plugin(function init(DropLab) {
console.error(e); // eslint-disable-line no-console console.error(e); // eslint-disable-line no-console
}) })
} else { } else {
_addData.apply(this, arguments); methodName.apply(this, arguments);
} }
}, },
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment