|
| 1 | +// Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as FrontendHelpers from '../../../../testing/EnvironmentHelpers.js'; |
| 6 | +import * as UI from '../../../legacy/legacy.js'; |
| 7 | +import * as Lit from '../../../lit/lit.js'; |
| 8 | +import * as ComponentHelpers from '../../helpers/helpers.js'; |
| 9 | + |
| 10 | +const {html} = Lit; |
| 11 | + |
| 12 | +await ComponentHelpers.ComponentServerSetup.setup(); |
| 13 | +await FrontendHelpers.initializeGlobalVars(); |
| 14 | + |
| 15 | +{ |
| 16 | + const menuButtonSection = document.querySelector('#menu-button') as HTMLElement; |
| 17 | + Lit.render( |
| 18 | + html` |
| 19 | + <devtools-menu-button |
| 20 | + icon-name="bin" |
| 21 | + .populateMenuCall=${(menu: UI.ContextMenu.ContextMenu) => { |
| 22 | + menu.defaultSection().appendItem('Item', () => { |
| 23 | + alert('Item clicked'); |
| 24 | + }, {jslogContext: 'item'}); |
| 25 | + }} |
| 26 | + jslogContext="my-menu-button" |
| 27 | + ></devtools-menu-button> |
| 28 | +`, |
| 29 | + menuButtonSection); |
| 30 | +} |
| 31 | + |
| 32 | +{ |
| 33 | + let checked = true; |
| 34 | + const simpleItemMenuSection = document.querySelector('#simple-items'); |
| 35 | + simpleItemMenuSection?.addEventListener('contextmenu', onSimpleMenu.bind(this)); |
| 36 | + |
| 37 | + function onSimpleMenu(event: Event) { |
| 38 | + const simpleMenu = new UI.ContextMenu.ContextMenu(event); |
| 39 | + |
| 40 | + // Regular item |
| 41 | + simpleMenu.defaultSection().appendItem('Regular item', () => { |
| 42 | + alert('Regular item clicked '); |
| 43 | + }, {jslogContext: 'regular-item'}); |
| 44 | + |
| 45 | + // Disabled item |
| 46 | + simpleMenu.defaultSection().appendItem('Disabled item', () => { |
| 47 | + alert('Will not be printed'); |
| 48 | + }, {jslogContext: 'disabled-item', disabled: true}); |
| 49 | + |
| 50 | + // Experimental item |
| 51 | + simpleMenu.defaultSection().appendItem('Experimental item', () => { |
| 52 | + alert('Experimental item clicked'); |
| 53 | + }, {jslogContext: 'experimental-item', isPreviewFeature: true}); |
| 54 | + |
| 55 | + // Separator |
| 56 | + simpleMenu.defaultSection().appendSeparator(); |
| 57 | + |
| 58 | + // Checkbox item |
| 59 | + simpleMenu.defaultSection().appendCheckboxItem('Checkbox item', () => { |
| 60 | + alert('Checkbox item clicked'); |
| 61 | + checked = !checked; |
| 62 | + }, {checked, jslogContext: 'checkbox-item'}); |
| 63 | + |
| 64 | + void simpleMenu.show(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +{ |
| 69 | + const customSectionMenuSection = document.querySelector('#custom-section'); |
| 70 | + customSectionMenuSection?.addEventListener('contextmenu', onCustomSectionMenu.bind(this)); |
| 71 | + |
| 72 | + function onCustomSectionMenu(event: Event) { |
| 73 | + const customSectionMenu = new UI.ContextMenu.ContextMenu(event); |
| 74 | + |
| 75 | + // First custom section |
| 76 | + const customSection = customSectionMenu.section('Custom section'); |
| 77 | + customSection.appendItem('Section inner item 1', () => {/* ... */}, {jslogContext: 'my-inner-item-1'}); |
| 78 | + customSection.appendItem('Section inner item 2', () => {/* ... */}, {jslogContext: 'my-inner-item-2'}); |
| 79 | + |
| 80 | + // Second custom section |
| 81 | + const customSection2 = customSectionMenu.section('Custom section 2'); |
| 82 | + customSection2.appendItem('Section inner item 1', () => {/* ... */}, {jslogContext: 'my-inner-item-3'}); |
| 83 | + |
| 84 | + void customSectionMenu.show(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +{ |
| 89 | + const subMenuSection = document.querySelector('#sub-menu'); |
| 90 | + subMenuSection?.addEventListener('contextmenu', onCustomSectionMenu.bind(this)); |
| 91 | + |
| 92 | + function onCustomSectionMenu(event: Event) { |
| 93 | + const subMenuMenu = new UI.ContextMenu.ContextMenu(event); |
| 94 | + |
| 95 | + const subMenu = |
| 96 | + subMenuMenu.defaultSection().appendSubMenuItem('Item to open sub menu', /* disabled */ false, 'my-sub-menu'); |
| 97 | + subMenu.defaultSection().appendItem('Sub menu inner item 1', () => {/* ... */}, {jslogContext: 'my-inner-item-1'}); |
| 98 | + subMenu.defaultSection().appendItem('Sub menu inner item 2', () => {/* ... */}, {jslogContext: 'my-inner-item-2'}); |
| 99 | + |
| 100 | + void subMenuMenu.show(); |
| 101 | + } |
| 102 | +} |
0 commit comments