From eb6bd473f5f4860cb8cc4d4f6bd1a1f63a540274 Mon Sep 17 00:00:00 2001 From: bordun Date: Sat, 25 Jan 2020 15:45:19 +0100 Subject: [PATCH] menu widget: doubletap & toggle modes --- src/client/app/widgets/buttons/menu.js | 109 +++++++++++++++----- src/client/app/widgets/mixins/double_tap.js | 4 +- src/scss/widgets/menu.scss | 33 +++--- 3 files changed, 101 insertions(+), 45 deletions(-) diff --git a/src/client/app/widgets/buttons/menu.js b/src/client/app/widgets/buttons/menu.js index ff8c7b71..8dd24c15 100644 --- a/src/client/app/widgets/buttons/menu.js +++ b/src/client/app/widgets/buttons/menu.js @@ -1,5 +1,6 @@ var Widget = require('../common/widget'), {iconify} = require('../../ui/utils'), + doubletab = require('../mixins/double_tap'), html = require('nanohtml'), raw = require('nanohtml/raw') @@ -32,6 +33,8 @@ class Menu extends Widget { '- If `circular` is `false`, set this to `true` for grid layout', '- Can be a `number` to define the number of columns' ]}, + toggle: {type: 'boolean', value: false, help: 'Set to `true` to make the menu stay opened after mouse/touch release'}, + doubleTap: {type: 'boolean', value: false, help: 'Set to `true` to make the menu require a double tap to be opened instead of a single tap'}, 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', @@ -41,13 +44,6 @@ class Menu extends Widget { 'Ignored when `grid` is `true`' ]} - }, [], { - - css: {type: 'string', value: '', help: [ - 'When `circular` is `false`, the layout can be altered by changing the following properties on the `.menu` selector:', - '- `flex-direction: column;` for a vertical layout', - '- `display: grid; grid-template-columns: repeat(X, 1fr);` for a X-columns grid', - ]} }) } @@ -69,30 +65,65 @@ class Menu extends Widget { 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}) + if (this.getProp('doubleTap')) { - this.on('dragend',()=>{ - if (!this.opened) return - this.close() - if (this.selected > -1) { - this.setValue(this.values[this.selected], {send: true, sync: true}) - } + doubletab(this.widget, (e)=>{ + if (this.opened) return + this.open(e) + }) - }, {element: this.widget}) + this.on('draginit',(e)=>{ + + if (this.opened && this.getProp('toggle')) { + + this.selectValue(e) + this.submitValue() + + } + + }, {element: this.widget}) + + + } else { + + this.on('draginit',(e)=>{ + + if (this.opened && this.getProp('toggle')) { + + this.selectValue(e) + this.submitValue() + + } else if (!this.opened) { + + this.open(e) + + } + + }, {element: this.widget}) + + } + + if (!this.getProp('toggle')) { + + this.on('drag',(e)=>{ + + this.selectValue(e) + + }, {element: this.widget}) + + this.on('dragend',()=>{ + + if (!this.opened) return + this.submitValue() + + }, {element: this.widget}) + + } else { + + this.menu.classList.add('norelease') + + } this.setMode() this.setSize() @@ -117,6 +148,28 @@ class Menu extends Widget { } + selectValue(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) + } + + } + + submitValue(e) { + + this.close() + if (this.selected > -1) { + this.setValue(this.values[this.selected], {send: true, sync: true}) + } + + } + parseValues() { var nval = 0, diff --git a/src/client/app/widgets/mixins/double_tap.js b/src/client/app/widgets/mixins/double_tap.js index d7bd14ac..ac4c2287 100644 --- a/src/client/app/widgets/mixins/double_tap.js +++ b/src/client/app/widgets/mixins/double_tap.js @@ -22,7 +22,9 @@ module.exports = (element, callback, options={})=>{ ) { // execute callback at next cycle to ensure draginit events are resolved first - setTimeout(callback, 0) + setTimeout(()=>{ + callback(event.detail) + }, 0) lastTapTime = 0 // prevent touchend diff --git a/src/scss/widgets/menu.scss b/src/scss/widgets/menu.scss index c56431a9..3e1327c6 100644 --- a/src/scss/widgets/menu.scss +++ b/src/scss/widgets/menu.scss @@ -1,7 +1,7 @@ -@keyframes menu-widget-fade { - 0%{opacity: 0; transform: scale(0.9);} - 100%{opacity: 1; transform: scale(1);} -} +// @keyframes menu-widget-fade { +// 0%{opacity: 0; transform: scale(0.9);} +// 100%{opacity: 1; transform: scale(1);} +// } .menu-container { @extend .modal-container; @@ -17,7 +17,7 @@ &.show { display:block; } - animation: menu-widget-fade .1s forwards; + // animation: menu-widget-fade .1s forwards; position: fixed; z-index: 999999999; @@ -47,17 +47,6 @@ } - &.active:after { - content:""; - position: absolute; - top:0; - left:0; - right:0; - bottom:0; - pointer-events: none; - background: var(--color-light); - } - div { text-align: center; pointer-events: none; @@ -65,6 +54,18 @@ } + .item.active:after, &.norelease .item:hover:after { + content:""; + position: absolute; + top:0; + left:0; + right:0; + bottom:0; + pointer-events: none; + background: var(--color-light); + } + + } &.circular .menu {