Sha256: 58bfca1492cb7abecef44ff69c30228af50474eb029388d2bb6082592f542529

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

(function($) {
  const COMPONENT_SELECTOR = '[data-thredded-topic-form]';
  class ThreddedTopicForm {
    constructor() {
      this.titleSelector = '[name$="topic[title]"]';
      this.textareaSelector = 'textarea';
      this.compactSelector = 'form.thredded--is-compact';
      this.expandedSelector = 'form.thredded--is-expanded';
      this.escapeElements = 'input, textarea';
      this.escapeKeyCode = 27;
    }

    toggleExpanded(child, expanded) {
      jQuery(child).closest(expanded ? this.compactSelector : this.expandedSelector).toggleClass('thredded--is-compact thredded--is-expanded');
    }

    init($nodes) {
      $nodes.find(this.textareaSelector).autosize();
      $nodes.filter(this.compactSelector).
        on('focus', this.titleSelector, e => {
          this.toggleExpanded(e.target, true);
        }).
        on('keydown', this.escapeElements, e => {
          if (e.keyCode == this.escapeKeyCode) {
            this.toggleExpanded(e.target, false);
            e.target.blur();
          }
        }).
        on('blur', this.escapeElements, e => {
          var blurredEl = e.target;
          $(document.body).one('mouseup touchend', e => {
            var $blurredElForm = $(blurredEl).closest('form');
            // Un-expand if the new focus element is outside of the same form and
            // all the input elements are empty.
            if (!$(e.target).closest('form').is($blurredElForm) &&
              $blurredElForm.find(this.escapeElements).is(function() {
                return !this.value;
              })) {
              this.toggleExpanded(blurredEl, false);
            }
          })
        });
    }

  }

  $(function() {
    var $nodes = $(COMPONENT_SELECTOR);
    if ($nodes.length) {
      new ThreddedTopicForm().init($nodes);
    }
  });
})(jQuery);


Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
thredded-0.7.0 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.6.3 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.6.2 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.6.1 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.6.0 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.5.1 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.5.0 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.4.0 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.3.2 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.3.1 app/assets/javascripts/thredded/components/topic_form.es6
thredded-0.3.0 app/assets/javascripts/thredded/components/topic_form.es6