PageTreeElement.ts 9.36 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 {html, customElement, property, query, LitElement, TemplateResult} from 'lit-element';
15
import {lll} from 'TYPO3/CMS/Core/lit-helper';
16
import {PageTree} from './PageTree';
Benni Mack's avatar
Benni Mack committed
17
import {PageTreeDragDrop, ToolbarDragHandler} from './PageTreeDragDrop';
18
19
import AjaxRequest from 'TYPO3/CMS/Core/Ajax/AjaxRequest';
import {AjaxResponse} from 'TYPO3/CMS/Core/Ajax/AjaxResponse';
Benni Mack's avatar
Benni Mack committed
20
21
import {select as d3select} from 'd3-selection';
import DebounceEvent from 'TYPO3/CMS/Core/Event/DebounceEvent';
22
import 'TYPO3/CMS/Backend/Element/IconElement';
23
import Persistent from 'TYPO3/CMS/Backend/Storage/Persistent';
24
25

/**
26
27
28
29
30
31
32
 * This module defines the Custom Element for rendering the navigation component for an editable page tree
 * including drag+drop, deletion, in-place editing and a custom toolbar for this component.
 *
 * It is used as custom element via "<typo3-backend-navigation-component-pagetree>".
 *
 * The navigationComponentName export is used by the NavigationContainer in order to
 * create an instance of PageTreeNavigationComponent via document.createElement().
33
 */
34
35
36
37
38
39

export const navigationComponentName: string = 'typo3-backend-navigation-component-pagetree';
const toolbarComponentName: string = 'typo3-backend-navigation-component-pagetree-toolbar';

@customElement(navigationComponentName)
export class PageTreeNavigationComponent extends LitElement {
40
41
  @property({type: String}) mountPointPath: string = null;

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
  // @todo: Migrate svg-tree-wrapper into a custom element
  @query('.svg-tree-wrapper') treeWrapper: HTMLElement;

  private readonly tree: PageTree = null;

  public constructor() {
    super();
    this.tree = new PageTree();
  }

  connectedCallback(): void {
    super.connectedCallback();
    document.addEventListener('typo3:pagetree:refresh', this.refresh);
    document.addEventListener('typo3:pagetree:mountPoint', this.setMountPoint);
    document.addEventListener('typo3:pagetree:selectFirstNode', this.selectFirstNode);
  }

  disconnectedCallback(): void {
    document.removeEventListener('typo3:pagetree:refresh', this.refresh);
    document.removeEventListener('typo3:pagetree:mountPoint', this.setMountPoint);
    document.removeEventListener('typo3:pagetree:selectFirstNode', this.selectFirstNode);
    super.disconnectedCallback();
  }

  // disable shadow dom for now
  protected createRenderRoot(): HTMLElement | ShadowRoot {
    return this;
  }

  protected render(): TemplateResult {
72
73
74
    return html`
      <div id="typo3-pagetree" class="svg-tree">
        <div>
75
76
77
          <div id="typo3-pagetree-toolbar" class="svg-toolbar">
              <typo3-backend-navigation-component-pagetree-toolbar .tree="${this.tree}"></typo3-backend-navigation-component-pagetree-toolbar>
          </div>
78
          <div id="typo3-pagetree-treeContainer" class="navigation-tree-container">
79
            ${this.renderMountPoint()}
80
            <div id="typo3-pagetree-tree" class="svg-tree-wrapper"></div>
81
82
83
84
85
86
87
88
          </div>
        </div>
        <div class="svg-tree-loader">
          <typo3-backend-icon identifier="spinner-circle-light" size="large"></typo3-backend-icon>
        </div>
      </div>
    `;
  }
89

90
91
  protected firstUpdated() {
    this.treeWrapper.dispatchEvent(new Event('svg-tree:visible'));
92
93
94
95
96
    const configurationUrl = top.TYPO3.settings.ajaxUrls.page_tree_configuration;
    (new AjaxRequest(configurationUrl)).get()
      .then(async (response: AjaxResponse): Promise<void> => {
        const configuration = await response.resolve('json');
        Object.assign(configuration, {
97
98
          dataUrl: top.TYPO3.settings.ajaxUrls.page_tree_data,
          filterUrl: top.TYPO3.settings.ajaxUrls.page_tree_filter,
99
100
          showIcons: true
        });
101
        const dragDrop = new PageTreeDragDrop(this.tree);
102
        // Initialize the toolbar once the tree was rendered
103
104
105
106
        this.treeWrapper.addEventListener('svg-tree:initialized', () => {
          // set up toolbar now with updated settings
          const toolbar = this.querySelector(toolbarComponentName) as Toolbar;
          toolbar.requestUpdate('tree').then(() => toolbar.initializeDragDrop(dragDrop));
107
108
109
          if (configuration.temporaryMountPoint) {
            this.mountPointPath = configuration.temporaryMountPoint;
          }
110
111
        });
        this.tree.initialize(this.treeWrapper, configuration, dragDrop);
112
113
      });
  }
114
115

  private refresh = (): void => {
116
117
    this.tree.refreshOrFilterTree();
  }
118
119

  private setMountPoint = (e: CustomEvent): void => {
120
    this.setTemporaryMountPoint(e.detail.pageId as number);
121
  }
122
123
124
125
126
127

