summaryrefslogtreecommitdiff
path: root/chromium/third_party/trace-viewer/src/ui/info_bar.js
blob: 1250f9b35a621a6a2a6bd9b3deaea5489bafa61f (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
// Copyright (c) 2013 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.

'use strict';

base.requireStylesheet('ui.info_bar');
base.require('ui');
base.require('ui.dom_helpers');

base.exportTo('ui', function() {
  /**
   * @constructor
   */
  var InfoBar = ui.define('x-info-bar');

  InfoBar.prototype = {
    __proto__: HTMLDivElement.prototype,

    decorate: function() {
      this.messageEl_ = ui.createSpan({className: 'message'});
      this.buttonsEl_ = ui.createSpan({className: 'buttons'});

      this.appendChild(this.messageEl_);
      this.appendChild(this.buttonsEl_);
      this.message = '';
      this.visible = false;
    },

    get message() {
      return this.messageEl_.textContent;
    },

    set message(message) {
      this.messageEl_.textContent = message;
    },

    get visible() {
      return this.classList.contains('info-bar-hidden');
    },

    set visible(visible) {
      if (visible)
        this.classList.remove('info-bar-hidden');
      else
        this.classList.add('info-bar-hidden');
    },

    removeAllButtons: function() {
      this.buttonsEl_.textContent = '';
    },

    addButton: function(text, clickCallback) {
      var button = document.createElement('button');
      button.textContent = text;
      button.addEventListener('click', clickCallback);
      this.buttonsEl_.appendChild(button);
      return button;
    }
  };

  /**
   * @constructor
   */
  var InfoBarGroup = ui.define('x-info-bar-group');

  InfoBarGroup.prototype = {
    __proto__: HTMLUnknownElement.prototype,

    decorate: function() {
      this.messages_ = [];
    },

    clearMessages: function() {
      this.messages_ = [];
      this.updateContents_();
    },

    addMessage: function(text, opt_buttons) {
      opt_buttons = opt_buttons || [];
      for (var i = 0; i < opt_buttons.length; i++) {
        if (opt_buttons[i].buttonText === undefined)
          throw new Error('buttonText must be provided');
        if (opt_buttons[i].onClick === undefined)
          throw new Error('onClick must be provided');
      }

      this.messages_.push({
        text: text,
        buttons: opt_buttons || []
      });
      this.updateContents_();
    },

    updateContents_: function() {
      this.textContent = '';
      this.messages_.forEach(function(message) {
        var bar = new InfoBar();
        bar.message = message.text;
        bar.visible = true;
        message.buttons.forEach(function(button) {
          bar.addButton(button.buttonText, button.onClick);
        }, this);
        this.appendChild(bar);
      }, this);
    }
  };


  return {
    InfoBar: InfoBar,
    InfoBarGroup: InfoBarGroup
  };
});