issuable_form.js 4.6 KB
Newer Older
1
/* eslint-disable func-names, prefer-rest-params, wrap-iife, no-use-before-define, no-useless-escape, no-new, object-shorthand, no-unused-vars, comma-dangle, no-alert, consistent-return, no-else-return, prefer-template, one-var, one-var-declaration-per-line, curly, max-len */
2 3
/* global GitLab */

4
import $ from 'jquery';
Phil Hughes's avatar
Phil Hughes committed
5
import Pikaday from 'pikaday';
6
import Autosave from './autosave';
7
import UsersSelect from './users_select';
8
import GfmAutoComplete from './gfm_auto_complete';
9
import ZenMode from './zen_mode';
10
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
11
import { parsePikadayDate, pikadayToString } from './lib/utils/datefix';
12
import groupsSelect from './groups_select';
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
export default class IssuableForm {
  constructor(form) {
    this.form = form;
    this.toggleWip = this.toggleWip.bind(this);
    this.renderWipExplanation = this.renderWipExplanation.bind(this);
    this.resetAutosave = this.resetAutosave.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.wipRegex = /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i;

    new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources).setup();
    new UsersSelect();
    groupsSelect();
    new ZenMode();

    this.titleField = this.form.find('input[name*="[title]"]');
    this.descriptionField = this.form.find('textarea[name*="[description]"]');
    if (!(this.titleField.length && this.descriptionField.length)) {
      return;
Fatih Acet's avatar
Fatih Acet committed
32 33
    }

34
    this.initAutosave();
35
    this.form.on('submit', this.handleSubmit);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    this.form.on('click', '.btn-cancel', this.resetAutosave);
    this.initWip();

    const $issuableDueDate = $('#issuable-due-date');

    if ($issuableDueDate.length) {
      const calendar = new Pikaday({
        field: $issuableDueDate.get(0),
        theme: 'gitlab-theme animate-picker',
        format: 'yyyy-mm-dd',
        container: $issuableDueDate.parent().get(0),
        parse: dateString => parsePikadayDate(dateString),
        toString: date => pikadayToString(date),
        onSelect: dateText => $issuableDueDate.val(calendar.toString(dateText)),
      });
      calendar.setDate(parsePikadayDate($issuableDueDate.val()));
    }
53 54 55 56 57 58

    this.$targetBranchSelect = $('.js-target-branch-select', this.form);

    if (this.$targetBranchSelect.length) {
      this.initTargetBranchDropdown();
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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
  }

  initAutosave() {
    new Autosave(this.titleField, [document.location.pathname, document.location.search, 'title']);
    return new Autosave(this.descriptionField, [document.location.pathname, document.location.search, 'description']);
  }

  handleSubmit() {
    return this.resetAutosave();
  }

  resetAutosave() {
    this.titleField.data('autosave').reset();
    return this.descriptionField.data('autosave').reset();
  }

  initWip() {
    this.$wipExplanation = this.form.find('.js-wip-explanation');
    this.$noWipExplanation = this.form.find('.js-no-wip-explanation');
    if (!(this.$wipExplanation.length && this.$noWipExplanation.length)) {
      return;
    }
    this.form.on('click', '.js-toggle-wip', this.toggleWip);
    this.titleField.on('keyup blur', this.renderWipExplanation);
    return this.renderWipExplanation();
  }

  workInProgress() {
    return this.wipRegex.test(this.titleField.val());
  }

  renderWipExplanation() {
    if (this.workInProgress()) {
      this.$wipExplanation.show();
      return this.$noWipExplanation.hide();
    } else {
      this.$wipExplanation.hide();
      return this.$noWipExplanation.show();
    }
  }

  toggleWip(event) {
    event.preventDefault();
    if (this.workInProgress()) {
      this.removeWip();
    } else {
      this.addWip();
    }
    return this.renderWipExplanation();
  }

  removeWip() {
    return this.titleField.val(this.titleField.val().replace(this.wipRegex, ''));
  }

  addWip() {
    this.titleField.val(`WIP: ${(this.titleField.val())}`);
  }
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

  initTargetBranchDropdown() {
    this.$targetBranchSelect.select2({
      ...AutoWidthDropdownSelect.selectOptions('js-target-branch-select'),
      ajax: {
        url: this.$targetBranchSelect.data('endpoint'),
        dataType: 'JSON',
        quietMillis: 250,
        data(search) {
          return {
            search,
          };
        },
        results(data) {
          return {
            // `data` keys are translated so we can't just access them with a string based key
            results: data[Object.keys(data)[0]].map(name => ({
              id: name,
              text: name,
            })),
          };
        },
      },
      initSelection(el, callback) {
        const val = el.val();

        callback({
          id: val,
          text: val,
        });
      },
    });
  }
150
}