summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/base/dropdown.html
blob: 2d0fe220eef4b9fb1981bfdcbf8cf2bb6139b544 (plain)
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
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/base.html">

<polymer-element name="tr-ui-b-dropdown">
  <template>
    <style>
    :host {
      position: relative;
      display: flex;
    }
    #outer {
      display: flex;
      flex: 0 0 auto;
      padding: 1px 4px 1px 4px;
      -webkit-user-select: none;
      cursor: default;
    }

    #state {
      display: flex;
      flex: 0 0 auto;
      margin-left: 2px;
      margin-right: 0px;
      flex: 0 0 auto;
    }

    #icon {
      display: flex;
      flex: 0 0 auto;
      flex: 0 0 auto;
    }
    dialog {
      position: absolute;
      padding: 0;
      border: 0;
      margin: 0;
    }
    dialog::backdrop {
      background: rgba(0,0,0,.05);
    }

    #dialog-frame {
      background-color: #fff;
      display: flex;
      flex-direction: column;
      flex: 1 1 auto;
      padding: 6px;
      border: 1px solid black;
      -webkit-user-select: none;
      cursor: default;
    }
    </style>
    <tr-ui-b-toolbar-button id="outer"
        on-keydown="{{ onOuterKeyDown_ }}"
        on-click="{{ onOuterClick_ }}">
      <div id="icon">&#9881;</div>
      <div id="state">&#9662;</div>
    </tr-ui-b-toolbar-button>
    <dialog id="dialog"
        on-click="{{ onDialogClick_ }}"
        on-cancel="{{ onDialogCancel_ }}">
      <div id="dialog-frame">
        <content></content>
      </div>
    </dialog>
  </template>
  <script>
  'use strict';

  Polymer({
    ready: function() {
      this.$.outer.tabIndex = 0;
    },

    get iconElement() {
      return this.$.icon;
    },

    onOuterKeyDown_: function(e) {
      if (e.keyCode === ' '.charCodeAt(0)) {
        this.toggle_();
        e.preventDefault();
        e.stopPropagation();
      }
    },

    onOuterClick_: function(e) {
      var or = this.$.outer.getBoundingClientRect();
      var inside = true;
      inside &= e.clientX >= or.left;
      inside &= e.clientX < or.right;
      inside &= e.clientY >= or.top;
      inside &= e.clientY < or.bottom;
      if (!inside)
        return;

      e.preventDefault();
      this.toggle_();
    },

    toggle_: function() {
      if (!this.isOpen)
        this.show();
      else
        this.close();
    },

    show: function() {
      if (this.isOpen)
        return;

      this.$.outer.classList.add('open');

      var ddr = this.$.outer.getBoundingClientRect();
      var rW = Math.max(ddr.width, 150);
      this.$.dialog.style.minWidth = rW + 'px';
      this.$.dialog.showModal();

      var ddw = this.$.outer.getBoundingClientRect().width;
      var w = this.$.dialog.getBoundingClientRect().width;
      this.$.dialog.style.top = ddr.bottom - 1 + 'px';
      this.$.dialog.style.left = ddr.left + 'px';
    },

    onDialogClick_: function(e) {
      if (!this.isOpen)
        return;
      if (e.srcElement !== this.$.dialog)
        return;
      e.preventDefault();
      this.close();
    },

    onDialogCancel_: function(e) {
      e.preventDefault();
      this.close();
    },

    close: function() {
      if (!this.isOpen)
        return;
      this.$.dialog.close();
      this.$.outer.classList.remove('open');
      this.$.outer.focus();
    },

    get isOpen() {
      return this.$.dialog.hasAttribute('open');
    }
  });
  </script>
</polymer-element>