Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function(){
- var carregado = true,
- filterLetter = "";
- $modal = $(document.getElementById("modal-content")),
- $modalEscola = $(document.getElementById('modal-escola')),
- az = true,
- za = false,
- voto = false,
- busca = false,
- pathname = window.location.pathname,
- nidUrl = pathname.split("/")[pathname.split("/").length-1],
- $retorno = $(document.getElementById("resultado-retorno")),
- $numResultados = $(document.getElementById("num-resultados")),
- $textoBuscado = $(document.getElementById("texto-buscado")),
- $verTodos = $(document.getElementById("verTodos")),
- $shareFacebooke = $(document.getElementById("share-facebook")),
- $shareTwitter = $(document.getElementById("share-twitter")),
- inputBusca = document.getElementById("nome-escolas"),
- $erroCaptcha = $(document.getElementById("erro-captcha")),
- textEscola = document.getElementById("textEscola"),
- textCategoria = document.getElementById("textCategoria");
- pathname = "/educacao/desafios/votacao/escolas"
- // pathname = pathname.split(nidUrl)[0]
- // console.log(pathname);
- if(nidUrl!="votacao" && nidUrl!=""){
- abreModal(nidUrl);
- }
- function ordenar(x,y){
- if(az) return ((x.name == y.name) ? 0 : ((x.name > y.name) ? 1 : -1 ));
- if(za) return ((x.name == y.name) ? 0 : ((x.name < y.name) ? 1 : -1 ));
- if(voto) return ((x.votos == y.votos) ? 0 : ((x.votos < y.votos) ? 1 : -1 ));
- }
- function atualizaVotos(nid){
- if(nid){
- $.getJSON(pathname + '?school_id=' + nid, function(data){
- voto = data[0].votos==1?" voto":" votos";
- var text = data[0].votos+voto;
- $modal.find(".votos").text(text);
- $(".list-escolas article").each(function(x){
- // console.log("data-nid: "+$(this).attr("data-nid"));
- // console.log("escola v: "+nid);
- if($(this).attr("data-nid") == nid){
- $(this).find(".cont-votos").text(text)
- }
- })
- });
- }
- }
- function populaModal(nid){
- var $antes = $("#antes").next(),
- $depois = $("#depois").next(),
- feedback = "";
- $.getJSON(pathname + '?school_id=' + nid, function(data){
- voto = data[0].votos==1?" voto":" votos";
- $(".modal-header h1").text(data[0].name);
- $(".foto-1").find("span").attr("data-src",data[0].images.big.img_1).next().attr("src",data[0].images.big.img_1);
- $(".foto-2").find("span").attr("data-src",data[0].images.big.img_2).next().attr("src",data[0].images.big.img_2);
- $(".foto-3").find("span").attr("data-src",data[0].images.big.img_3).next().attr("src",data[0].images.big.img_3);
- $modal.find(".votos").text(data[0].votos+voto);
- $(document.getElementById("bt-votar")).attr("data-nid",data[0].nid_crypted);
- $(document.getElementById("desfazer")).attr("data-nid",data[0].voted_id);
- $shareFacebooke.attr("data-escola",data[0].nid_crypted)
- $shareTwitter.attr("data-escola",data[0].nid_crypted)
- if(!data[0].votes_count)
- feedback = "só poderá votar em até 3 escolas. <b>Confirmar voto?</b>";
- else if(data[0].votes_count == 1)
- feedback = "ainda pode votar em 2 escolas. <b>Confirmar voto?</b>";
- else if(data[0].votes_count == 2){
- feedback = "ainda pode votar em 1 escola. <b>Confirmar voto?</b>";
- }else if( data[0].votes_count>=3 ){
- $(".box-votacao").addClass("check")
- $modal.find(".inicial").find("button").addClass("disabled").attr("disabled","disabled");
- }
- if(data[0].votes_count>=3){
- $(document.getElementById("noVoto")).show();
- }else{
- $(document.getElementById("noVoto")).hide();
- }
- $modal.find(document.getElementById("feedback-votos")).html(feedback);
- if(data[0].voted){
- $modal.find(document.getElementById("voted_school")).attr("data-nid",data[0].voted_id).html(data[0].voted_school);
- $modal.find(".efetuado").hide();
- }else{
- $modal.find(".box-votacao").find(".inicial").show();
- }
- if(data[0].voted_id == nid && data[0].voted){
- $modal.find(".efetuado").show();
- }
- if(data[0].voted_id != nid && data[0].voted){
- $modal.find(".desativado").show();
- }
- }).error(function(jqXHR, textStatus, errorThrown){
- alert("error occurred!");
- });
- }
- $(document.getElementById("voted_school")).bind("click",function(){
- nid = $(this).attr('data-nid');
- populaModal(nid);
- });
- $(document.getElementById("filtro-drop")).find("span").bind("click",function(){
- $this = $(this);
- $(document.getElementById("filtro-alfa")).find(".filtro-select").text($this.text());
- $(document.getElementById("filtro-drop")).find("span").removeClass("ativo")
- $this.addClass("ativo");
- az = $this.hasClass("ordAZ")?true:false;
- za = $this.hasClass("ordZA")?true:false;
- voto = $this.hasClass("ordVotos")?true:false;
- $(".list-escolas").find("article").remove();
- if(inputBusca.value != "") index = 0;
- loadData(index,perPage,true, filterLetter);
- });
- $verTodos.bind("click",function(){
- busca = false;
- $(".list-escolas").find("article").remove();
- $retorno.hide();
- loadData(index,perPage,true, filterLetter);
- inputBusca.value = "";
- })
- $(document.getElementById("search-escolas")).submit(function(event){
- if(inputBusca.value != ""){
- $(".list-escolas").find("article").remove();
- loadData(index,perPage,true, filterLetter);
- busca = true;
- }else{
- busca = false;
- }
- event.preventDefault();
- });
- $modalEscola.on("click", "#overlay, #modal-close",function(){
- $(".modalAberta").removeClass("modalAberta");
- $modalEscola.stop(true,true).fadeOut('fast');
- $(".foto-zoom").hide().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-314.gif");
- $modal.find(".fotos ul").show().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-157.gif");
- $modal.find(".desativado,.efetuado,.inicial,.confirmacao").hide();
- $erroCaptcha.hide();
- $modal.find(textCategoria).hide();
- $modal.find(textEscola).show();
- });
- $modal.find(".fotos li").hover(function(){
- var index = $(this).index(),
- $fotos = $(this).parents(".fotos");
- $modal.find("li").addClass("nohover");
- $(this).removeClass("nohover");
- $fotos.prev().find("li").eq(index).removeClass("nohover").addClass("hover");
- $fotos.next().find("li").eq(index).removeClass("nohover").addClass("hover");
- },function(){
- $modal.find("li").removeClass("hover").removeClass("nohover");
- });
- // Ao clicar na foto, dentro da modal
- $modal.find(".fotos li").on("click",".mask",function(){
- $('#modal-content').hide();
- // var alt = $(this).parent().find("span").attr('data-descricao');
- var src = $(this).parent().find("span").attr('data-src'),
- title = $(this).parent().find("span").html();
- $('.foto-zoom-title').html(title);
- $('.foto-zoom img').attr("src", src);
- $('.foto-zoom').fadeIn();
- // $('.foto-zoom img').attr("alt", alt);
- // var $this = $(this),
- // antes = $this.attr('data-antes'),
- // depois = $this.attr('data-depois'),
- // textoCat= $this.attr('data-descricao');
- // $(".foto-zoom").fadeIn();
- // $modal.find(textEscola).hide();
- // $modal.find(textCategoria).show().find("p").text(textoCat);
- // $(document.getElementById("antes")).find("img").attr({"src":antes});
- // $(document.getElementById("depois")).find("img").attr({"src":depois});
- // $modal.find("span.text b").text("Clique nas fotos para reduzi-las");
- });
- $(".foto-zoom").on("click",function(){
- var $this = $(this),
- textoCat= $this.attr('data-descricao');
- $(".foto-zoom").hide().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-314.gif");
- $('#modal-content').fadeIn();
- // $modal.find(".fotos").find("ul").fadeIn();
- // $modal.find(".text p").text(textoCat);
- // $modal.find("span.text b").text("Clique nas fotos para ampliá-las");
- // $modal.find(textCategoria).hide();
- // $modal.find(textEscola).show();
- });
- $('.list-escolas').on("click",'.open', function(){
- var $this = $(this),
- nid = $this.attr('data-nid');
- console.log(nid);
- $("body").attr("SCROLL","no");
- $this.addClass("modalAberta");
- abreModal(nid)
- });
- function abreModal(nid){
- var hwindow = $(window).height();
- if(hwindow < 618){
- $(".modal-sty .modal-container").css({
- "height": hwindow,
- "top": 0,
- "margin-top": 0
- })
- $("#modal-scrtoll").css({
- "height": 520-(618-hwindow)
- })
- }
- $modalEscola.stop(true,true).fadeIn('fast');
- populaModal(nid);
- }
- // LISTA DE ESCOLAS
- index = 0;
- perPage = 6;
- contentSelector = ".list-escolas";
- loadingSelector = ".loading";
- // var str = "Escola 10",
- // regular_expression = new RegExp(str,'ig');
- // function econtra(obj){
- // return regular_expression.exec(obj)!=null?true:false;
- // }
- function econtra(obj){
- var str = inputBusca.value;
- var regular_expression = new RegExp(str,'ig');
- return regular_expression.exec(obj)!=null?true:false;
- }
- function concatenar(data){
- voto = data.votos==1?" voto":" votos";
- return "<article class='open' data-nid='"+data.nid_crypted+"'>"+
- "<div class='border-sty'>"+
- "<div class='content-escola'>"+
- "<div class='clearfix box-fotos'>"+
- "<div class='fotos'>"+
- "<img src='"+data.images.medium.img_1+"' width='133' height='133'>"+
- "</div>"+
- "<div class='fotos'>"+
- "<img src='"+data.images.medium.img_2+"' width='133' height='133'>"+
- "</div>"+
- "<div class='fotos'>"+
- "<img src='"+data.images.medium.img_3+"' width='133' height='133'>"+
- "</div>"+
- "</div>"+
- "<div class='nome-votos'>"+
- "<strong class='nome-escola'>"+data.name+"</strong>"+
- "<span class='cont-votos'>"+data.votos+voto+"</span>"+
- "</div>"+
- "</div>"+
- "</div>"+
- "<div class='btn-conheca'>"+
- "<div class='ctt-center'>"+
- "<span class='btn-mais'>Conheça</span>"+
- "<span class='btn-text'>Conheça o projeto e vote</span>"+
- "</div>"+
- "</div>"+
- "</article>";
- }
- function loadData(i,p,clear, filter){
- carregado = false;
- $(loadingSelector).show();
- $.getJSON(pathname,function(data){
- var html = "";
- var postId = clear?0:index;
- var contador = 0;
- if(clear) index = 0;
- data.sort(ordenar);
- if(busca){
- while(postId < data.length){
- if(econtra(data[postId].name)){
- html += concatenar(data[postId]);
- contador++;
- }
- postId++;
- }
- $retorno.show();
- $textoBuscado.text(inputBusca.value);
- $numResultados.text(contador);
- contador = 0;
- }else{
- while(postId < data.length && postId < index+perPage){
- if (filterLetter != "") {
- if (data[postId].name.charAt(0).toLowerCase() == filter.toLowerCase()) {
- html += concatenar(data[postId]);
- }
- }else{
- html += concatenar(data[postId]);
- }
- postId++;
- }
- }
- $(loadingSelector).before(html);
- index = index + perPage;
- $(loadingSelector).hide();
- }).done(function( json ) {
- carregado = true;
- });
- }
- loadData(index,perPage,false, filterLetter);
- var w = $(window);
- w.scroll(function(){
- if(carregado)
- if(w.scrollTop() + w.height() == $(document).height()){
- if(!busca){
- if (filterLetter != "") {
- loadData(index,perPage,false, filterLetter);
- }else{
- loadData(index,perPage,false, filterLetter);
- }
- $retorno.hide()
- }
- }
- });
- $(".box-votacao .inicial").find("button").bind("click",function(){
- $(this).parent().hide().next().fadeIn().parent().addClass("check");
- });
- $(".box-votacao .confirmacao").find("button").bind("click",function(){
- $this = $(this);
- $erroCaptcha.hide();
- if($this.hasClass("sim")){
- $this.addClass("disabled").attr("disabled","disabled");
- var $id = $this.attr("data-nid");
- challengeField = $("input#recaptcha_challenge_field").val();
- responseField = $("input#recaptcha_response_field").val();
- $.ajax({
- type: "POST",
- url: pathname + '/votar',
- data: "recaptcha_challenge_field=" + challengeField +
- "&recaptcha_response_field=" + responseField +
- "&school_id=" + $id,
- async: true,
- success: function(data) {
- if(data.success){
- $this.parent().hide().next().fadeIn();
- var nid = $(".modalAberta").attr("data-nid");
- atualizaVotos(nid);
- }
- if(!data.captcha){
- $erroCaptcha.fadeIn();
- }
- Recaptcha.reload();
- $this.removeClass("disabled").removeAttr("disabled");
- }
- });
- }else{
- $(this).parent().hide().prev().fadeIn().parent().removeClass("check");
- }
- })
- $(document.getElementById("desfazer")).bind("click",function(){
- $(document.getElementById("noVoto")).hide();
- $this = $(this);
- $id = $("#bt-votar").attr("data-nid");
- $.getJSON(pathname + '/cancelar?school_id='+$id, function(data) {
- if (data.success) {
- atualizaVotos($id);
- $(document.getElementById("bt-votar")).removeAttr('disabled').removeClass("disabled");
- $modal.find(".inicial").find("button").removeClass("disabled").removeAttr("disabled");
- $this.parent().hide().parent().removeClass("check").find(".inicial").fadeIn();
- }
- });
- });
- // <meta property="og:title" content="Topa o Desafio?" />
- // <meta property="og:summary" content="Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ" />
- // <meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
- $(".bar-filtro .search").click(function(){
- if ($("#nome-escolas").hasClass("active")) {
- $("#nome-escolas").animate({width : "0px"}, 400, function(){
- $(this).removeClass("active")
- });
- }else{
- $(".bar-filtro .search").hide();
- $("#nome-escolas").animate({width : "164px"}, 400, function(){
- $(this).addClass("active")
- });
- }
- });
- // FILTRO AZ
- $(".filtro-az a").click(function(){
- filterLetter = $(this).text();
- $("article").remove();
- loadData(index,perPage,true, filterLetter, filterLetter);
- });
- $("#nome-escolas").blur(function(){
- $(this).animate({width : "0px"}, 400, function(){
- $(this).removeClass("active");
- $(".bar-filtro .search").show();
- });
- });
- $(".button-share-facebook,#share-voto").click(function(event){
- var $this = $(this),
- url = "http://www.facebook.com/sharer/sharer.php?s=100",
- title = $("title").text().split("|"),
- href = $this.hasClass("btn-modal")?location.hostname+pathname+"/"+$shareFacebooke.attr("data-escola"):location.hostname+pathname;
- title = (title && title[0] && $.trim(title[0])) || "Topa o Desafio?";
- url += '&p[title]=' + title;
- url += '&p[url]=' + encodeURIComponent(href);
- url += '&p[summary]=' + encodeURIComponent("Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ");
- url += '&p[image]=';
- event.preventDefault();
- window.open(url, 'sharer', 'toolbar=0,status=0,width=626,height=436');
- console.log(href);
- });
- $(".button-share-twitter").click(function(event){
- event.preventDefault();
- window.open("https://twitter.com/intent/tweet?text=" + encodeURIComponent("Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ"), 'sharer', 'toolbar=0,status=0,width=626,height=436');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement