• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16
17import {globals} from '../globals';
18import {fullscreenModalContainer, ModalDefinition} from '../modal';
19import {AnyAttrsVnode} from '../panel_container';
20import {ArgumentPopup} from '../pivot_table_argument_popup';
21
22export class AttributeModalHolder {
23  showModal = false;
24  typedArgument = '';
25
26  callback: (arg: string) => void;
27
28  constructor(callback: (arg: string) => void) {
29    this.callback = callback;
30  }
31
32  start() {
33    this.showModal = true;
34    fullscreenModalContainer.createNew(this.renderModal());
35    globals.rafScheduler.scheduleFullRedraw();
36  }
37
38  private renderModal(): ModalDefinition {
39    return {
40      title: 'Enter argument name',
41      content:
42          m(ArgumentPopup, {
43            knownArguments:
44                globals.state.nonSerializableState.pivotTable.argumentNames,
45            onArgumentChange: (arg) => {
46              this.typedArgument = arg;
47            },
48          }) as AnyAttrsVnode,
49      buttons: [
50        {
51          text: 'Add',
52          action: () => {
53            this.callback(this.typedArgument);
54            this.typedArgument = '';
55          },
56        },
57      ],
58      onClose: () => {
59        this.showModal = false;
60      },
61    };
62  }
63
64  // A method that should be called in `view` method of whatever component is
65  // using the attribute modal.
66  update() {
67    if (this.showModal) {
68      fullscreenModalContainer.updateVdom(this.renderModal());
69    }
70  }
71}
72