Wizard.ts 9.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * 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!
 */

import {SeverityEnum} from './Enum/Severity';
15
import $ from 'jquery';
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
import Modal = require('./Modal');
import Severity = require('./Severity');
import Icons = require('./Icons');


interface WizardSettings {
  [key: string]: any;
}

interface WizardSetup {
  slides: Array<any>;
  settings: WizardSettings;
  forceSelection: boolean;
  $carousel: JQuery;
}

interface Slide {
  identifier: string;
  title: string;
  content: string|JQuery;
  severity: SeverityEnum;
  callback?: Function;
}

/**
 * Module: TYPO3/CMS/Backend/Wizard
 * @exports TYPO3/CMS/Backend/Wizard
 */
class Wizard {
  private setup: WizardSetup;
  private readonly originalSetup: WizardSetup;

  constructor() {
    this.setup = {
      slides: [],
      settings: {},
      forceSelection: true,
      $carousel: null,
    };
    this.originalSetup = $.extend(true, {}, this.setup);
  }

  public set(key: string, value: any): Wizard {
    this.setup.settings[key] = value;
    return this;
  }

  public addSlide(
    identifier: string,
    title: string,
    content: string = '',
    severity: SeverityEnum = SeverityEnum.info,
    callback?: Function,
  ): Wizard {
    const slide: Slide = {
      identifier: identifier,
      title: title,
      content: content,
      severity: severity,
      callback: callback,
    };
    this.setup.slides.push(slide);
    return this;
  }

81
  public addFinalProcessingSlide(callback?: Function): Promise<void> {
82
83
84
85
86
87
    if (!callback) {
      callback = (): void => {
        this.dismiss();
      };
    }

88
    return Icons.getIcon('spinner-circle-dark', Icons.sizes.large, null, null).then((markup: string) => {
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
      let $processingSlide = $('<div />', {class: 'text-center'}).append(markup);
      this.addSlide(
        'final-processing-slide', top.TYPO3.lang['wizard.processing.title'],
        $processingSlide[0].outerHTML,
        Severity.info,
        callback,
      );
    });
  }

  public show(): void {
    let $slides = this.generateSlides();
    let firstSlide = this.setup.slides[0];

    Modal.confirm(
      firstSlide.title,
      $slides,
      firstSlide.severity,
      [{
        text: top.TYPO3.lang['wizard.button.cancel'],
        active: true,
        btnClass: 'btn-default',
        name: 'cancel',
        trigger: (): void => {
          this.getComponent().trigger('wizard-dismiss');
        },
      }, {
        text: top.TYPO3.lang['wizard.button.next'],
        btnClass: 'btn-' + Severity.getCssClass(firstSlide.severity),
        name: 'next',
      }],
    );

    if (this.setup.forceSelection) {
      this.lockNextStep();
    }

    this.addProgressBar();
    this.initializeEvents();

    this.getComponent().on('wizard-visible', (): void => {
130
      this.runSlideCallback(firstSlide, this.setup.$carousel.find('.carousel-item').first());
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
    }).on('wizard-dismissed', (): void => {
      this.setup = $.extend(true, {}, this.originalSetup);
    });
  }

  public getComponent(): JQuery {
    if (this.setup.$carousel === null) {
      this.generateSlides();
    }
    return this.setup.$carousel;
  }

  public dismiss(): void {
    Modal.dismiss();
  }

  public lockNextStep(): JQuery {
    let $button = this.setup.$carousel.closest('.modal').find('button[name="next"]');
    $button.prop('disabled', true);
    return $button;
  }

