NecromancerCoding

Votaciones con Menciones para Robin

Jul 15th, 2026 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.52 KB | None | 0 0
  1. var votacionClass = '.votacion' /*Clase que tiene que estar presente en el post inicial del tema*/
  2. var mentionClass = '.mentiontag' /*No deberías de necesitar tocarla ya que es la clase automática de las menciones*/
  3. var votacionEnv = '#posts' /*Caja que contiene todos los posts y a la cual se adherirá la caja de conteo arriba del todo*/
  4. var votacionPagination = '.paginationTop > span > a'; /*Links de siguientes páginas*/
  5. var tituloConteo = 'Resultado actual de la votación';
  6.  
  7. $(votacionEnv+':has('+votacionClass+')').each(function(){
  8. var votos = {};
  9. var totalVotos = 0;
  10.  
  11. var paginas = [];
  12. $(votacionPagination).each(function() {
  13.   var url = $(this).attr('href');
  14.   if (url && paginas.indexOf(url) === -1) {
  15.     paginas.push(url);
  16.   }
  17. });
  18.  
  19. function procesarVotos(contexto) {
  20.   $(contexto).find(mentionClass).each(function() {
  21.     var href = $(this).attr('href');
  22.     var nombre = $(this).text().replace('@', '').trim();
  23.     if (href && nombre) {
  24.       if (!votos[href]) {
  25.           votos[href] = {
  26.               nombre: nombre,
  27.               cantidad: 0
  28.           };
  29.       }
  30.       votos[href].cantidad++;
  31.       totalVotos++;
  32.     }
  33.   });
  34. }
  35.  
  36. procesarVotos(votacionEnv);
  37.  
  38. if (paginas.length > 0) {
  39.     var peticiones = paginas.map(function(url) {
  40.         return $.get(url).then(function(data) {
  41.  
  42.             var postsDePagina = $(data).find(votacionEnv);
  43.             procesarVotos(postsDePagina);
  44.         });
  45.     });
  46.  
  47.     $.when.apply($, peticiones).then(function() {
  48.         dibujarResultados();
  49.     });
  50. } else {
  51.   dibujarResultados();
  52. }
  53.  
  54. function dibujarResultados() {
  55.     var $contenedor = $('<div class="votacionConteo"><h1>'+tituloConteo+'</h1><ul></ul></div>');
  56.     var $lista = $contenedor.find('ul');
  57.  
  58.     var ranking = [];
  59.     for (var href in votos) {
  60.         ranking.push({
  61.             href: href,
  62.             nombre: votos[href].nombre,
  63.             cantidad: votos[href].cantidad
  64.         });
  65.     }
  66.  
  67.     ranking.sort(function(a, b) {
  68.         return b.cantidad - a.cantidad;
  69.     });
  70.  
  71.     ranking.forEach(function(usuario) {
  72.         var li = '<li>' +
  73.                     '<a href="' + usuario.href + '">' + usuario.nombre + '</a>' +
  74.                     '<progress title="'+usuario.cantidad+'/'+totalVotos+'" value="' + usuario.cantidad + '" max="' + totalVotos + '">' +
  75.                         usuario.cantidad + '/' + totalVotos +
  76.                     '</progress>' +
  77.                   '</li>';
  78.         $lista.append(li);
  79.     });
  80.  
  81.     $(votacionEnv).prepend($contenedor);
  82. }
  83.  
  84. });
Advertisement
Add Comment
Please, Sign In to add comment