summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/wrench_menu.js
blob: 85662705999360d490ff13e98dedada7163dce77 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2010 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.

/**
 * ButtonCommand class for small buttons on menu items.
 */
var ButtonCommand = cr.ui.define('div');

ButtonCommand.prototype = {
  __proto__: HTMLDivElement.prototype,

  /**
   * Decorate Button item.
   */
  decorate: function() {
  },

  /**
   * Changes the selection state of the menu item.
   * @param {boolean} selected True to set the selection, or false otherwise.
   */
  set selected(selected) {
    if (selected) {
      this.classList.add('selected');
      this.menu_.selectedItem = this;
    } else {
      this.classList.remove('selected');
    }
  },

  /**
   * Activate the menu item.
   */
  activate: function() {
    sendActivate(this.menu_.getMenuItemIndexOf(this),
                 'close_and_activate');
  },
};

/**
 * EditCommand implements Copy and Paste command.
 */
var EditCommand = cr.ui.define('div');

EditCommand.prototype = {
  __proto__: ButtonCommand.prototype,

  /**
   * Initialize the menu item.
   * @override
   */
  init: function(menu, attrs, model) {
    this.menu_ = menu;
    this.attrs = attrs;
    if (this.attrs.font) {
      this.style.font = attrs.font;
    }
    menu.addHandlers(this, this);
    if (attrs.command_id == menu.config_.IDC_COPY) {
      menu.addLabelTo(this, menu.config_.IDS_COPY, this,
                      false /* no mnemonic */);
    } else {
      menu.addLabelTo(this, menu.config_.IDS_PASTE, this,
                      false /* no mnemonic */);
    }
  },
};

/**
 * EditMenuItem which has Copy and Paste commands inside.
 */
var EditMenuItem = cr.ui.define('div');

EditMenuItem.prototype = {
  __proto__: MenuItem.prototype,

  /**
   * Initialize
   */
  decorate: function() {
    this.className = 'menu-item';
    this.label_ = document.createElement('div');
    this.label_.className = 'menu-label';
    this.cut_ = document.createElement('div');
    this.cut_.className = 'edit-button left-button';
    this.copy_ = new EditCommand();
    this.copy_.className = 'edit-button center-button';
    this.paste_ = new EditCommand();
    this.paste_.className = 'edit-button right-button';

    this.appendChild(this.label_);
    this.appendChild(this.cut_);
    this.appendChild(this.copy_);
    this.appendChild(this.paste_);
  },

  /**
   * Activates the command.
   * @override
   */
  activate: function() {
    sendActivate(this.menu_.getMenuItemIndexOf(this),
                 'close_and_activate');
  },

  /**
   * @override
   */
  set selected(selected) {
    if (selected) {
      this.cut_.classList.add('selected');
      this.menu_.selectedItem = this;
    } else {
      this.cut_.classList.remove('selected');
    }
  },

  /**
   * Initialize the edit items with configuration info.
   * @override
   */
  initMenuItem_: function() {
    this.label_.textContent =
        this.menu_.config_.IDS_EDIT2;
    if (this.attrs.font) {
      this.label_.style.font = this.attrs.font;
      this.cut_.style.font = this.attrs.font;
    }
    this.menu_.addLabelTo(
        this, this.menu_.config_.IDS_CUT, this.cut_,
        false /* no mnemonic */);
    this.menu_.addHandlers(this, this.cut_);
  },
};

/**
 * ZoomCommand class implements Zoom plus and fullscreen.
 */
var ZoomCommand = cr.ui.define('div');

ZoomCommand.prototype = {
  __proto__: ButtonCommand.prototype,

  /**
   * Initialize the menu item.
   * @override
   */
  init: function(menu, attrs, model) {
    this.menu_ = menu;
    this.attrs = attrs;
    menu.addHandlers(this, this);
    if (attrs.command_id == menu.config_.IDC_ZOOM_PLUS) {
      this.textContent = '+';
    }
    if (this.attrs.font) {
      this.style.font = attrs.font;
    }
  },

  /**
   * Activate zoom plus and full screen commands.
   * @override
   */
  activate: function() {
    sendActivate(this.menu_.getMenuItemIndexOf(this),
                 this.attrs.command_id == this.menu_.config_.IDC_ZOOM_PLUS ?
                 'activate_no_close' : 'close_and_activate');
  },
};

