Newer
Older
dmpopidor / lib / assets / javascripts / views / orgs / admin_edit.js
@Quentin Sonrel Quentin Sonrel on 2 Jul 2018 2 KB Added TinyMCE and tooltip + minor fixes
// TODO: we need to be able to swap in the appropriate locale here
import 'number-to-text/converters/en-us';
import { enableValidations, validate } from '../../utils/validation';
import { isString } from '../../utils/isType';
import { Tinymce } from '../../utils/tinymce';
import { eachLinks } from '../../utils/links';

$(() => {
  const toggleFeedback = () => {
    if ($('#org_feedback_enabled_true').is(':checked')) {
      $('#feeback-email input, #feeback-email textarea').removeAttr('disabled');
    } else {
      $('#feeback-email input, #feeback-email textarea').attr('disabled', true);
    }
  };

  $('#edit_org_feedback_form input[type="radio"]').click(() => {
    toggleFeedback();
  });

  // Initialises tinymce for any target element with class tinymce_answer
  Tinymce.init({ selector: '#org_feedback_email_msg' });
  toggleFeedback();
  enableValidations($('#edit_org_profile_form .form-group:not(.link-input)'));
  // Only enable validations on the links section if it has values initially
  $('.link').each((idx, el) => {
    const linkVal = $(el).find('input[name="link_link"]').val();
    const textVal = $(el).find('input[name="link_text"]').val();
    if (isString(linkVal) && isString(textVal)) {
      // Validations are enabled if non-empty value is found
      if (linkVal.length > 0 || textVal.length > 0) {
        enableValidations(el);
      }
    }
  });

  // update the hidden org_type field based on the checkboxes selected
  const calculateOrgType = () => {
    let orgType = 0;
    $('input.org_types:checked').each((i, el) => {
      orgType += parseInt($(el).val(), 10);
    });
    $('#org_org_type').val((orgType === 0 ? '' : orgType.toString()));
  };
  $('input.org_types').on('click', calculateOrgType);

  $('#edit_org_profile_form').on('submit', (e) => {
    // Collect links
    const links = {};
    eachLinks((ctx, value) => {
      links[ctx] = value;
    }).done(() => {
      $('#org_links').val(JSON.stringify(links));
    });

    if (!validate(e.target)) {
      e.preventDefault();
    }
  });

  Tinymce.init({
    selector: '#org_banner_text',
    forced_root_block: '',
    toolbar: 'bold italic underline | link',
  });
});