issuable_template_selector.js.es6 2.23 KB
Newer Older
1 2 3
/*= require ../blob/template_selector */

((global) => {
4
  class IssuableTemplateSelector extends gl.TemplateSelector {
5 6 7 8 9 10 11 12 13 14 15 16 17 18
    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', () => {
19 20 21 22 23 24 25
        this.setInputValueToTemplateContent();
      });

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

    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();
35
        this.setInputValueToTemplateContent(true);
36 37 38 39
      });
      return;
    }

40
    setInputValueToTemplateContent(append) {
41
      // `this.requestFileSuccess` sets the value of the description input field
42 43 44
      // to the content of the template selected. If `append` is true, the
      // template content will be appended to the previous value of the field,
      // separated by a blank line if the previous value is non-empty.
45 46
      if (this.titleInput.val() === '') {
        // If the title has not yet been set, focus the title input and
47 48 49
        // skip focusing the description input by setting `true` as the
        // `skipFocus` option to `requestFileSuccess`.
        this.requestFileSuccess(this.currentTemplate, {skipFocus: true, append});
50 51
        this.titleInput.focus();
      } else {
52
        this.requestFileSuccess(this.currentTemplate, {skipFocus: false, append});
53 54 55 56 57 58
      }
      return;
    }
  }

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