From 7d26b83ee956dfd7d1549733abe8a3d7635208eb Mon Sep 17 00:00:00 2001 From: serapath Date: Thu, 31 Aug 2017 15:41:47 +0200 Subject: [PATCH 1/2] ADD dropdown menu component --- src/app/ui/dropdown.js | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/app/ui/dropdown.js diff --git a/src/app/ui/dropdown.js b/src/app/ui/dropdown.js new file mode 100644 index 0000000000..4bd6b42b8f --- /dev/null +++ b/src/app/ui/dropdown.js @@ -0,0 +1,141 @@ +var yo = require('yo-yo') +var csjs = require('csjs-inject') +var EventManager = require('ethereum-remix').lib.EventManager + +var css = csjs` + .dropdown { + position : relative; + display : flex; + flex-direction : column; + color : black; + } + .selectbox { + display : flex; + align-items : center; + background-color : lightgrey; + border-radius : 5px; + margin : 3px; + cursor : pointer; + } + .selected { + display : inline-block; + min-width : 30ch; + max-width : 30ch; + white-space : nowrap; + text-overflow : ellipsis; + overflow : hidden; + } + .icon { + padding : 0px 5px; + } + .options { + position : absolute; + display : flex; + flex-direction : column; + min-width : 30ch; + max-width : 30ch; + top : 21px; + left : 10px; + background-color : white; + border : 1px solid black; + } + .option { + margin: 0; + } +` +/* USAGE: + + var dropdown = new Dropdown({ + options: [ + 'knownTransaction', + 'unknownTransaction', + 'log', + 'info', + 'error' + ], + defaults: ['knownTransaction'] + }) + dropdown.event.register('deselect', function (label) { }) + dropdown.event.register('select', function (label) { }) + +*/ +class Dropdown { + constructor (opts = {}) { + var self = this + self.event = new EventManager() + self.data = { + _options: opts.options || ['script'], + selected: opts.defaults || [] + } + if (self.data._options.indexOf('script') !== -1) { + if (!opts.defaults) self.data.selected.push('script') + } + self._view = {} + self._api = opts.api + self._events = opts.events + } + render () { + var self = this + if (self._view.el) return self._view.el + self._view.selected = yo` +
+ [${self.data.selected.length}] ${self.data.selected.join(', ')} + +
+ ` + self._view.el = yo` +
+ ${self._view.selected} + +
+ ` + return self._view.el + function emit (event) { + var input = event.currentTarget + var label = input.nextSibling.innerText + if (input.checked) { + self.data.selected.push(label) + self.event.trigger('select', [label]) + } else { + var idx = self.data.selected.indexOf(label) + self.data.selected.splice(idx, 1) + self.event.trigger('deselect', [label]) + } + self._view.selected.children[0].innerText = `[${self.data.selected.length}] ${self.data.selected.join(', ')}` + } + function show (event) { + event.stopPropagation() + var options = event.currentTarget.children[1] + var parent = event.target.parentElement + var isOption = parent === options || parent.parentElement === options + if (isOption) return + if (options.style.display === 'none') { + options.style.display = '' + document.body.addEventListener('click', handler) + } else { + options.style.display = 'none' + document.body.removeEventListener('click', handler) + } + function handler (event) { + options.style.display = 'none' + document.body.removeEventListener('click', handler) + } + } + } +} + +module.exports = Dropdown From 0515e3020df4e6de8150dfa9064335c95e8eb43b Mon Sep 17 00:00:00 2001 From: serapath Date: Thu, 31 Aug 2017 17:59:39 +0200 Subject: [PATCH 2/2] remove default options from dropdown --- src/app/ui/dropdown.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/app/ui/dropdown.js b/src/app/ui/dropdown.js index 4bd6b42b8f..60567adf0d 100644 --- a/src/app/ui/dropdown.js +++ b/src/app/ui/dropdown.js @@ -64,12 +64,9 @@ class Dropdown { var self = this self.event = new EventManager() self.data = { - _options: opts.options || ['script'], + _options: opts.options || [], selected: opts.defaults || [] } - if (self.data._options.indexOf('script') !== -1) { - if (!opts.defaults) self.data.selected.push('script') - } self._view = {} self._api = opts.api self._events = opts.events