BigW Consortium Gitlab

issuable_form.js 5.46 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-use-before-define, no-useless-escape, no-new, quotes, 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 4 5
/* global GitLab */
/* global UsersSelect */
/* global ZenMode */
/* global Autosave */
6 7
/* global dateFormat */
/* global Pikaday */
8

Fatih Acet committed
9
(function() {
10
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
Fatih Acet committed
11 12 13 14 15 16 17

  this.IssuableForm = (function() {
    IssuableForm.prototype.issueMoveConfirmMsg = 'Are you sure you want to move this issue to another project?';

    IssuableForm.prototype.wipRegex = /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i;

    function IssuableForm(form) {
Phil Hughes committed
18
      var $issuableDueDate, calendar;
Fatih Acet committed
19 20 21 22 23
      this.form = form;
      this.toggleWip = bind(this.toggleWip, this);
      this.renderWipExplanation = bind(this.renderWipExplanation, this);
      this.resetAutosave = bind(this.resetAutosave, this);
      this.handleSubmit = bind(this.handleSubmit, this);
24
      gl.GfmAutoComplete.setup();
Fatih Acet committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
      new UsersSelect();
      new ZenMode();
      this.titleField = this.form.find("input[name*='[title]']");
      this.descriptionField = this.form.find("textarea[name*='[description]']");
      this.issueMoveField = this.form.find("#move_to_project_id");
      if (!(this.titleField.length && this.descriptionField.length)) {
        return;
      }
      this.initAutosave();
      this.form.on("submit", this.handleSubmit);
      this.form.on("click", ".btn-cancel", this.resetAutosave);
      this.initWip();
      this.initMoveDropdown();
      $issuableDueDate = $('#issuable-due-date');
      if ($issuableDueDate.length) {
Phil Hughes committed
40
        calendar = new Pikaday({
41
          field: $issuableDueDate.get(0),
42
          theme: 'gitlab-theme',
43
          format: 'yyyy-mm-dd',
44 45
          onSelect: function(dateText) {
            $issuableDueDate.val(dateFormat(new Date(dateText), 'yyyy-mm-dd'));
Fatih Acet committed
46
          }
47
        });
48
        calendar.setDate(new Date($issuableDueDate.val()));
Fatih Acet committed
49 50 51 52 53 54 55 56 57
      }
    }

    IssuableForm.prototype.initAutosave = function() {
      new Autosave(this.titleField, [document.location.pathname, document.location.search, "title"]);
      return new Autosave(this.descriptionField, [document.location.pathname, document.location.search, "description"]);
    };

    IssuableForm.prototype.handleSubmit = function() {
58
      var fieldId = (this.issueMoveField != null) ? this.issueMoveField.val() : null;
59
      if ((parseInt(fieldId, 10) || 0) > 0) {
Fatih Acet committed
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
        if (!confirm(this.issueMoveConfirmMsg)) {
          return false;
        }
      }
      return this.resetAutosave();
    };

    IssuableForm.prototype.resetAutosave = function() {
      this.titleField.data("autosave").reset();
      return this.descriptionField.data("autosave").reset();
    };

    IssuableForm.prototype.initWip = function() {
      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();
    };

    IssuableForm.prototype.workInProgress = function() {
      return this.wipRegex.test(this.titleField.val());
    };

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

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

    IssuableForm.prototype.removeWip = function() {
      return this.titleField.val(this.titleField.val().replace(this.wipRegex, ""));
    };

    IssuableForm.prototype.addWip = function() {
      return this.titleField.val("WIP: " + (this.titleField.val()));
    };

    IssuableForm.prototype.initMoveDropdown = function() {
116
      var $moveDropdown, pageSize;
Fatih Acet committed
117 118
      $moveDropdown = $('.js-move-dropdown');
      if ($moveDropdown.length) {
119
        pageSize = $moveDropdown.data('page-size');
Fatih Acet committed
120 121 122
        return $('.js-move-dropdown').select2({
          ajax: {
            url: $moveDropdown.data('projects-url'),
123 124
            quietMillis: 125,
            data: function(term, page, context) {
Fatih Acet committed
125
              return {
126 127
                search: term,
                offset_id: context
Fatih Acet committed
128 129
              };
            },
130 131 132 133 134 135 136 137 138 139
            results: function(data) {
              var context,
                more;

              if (data.length >= pageSize)
                more = true;

              if (data[data.length - 1])
                context = data[data.length - 1].id;

Fatih Acet committed
140
              return {
141 142 143
                results: data,
                more: more,
                context: context
Fatih Acet committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
              };
            }
          },
          formatResult: function(project) {
            return project.name_with_namespace;
          },
          formatSelection: function(project) {
            return project.name_with_namespace;
          }
        });
      }
    };

    return IssuableForm;
  })();
159
}).call(window);