﻿var webService = "WsForum"; var currentPage = 1; var htmlError = '<p class="loading"><img src="/Mercado/Images/exclamation.png" />Erro ao obter as mensagens!</p>'; var htmlLoading = '<p class="loading"><img src="/Mercado/Images/loading.gif" />{0}</p>'; $(function() { ChangeMenu(1); GetMessages(1, true); $(".controleconteudo:eq(1)").css("margin-top", "10px") }); function GetMessages(c, a) { var b = "{page: '" + c + "'}"; if ($("#mensagens").html() != "") { AtualizarNovidades() } LoadMenu(false); $("#conteudo h1").css("font-size", "30px"); $("#conteudo h1").html("Forum de Mercado"); $("#mensagens").html('<p class="loading"><img src="../Images/loading.gif" />Carregando, aguarde...</p>'); scrollToTop(); currentPage = c; ajaxProxy(webService, "GetMessages", b, function(d) { if (d.Error) { $("#mensagens").html(htmlError) } else { MessagesToHtml(d, c); $("#lateral").css("height", $("#miolo").height() - 50 + "px") } }) } function NewMessage() { ValidateSession(function(a) { if (a) { currentMessage = { id: "0", isTopic: "true" }; $.get("../Controls/EditorMensagem.aspx", { titulo: "Nova Mensagem" }, function(b) { $.facebox(b); formReset("#editormensagem"); $("#btnEnviar").show(); $("#btnEnviar").unbind("click").click(function() { SaveTopic() }); $("#titulo").show() }) } }, "enviar mensagens") } function SaveTopic() { var d = $.truncateText($.jsonText($("#txttitulo").val()), 100); var c = $.jsonText($("#txttexto").val()); var b = $("#ImageContainer img:eq(0)").attr("src") == undefined ? "" : $("#ImageContainer img:eq(0)").attr("src"); if ($.trim(d) == "") { alert("Informe o título da mensagem."); return } if ($.clearText(c) == "" && b == "") { alert("Informe o texto da mensagem."); return } $("#btnEnviar").hide(); var a = "{id: '" + currentMessage.id + "', title: '" + d + "', text: '" + c + "', image: '" + b + "'}"; ajaxProxy(webService, "SaveTopic", a, function(e) { if (e.Error) { alert(e.Error) } else { GetMessages(currentPage); $(document).trigger("close.facebox") } }) } function ReplyMessage(a) { ValidateSession(function(b) { if (b) { currentMessage = { id: "0", idTopic: a }; $.get("../Controls/EditorMensagem.aspx", { titulo: "Responder Mensagem" }, function(c) { $.facebox(c); formReset("#editormensagem"); $("#btnEnviar").show(); $("#btnEnviar").unbind("click").click(function() { SaveReply() }); $("#titulo").hide() }) } }, "enviar mensagens") } function SaveReply() { var c = $.jsonText($("#txttexto").val()); var b = $("#ImageContainer img:eq(0)").attr("src") == undefined ? "" : $("#ImageContainer img:eq(0)").attr("src"); if ($.clearText(c) == "" && b == "") { alert("Informe o texto da mensagem."); return } $("#btnEnviar").hide(); var a = "{id: '" + currentMessage.id + "', idTopic: '" + currentMessage.idTopic + "', text: '" + c + "', image: '" + b + "'}"; ajaxProxy(webService, "SaveReply", a, function(d) { if (d.Error) { alert(d.Error) } else { GetMessages(currentPage); $(document).trigger("close.facebox") } }) } function EditMessage(id, isTopic) { ValidateSession(function(isValid) { if (isValid) { var jsonString = "{id: '" + id + "', idTopic: '0', isTopic: '" + isTopic + "'}"; currentMessage = eval("(" + jsonString + ")"); ajaxProxy(webService, "GetMessage", jsonString, function(result) { if (result.Error) { alert(result.Error) } else { $.get("../Controls/EditorMensagem.aspx", { titulo: "Editar Mensagem" }, function(data) { $.facebox(data); formReset("#editormensagem"); $("#btnEnviar").unbind("click"); if (isTopic) { $("#btnEnviar").click(function() { SaveTopic() }); $("#titulo").show(); $("#txttitulo").val(result.Title) } else { $("#btnEnviar").click(function() { SaveReply() }); $("#titulo").hide() } currentMessage.idTopic = result.TopicID; $("#txttexto").val(result.Text) }) } }) } }, "editar mensagem") } function DeleteMessage(b, a) { ValidateSession(function(d) { if (d) { var c = "{id: '" + b + "', isTopic: '" + a + "'}"; if (!confirm("Deseja excluir esta mensagem ?")) { return false } ajaxProxy(webService, "DeleteMessage", c, function(e) { if (e.Error) { alert(e.Error) } else { GetMessages(currentPage); alert(e.Return) } }) } }, "deletar mensagem") } function RestoreMessage(b, a) { ValidateSession(function(d) { if (d) { var c = "{id: '" + b + "', isTopic: '" + a + "'}"; if (!confirm("Deseja restaurar esta mensagem ?")) { return false } ajaxProxy(webService, "RestoreMessage", c, function(e) { if (e.Error) { alert(e.Error) } else { GetMessages(currentPage); alert(e.Return) } }) } }, "restaurar mensagem") } function NewSearch() { $.get("../Controls/EditorPesquisa.aspx", function(a) { $.facebox(a); $("#btnPesquisar").show(); $("#btnPesquisar").unbind("click").click(function() { SearchMessages(1) }); if (Date.dayNames == undefined) { $.getScript("../Js/date.js", function() { $.getScript("../Js/jquery.datePicker.min-2.1.2.js", function() { $("#pesquisamensagem .data").datePicker({ clickInput: true, startDate: "01/01/2001" }) }) }) } else { $("#pesquisamensagem .data").datePicker({ clickInput: true, startDate: "01/01/2001" }) } }) } function SearchMessages(d) { $("#btnPesquisar").hide(); var f = $.trim($("#searchText").val()); var a = $.trim($("#searchOwner").val()); var c = ($("#searchIniDate").val() != "" ? $("#searchIniDate").val() : "01/01/2001"); var e = ($("#searchEndDate").val() != "" ? $("#searchEndDate").val() : "01/01/2020"); if (!isDate(c)) { alert("Data inicial inválida!"); return } if (!isDate(e)) { alert("Data final inválida!"); return } if (f == "" && a == "" && c == "01/01/2001" && e == "01/01/2020") { alert("Informe pelo menos um critério"); return } if (f != "" && f.lenght < 5) { alert("Informe o texto com mais de 5 caracteres."); return } if (a != "" && a.lenght < 5) { alert("Informe o autor com mais de 5 caracteres."); return } var b = "{page: '" + d + "', criteria: '" + $("#searchText").val() + "', owner: '" + a + "', iniDate: '" + c + "', endDate: '" + e + "'}"; $(document).trigger("close.facebox"); $("#mensagens").html('<p class="loading"><img src="../Images/loading.gif" />Pesquisando, aguarde...</p>'); scrollToTop(); currentPage = d; ajaxProxy(webService, "SearchMessages", b, function(g) { if (g.Error) { alert(g.Error) } else { $("#conteudo h1").css("font-size", "25px"); if (g.Mensagens.length == 0) { $("#conteudo h1").html("Forum de Mercado: nenhum tópico encontrado"); $("#mensagens").html('<p class="alert">Nenhuma mensagem encontrada!</p>'); scrollToTop(); return } else { MessagesToHtml(g, d, true); $("#conteudo h1").html("Forum de Mercado: a pesquisa retornou " + g.Total + " tópico(s)") } } }) } function GetMessagesByUser(d, c, a) { currentPage = d; scrollToTop(); $("#mensagens").html(htmlLoading.replace("{0}", "Aguarde, carregando mensagens de <strong>" + a + "</strong>...")); var b = "{page: '" + d + "', usuarioID: '" + c + "'}"; ajaxProxy(webService, "GetMessagesByUser", b, function(e) { if (e.Error) { $("#mensagens").html(htmlError) } else { $("#conteudo h1").css("font-size", "25px"); MessagesToHtml(e, d, false, "GetMessagesByUser('{0}','" + c + "','" + a + "')"); $("#conteudo h1").html("Forum de Mercado: " + e.Total + " mensagens de " + a + ".") } }) } function MessagesToHtml(result, page, isSearch, pager) { var html = ""; for (var t in result.Mensagens) { var topico = result.Mensagens[t]; html += '<div class="topico' + (topico.Status == 5 ? " topico-deletado" : "") + '"><h2>' + topico.Titulo + "</h2><a href=\"javascript:;\" onclick=\"GetMessagesByUser('1','" + topico.UsuarioID + "','" + topico.Login + '\')" title="Ver mensagens deste forense" class="mensagemusuario">&nbsp;</a><a href="javascript:;" onclick="AdicionarFavorito(' + topico.ID + ')" title="Salvar tópico como favorito" class="mensagemfavorito">&nbsp;</a><span class="texto">' + topico.Texto + "</span>"; if (topico.Imagem != "") { html += '<div class="imagem"><a href="javascript:;" onclick="$.facebox({ image: \'' + topico.Imagem + '\' })" class="thickbox"><img src="' + topico.Imagem + '" alt="Clique para exibir a imagem"/></a></div>' } html += '<div class="acoestopico">'; html += '<div class="autortopico">enviado por <strong>' + (topico.Status ? '<a href="javascript:;" onclick="window.open(\'/Mercado/Admin/Usuario.aspx?id=' + topico.UsuarioID + "', '', 'width=450,height=580,scrollbars=1')\">" + topico.Login + "</a>" : topico.Login) + "</strong> às " + topico.Data + "</div>"; html += '<div class="respondertopico">'; html += '<a href="javascript:;" onclick="ReplyMessage(' + topico.ID + ')" class="option">Responder</a>'; if (topico.IsEditable) { html += '<a href="javascript:;" onClick="EditMessage(' + topico.ID + ', true)" class="option">Editar</a>' } if (topico.Status == 1) { html += '<a href="javascript:;" onClick="DeleteMessage(' + topico.ID + ', true)" class="option">Excluir</a>' } else { if (topico.Status == 5) { html += '<a href="javascript:;" onClick="RestoreMessage(' + topico.ID + ', true)" class="option">Restaurar</a>' } } html += "</div></div></div>"; if (topico.Respostas != "") { for (var r in topico.Respostas) { var resposta = topico.Respostas[r]; html += '<div class="resposta' + (resposta.Status == 5 ? " resposta-deletado" : "") + '"><a href="javascript:;" onclick="GetMessagesByUser(\'1\',\'' + resposta.UsuarioID + "','" + resposta.Login + '\')" title="Ver mensagens deste forense" class="mensagemusuario">&nbsp;</a><span class="texto">' + resposta.Texto + "</span>"; if (resposta.Imagem != "") { html += '<div class="imagem"><a href="javascript:;" onclick="$.facebox({ image: \'' + resposta.Imagem + '\' })" class="thickbox"><img src="' + resposta.Imagem + '" width="90" height="60" alt="Clique para exibir a imagem" /></a></div>' } html += '<div class="rodape">'; html += '<span class="autor">enviado por <strong>' + (resposta.Status ? '<a href="javascript:;" onclick="window.open(\'/Mercado/Admin/Usuario.aspx?id=' + resposta.UsuarioID + "', '', 'width=450,height=580,scrollbars=1')\">" + resposta.Login + "</a>" : resposta.Login) + "</strong> às " + resposta.Data + "</span>"; html += '<span class="funcoes"><a href="javascript:;" onclick="ReplyMessage(' + topico.ID + ')" class="option">Responder</a>'; if (resposta.IsEditable) { html += '<a href="javascript:;" onClick="EditMessage(' + resposta.ID + ', false)" class="option">Editar</a>' } if (resposta.Status == 1) { html += '<a href="javascript:;" onClick="DeleteMessage(' + resposta.ID + ', false)" class="option">Excluir</a>' } else { if (resposta.Status == 5) { html += '<a href="javascript:;" onClick="RestoreMessage(' + resposta.ID + ', false)" class="option">Restaurar</a>' } } html += "</span></div></div>" } } } if (result.Paginas != "") { var htmlPagger = ""; var paginas = eval("(" + result.Paginas + ")"); var cssClass = ""; var label = ""; for (var key in paginas) { if (paginas.hasOwnProperty(key)) { cssClass = ""; if (paginas[key] == page) { cssClass = "atual" } else { if (key == "prev") { cssClass = "anterior" } else { if (key == "next") { cssClass = "proximo" } } } label = (key != "prev" && key != "next" ? key : ""); if (pager == null) { htmlPagger += '<a href="javascript:;" alt="' + key + '" onclick="' + (isSearch ? "SearchMessages" : "GetMessages") + "(" + paginas[key] + ')" class="' + cssClass + '">' + label + "</a>" } else { htmlPagger += '<a href="javascript:;" alt="' + key + '" onclick="' + pager.replace("{0}", paginas[key]) + '" class="' + cssClass + '">' + label + "</a>" } } } $(".paginacao").html(htmlPagger) } $("#mensagens").html(html) } function LoadMenu(a) { var b = ""; var c = '<a href="javascript:;" onclick="[METHOD]" class="option">[TITLE]</a>'; if (!a) { b += c.replace("[METHOD]", "GetMessages(1)").replace("[TITLE]", "Atualizar"); b += c.replace("[METHOD]", "NewMessage()").replace("[TITLE]", "Nova Mensagem") } else { b += c.replace("[METHOD]", "GetMessages(1)").replace("[TITLE]", "Voltar para Mensagens") } b += c.replace("[METHOD]", "NewSearch()").replace("[TITLE]", "Pesquisar"); b += '<a href="Resumo.aspx" class="option">Resumo</a>'; $(".comandos").html(b) } function AdicionarFavorito(a) { ValidateSession(function(c) { if (c) { var b = "{topicoID: '" + a + "'}"; ajaxProxy(webService, "AdicionarFavorito", b, function(d) { if (d.Error) { alert(d.Error) } else { alert(d.Return) } }) } }, "adicionar favorito") };