  public unlockNextStep(): JQuery {
    let $button = this.setup.$carousel.closest('.modal').find('button[name="next"]');
    $button.prop('disabled', false);
    return $button;
  }

159
160
161
162
  public setForceSelection(force: boolean): void {
    this.setup.forceSelection = force;
  }

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
  private initializeEvents(): void {
    let $modal = this.setup.$carousel.closest('.modal');
    let $modalTitle = $modal.find('.modal-title');
    let $modalFooter = $modal.find('.modal-footer');
    let $nextButton = $modalFooter.find('button[name="next"]');

    $nextButton.on('click', (): void => {
      this.setup.$carousel.carousel('next');
    });

    this.setup.$carousel.on('slide.bs.carousel', (): void => {
      let nextSlideNumber = this.setup.$carousel.data('currentSlide') + 1;
      let currentIndex = this.setup.$carousel.data('currentIndex') + 1;

      $modalTitle.text(this.setup.slides[currentIndex].title);

      this.setup.$carousel.data('currentSlide', nextSlideNumber);
      this.setup.$carousel.data('currentIndex', currentIndex);

      if (nextSlideNumber >= this.setup.$carousel.data('realSlideCount')) {
        // Point of no return - hide modal footer disable any closing ability
        $modal.find('.modal-header .close').remove();
        $modalFooter.slideUp();
      } else {
        $modalFooter.find('.progress-bar')
          .width(this.setup.$carousel.data('initialStep') * nextSlideNumber + '%')
          .text(top.TYPO3.lang['wizard.progress']
            .replace('{0}', nextSlideNumber)
            .replace('{1}', this.setup.$carousel.data('slideCount')));
      }

      $nextButton
        .removeClass('btn-' + Severity.getCssClass(this.setup.slides[currentIndex - 1].severity))
        .addClass('btn-' + Severity.getCssClass(this.setup.slides[currentIndex].severity));

      $modal
        .removeClass('modal-severity-' + Severity.getCssClass(this.setup.slides[currentIndex - 1].severity))
        .addClass('modal-severity-' + Severity.getCssClass(this.setup.slides[currentIndex].severity));
    }).on('slid.bs.carousel', (evt: JQueryEventObject): void => {
      let currentIndex = this.setup.$carousel.data('currentIndex');
      let slide = this.setup.slides[currentIndex];

      this.runSlideCallback(slide, $(evt.relatedTarget));

      if (this.setup.forceSelection) {
        this.lockNextStep();
      }
    });

    /**
     * Custom event, closes the wizard
     */
    let cmp = this.getComponent();
    cmp.on('wizard-dismiss', this.dismiss);

    Modal.currentModal.on('hidden.bs.modal', (): void => {
      cmp.trigger('wizard-dismissed');
    }).on('shown.bs.modal', (): void => {
      cmp.trigger('wizard-visible');
    });
  }

  private runSlideCallback(slide: Slide, $slide: JQuery): void {
    if (typeof slide.callback === 'function') {
      slide.callback($slide, this.setup.settings, slide.identifier);
    }
  }

  private addProgressBar(): void {
232
    let realSlideCount = this.setup.$carousel.find('.carousel-item').length;
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
    let slideCount = Math.max(1, realSlideCount);
    let initialStep;
    let $modal = this.setup.$carousel.closest('.modal');
    let $modalFooter = $modal.find('.modal-footer');

    initialStep = Math.round(100 / slideCount);

    this.setup.$carousel
      .data('initialStep', initialStep)
      .data('slideCount', slideCount)
      .data('realSlideCount', realSlideCount)
      .data('currentIndex', 0)
      .data('currentSlide', 1);

    // Append progress bar to modal footer
    if (slideCount > 1) {
      $modalFooter.prepend(
        $('<div />', {class: 'progress'}).append(
          $('<div />', {
            role: 'progressbar',
            class: 'progress-bar',
            'aria-valuemin': 0,
            'aria-valuenow': initialStep,
            'aria-valuemax': 100,
          }).width(initialStep + '%').text(
            top.TYPO3.lang['wizard.progress']
              .replace('{0}', '1')
              .replace('{1}', slideCount),
          ),
        ),
      );
    }
  }

  private generateSlides(): JQuery {
    // Check whether the slides were already generated
    if (this.setup.$carousel !== null) {
      return this.setup.$carousel;
    }

273
    let slides = '<div class="carousel slide" data-bs-ride="carousel" data-bs-interval="false">'
274
275
      + '<div class="carousel-inner" role="listbox">';

276
    for (let currentSlide of Object.values(this.setup.slides)) {
277
278
279
280
281
      let slideContent = currentSlide.content;

      if (typeof slideContent === 'object') {
        slideContent = slideContent.html();
      }
282
      slides += '<div class="carousel-item" data-bs-slide="' + currentSlide.identifier + '">' + slideContent + '</div>';
283
284
285
286
287
    }

    slides += '</div></div>';

    this.setup.$carousel = $(slides);
288
    this.setup.$carousel.find('.carousel-item').first().addClass('active');
289
290
291
292
293

    return this.setup.$carousel;
  }
}

294
let wizardObject: any;
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
try {
  // fetch from opening window
  if (window.opener && window.opener.TYPO3 && window.opener.TYPO3.Wizard) {
    wizardObject = window.opener.TYPO3.Wizard;
  }

  // fetch from parent
  if (parent && parent.window.TYPO3 && parent.window.TYPO3.Wizard) {
    wizardObject = parent.window.TYPO3.Wizard;
  }

  // fetch object from outer frame
  if (top && top.TYPO3 && top.TYPO3.Wizard) {
    wizardObject = top.TYPO3.Wizard;
  }
310
} catch {
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  // This only happens if the opener, parent or top is some other url (eg a local file)
  // which loaded the current window. Then the browser's cross domain policy jumps in
  // and raises an exception.
  // For this case we are safe and we can create our global object below.
}

if (!wizardObject) {
  wizardObject = new Wizard();

  // attach to global frame
  if (typeof TYPO3 !== 'undefined') {
    TYPO3.Wizard = wizardObject;
  }
}

export = wizardObject;