DebugConsole.ts 7.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

14
import $ from 'jquery';
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

/**
 * Module: TYPO3/CMS/Backend/DebugConsole
 * The debug console shown at the bottom of the backend
 * @exports TYPO3/CMS/Backend/DebugConsole
 */
class DebugConsole {
  private $consoleDom: any;
  private settings: any = {
    autoscroll: true,
  };

  /**
   * Increment the counter of unread messages in the given tab
   *
   * @param {JQuery} $tab
   */
  private static incrementInactiveTabCounter($tab: JQuery): void {
    if (!$tab.hasClass('active')) {
      const $badge = $tab.find('.badge');
      let value = parseInt($badge.text(), 10);

      if (isNaN(value)) {
        value = 0;
      }
      $badge.text(++value);
    }
  }

  constructor() {
    $((): void => {
      this.createDom();
    });
  }

  /**
   * Add the debug message to the console
   *
   * @param {String} message
   * @param {String} header
   * @param {String} [group=Debug]
   */
  public add(message: string, header: string, group: string): void {
    this.attachToViewport();

    const $line = $('<p />').html(message);
    if (typeof header !== 'undefined' && header.length > 0) {
      $line.prepend($('<strong />').text(header));
    }

    if (typeof group === 'undefined' || group.length === 0) {
      group = 'Debug';
    }

    const tabIdentifier = 'debugtab-' + group.toLowerCase().replace(/\W+/g, '-');
    const $debugTabs = this.$consoleDom.find('.t3js-debuggroups');
    const $tabContent = this.$consoleDom.find('.t3js-debugcontent');
    let $tab = this.$consoleDom.find('.t3js-debuggroups li[data-identifier=' + tabIdentifier + ']');

    // check if group tab exists
    if ($tab.length === 0) {
      // create new tab
      $tab =
78
        $('<li />', {role: 'presentation', class: 'nav-item', 'data-identifier': tabIdentifier}).append(
79
80
          $('<a />', {
            'aria-controls': tabIdentifier,
81
            'data-bs-toggle': 'tab',
82
            class: 'nav-link',
83
            href: '#' + tabIdentifier,
84
            role: 'tab',
85
          }).text(group + ' ').append(
86
87
            $('<span />', {'class': 'badge'}),
          ),
88
89
90
91
92
93
        ).on('shown.bs.tab', (e: Event) => {
          $(e.currentTarget).find('.badge').text('');
        });
      $debugTabs.append($tab);
      $tabContent.append(
        $('<div />', {role: 'tabpanel', 'class': 'tab-pane', id: tabIdentifier}).append(
94
95
          $('<div />', {'class': 't3js-messages messages'}),
        ),
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
      );
    }

    // activate the first tab if no one is active
    if ($debugTabs.find('.active').length === 0) {
      $debugTabs.find('a:first').tab('show');
    }

    DebugConsole.incrementInactiveTabCounter($tab);
    this.incrementUnreadMessagesIfCollapsed();

    const $messageBox = $('#' + tabIdentifier + ' .t3js-messages');
    const isMessageBoxActive = $messageBox.parent().hasClass('active');

    $messageBox.append($line);
    if (this.settings.autoscroll && isMessageBoxActive) {
      $messageBox.scrollTop($messageBox.prop('scrollHeight'));
    }
  }

