Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var votacionClass = '.votacion' /*Clase que tiene que estar presente en el post inicial del tema*/
- var mentionClass = '.mentiontag' /*No deberías de necesitar tocarla ya que es la clase automática de las menciones*/
- var votacionEnv = '#posts' /*Caja que contiene todos los posts y a la cual se adherirá la caja de conteo arriba del todo*/
- var votacionPagination = '.paginationTop > span > a'; /*Links de siguientes páginas*/
- var tituloConteo = 'Resultado actual de la votación';
- $(votacionEnv+':has('+votacionClass+')').each(function(){
- var votos = {};
- var totalVotos = 0;
- var paginas = [];
- $(votacionPagination).each(function() {
- var url = $(this).attr('href');
- if (url && paginas.indexOf(url) === -1) {
- paginas.push(url);
- }
- });
- function procesarVotos(contexto) {
- $(contexto).find(mentionClass).each(function() {
- var href = $(this).attr('href');
- var nombre = $(this).text().replace('@', '').trim();
- if (href && nombre) {
- if (!votos[href]) {
- votos[href] = {
- nombre: nombre,
- cantidad: 0
- };
- }
- votos[href].cantidad++;
- totalVotos++;
- }
- });
- }
- procesarVotos(votacionEnv);
- if (paginas.length > 0) {
- var peticiones = paginas.map(function(url) {
- return $.get(url).then(function(data) {
- var postsDePagina = $(data).find(votacionEnv);
- procesarVotos(postsDePagina);
- });
- });
- $.when.apply($, peticiones).then(function() {
- dibujarResultados();
- });
- } else {
- dibujarResultados();
- }
- function dibujarResultados() {
- var $contenedor = $('<div class="votacionConteo"><h1>'+tituloConteo+'</h1><ul></ul></div>');
- var $lista = $contenedor.find('ul');
- var ranking = [];
- for (var href in votos) {
- ranking.push({
- href: href,
- nombre: votos[href].nombre,
- cantidad: votos[href].cantidad
- });
- }
- ranking.sort(function(a, b) {
- return b.cantidad - a.cantidad;
- });
- ranking.forEach(function(usuario) {
- var li = '<li>' +
- '<a href="' + usuario.href + '">' + usuario.nombre + '</a>' +
- '<progress title="'+usuario.cantidad+'/'+totalVotos+'" value="' + usuario.cantidad + '" max="' + totalVotos + '">' +
- usuario.cantidad + '/' + totalVotos +
- '</progress>' +
- '</li>';
- $lista.append(li);
- });
- $(votacionEnv).prepend($contenedor);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment