File: thread-ui.js

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 6,008 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This class represent the UI for Thread drawing filter.
//
class ThreadUI extends HTMLElement {
    constructor() {
      super();
    }

    connectedCallback() {
      this.innerHTML = `
  <style>
  .row {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
  }

  #thread-container {
    max-width: 420px;
    min-width: 350px;
    font-family: Roboto;
    font-size: 10pt;
    background-color: white;
    padding: 10px;
  }

  #saveThreadOptions {
    margin: 20px 10px;
    margin-right: 0px;
  }
  </style>

  <div id='thread-container'>
    <div class='sectionTitle' id='thread-ui-title'>
      Edit Thread Draw Options
    </div>
    <div style='display: flex'>
      <div style='flex-grow: 1'>
        <div class='sectionTitle'>Action</div>
        <div class='section'>
          <div>
            <form id="actionform">
              <input type='radio' id='threadOverride' name='dowhat'
                value='threadOverridesFilters'/>Override filters
              <input type='color' id='threadColor' name='drawcolor'
                value='#000000'/>
              opacity <input type='range' name='fillalpha' min='0' max='100'
               step='10' value='50' list='alphastep'/>
              <datalist id='alphastep'>
                <option>0</option><option>10</option><option>20</option>
                <option>30</option><option>40</option><option>50</option>
                <option>60</option> <option>70</option><option>80</option>
                <option>90</option><option>100</option>
              </datalist>
              <input checked type='radio' name='dowhat'
                value='drawWithFilterOptions'/>
              Draw with filter color and opacity
              <br/>
            </form>
          </div>
        </div>
      </div>
      <button id='saveThreadOptions'>Save</button>
    </div>
  <div>
  `;
      this.setUpButtons_();

      // Event listener to check if custom color selected
      // to automatically select override button
      document.getElementById('threadColor').addEventListener("change", () => {
        document.getElementById('threadOverride').checked = true;
      });
    }

    setUpButtons_() {
        const saveButton = this.querySelector('#saveThreadOptions');
        saveButton.addEventListener('click', () => {
            const threadOptions = { override: false };
            let input = this.querySelector('input[name="dowhat"]:checked');
            if (input.value === 'threadOverridesFilters') {
                threadOptions.color =
                    this.querySelector('input[name="drawcolor"]').value;
                threadOptions.alpha =
                    this.querySelector('input[name="fillalpha"]').value;
                threadOptions.override = true;
            }

            this.dispatchEvent(new CustomEvent('saveThreadOptionsEvent', {
                detail: threadOptions
            }));
        });
    }
};

window.customElements.define('thread-ui', ThreadUI);

function createThreadChip(thread) {
    const chip = document.createElement('div');
    chip.className = "mdc-chip";
    chip.setAttribute("role", "row");
    chip.style.margin = '5px';
    chip.style.borderRadius = '0px';
    if (thread.enabled_) {
      chip.style.border = thread.drawColor_ ?
        `2px solid ${thread.drawColor_}` : `1px solid black`;
      if (thread.fillAlpha_ > 0) {
        var alpha = Math.min(.6, parseFloat(thread.fillAlpha_) / 100);
        var rgba = thread.drawColor_ + DrawCall.alphaFloatToHex(alpha);
        chip.style.backgroundColor = rgba;
        chip.style.fontWeight = 'bold';
      }
      else {
        chip.style.backgroundColor = `white`;
      }
    }
    else {
      chip.style.border = `1px dashed grey`;
      chip.style.backgroundColor = `white`;
    }

    chip.innerHTML =
      `
    <input checked type="checkbox"/>
    <span role="gridcell">
      <span class="mdc-chip__text" id="thread-name"></span>
    </span>
    <span role="gridcell">
        <i class="material-icons-outlined" style="margin-left: 10px"
          onclick="showEditThreadPopup(this)">edit</i></span>
    </span>
    `;

    chip.querySelector('#thread-name').innerHTML = ("~ "  + thread.threadName_);

    const check = chip.querySelector('input');
    check.addEventListener('change', () => {
      Thread.getThread(thread.threadName_).toggleEnableThread();
      Player.instance.refresh();
    });

    return chip;
}

function showEditThreadPopup(item) {
    var chip = item.closest(".mdc-chip");
    // Slice thread name from HTML so that we exclude the "~ ".
    const threadName = chip.querySelector('#thread-name').innerHTML.slice(2);
    const thread = Thread.getThread(threadName);

    const threadUi = document.createElement('thread-ui');
    threadUi.addEventListener('saveThreadOptionsEvent', (event) => {
      if (event.detail.color && event.detail.alpha) {
        thread.drawColor_ = event.detail.color;
        thread.fillAlpha_ = event.detail.alpha;
        const newThreadChip = createThreadChip(thread);
        const threadFilters = document.querySelector('#threads');
        threadFilters.appendChild(newThreadChip);
        chip.replaceWith(newThreadChip);
      }
      thread.overrideFilters_ = event.detail.override;
      hideModal();
      Player.instance.refresh();
    });
    threadUi.style.position = 'absolute';
    threadUi.style.top = (chip.offsetTop + chip.offsetHeight) + 'px';
    threadUi.style.left = (chip.offsetLeft + 20) + 'px';
    threadUi.style.zIndex = maxZIndex;

    showModal(threadUi);

    var actionform = threadUi.querySelector('#actionform');
    actionform.dowhat.value = thread.overrideFilters_ ?
        'threadOverridesFilters' : 'drawWithFilterOptions';
    actionform.drawcolor.value = thread.drawColor_;
    actionform.fillalpha.value = thread.fillAlpha_;
  }