From ec3113501e3c505364a5c8d9696142aec9213642 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 4 Jan 2018 20:10:45 +0100 Subject: [PATCH] editor: keep context menu items open when the mouse leaves --- src/browser/js/app/editor/context-menu.js | 116 ++++++++++++++-------- src/browser/js/app/editor/init.js | 11 +- src/browser/scss/_editor.scss | 2 +- 3 files changed, 81 insertions(+), 48 deletions(-) diff --git a/src/browser/js/app/editor/context-menu.js b/src/browser/js/app/editor/context-menu.js index cf9ee0cd..aaf0d930 100644 --- a/src/browser/js/app/editor/context-menu.js +++ b/src/browser/js/app/editor/context-menu.js @@ -1,59 +1,91 @@ -var correctPosition = function (m, parent){ - var pos = m.offset(), - width = m.outerWidth(), - height = m.outerHeight(), - totalWidth = $('body').outerWidth(), - totalHeight = $('body').outerHeight(), - leftAdjust = parent=='body'?0:parent.outerWidth()-2 +class ContextMenu { - if (width + pos.left > totalWidth) { - m.css({'margin-left':totalWidth-(width + pos.left + leftAdjust)}) - } - if (height + pos.top > totalHeight) { - m.css({'margin-top':totalHeight-(height + pos.top)}) - } -} + constructor(){ -var ev = 'fake-click' + this.menu = null + this.event = 'fake-click.editor' + this.root = 'body' - -var menu = function(e,actions,parent){ - - var m = $('
') - if (parent=='body') { - m.css({top:e.pageY+'px',left:e.pageX+'px'}) } - for (let label in actions) { + open(e, actions, parent) { - if (typeof actions[label] == 'object') { + var menu = $('
') - var item = $(`
${label}
`).appendTo(m).on(ev,function(e){e.stopPropagation()}) - menu(e,actions[label],item) - - - } else { - - $(`
${label}
`).on(ev + '.editor',function(){ - var callback = actions[label] - return function(e){ - callback() - $('.context-menu').remove() - } - }()).appendTo(m) + if (!parent) { + this.menu = menu + menu.css({ + top: e.pageY + 'px', + left: e.pageX + 'px' + }) } + + for (let label in actions) { + + if (typeof actions[label] == 'object') { + + var item = $(`
${label}
`) + .appendTo(menu) + .on(this.event, function(e){e.stopPropagation()}) + + this.open(e,actions[label],item) + + + } else { + + $(`
${label}
`).on(this.event,()=>{ + actions[label]() + this.close() + }).appendTo(menu) + } + + } + + if (!parent) { + + menu.find('.item').hover(function(){ + $(this).siblings().removeClass('focus').find('.focus').removeClass('focus') + $(this).addClass('focus') + }) + + } + + menu.appendTo(parent || this.root) + + this.correctPosition(menu,parent) + } - m.appendTo(parent) + close() { + if (this.menu) { + this.menu.remove() + this.menu = null + + } + + } + + correctPosition(menu, parent) { + + var pos = menu.offset(), + width = menu.outerWidth(), + height = menu.outerHeight(), + totalWidth = $('body').outerWidth(), + totalHeight = $('body').outerHeight(), + leftAdjust = !parent ? 0 : parent.outerWidth() - 2 + + if (width + pos.left > totalWidth) { + menu.css({'margin-left': totalWidth - (width + pos.left + leftAdjust)}) + } + if (height + pos.top > totalHeight) { + menu.css({'margin-top': totalHeight - (height + pos.top)}) + } - if (parent=='body') { - correctPosition(m,parent) - } else { - parent.hover(function(){correctPosition(m,parent)}) } } -module.exports = menu + +module.exports = ContextMenu diff --git a/src/browser/js/app/editor/init.js b/src/browser/js/app/editor/init.js index 5b412164..35a5bdd2 100644 --- a/src/browser/js/app/editor/init.js +++ b/src/browser/js/app/editor/init.js @@ -3,9 +3,10 @@ var {getObjectData, updateDom, incrementWidget} = require('./data-workers'), createPopup = require('../utils').createPopup, {widgets, categories} = require('../widgets/'), widgetManager = require('../managers/widgets'), - menu = require('./context-menu') + ContextMenu = require('./context-menu') +var menu = new ContextMenu() var init = function(){ @@ -17,7 +18,7 @@ var init = function(){ // already handled in drag.js // if (e.type=='mousedown' && e.originalEvent.sourceCapabilities.firesTouchEvents) return - $('.context-menu').remove() + menu.close() if (e.target.classList.contains('not-editable')) return @@ -41,12 +42,12 @@ var init = function(){ if (e.type!='fake-right-click') return if (container.hasClass('root-container')) { - menu(d,{ + menu.open(d,{ ' Add tab': function(){ data.tabs.push({}) updateDom(container,data) } - },'body') + }) return } @@ -168,7 +169,7 @@ var init = function(){ } - menu(d,actions,'body') + menu.open(d,actions) }) diff --git a/src/browser/scss/_editor.scss b/src/browser/scss/_editor.scss index 91ba529c..e1adccc1 100644 --- a/src/browser/scss/_editor.scss +++ b/src/browser/scss/_editor.scss @@ -268,7 +268,7 @@ width: 15rem; text-align: center; } - &:hover, &:focus { + &.focus, &:focus { > .context-menu { pointer-events: auto; opacity: 1;