Select Git revision
vcontextmenu.js 11.85 KiB
/*
view/vcontextmenu.js
Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
// writes the context (right-click) menu ...
function cfContextMenu(evt, view, dt) {
// coupla handy utilities:
let addContextTitle = (text) => {
$(view.dom).find('.contextmenu').get(0).append($('<ul>' + text + '/</li>').get(0))
}
let addContextOption = (text, click) => {
$(view.dom).find('.contextmenu').get(0).append($('<li>' + text + '</li>').click((click)).get(0))
}
let changeContextTitle = (text) => {
// clear,
$(view.dom).find('.contextmenu').children().remove()
// overkill, but fun
let menu = $(view.dom).find('.contextmenu').get(0)
let title = $(`<div>${text}</div>`).addClass('contextTitle').get(0)
$(view.dom).find('.contextmenu').append(title)
// // add the text window, select it ...
// also want: keyboard and enter ... track which is selected, then ?
let tinput = $('<input>').attr('type', 'text').attr('size', 28).attr('value', '').get(0)
$(view.dom).find('.contextmenu').children('.contextTitle').get(0).append(tinput)
$(tinput).focus()
$(tinput).select()
tinput.addEventListener('keyup', (evt) => {
let arr = $(view.dom).find('.contextmenu').children('li').toArray()
if (evt.keyCode == 13) {
// enter case
let pick = $(view.dom).find('.contextmenu').children('.highlighted').get(0)
pick.click()
} else {
// key character ?
let filts = tinput.value.toUpperCase().split(' ')
let sel = false
// separate items by space, search for either,
// reorder by search quality
for (let item of arr) {
// we want the items that match *some part of both items in the filt*
let pass = true
for (let conf of filts) {
if ($(item).text().toUpperCase().indexOf(conf) < 0) {
pass = false
}
}
if (pass) {
item.style.display = ""
// mark as selected, then this is our enter-docu
if (!sel) {
$(item).addClass('highlighted')
sel = true
} else {
$(item).removeClass('highlighted')
}
} else {
item.style.display = "none"
$(item).removeClass('highlighted')
}