BigW Consortium Gitlab

gl_form.js 2.07 KB
Newer Older
1 2 3 4 5
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-new, padded-blocks, max-len */
/* global GitLab */
/* global DropzoneInput */
/* global autosize */

6 7
var autosize = require('vendor/autosize');

Fatih Acet committed
8 9 10 11 12
(function() {
  this.GLForm = (function() {
    function GLForm(form) {
      this.form = form;
      this.textarea = this.form.find('textarea.js-gfm-input');
13
      // Before we start, we should clean up any previous data for this form
Fatih Acet committed
14
      this.destroy();
15
      // Setup the form
Fatih Acet committed
16 17 18 19 20
      this.setupForm();
      this.form.data('gl-form', this);
    }

    GLForm.prototype.destroy = function() {
21
      // Clean form listeners
Fatih Acet committed
22 23 24 25 26 27 28 29 30 31 32
      this.clearEventListeners();
      return this.form.data('gl-form', null);
    };

    GLForm.prototype.setupForm = function() {
      var isNewForm;
      isNewForm = this.form.is(':not(.gfm-form)');
      this.form.removeClass('js-new-note-form');
      if (isNewForm) {
        this.form.find('.div-dropzone').remove();
        this.form.addClass('gfm-form');
33
        // remove notify commit author checkbox for non-commit notes
34
        gl.utils.disableButtonIfEmptyField(this.form.find('.js-note-text'), this.form.find('.js-comment-button'));
35
        gl.GfmAutoComplete.setup(this.form.find('.js-gfm-input'));
Fatih Acet committed
36 37
        new DropzoneInput(this.form);
        autosize(this.textarea);
38
        // form and textarea event listeners
Fatih Acet committed
39 40
        this.addEventListeners();
      }
Fatih Acet committed
41
      gl.text.init(this.form);
42
      // hide discard button
Fatih Acet committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      this.form.find('.js-note-discard').hide();
      return this.form.show();
    };

    GLForm.prototype.clearEventListeners = function() {
      this.textarea.off('focus');
      this.textarea.off('blur');
      return gl.text.removeListeners(this.form);
    };

    GLForm.prototype.addEventListeners = function() {
      this.textarea.on('focus', function() {
        return $(this).closest('.md-area').addClass('is-focused');
      });
      return this.textarea.on('blur', function() {
        return $(this).closest('.md-area').removeClass('is-focused');
      });
    };

    return GLForm;

  })();

}).call(this);