<commit_before>/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
import $ from 'jquery';
import 'jquery-ui/ui/widgets/autocomplete';
import Tag from './Tag.class';
import i18next from 'i18next';
import { getCheckedBoxes, notif, reloadEntitiesShow, getEntity } from './misc';
import { Ajax } from './Ajax.class';
import { Payload, Method, Model, Action, Target } from './interfaces';

document.addEventListener('DOMContentLoaded', () => {
  let type = $('#info').data('type');
  if (type === undefined) {
    type = 'experiments_templates';
  }

  const AjaxC = new Ajax();
  const entity = getEntity();
  const TagC = new Tag(entity);

  // CREATE TAG
  $(document).on('keypress blur', '.createTagInput', function(e) {
    if ($(this).val() === '') {
      return;
    }
    // Enter is ascii code 13
    if (e.which === 13 || e.type === 'focusout') {
      TagC.create($(this).val() as string).then(json => {
        if (json.res === false) {
          notif(json);
        }
        $('#tags_div_' + entity.id).load(window.location.href + ' #tags_div_' + entity.id + ' > *');
        $(this).val('');
      });
    }
  });

  // CREATE TAG for several entities
  $(document).on('keypress blur', '.createTagInputMultiple', function(e) {
    if ($(this).val() === '') {
      return;
    }
    // Enter is ascii code 13
    if (e.which === 13 || e.type === 'focusout') {
      // get the ids of selected entities
      const checked = getCheckedBoxes();
      if (checked.length === 0) {
        const json = {
          'msg': 'Nothing selected!',
          'res': false,
        };
        notif(json);
        return;
      }

      // loop over it and add tags
      const results = [];
      checked.forEach(checkBox => {
        results.push(TagC.create((document.getElementById('createTagInputMultiple') as HTMLInputElement).value as string, checkBox['id']));
      });

      Promise.all(results).then(() => {
        reloadEntitiesShow();
      });

      $(this).val('');
    }
  });

  // AUTOCOMPLETE
  const cache = {};
  ($('.createTagInput') as any).autocomplete({
    source: function(request: any, response: any) {
      const term  = request.term;
      if (term in cache) {
        response(cache[term]);
        return;
      }
      const payload: Payload = {
        method: Method.GET,
        action: Action.Read,
        model: Model.Tag,
        entity: entity,
        target: Target.List,
        content: term,
      };
      AjaxC.send(payload).then(json => {
        cache[term] = json.value;
        response(json.value);
      });
    },
  });

  // make the tag editable (on admin.ts)
  $(document).on('mouseenter', '.tag-editable', function() {
    ($(this) as any).editable(function(value) {
      // we need to have an entity so the Tags model is built correctly
      // also it's a mandatory constructor param for Tag.class.ts
      TagC.update(value, $(this).data('tagid'));
      return (value);
    }, {
      tooltip : i18next.t('click-to-edit'),<commit_msg>add tag autocompletion in show mode too<commit_after>/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
import $ from 'jquery';
import 'jquery-ui/ui/widgets/autocomplete';
import Tag from './Tag.class';
import i18next from 'i18next';
import { getCheckedBoxes, notif, reloadEntitiesShow, getEntity } from './misc';
import { Ajax } from './Ajax.class';
import { Payload, Method, Model, Action, Target } from './interfaces';

document.addEventListener('DOMContentLoaded', () => {
  let type = $('#info').data('type');
  if (type === undefined) {
    type = 'experiments_templates';
  }

  const AjaxC = new Ajax();
  const entity = getEntity();
  const TagC = new Tag(entity);

  // CREATE TAG
  $(document).on('keypress blur', '.createTagInput', function(e) {
    if ($(this).val() === '') {
      return;
    }
    // Enter is ascii code 13
    if (e.which === 13 || e.type === 'focusout') {
      TagC.create($(this).val() as string).then(json => {
        if (json.res === false) {
          notif(json);
        }
        $('#tags_div_' + entity.id).load(window.location.href + ' #tags_div_' + entity.id + ' > *');
        $(this).val('');
      });
    }
  });

  // CREATE TAG for several entities
  $(document).on('keypress blur', '.createTagInputMultiple', function(e) {
    if ($(this).val() === '') {
      return;
    }
    // Enter is ascii code 13
    if (e.which === 13 || e.type === 'focusout') {
      // get the ids of selected entities
      const checked = getCheckedBoxes();
      if (checked.length === 0) {
        const json = {
          'msg': 'Nothing selected!',
          'res': false,
        };
        notif(json);
        return;
      }

      // loop over it and add tags
      const results = [];
      checked.forEach(checkBox => {
        results.push(TagC.create((document.getElementById('createTagInputMultiple') as HTMLInputElement).value as string, checkBox['id']));
      });

      Promise.all(results).then(() => {
        reloadEntitiesShow();
      });

      $(this).val('');
    }
  });

  // AUTOCOMPLETE
  const cache = {};
  ($('.createTagInput, .createTagInputMultiple') as any).autocomplete({
    source: function(request: any, response: any) {
      const term  = request.term;
      if (term in cache) {
        response(cache[term]);
        return;
      }
      const payload: Payload = {
        method: Method.GET,
        action: Action.Read,
        model: Model.Tag,
        entity: entity,
        target: Target.List,
        content: term,
      };
      AjaxC.send(payload).then(json => {
        cache[term] = json.value;
        response(json.value);
      });
    },
  });

  // make the tag editable (on admin.ts)
  $(document).on('mouseenter', '.tag-editable', function() {
    ($(this) as any).editable(function(value) {
      // we need to have an entity so the Tags model is built correctly
      // also it's a mandatory constructor param for Tag.class.ts
      TagC.update(value, $(this).data('tagid'));
      return (value);
    }, {
      tooltip : i18next.t('click-to-edit'),
{'fraction_non_alphanumeric': 0.17260668051782005, 'fraction_numerical': 0.006712482020137446, 'mean_word_length': 2.4555494202098287, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5581460', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1916, 'n_words': 480}