<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
define('macroSelectorTranslationKeys', [], [
  'title',
  'filter.text.placeholder',
  'filter.category.all',
  'filter.category.other',
  'failedToRetrieveMacros',
  'select'
]);

define('macroSelector', ['jquery', 'modal', 'l10n!macroSelector'], function($, $modal, translations) {
  'use strict';
  var macrosBySyntax = {},

  getMacros = function(syntaxId) {
    var deferred = $.Deferred();
    var macros = macrosBySyntax[syntaxId || ''];
    if (macros) {
      deferred.resolve(macros);
    } else {
      var url = new XWiki.Document('MacroService', 'CKEditor').getURL('get', 'outputSyntax=plain');
      $.get(url, {data: 'list', syntaxId: syntaxId}).done(function(macros) {
        if ($.isArray(macros)) {
          macrosBySyntax[syntaxId || ''] = macros;
          deferred.resolve(macros);
        } else {
          deferred.reject.apply(deferred, arguments);
        }
      }).fail(function() {
        deferred.reject.apply(deferred, arguments);
      });
    }
    return deferred.promise();
  },

  macroListTemplate = '<ul class="macro-list form-control" tabindex="0"></ul>',
  macroListItemTemplate =
    '<li data-macroCategory="" data-macroId="">' +
      '<div class="macro-name"></div>' +
      '<div class="macro-description"></div>' +
    '</li>',

  displayMacros = function(macros) {
    var list = $(macroListTemplate);
    var categories = {};
    macros.forEach(function(macro) {
      var macroCategory = macro.defaultCategory || '';
      categories[macroCategory] = 1;
      var macroListItem = $(macroListItemTemplate).attr({
        'data-macroId': macro.id.id,
        'data-macroCategory': macroCategory
      }).appendTo(list);
      macroListItem.find('.macro-name').text(macro.name);
      macroListItem.find('.macro-description').text(macro.description);
    });
    categories = Object.keys(categories).sort();
    var categoryFilter = createCategoryFilter(categories);
    var textFilter = $(document.createElement('input')).attr({
      'type': 'text',
      'class': 'macro-textFilter',
      'placeholder': translations.get('filter.text.placeholder')
    });
    var filters = $(document.createElement('div')).addClass('macro-filters input-group');
    filters.append(textFilter).append(categoryFilter);
    this.removeClass('loading').append(filters).append(list);
  },

  createCategoryFilter = function(categories) {
    var categoryFilter = $(
      '<div class="macro-categories input-group-btn">' +
        '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" ' +
          'aria-haspopup="true" aria-expanded="false"><span class="caret"/></button>' +
        '<ul class="dropdown-menu dropdown-menu-right">' +
          '<li><a href="#"/></li>' +
        '</ul>' +
      '</div>'
    );
    var allMacrosCategoryName = translations.get('filter.category.all');
    categoryFilter.find('.caret').before(document.createTextNode(allMacrosCategoryName + ' '));
    categoryFilter.find('a').text(allMacrosCategoryName);
    var separator = '<li role="separator" class="divider"></li>';
    var categoryList = categoryFilter.find('ul.dropdown-menu');
    if (categories.length > 0) {
      categoryList.append(separator);
    }
    var otherCategory;
    categories.forEach(function(category) {
      var item = $('<li><a href="#"></a></li>').attr('data-category', category);
      item.children('a').text(category || translations.get('filter.category.other'));
      if (category) {
        categoryList.append(item);
      } else {
        otherCategory = item;
      }
    });
    if (otherCategory) {
      if (categories.length > 1) {
        categoryList.append(separator);
      }
      categoryList.append(otherCategory);
    }
    return categoryFilter;
  },

  scrollIntoList = function(item) {
    var itemPositionTop = item.position().top;
    var list = item.parent();
    if (itemPositionTop < 0) {
      list.scrollTop(list.scrollTop() + itemPositionTop);
    } else {
      var delta = itemPositionTop + item.outerHeight() - list.height();
      if (delta > 0) {
        list.scrollTop(list.scrollTop() + delta);
      }
    }
  },

  filterMacros = function() {
    var text = $(this).find('.macro-textFilter').val().toLowerCase();
    var selectedCategory = $(this).find('.macro-categories .dropdown-toggle').attr('data-category');
    var macroSelector = $(this).closest('.macro-selector');
    macroSelector.find('.macro-list').scrollTop(0).children().each(function() {
      var name = $(this).find('.macro-name').text().toLowerCase();
      var description = $(this).find('.macro-description').text().toLowerCase();
      var category = $(this).attr('data-macroCategory');
      var hide = (text && name.indexOf(text) < 0 && description.indexOf(text) < 0) ||
        (typeof selectedCategory === 'string' && category !== selectedCategory);
      $(this).removeClass('selected').toggleClass('hidden', hide);
    }).not('.hidden').first().addClass('selected');<commit_msg>CKEDITOR-176: Hide Deprecated and Internal Macro categories by default<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
define('macroSelectorTranslationKeys', [], [
  'title',
  'filter.text.placeholder',
  'filter.category.all',
  'filter.category.other',
  'failedToRetrieveMacros',
  'select'
]);

define('macroSelector', ['jquery', 'modal', 'l10n!macroSelector'], function($, $modal, translations) {
  'use strict';
  var macrosBySyntax = {},

  getMacros = function(syntaxId) {
    var deferred = $.Deferred();
    var macros = macrosBySyntax[syntaxId || ''];
    if (macros) {
      deferred.resolve(macros);
    } else {
      var url = new XWiki.Document('MacroService', 'CKEditor').getURL('get', 'outputSyntax=plain');
      $.get(url, {data: 'list', syntaxId: syntaxId}).done(function(macros) {
        if ($.isArray(macros)) {
          macrosBySyntax[syntaxId || ''] = macros;
          deferred.resolve(macros);
        } else {
          deferred.reject.apply(deferred, arguments);
        }
      }).fail(function() {
        deferred.reject.apply(deferred, arguments);
      });
    }
    return deferred.promise();
  },

  macroListTemplate = '<ul class="macro-list form-control" tabindex="0"></ul>',
  macroListItemTemplate =
    '<li data-macroCategory="" data-macroId="">' +
      '<div class="macro-name"></div>' +
      '<div class="macro-description"></div>' +
    '</li>',

  displayMacros = function(macros) {
    var list = $(macroListTemplate);
    var categories = {};
    macros.forEach(function(macro) {
      var macroCategory = macro.defaultCategory || '';
      categories[macroCategory] = 1;
      var macroListItem = $(macroListItemTemplate).attr({
        'data-macroId': macro.id.id,
        'data-macroCategory': macroCategory
      }).appendTo(list);
      macroListItem.find('.macro-name').text(macro.name);
      macroListItem.find('.macro-description').text(macro.description);
    });
    categories = Object.keys(categories).sort();
    var categoryFilter = createCategoryFilter(categories);
    var textFilter = $(document.createElement('input')).attr({
      'type': 'text',
      'class': 'macro-textFilter',
      'placeholder': translations.get('filter.text.placeholder')
    });
    var filters = $(document.createElement('div')).addClass('macro-filters input-group');
    filters.append(textFilter).append(categoryFilter);
    this.removeClass('loading').append(filters).append(list);
  },

  createCategoryFilter = function(categories) {
    var categoryFilter = $(
      '<div class="macro-categories input-group-btn">' +
        '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" ' +
          'aria-haspopup="true" aria-expanded="false"><span class="caret"/></button>' +
        '<ul class="dropdown-menu dropdown-menu-right">' +
          '<li><a href="#"/></li>' +
        '</ul>' +
      '</div>'
    );
    var allMacrosCategoryName = translations.get('filter.category.all');
    categoryFilter.find('.caret').before(document.createTextNode(allMacrosCategoryName + ' '));
    categoryFilter.find('a').text(allMacrosCategoryName);
    var separator = '<li role="separator" class="divider"></li>';
    var categoryList = categoryFilter.find('ul.dropdown-menu');
    if (categories.length > 0) {
      categoryList.append(separator);
    }
    var otherCategory;
    categories.forEach(function(category) {
      var item = $('<li><a href="#"></a></li>').attr('data-category', category);
      item.children('a').text(category || translations.get('filter.category.other'));
      if (category) {
        categoryList.append(item);
      } else {
        otherCategory = item;
      }
    });
    if (otherCategory) {
      if (categories.length > 1) {
        categoryList.append(separator);
      }
      categoryList.append(otherCategory);
    }
    return categoryFilter;
  },

  scrollIntoList = function(item) {
    var itemPositionTop = item.position().top;
    var list = item.parent();
    if (itemPositionTop < 0) {
      list.scrollTop(list.scrollTop() + itemPositionTop);
    } else {
      var delta = itemPositionTop + item.outerHeight() - list.height();
      if (delta > 0) {
        list.scrollTop(list.scrollTop() + delta);
      }
    }
  },

  filterMacros = function() {
    var text = $(this).find('.macro-textFilter').val().toLowerCase();
    var selectedCategory = $(this).find('.macro-categories .dropdown-toggle').attr('data-category');
    var macroSelector = $(this).closest('.macro-selector');
    macroSelector.find('.macro-list').scrollTop(0).children().each(function() {
      var name = $(this).find('.macro-name').text().toLowerCase();
      var description = $(this).find('.macro-description').text().toLowerCase();
      var category = $(this).attr('data-macroCategory');
      // By default we hide Macros located in the special Internal and Deprecated categories so that they
      // are less visible to users.
      var hide = (text && name.indexOf(text) < 0 && description.indexOf(text) < 0) ||
        (typeof selectedCategory === 'string' && category !== selectedCategory) ||
          (typeof selectedCategory !== 'string' && (category === 'Internal' || category === 'Deprecated'));
      $(this).removeClass('selected').toggleClass('hidden', hide);
    }).not('.hidden').first().addClass('selected');
{'fraction_non_alphanumeric': 0.15947669123126604, 'fraction_numerical': 0.004222903038834148, 'mean_word_length': 3.4949758094529213, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, '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': '5523365', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3371, 'n_words': 781}