summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/pdf/elements/viewer-pen-options.js
blob: d63343404fb75ee3165332706613b5fce32a30c9 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2019 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.

const colors = [
  // row 1
  {name: 'annotationColorBlack', color: '#000000'},
  {name: 'annotationColorRed', color: '#ff5252'},
  {name: 'annotationColorYellow', color: '#ffbc00'},
  {name: 'annotationColorGreen', color: '#00c853'},
  {name: 'annotationColorCyan', color: '#00b0ff'},
  {name: 'annotationColorPurple', color: '#d500f9'},
  {name: 'annotationColorBrown', color: '#8d6e63'},
  // row 2
  {name: 'annotationColorWhite', color: '#fafafa', outline: true},
  {name: 'annotationColorCrimson', color: '#a52714'},
  {name: 'annotationColorAmber', color: '#ee8100'},
  {name: 'annotationColorAvocadoGreen', color: '#558b2f'},
  {name: 'annotationColorCobaltBlue', color: '#01579b'},
  {name: 'annotationColorDeepPurple', color: '#8e24aa'},
  {name: 'annotationColorDarkBrown', color: '#4e342e'},
  // row 3
  {name: 'annotationColorDarkGrey', color: '#90a4ae'},
  {name: 'annotationColorHotPink', color: '#ff4081'},
  {name: 'annotationColorOrange', color: '#ff6e40'},
  {name: 'annotationColorLime', color: '#aeea00'},
  {name: 'annotationColorBlue', color: '#304ffe'},
  {name: 'annotationColorViolet', color: '#7c4dff'},
  {name: 'annotationColorTeal', color: '#1de9b6'},
  // row 4
  {name: 'annotationColorLightGrey', color: '#cfd8dc'},
  {name: 'annotationColorLightPink', color: '#f8bbd0'},
  {name: 'annotationColorLightOrange', color: '#ffccbc'},
  {name: 'annotationColorLightGreen', color: '#f0f4c3'},
  {name: 'annotationColorLightBlue', color: '#9fa8da'},
  {name: 'annotationColorLavender', color: '#d1c4e9'},
  {name: 'annotationColorLightTeal', color: '#b2dfdb'},
];

const sizes = [
  {name: 'annotationSize1', size: 0},
  {name: 'annotationSize2', size: 0.1429},
  {name: 'annotationSize3', size: 0.2857},
  {name: 'annotationSize4', size: 0.4286},
  {name: 'annotationSize8', size: 0.5714},
  {name: 'annotationSize12', size: 0.7143},
  {name: 'annotationSize16', size: 0.8571},
  {name: 'annotationSize20', size: 1},
];

/**
 * Displays a set of radio buttons to select from
 * a predefined list of colors and sizes.
 */
Polymer({
  is: 'viewer-pen-options',

  properties: {
    expanded_: {
      type: Boolean,
      value: false,
    },
    selectedSize: {
      type: Number,
      value: 0.250,
      notify: true,
    },
    selectedColor: {
      type: String,
      value: '#000000',
      notify: true,
    },
    sizes_: {
      type: Array,
      value: sizes,
    },
    colors_: {
      type: Array,
      value: colors,
    },
    strings: Object,
  },

  /** @type {Array<!Animation>} */
  expandAnimations_: null,

  /** @param {Event} e */
  sizeChanged_: function(e) {
    this.selectedSize = Number(e.target.value);
  },

  /** @param {Event} e */
  colorChanged_: function(e) {
    this.selectedColor = e.target.value;
  },

  /** @private */
  toggleExpanded_: function() {
    this.expanded_ = !this.expanded_;
    this.updateExpandedState_();
  },

  /** @private */
  updateExpandedStateAndFinishAnimations_: function() {
    this.updateExpandedState_();
    for (const animation of /** @type {!Array<!Animation>} */ (
             this.expandAnimations_)) {
      animation.finish();
    }
  },

  /** @override */
  attached: function() {
    Polymer.RenderStatus.beforeNextRender(this, () => {
      this.updateExpandedStateAndFinishAnimations_();
    });
  },

  /**
   * Updates the state of the UI to reflect the current value of `expanded`.
   * Starts or reverses animations and enables/disable controls.
   */
  updateExpandedState_: function() {
    const colors = this.$.colors;
    if (!this.expandAnimations_) {
      const separator = this.$.separator;
      const expand = this.$.expand;
      this.expandAnimations_ = [
        colors.animate({height: ['32px', '188px']}, {
          easing: 'ease-in-out',
          duration: 250,
          fill: 'both',
        }),
        separator.animate({opacity: [0, 1]}, {
          easing: 'ease-in-out',
          duration: 250,
          fill: 'both',
        }),
        expand.animate({transform: ['rotate(0deg)', 'rotate(180deg)']}, {
          easing: 'ease-in-out',
          duration: 250,
          fill: 'forwards',
        }),
      ];
    }
    for (const animation of this.expandAnimations_) {
      // TODO(dstockwell): Ideally we would just set playbackRate,
      // but there appears to be a web-animations bug that
      // results in the animation getting stuck in the 'pending'
      // state sometimes. See crbug.com/938857
      const currentTime = animation.currentTime;
      animation.cancel();
      animation.playbackRate = this.expanded_ ? 1 : -1;
      animation.currentTime = currentTime;
      animation.play();
    }
    for (const input of colors.querySelectorAll('input:nth-child(n+8)')) {
      if (this.expanded_) {
        input.removeAttribute('disabled');
      } else {
        input.setAttribute('disabled', '');
      }
    }
  },

  /**
   * Used to determine equality in computed bindings.
   *
   * @param {*} a
   * @param {*} b
   */
  equal_: function(a, b) {
    return a == b;
  },

  /**
   * Used to lookup a string in a computed binding.
   *
   * @param {Object} strings
   * @param {string} name
   * @return {string}
   */
  lookup_: function(strings, name) {
    return strings ? strings[name] : '';
  },

  /**
   * Used to remove focus when clicking or tapping on a styled input
   * element. This is a workaround until we can use the :focus-visible
   * pseudo selector.
   */
  blurOnPointerDown(e) {
    const target = e.target;
    setTimeout(() => target.blur(), 0);
  },
});