  private createDom(): void {
    if (typeof this.$consoleDom !== 'undefined') {
      return;
    }

    this.$consoleDom =
      $('<div />', {id: 'typo3-debug-console'}).append(
        $('<div />', {'class': 't3js-topbar topbar'}).append(
          $('<p />', {'class': 'pull-left'}).text(' TYPO3 Debug Console').prepend(
125
            $('<span />', {'class': 'fa fa-terminal topbar-icon'}),
126
          ).append(
127
            $('<span />', {'class': 'badge'}),
128
          ),
129
          $('<div />', {'class': 't3js-buttons btn-group pull-right'}),
130
131
132
        ),
        $('<div />').append(
          $('<div />', {role: 'tabpanel'}).append(
133
            $('<ul />', {'class': 'nav nav-tabs t3js-debuggroups', role: 'tablist'}),
134
          ),
135
136
          $('<div />', {'class': 'tab-content t3js-debugcontent'}),
        ),
137
138
139
140
141
      );

    this.addButton(
      $('<button />', {
        'class': 'btn btn-default btn-sm ' + (this.settings.autoscroll ? 'active' : ''),
142
        title: TYPO3.lang['debuggerconsole.autoscroll'],
143
144
145
146
      }).append($('<span />', {'class': 't3-icon fa fa-magnet'})),
      (): void => {
        $(this).button('toggle');
        this.settings.autoscroll = !this.settings.autoscroll;
147
      },
148
149
150
    ).addButton(
      $('<button />', {
        'class': 'btn btn-default btn-sm',
151
        title: TYPO3.lang['debuggerconsole.toggle.collapse'],
152
153
154
155
156
157
158
159
160
161
162
163
164
165
      }).append($('<span />', {'class': 't3-icon fa fa-chevron-down'})),
      (e: Event): void => {
        let $button = $(e.currentTarget);
        let $icon = $button.find('.t3-icon');
        let $innerContainer = this.$consoleDom.find('.t3js-topbar').next();
        $innerContainer.toggle();
        if ($innerContainer.is(':visible')) {
          $button.attr('title', TYPO3.lang['debuggerconsole.toggle.collapse']);
          $icon.toggleClass('fa-chevron-down', true).toggleClass('fa-chevron-up', false);
          this.resetGlobalUnreadCounter();
        } else {
          $button.attr('title', TYPO3.lang['debuggerconsole.toggle.expand']);
          $icon.toggleClass('fa-chevron-down', false).toggleClass('fa-chevron-up', true);
        }
166
      },
167
168
169
170
171
172
    ).addButton(
      $('<button />', {
        'class': 'btn btn-default btn-sm',
        title: TYPO3.lang['debuggerconsole.clear']}).append($('<span />', {class: 't3-icon fa fa-undo'})),
      (): void => {
        this.flush();
173
      },
174
175
176
177
178
179
180
    ).addButton(
      $('<button />', {
        'class': 'btn btn-default btn-sm',
        title: TYPO3.lang['debuggerconsole.close']}).append($('<span />', {'class': 't3-icon fa fa-times'})),
      (): void => {
        this.destroy();
        this.createDom();
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
    );
  }

  /**
   * Adds a button and it's callback to the console's toolbar
   *
   * @param {JQuery} $button
   * @param callback
   * @returns {DebugConsole}
   */
  private addButton($button: JQuery, callback: any): this {
    $button.on('click', callback);
    this.$consoleDom.find('.t3js-buttons').append($button);

    return this;
  }

  /**
   * Attach the Debugger Console to the viewport
   */
  private attachToViewport(): void {
    const $viewport = $('.t3js-scaffold-content');
    if ($viewport.has(this.$consoleDom).length === 0) {
      $viewport.append(this.$consoleDom);
    }
  }

  /**
   * Increment the counter of unread messages in the tabbar
   */
  private incrementUnreadMessagesIfCollapsed(): void {
    const $topbar = this.$consoleDom.find('.t3js-topbar');
    const $innerContainer = $topbar.next();

    if ($innerContainer.is(':hidden')) {
      const $badge = $topbar.find('.badge');
      let value = parseInt($badge.text(), 10);

      if (isNaN(value)) {
        value = 0;
      }
      $badge.text(++value);
    }
  }

  /**
   * Reset global unread counter
   */
  private resetGlobalUnreadCounter(): void {
    this.$consoleDom.find('.t3js-topbar').find('.badge').text('');
  }

  /**
   * Reset the console
   */
  private flush(): void {
    const $debugTabs = this.$consoleDom.find('.t3js-debuggroups');
    const $tabContent = this.$consoleDom.find('.t3js-debugcontent');

    $debugTabs.children().remove();
    $tabContent.children().remove();
  }

  /**
   * Destroy everything of the console
   */
  private destroy(): void {
    this.$consoleDom.remove();
    this.$consoleDom = undefined;
  }
}

const debugConsole = new DebugConsole();

// expose as global object
TYPO3.DebugConsole = debugConsole;
export = debugConsole;