  private selectFirstNode = (): void => {
    const node = this.tree.nodes[0];
    if (node) {
      this.tree.selectNode(node);
    }
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

  private unsetTemporaryMountPoint() {
    this.mountPointPath = null;
    Persistent.unset('pageTree_temporaryMountPoint').then(() => {
      this.tree.refreshTree();
    });
  }

  private renderMountPoint(): TemplateResult {
    if (this.mountPointPath === null) {
      return html``;
    }
    return html`
      <div class="node-mount-point">
        <div class="node-mount-point__icon"><typo3-backend-icon identifier="actions-document-info" size="small"></typo3-backend-icon></div>
        <div class="node-mount-point__text">${this.mountPointPath}</div>
        <div class="node-mount-point__icon mountpoint-close" @click="${() => this.unsetTemporaryMountPoint()}" title="${lll('labels.temporaryDBmount')}">
          <typo3-backend-icon identifier="actions-close" size="small"></typo3-backend-icon>
        </div>
      </div>
    `;
  }

  private setTemporaryMountPoint(pid: number): void {
    (new AjaxRequest(top.TYPO3.settings.ajaxUrls.page_tree_set_temporary_mount_point))
      .post('pid=' + pid, {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest'},
      })
      .then((response) => response.resolve())
      .then((response) => {
        if (response && response.hasErrors) {
          this.tree.errorNotification(response.message, true);
161
          this.tree.updateVisibleNodes();
162
163
164
165
166
167
168
169
170
        } else {
          this.mountPointPath = response.mountPointPath;
          this.tree.refreshOrFilterTree();
        }
      })
      .catch((error) => {
        this.tree.errorNotification(error, true);
      });
  }
171
}
Benni Mack's avatar
Benni Mack committed
172

173
174
175
176
@customElement(toolbarComponentName)
class Toolbar extends LitElement {
  @property({type: PageTree}) tree: PageTree = null;

Benni Mack's avatar
Benni Mack committed
177
178
179
180
181
  private settings = {
    searchInput: '.search-input',
    filterTimeout: 450
  };

182
183
184
185
186
187
188
189
190
191
192
  public initializeDragDrop(dragDrop: PageTreeDragDrop): void
  {
    if (this.tree.settings?.doktypes?.length) {
      this.tree.settings.doktypes.forEach((item: any) => {
        if (item.icon) {
          const htmlElement = this.querySelector('[data-tree-icon="' + item.icon + '"]');
          d3select(htmlElement).call(this.dragToolbar(item, dragDrop));
        } else {
          console.warn('Missing icon definition for doktype: ' + item.nodeType);
        }
      });
Benni Mack's avatar
Benni Mack committed
193
194
195
    }
  }

196
197
198
  // disable shadow dom for now
  protected createRenderRoot(): HTMLElement | ShadowRoot {
    return this;
Benni Mack's avatar
Benni Mack committed
199
200
  }

201
  protected firstUpdated(): void
Benni Mack's avatar
Benni Mack committed
202
  {
203
    const inputEl = this.querySelector(this.settings.searchInput) as HTMLInputElement;
Benni Mack's avatar
Benni Mack committed
204
205
    if (inputEl) {
      new DebounceEvent('input', (evt: InputEvent) => {
206
207
        const el = evt.target as HTMLInputElement;
        this.tree.filter(el.value.trim());
Benni Mack's avatar
Benni Mack committed
208
209
210
211
212
213
214
215
216
217
      }, this.settings.filterTimeout).bindTo(inputEl);
      inputEl.focus();
      inputEl.clearable({
        onClear: () => {
          this.tree.resetFilter();
        }
      });
    }
  }

218
  protected render(): TemplateResult {
Benni Mack's avatar
Benni Mack committed
219
220
    /* eslint-disable @typescript-eslint/indent */
    return html`
221
      <div class="tree-toolbar">
Benni Mack's avatar
Benni Mack committed
222
223
224
225
226
        <div class="svg-toolbar__menu">
          <div class="svg-toolbar__search">
              <input type="text" class="form-control form-control-sm search-input" placeholder="${lll('tree.searchTermInfo')}">
          </div>
          <button class="btn btn-default btn-borderless btn-sm" @click="${() => this.refreshTree()}" data-tree-icon="actions-refresh" title="${lll('labels.refresh')}">
227
              <typo3-backend-icon identifier="actions-refresh" size="small"></typo3-backend-icon>
Benni Mack's avatar
Benni Mack committed
228
229
230
          </button>
        </div>
        <div class="svg-toolbar__submenu">
231
          ${this.tree.settings?.doktypes?.length
Benni Mack's avatar
Benni Mack committed
232
233
234
235
            ? this.tree.settings.doktypes.map((item: any) => {
              return html`
                <div class="svg-toolbar__drag-node" data-tree-icon="${item.icon}" data-node-type="${item.nodeType}"
                     title="${item.title}" tooltip="${item.tooltip}">
236
                  <typo3-backend-icon identifier="${item.icon}" size="small"></typo3-backend-icon>
Benni Mack's avatar
Benni Mack committed
237
238
239
240
241
242
243
244
245
246
                </div>
              `;
              })
            : ''
          }
        </div>
      </div>
    `;
  }

247
248
249
250
  private refreshTree(): void {
    this.tree.refreshOrFilterTree();
  }

Benni Mack's avatar
Benni Mack committed
251
252
253
254
  /**
   * Register Drag and drop for new elements of toolbar
   * Returns method from d3drag
   */
255
256
  private dragToolbar(item: any, dragDrop: PageTreeDragDrop) {
    return dragDrop.connectDragHandler(new ToolbarDragHandler(item, this.tree, dragDrop));
Benni Mack's avatar
Benni Mack committed
257
258
  }
}