Advertisement
NecromancerCoding

Tags de tema

Jun 5th, 2023 (edited)
1,685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.14 KB | None | 0 0
  1. $(function() {
  2.   // Tus tags predefinidos, cada tag va entre corchetes y debe tener los valores declarados en el ejemplo
  3.   var threadTags = [
  4.     {
  5.       filter: 'privado', // Valor que buscará, ponlo siempre todo en minúsculas, obligatorio
  6.       color: '#3774ad', // Color de acento, obligatorio
  7.       icon: '<em class="fas fa-times"></em>' // Icono, si no necesitas, déjalo como en el ejemplo de 'libre'
  8.     },
  9.     {
  10.       filter: '+18',
  11.       color: '#ad3737',
  12.       icon: '<em class="fas fa-exclamation-triangle"></em>'
  13.     },
  14.     {
  15.       filter: 'libre',
  16.       color: '#37ad3f',
  17.       icon:''
  18.     }
  19.   ];
  20.   var threadTagClass = 'threadtag'; // Título de la clase de tus tags
  21.   var threadTagElement = 'span'; // Elemento que serán tus tags (div, span, i, etc)
  22.   var undeclaredTagColor = '#655670'; // Color de acento para los tags no predefinidos
  23.   var lastPostLink = '.forums .row .lastpost>span>a:first-child'; // Link del último tema en el listado de foros
  24.  
  25.   // NO EDITES A PARTIR DE AQUÍ
  26.   // EN SERIO, SI EDITAS Y LO ROMPES ES COSA TUYA
  27.  
  28.   $('.module-title, .post-title, .title-thread, a.topictitle').each(function() {
  29.     var tagsFound = []; // Array para guardar etiquetas encontradas
  30.     var $element = $(this); // Guardar elemento seleccionado
  31.  
  32.     var titleOnly = '';
  33.     var string = $element.text();
  34.     if (/\{.*?\}/.test(string)) {
  35.       titleOnly = string.replace(/\{.*?\}/g, "").trim();
  36.     } else {
  37.       titleOnly = string.trim();
  38.     }
  39.     var tags = string.toLowerCase().match(/[^{]+(?=\})/g);
  40.  
  41.     if (tags) {
  42.       $.each(tags, function(index, tag) {
  43.         var foundTag = false; // Valor para checar si el tag ha sido encontrado en threadTags
  44.         $.each(threadTags, function(index, threadTag) {
  45.           if (tag === threadTag.filter) {
  46.             // El valor de la etiqueta coincide con un filtro de threadTags
  47.             tagsFound.push('<'+threadTagElement+' class="'+threadTagClass+'" data-filter="' + threadTag.filter + '" style="--accent:' + threadTag.color + '">' + threadTag.icon + '' + threadTag.filter + '</'+threadTagElement+'>'); // Añade el tag encontrado al array
  48.             foundTag = true;
  49.           }
  50.         });
  51.  
  52.         if (!foundTag) {
  53.           tagsFound.push('<'+threadTagElement+' class="'+threadTagClass+'" data-filter="' + tag + '" style="--accent:' + undeclaredTagColor + '">' + tag + '</'+threadTagElement+'>'); // Añade el tag no predefinido al array
  54.         }
  55.       });
  56.     }
  57.  
  58.     var tagsString = tagsFound.join(''); // Formar string con los tags encontrados
  59.     if (tagsString.length > 0) {
  60.       var titleAndTags = tagsString + titleOnly;
  61.       $element.html(titleAndTags);
  62.     }
  63.     if ($element.hasClass('title-thread')) {
  64.       $('title').text(titleOnly); // Cambiar valor de title si se está previsualizando un tema con tags
  65.     }
  66.   });
  67.  
  68.   // Limpiar links en últimos posts de foros
  69.   $(lastPostLink).each(function() {
  70.     var $element = $(this);
  71.     var string = $element.attr('title');
  72.     if (/\{.*?\}/.test(string)) {
  73.       string = string.replace(/\{.*?\}/g, "").trim();
  74.       $element.text(string).attr('title',string);
  75.     }
  76.   });
  77.  
  78.   // Insertar formulario en creación de temas
  79.   $('form[action="/post"]').find('.threadSubject').each(function() {
  80.     var $threadSubject = $(this);
  81.     var subjectText = $threadSubject.text().trim();
  82.     if (subjectText.indexOf('tema') !== -1) {
  83.       var formHTML = '<form id="tagForm"><div>'; // Insertar formulario
  84.       $.each(threadTags, function(index, threadTag) {
  85.         var idTag = threadTag.filter.replace(/\s/g, '');
  86.         var checkboxHTML = '<input type="checkbox" name="tags" id="tag-'+idTag+'" value="' + threadTag.filter + '"><label for="tag-'+idTag+'"><'+threadTagElement+' class="'+threadTagClass+'" data-filter="' + threadTag.filter + '" style="--accent:' + threadTag.color + '">' + threadTag.icon + '' + threadTag.filter + '</'+threadTagElement+'></label>';
  87.         formHTML += checkboxHTML; // Añadir tags al HTML del formulario
  88.       });
  89.       formHTML += '</div><br/><button id="addTagsBtn">Modificar título de tema</button></form>'; // Insertar botón al formulario
  90.  
  91.       $threadSubject.parents('fieldset').before(formHTML); // Colocar el formulario sobre la edición de título de tema
  92.  
  93.       // Marcar como checks los inputs presentes en el título al cargar la página
  94.       var threadSubjectInput = $threadSubject.parents('dl').find('dd input[name="subject"]');
  95.       var inputValue = threadSubjectInput.val();
  96.  
  97.       threadTags.forEach(function(tag) {
  98.         if (inputValue.toLowerCase().includes(tag.filter.toLowerCase())) {
  99.           $('#tagForm').find('input[name="tags"][value="' + tag.filter + '"]').prop('checked', true);
  100.         }
  101.       });
  102.     }
  103.   });
  104.  
  105.   // Botón de añadir tags
  106.   $('#addTagsBtn').click(function() {
  107.     var selectedTags = $(this).prev().prev().find('input[name="tags"]:checked')
  108.       .map(function() {
  109.         return '{' + $(this).val() + '}';
  110.       })
  111.       .get();
  112.  
  113.     var threadSubjectInput = $(this).parents('form').next('fieldset').find('.threadSubject').parents('dl').find('dd input[name="subject"]');
  114.     var inputValue = threadSubjectInput.val();
  115.     var existingTags = inputValue.match(/\{.*?\}/g) || [];
  116.  
  117.     // Quitar los tags deseleccionados
  118.     var deselectedTags = existingTags.filter(function(tag) {
  119.       var tagFilter = tag.substring(1, tag.length - 1).toLowerCase();
  120.       return !selectedTags.includes(tag) && threadTags.some(function(threadTag) {
  121.         return threadTag.filter.toLowerCase() === tagFilter;
  122.       });
  123.     });
  124.     deselectedTags = deselectedTags.filter(function(tag, index) {
  125.       return deselectedTags.indexOf(tag) === index;
  126.     });
  127.     var updatedTags = existingTags.filter(function(tag) {
  128.       return !deselectedTags.includes(tag);
  129.     });
  130.  
  131.     // Añadir tags seleccionados no presentes
  132.     selectedTags.forEach(function(tag) {
  133.       if (!updatedTags.includes(tag)) {
  134.         updatedTags.push(tag);
  135.       }
  136.     });
  137.  
  138.     // Actualizar título
  139.     var updatedSubject = updatedTags.join('') + ' ' + inputValue.replace(/\{.*?\}/g, '').trim();
  140.     threadSubjectInput.val(updatedSubject.trim());
  141.     return false;
  142.   });
  143. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement