BigW Consortium Gitlab

issuable_template_selector.js.es6 2.05 KB
Newer Older
1
/* eslint-disable */
2 3 4
/*= require ../blob/template_selector */

((global) => {
5
  class IssuableTemplateSelector extends gl.TemplateSelector {
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    constructor(...args) {
      super(...args);
      this.projectPath = this.dropdown.data('project-path');
      this.namespacePath = this.dropdown.data('namespace-path');
      this.issuableType = this.wrapper.data('issuable-type');
      this.titleInput = $(`#${this.issuableType}_title`);

      let initialQuery = {
        name: this.dropdown.data('selected')
      };

      if (initialQuery.name) this.requestFile(initialQuery);

      $('.reset-template', this.dropdown.parent()).on('click', () => {
20 21 22 23 24 25 26
        this.setInputValueToTemplateContent();
      });

      $('.no-template', this.dropdown.parent()).on('click', () => {
        this.currentTemplate = '';
        this.setInputValueToTemplateContent();
        $('.dropdown-toggle-text', this.dropdown).text('Choose a template');
27 28 29 30 31 32 33 34 35
      });
    }

    requestFile(query) {
      this.startLoadingSpinner();
      Api.issueTemplate(this.namespacePath, this.projectPath, query.name, this.issuableType, (err, currentTemplate) => {
        this.currentTemplate = currentTemplate;
        if (err) return; // Error handled by global AJAX error handler
        this.stopLoadingSpinner();
Luke Bennett committed
36
        this.setInputValueToTemplateContent();
37 38 39 40
      });
      return;
    }

Luke Bennett committed
41
    setInputValueToTemplateContent() {
42
      // `this.requestFileSuccess` sets the value of the description input field
Luke Bennett committed
43
      // to the content of the template selected.
44 45
      if (this.titleInput.val() === '') {
        // If the title has not yet been set, focus the title input and
46 47
        // skip focusing the description input by setting `true` as the
        // `skipFocus` option to `requestFileSuccess`.
Luke Bennett committed
48
        this.requestFileSuccess(this.currentTemplate, {skipFocus: true});
49 50
        this.titleInput.focus();
      } else {
Luke Bennett committed
51
        this.requestFileSuccess(this.currentTemplate, {skipFocus: false});
52 53 54 55 56 57
      }
      return;
    }
  }

  global.IssuableTemplateSelector = IssuableTemplateSelector;
58
})(window.gl || (window.gl = {}));