Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found in the LICENSE file. |
| 6 | --> |
| 7 | |
| 8 | <link rel="import" href="/base/base.html"> |
| 9 | |
| 10 | <polymer-element name="tv-b-dropdown"> |
| 11 | <template> |
| 12 | <style> |
| 13 | :host { |
| 14 | position: relative; |
| 15 | display: flex; |
| 16 | } |
| 17 | #outer { |
| 18 | display: flex; |
| 19 | flex: 0 0 auto; |
| 20 | padding: 1px 4px 1px 4px; |
| 21 | border: 1px solid black; |
| 22 | -webkit-user-select: none; |
| 23 | cursor: default; |
| 24 | } |
| 25 | |
| 26 | #state { |
| 27 | display: flex; |
| 28 | flex: 0 0 auto; |
| 29 | margin-left: 2px; |
| 30 | margin-right: 0px; |
| 31 | flex: 0 0 auto; |
| 32 | } |
| 33 | |
| 34 | #icon { |
| 35 | display: flex; |
| 36 | flex: 0 0 auto; |
| 37 | flex: 0 0 auto; |
| 38 | } |
| 39 | dialog { |
| 40 | position: absolute; |
| 41 | padding: 0; |
| 42 | border: 0; |
| 43 | margin: 0; |
| 44 | } |
| 45 | dialog::backdrop { |
| 46 | background: rgba(0,0,0,.05); |
| 47 | } |
| 48 | |
| 49 | #dialog-frame { |
| 50 | background-color: #fff; |
| 51 | display: flex; |
| 52 | flex-direction: column; |
| 53 | flex: 1 1 auto; |
| 54 | padding: 6px; |
| 55 | border: 1px solid black; |
| 56 | -webkit-user-select: none; |
| 57 | cursor: default; |
| 58 | } |
| 59 | </style> |
| 60 | <div id="outer"> |
| 61 | <div id="icon">⚙</div> |
| 62 | <div id="state">▾</div> |
| 63 | </span> |
| 64 | <dialog id="dialog"> |
| 65 | <div id="dialog-frame"> |
| 66 | <content></content> |
| 67 | </div> |
| 68 | </dialog> |
| 69 | </template> |
| 70 | <script> |
| 71 | 'use strict'; |
| 72 | |
| 73 | Polymer({ |
| 74 | ready: function() { |
| 75 | |
| 76 | this.$.outer.addEventListener('click', this.onOuterClick_.bind(this)); |
| 77 | this.$.dialog.addEventListener('click', this.onDialogClick_.bind(this)); |
| 78 | |
| 79 | this.$.dialog.addEventListener('cancel', this.onDialogCancel_.bind(this)); |
| 80 | }, |
| 81 | |
| 82 | get iconElement() { |
| 83 | return this.$.icon; |
| 84 | }, |
| 85 | |
| 86 | onOuterClick_: function(e) { |
| 87 | var or = this.$.outer.getBoundingClientRect(); |
| 88 | var inside = true; |
| 89 | inside &= e.clientX >= or.left; |
| 90 | inside &= e.clientX < or.right; |
| 91 | inside &= e.clientY >= or.top; |
| 92 | inside &= e.clientY < or.bottom; |
| 93 | if (!inside) |
| 94 | return; |
| 95 | |
| 96 | e.preventDefault(); |
| 97 | if (!this.isOpen) |
| 98 | this.show(); |
| 99 | else |
| 100 | this.close(); |
| 101 | }, |
| 102 | |
| 103 | show: function() { |
| 104 | if (this.isOpen) |
| 105 | return; |
| 106 | |
| 107 | this.$.outer.classList.add('open'); |
| 108 | |
| 109 | var ddr = this.$.outer.getBoundingClientRect(); |
| 110 | var rW = Math.max(ddr.width, 150); |
| 111 | this.$.dialog.style.minWidth = rW + 'px'; |
| 112 | this.$.dialog.showModal(); |
| 113 | |
| 114 | var ddw = this.$.outer.getBoundingClientRect().width; |
| 115 | var w = this.$.dialog.getBoundingClientRect().width; |
| 116 | this.$.dialog.style.top = ddr.bottom - 1 + 'px'; |
| 117 | this.$.dialog.style.left = ddr.left + 'px'; |
| 118 | }, |
| 119 | |
| 120 | onDialogClick_: function(e) { |
| 121 | if (!this.isOpen) |
| 122 | return; |
| 123 | var dr = this.$.dialog.getBoundingClientRect(); |
| 124 | var inside = true; |
| 125 | inside &= e.clientX >= dr.left; |
| 126 | inside &= e.clientX < dr.right; |
| 127 | inside &= e.clientY >= dr.top; |
| 128 | inside &= e.clientY < dr.bottom; |
| 129 | if (!inside) { |
| 130 | e.preventDefault(); |
| 131 | this.close(); |
| 132 | } |
| 133 | }, |
| 134 | |
| 135 | onDialogCancel_: function(e) { |
| 136 | e.preventDefault(); |
| 137 | this.close(); |
| 138 | }, |
| 139 | |
| 140 | close: function() { |
| 141 | if (!this.isOpen) |
| 142 | return; |
| 143 | this.$.dialog.close(); |
| 144 | this.$.outer.classList.remove('open'); |
| 145 | }, |
| 146 | |
| 147 | get isOpen() { |
| 148 | return this.$.dialog.hasAttribute('open'); |
| 149 | } |
| 150 | }); |
| 151 | </script> |
| 152 | </polymer-element> |