1
0
mirror of https://github.com/mattermost/focalboard.git synced 2025-01-26 18:48:15 +02:00
focalboard/webapp/src/propertyMenu.ts

103 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-10-20 12:50:53 -07:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-20 12:52:56 -07:00
import {IPropertyTemplate, PropertyType} from './blocks/board'
import {Menu} from './menu'
import {Utils} from './utils'
2020-10-08 09:21:27 -07:00
class PropertyMenu extends Menu {
2020-10-20 12:50:53 -07:00
static shared = new PropertyMenu()
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
property: IPropertyTemplate
onNameChanged?: (name: string) => void
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
private nameTextbox: HTMLElement
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
constructor() {
super()
2020-10-20 13:26:06 -07:00
const typeMenuOptions = [
2020-10-20 12:50:53 -07:00
{id: 'text', name: 'Text'},
2020-10-20 13:26:06 -07:00
{id: 'number', name: 'Number'},
2020-10-20 12:50:53 -07:00
{id: 'select', name: 'Select'},
{id: 'createdTime', name: 'Created Time'},
{id: 'updatedTime', name: 'Updated Time'},
2020-10-20 13:26:06 -07:00
]
this.subMenuOptions.set('type', typeMenuOptions)
2020-10-20 12:50:53 -07:00
}
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
createMenuElement(): HTMLElement {
2020-10-20 12:50:53 -07:00
const menu = Utils.htmlToElement('<div class="menu noselect" style="min-width: 200px;"></div>')
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
const ul = menu.appendChild(Utils.htmlToElement('<ul class="menu-options"></ul>'))
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
const nameTextbox = ul.appendChild(Utils.htmlToElement('<li class="menu-textbox"></li>'))
2020-10-20 13:26:06 -07:00
this.nameTextbox = nameTextbox
2020-10-20 12:52:56 -07:00
let propertyValue = this.property ? this.property.name : ''
2020-10-20 13:26:06 -07:00
nameTextbox.innerText = propertyValue
2020-10-20 12:52:56 -07:00
nameTextbox.contentEditable = 'true'
2020-10-20 13:26:06 -07:00
nameTextbox.onclick = (e) => {
e.stopPropagation()
}
nameTextbox.onblur = () => {
if (nameTextbox.innerText !== propertyValue) {
propertyValue = nameTextbox.innerText
2020-10-20 12:50:53 -07:00
if (this.onNameChanged) {
this.onNameChanged(nameTextbox.innerText)
}
}
2020-10-20 13:26:06 -07:00
}
2020-10-20 12:50:53 -07:00
nameTextbox.onmouseenter = () => {
this.hideSubMenu()
2020-10-20 13:26:06 -07:00
}
nameTextbox.onkeydown = (e) => {
2020-10-20 12:50:53 -07:00
if (e.keyCode === 13 || e.keyCode === 27) {
2020-10-20 13:26:06 -07:00
nameTextbox.blur()
e.stopPropagation()
2020-10-20 12:50:53 -07:00
}
}
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
ul.appendChild(Utils.htmlToElement('<li class="menu-separator"></li>'))
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
this.appendMenuOptions(ul)
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
return menu
2020-10-20 12:50:53 -07:00
}
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
showAt(left: number, top: number): void {
2020-10-20 12:50:53 -07:00
this.options = [
{id: 'type', name: this.typeDisplayName(this.property.type), type: 'submenu'},
{id: 'delete', name: 'Delete'},
2020-10-20 13:26:06 -07:00
]
2020-10-08 09:21:27 -07:00
2020-10-20 13:26:06 -07:00
super.showAt(left, top)
2020-10-20 12:50:53 -07:00
setTimeout(() => {
this.nameTextbox.focus()
2020-10-20 13:26:06 -07:00
document.execCommand('selectAll', false, null)
2020-10-20 12:50:53 -07:00
}, 20)
}
2020-10-08 09:21:27 -07:00
2020-10-20 12:50:53 -07:00
private typeDisplayName(type: PropertyType): string {
switch (type) {
2020-10-20 13:26:06 -07:00
case 'text': return 'Text'
case 'number': return 'Number'
case 'select': return 'Select'
case 'multiSelect': return 'Multi Select'
case 'person': return 'Person'
2020-10-20 12:52:56 -07:00
case 'file': return 'File or Media'
case 'checkbox': return 'Checkbox'
case 'url': return 'URL'
2020-10-20 13:26:06 -07:00
case 'email': return 'Email'
2020-10-20 12:52:56 -07:00
case 'phone': return 'Phone'
2020-10-20 13:26:06 -07:00
case 'createdTime': return 'Created Time'
2020-10-20 12:52:56 -07:00
case 'createdBy': return 'Created By'
case 'updatedTime': return 'Updated Time'
2020-10-20 13:26:06 -07:00
case 'updatedBy': return 'Updated By'
default: {
Utils.assertFailure(`typeDisplayName, unhandled type: ${type}`)
return type
}
2020-10-20 12:50:53 -07:00
}
}
2020-10-08 09:21:27 -07:00
}
2020-10-20 12:50:53 -07:00
export {PropertyMenu}