widgets: new menu widget, fresh implementation (#468)

This commit is contained in:
bordun 2020-01-23 23:53:44 +01:00
parent 5487b3ff39
commit 201c8780cc
6 changed files with 313 additions and 2 deletions

View file

@ -2,6 +2,8 @@
## 0.48.9
- widgets:
- **new** `menu` widget, circular alternative to the dropdown & switch widgets
- editor:
- convert invalid quote characters automatically to avoid errors

View file

@ -0,0 +1,184 @@
var Widget = require('../common/widget'),
{iconify} = require('../../ui/utils'),
html = require('nanohtml'),
raw = require('nanohtml/raw')
class Menu extends Widget {
static defaults() {
return super.defaults({
_menu:'menu',
size: {type: 'number', value: 200},
values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [
'`Array` of possible values to switch between : `[1,2,3]`',
'`Object` of label:value pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
]},
weights: {type: 'array', value: '', help: [
'`Array` of `number` defining the weights of each value in `values`'
]}
})
}
constructor(options) {
super({...options, html: html`<div class="toggle"></div>`})
this.menu = this.widget.appendChild(html`<div class="menu"></div>`)
this.opened = false
this.values = []
this.selected = -1
this.value = undefined
this.parseValues()
this.parentScroll = [0,0]
this.opened = false
this.on('draginit',(e)=>{
if (this.opened) return
this.open(e)
}, {element: this.widget})
this.on('drag',(e)=>{
if (e.target === this.menu) {
DOM.each(this.menu, '.active', (el)=>{el.classList.remove('active')})
this.selected = -1
} else if (!e.target.classList.contains('active')) {
DOM.each(this.menu, '.active', (el)=>{el.classList.remove('active')})
e.target.classList.add('active')
this.selected = DOM.index(e.target)
}
}, {element: this.widget})
this.on('dragend',()=>{
if (!this.opened) return
this.close()
if (this.selected > -1) {
this.setValue(this.values[this.selected], {send: true, sync: true})
}
}, {element: this.widget})
this.container.style.setProperty('--size', parseInt(this.getProp('size')) + 'rem')
}
open(e) {
this.opened = true
var off = this.scrollOffset()
this.menu.style.setProperty('--x', (e.offsetX - off[0]) / PXSCALE + 'rem')
this.menu.style.setProperty('--y', (e.offsetY - off[1]) / PXSCALE + 'rem')
this.menu.classList.add('show')
}
close() {
this.opened = false
this.menu.classList.remove('show')
DOM.each(this.menu, '.active', (el)=>{el.classList.remove('active')})
}
parseValues() {
var nval = 0,
i = 0,
values = this.getProp('values'),
weights = this.getProp('weights'),
totalWeight
if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) {
values = values !== '' ? [values] : []
}
this.values = []
this.menu.innerHTML = ''
nval = Array.isArray(values) ? values.length : Object.keys(values).length
weights = Array.isArray(weights) ? weights.slice(0, nval) : Array(nval).fill(1)
if (weights.length < nval) weights = weights.concat(Array(nval - weights.length).fill(1))
totalWeight = weights.reduce((a, b) => a + b, 0)
var ac = 0
for (let k in values) {
let angle = Math.min(360 * weights[i] / totalWeight, 120),
skew = 90 - angle
this.values.push(values[k])
this.menu.appendChild(html`
<div class="item" style="transform: rotate(${ac}deg) skew(${skew}deg)">
<div style="transform: skew(${-skew}deg) rotate(${-90 + angle / 2}deg)"><span style="transform: rotate(${-ac + 90 - angle / 2}deg)">${raw(iconify(parseFloat(k) != k ? k : values[k]))}</span></div>
</div>
`)
i++
ac+=angle
}
}
scrollOffset() {
var parent = this.parent,
scrollOffset = [0,0]
while (parent && parent.props && parent.getProp('type') !== 'tab') {
scrollOffset[0] += parent.widget.scrollLeft
scrollOffset[1] += parent.widget.scrollTop
parent = parent.parent
}
return scrollOffset
}
setValue(v,options={}) {
var i = this.values.indexOf(v)
if (i > -1) this.value = this.values[i]
DOM.each(this.menu, '.on', (el)=>{el.classList.remove('on')})
if (i > -1) DOM.get(this.menu, '.item')[i].classList.add('on')
if (options.send) this.sendValue()
if (options.sync) this.changed(options)
}
onPropChanged(propName, options, oldPropValue) {
var ret = super.onPropChanged(...arguments)
switch (propName) {
case 'values':
this.parseValues()
this.setValue(this.value)
return
}
return ret
}
}
Menu.dynamicProps = Menu.prototype.constructor.dynamicProps.concat(
'values'
)
module.exports = Menu