/**
 * ZoomMenuItem which has plus and fullscreen buttons inside.
 */
var ZoomMenuItem = cr.ui.define('div');

ZoomMenuItem.prototype = {
  __proto__: MenuItem.prototype,

  /**
   * Decorate Zoom button item.
   */
  decorate: function() {
    this.className = 'menu-item';

    this.label_ = document.createElement('div');
    this.label_.className = 'menu-label';
    this.minus_ = document.createElement('div');
    this.minus_.className = 'zoom-button left-button';
    this.minus_.textContent = '-';
    this.plus_ = new ZoomCommand();
    this.plus_.className = 'zoom-button right-button';
    this.percent_ = document.createElement('div');
    this.percent_.className = 'zoom-percent center-button';
    this.fullscreen_ = new ZoomCommand();
    this.fullscreen_.className = 'fullscreen';

    this.appendChild(this.label_);
    this.appendChild(this.minus_);
    this.appendChild(this.percent_);
    this.appendChild(this.plus_);
    this.appendChild(this.fullscreen_);
  },

  /**
   * Activates the cut command.
   * @override
   */
  activate: function() {
    sendActivate(this.menu_.getMenuItemIndexOf(this),
                 'activate_no_close');
  },

  /**
   * Updates zoom controls.
   * @params {JSON} params JSON object to configure zoom controls.
   */
  updateZoomControls: function(params) {
    this.attrs.enabled = params.plus;
    if (params.plus) {
      this.plus_.classList.remove('disabled');
    } else {
      this.plus_.classList.add('disabled');
    }
    this.attrs.enabled = params.minus;
    if (params.minus) {
      this.classList.remove('disabled');
    } else {
      this.classList.add('disabled');
    }
    this.percent_.textContent = params.percent;
  },

  /**
   * @override
   */
  set selected(selected) {
    if (selected) {
      this.minus_.classList.add('selected');
      this.menu_.selectedItem = this;
    } else {
      this.minus_.classList.remove('selected');
    }
  },

  /**
   * Initializes the zoom menu item with configuration info.
   * @override
   */
  initMenuItem_: function() {
    this.label_.textContent =
        this.menu_.config_.IDS_ZOOM_MENU2;
    this.menu_.addHandlers(this, this.minus_);

    if (this.attrs.font) {
      this.label_.style.font = this.attrs.font;
      this.minus_.style.font = this.attrs.font;
      this.percent_.style.font = this.attrs.font;
    }
  },
};

/**
 * WrenchMenu
 */
var WrenchMenu = cr.ui.define('div');

WrenchMenu.prototype = {
  __proto__: Menu.prototype,

  /**
   * Decorate Zoom button item.
   */
  decorate: function() {
    Menu.prototype.decorate.call(this);
    this.edit_ = new EditMenuItem();
    this.zoom_ = new ZoomMenuItem();
  },

  /**
   * Create a MenuItem for given {@code attrs}.
   * @override
   */
  createMenuItem: function(attrs) {
    switch(attrs.command_id) {
      case this.config_.IDC_CUT:
        return this.edit_;
      case this.config_.IDC_COPY:
        return this.edit_.copy_;
      case this.config_.IDC_PASTE:
        return this.edit_.paste_;
      case this.config_.IDC_ZOOM_MINUS:
        return this.zoom_;
      case this.config_.IDC_ZOOM_PLUS:
        return this.zoom_.plus_;
      case this.config_.IDC_FULLSCREEN:
        return this.zoom_.fullscreen_;
      default:
        return new MenuItem();
    }
  },

  updateZoomControls: function(params) {
    this.zoom_.updateZoomControls(params);
  },
};

function updateZoomControls(params) {
  var menu = document.getElementById('viewport');
  menu.updateZoomControls(params);
}