View file

@ -14,6 +14,7 @@ module.exports.widgets = {
push: require('./buttons/push'),
switch: require('./buttons/switch'),
dropdown: require('./buttons/dropdown'),
menu: require('./buttons/menu'),
// pads
xy: require('./pads/xy'),
@ -61,7 +62,7 @@ module.exports.widgets = {
module.exports.categories = {
'Sliders':['fader','knob', 'encoder', 'range'],
'Buttons':['toggle','push','switch', 'dropdown'],
'Buttons':['toggle','push','switch', 'dropdown', 'menu'],
'Pads':['xy','rgb','multixy'],
'Matrices':['matrix', 'keyboard', 'patchbay'],
'Plots':['plot','eq','visualizer','led', 'rgbled','meter','text', 'image', 'svg', 'html', 'frame'],

View file

@ -98,6 +98,7 @@
@import 'widgets/toggle';
@import 'widgets/push';
@import 'widgets/switch';
@import 'widgets/menu';
@import 'widgets/panel';

123
src/scss/widgets/menu.scss Normal file
View file

@ -0,0 +1,123 @@
@keyframes menu-widget-fade {
0%{opacity: 0; transform: scale(0.9);}
100%{opacity: 1; transform: scale(1);}
}
.menu-container {
@extend .modal-container;
border-radius: 0!important; // border radius breaks it somehow
z-index: initial!important;
--size:300rem;
.menu {
display:none;
&.show {
display:block;
animation: menu-widget-fade .1s forwards;
}
width:var(--size);
height:var(--size);
position: fixed;
z-index: 999999999;
margin-left: calc(var(--x) - var(--size) / 2);
margin-top: calc(var(--y) - var(--size) / 2);
background: var(--color-fg);
border-radius: 100%;
overflow: hidden;
box-shadow: 0 0 0 1rem var(--color-light) inset, 0 0 5rem 0 var(--color-bg) ;
&:after {
content:"";
position: absolute;
width: calc(var(--size) / 3);
height: calc(var(--size) / 3);
top:50%;
left: 50%;
margin-left: calc(-1 * var(--size) / 6);
margin-top: calc(-1 * var(--size) / 6);
background: var(--color-raised);
box-shadow: 0 0 2rem 0 var(--color-bg), 0 0 0 2rem var(--color-light) inset;
border-radius: 100%;
z-index: 10;
}
.item {
position: absolute;
z-index: 1;
top: calc(-1 * var(--size) / 2);
left: calc(-1 * var(--size) / 2);
width: 100%;
height: 100%;
transform-origin: 100% 100%;
overflow: hidden;
box-shadow: 0 0 0 2.5rem var(--color-light) inset, 0 0 0 1.5rem var(--color-bg) inset;
&.on div:before {
content:"";
position: absolute;
width: calc(var(--size) / 3);
height: calc(var(--size) / 3);
left: calc(-1 * var(--size) / 6);
top: calc(-1 * var(--size) / 6);
border-radius: 100%;
pointer-events: none;
box-shadow: 0 0 0 4rem var(--color-custom);
}
&.on:before {
content:"";
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
pointer-events: none;
background: var(--color-custom);
opacity: 0.3;
}
&.active:after {
content:"";
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
pointer-events: none;
background: var(--color-light);
}
div {
position: absolute;
bottom: 0;
right: 0;
text-align: center;
pointer-events: none;
}
span {
position: absolute;
top: calc(-1 * var(--size) / 3);
width: calc(var(--size) / 2);
margin-left: calc(-1 * var(--size) / 4);
}
}
}
}
.widget.menu-container > .label:after {
content:"\f51f";
transform: none;
font-size: 12rem;
top:4rem;
right:6rem;
}

View file

@ -182,7 +182,7 @@
@extend .toggle-container;
> .label:after {
content:"\f0da"!important;
content:"\f0da";
font-family: FontAwesome;
transform: rotateZ(-45deg);
font-size: 20rem;