dynamic/js/script.js
2017-12-05 22:18:14 +03:00

315 lines
11 KiB
JavaScript

// Copyright 2016-2017 Sudnitsyna Anna lessmore.pw
// Licensed under GPLv3
var html = '';
var options = '';
var checks = '';
var echo = '';
var author = '';
var this_page = 1;
var show_rows = 5;
var from = 0;
//var date = new Date(new Date().getTime() + 60 * 1000);
var date = new Date;
date.setDate(date.getDate() + 7);
var echo = document.cookie;
hash = parseLocationHash();
if (hash.matched) {
console.log("We are here");
requestMessage(hash.hash);
}
$(function() {
//extentions
$.ajax({
url:"https://dynamic.lessmore.pw/idec/x/features",
success:function(data) {
$("#extentions").html("<b>" + data.replace("\n","<br>") + "</b>");
}
})
//initial load
if (echo != '') {
requestEcho();
} else {
requestEmptyEcho();
}
//create echoes list
$.ajax({
url: "https://dynamic.lessmore.pw/search",
type: "POST",
data: '{"sort": [{ "date": { "order": "desc" }}, { "_score": { "order": "desc" }}],"aggs": {"my_fields": { "terms": { "field": "echo","size": 1000}}}}}',
success:function(messages) {
for (i in messages['aggregations']['my_fields']['buckets']) {
var msg = messages['aggregations']['my_fields']['buckets'][i]['key'];
var doc_count = messages['aggregations']['my_fields']['buckets'][i]['doc_count'];
msg_test = msg + " ";
echo_test = echo + " ";
if((echo_test).indexOf(msg_test) > -1 && echo != '') {
options += '<option value="' + msg + '" selected>' + msg + " (<b>" + doc_count + "</b>)</option>";
checks += '<span class="row"><input type="checkbox" class="echoes" name="ec" value="' + msg + '" checked><label>'
+ msg
+ '</label> <span style="color:#838784">('+ doc_count
+ ')</span><br></span>';
} else {
options += '<option value="' + msg + '" >' + msg + " (<b>" + doc_count + "</b>)</option>";
checks += '<span class="row"><input type="checkbox" class="echoes" name="ec" value="' + msg + '"><label>'
+ msg
+ '</label> <span style="color:#838784">('+ doc_count
+ ')</span><br></span>';}
}
$('#echoSelected').html("Выбранные эхи: " + document.cookie);
$('#echoSelector').html(options);
$('#echoCheckBox').html(checks);}
})
$('.echoes:checked').each(function() {
echo = '';
echo += $(this).val()[i] + " ";
});
var boxes = $("input[type=checkbox]");
$("#echoCheckBox").on('change', function() {
echo = '';
for (var i=0; i<$("input[type=checkbox]:checked").length; i++) {
echo += $("input[type=checkbox]:checked")[i]["defaultValue"] + " ";}
document.cookie = echo +"; path=/; expires=" + date.toUTCString();
$('#echoSelected').html("Выбранные эхи: " + echo);
});
$('#echoSelector').on('change', function() {
echo = '';
html = '';
for (i in $(this).val()) {
echo += $(this).val()[i] + " ";
}
document.cookie = echo +"; path=/; expires=" + date.toUTCString();
requestEcho();
$('#echoSelected').html("Выбранные эхи: " + echo)
if (echo == "") requestEmptyEcho();
});
$('#searchInput').on('change', function() {
text = $(this).val();
simpleTextSearch(text);
})
//scroll to show more messages
$(window).scroll(function() {
hash = parseLocationHash();
// console.log($(document).height() - $(window).height()-$(window).scrollTop());
if ($(document).height() - $(window).height() - $(window).scrollTop() < 1.5 && hash.matched != true) {
showMore();
//console.log($(document).height() - $(window).height() - $(window).scrollTop());
}
});
}) //end of .ready() method
// Main AJAX function
function ajaxRequest(data) {
$.ajax({
url:"https://dynamic.lessmore.pw/search",
type:"POST",
data: data,
success:function(messages){
callback(messages);
$("#mes").html(html);
}
})
}
// Cleanup AJAX function
function ajaxCleanupRequest(data) {
$.ajax({
url:"https://dynamic.lessmore.pw/search",
type:"POST",
data: data,
success:function(messages){
cleanupCallback(messages);
$("#mes").html(html);
}
})
}
function requestEmptyEcho() {
var data = '{"sort": [{"date": {"order": "desc"}}, {"_score": {"order": "desc" }}], "size": 5}';
ajaxRequest(data);
}
function requestEcho() {
var data = '{"sort": [{"date": {"order": "desc"}}, {"_score": { "order": "desc" }}], "query": {"query_string" : {"fields": ["echo"], "query": "' + echo + '"}}, "size":' + 5 + '}';
ajaxRequest(data);
}
function requestEmptyEchoOffset() {
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}],"size":'+ show_rows + ', "from":'+ from + '}';
ajaxRequest(data);
}
function requestEchoOffset() {
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["echo"], "query" :"' + echo + '"}}, "size":'+ show_rows + ', "from":'+ from + '}';
ajaxRequest(data);
}
function requestAuthorOffset() {
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["author"], "query" :"' + author + '"}}, "size":'+ show_rows + ', "from":'+ from + '}';
echo = '';
console.log("Author offset");
ajaxRequest(data);
}
function requestAuthorMessages(_author) {
author = _author;
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["author"], "query" :"' + author + '"}}, "size": 5 }';
html = '';
echo = '';
console.log(author);
console.log(data);
ajaxRequest(data);
}
function requestMessage(msgid) {
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["msgid"], "query" :"' + msgid + '"}}, "size": 5 }';
console.log("Link to message: " + msgid);
html = '';
echo = '';
author = '';
ajaxRequest(data);
}
// Search functions
// ----------------
function simpleTextSearch(text) {
var data = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"query" :"' + text + '"}}, "size": 25 }';
console.log(text);
html = '';
echo = '';
author = '';
ajaxRequest(data);
}
//show messages
function callback(messages) {
var msgArray = messages["hits"]["hits"];
console.log(msgArray);
for (hit in msgArray) {
var msgItem = msgArray[hit]['_source'];
var t = new Date(msgItem['date'] * 1);
var t_formatted = t.getFullYear() + "/" + (t.getMonth() + 1) + "/" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
msg = msgItem['message'].replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(new RegExp("\n",'g'), "<br>\n");
var to =
html += '<div class="msg"><p align="right">' + t_formatted + '</p> <p style="font: 1.5em Open Sans, sans-serif;">'
+ msgItem['subg'] + '</p><p>Echo: '
+ msgItem['echo'] + '<br>Author: ' + '<a href="#" onclick="requestAuthorMessages(\'' + msgItem['author'] + '\');">' + msgItem['author'] + '</a>'
+ '<br>To: ' + '<a href="#" onclick="requestAuthorMessages(\'' + msgItem['to'] + '\', true);">' + msgItem['to'] + '</a>'
+ '</p><p>' + msg.autoLink()
+ '</p><p align="right">'
+ '<a href="?' + msgItem['msgid'] +'" onclick="requestMessage(\'' + msgItem['msgid'] + '\');">#' + msgItem['msgid'] + '</a></p></div>';
}
}
// Clenaup HTML and show messages
function cleanupCallback(messages) {
var msgArray = messages["hits"]["hits"];
for (hit in msgArray) {
var msgItem = msgArray[hit]['_source'];
var t = new Date(msgItem['date'] * 1);
var t_formatted = t.getFullYear() + "/" + (t.getMonth() + 1) + "/" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
msg = msgItem['message'].replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(new RegExp("\n",'g'), "<br>\n");
html = '';
echo = '';
var to = '<div class="msg"><p align="right">' + t_formatted + '</p> <p style="font: 1.5em Open Sans, sans-serif;">'
+ msgItem['subg'] + '</p><p>Echo: '
+ msgItem['echo'] + '<br>Author: ' + '<a href="#" onclick="requestAuthorMessages(\'' + msgItem['author'] + '\');">' + msgItem['author'] + '</a>'
+ '<br>To: ' + msgItem['to']
+ '</p><p>' + msg
+ '</p><p align="right">'
+ msgItem['msgid'] + '</a>' + '</p></div>';
}
}
//show more messages
function showMore(){
console.log("Show more messages, please.");
console.log("Selected echo: " + echo);
console.log("Selected author: " + author);
this_page += 1;
from += show_rows;
if (author != '') {
requestAuthorOffset();
} else if (echo != '') {
requestEchoOffset();
} else {
requestEmptyEchoOffset();
}
}
function searchEcho() {
var input, filter, box, row;
input = $("#myInput").val();
input = input.toLowerCase();
box = $("label");
row = $(".row");
for (i = 0; i<box.length; i++) {
if (box[i].innerHTML.indexOf(input) > -1) {
row[i].style.display = "";
} else {
row[i].style.display = "none";
}
}
}
function clearFilter() {
document.cookie = "";
}
function showMenu() {
if ($("#dropdown-content").css("display") == "none") {
$("#dropdown-content").css("display","block");
} else {
$("#dropdown-content").css("display","none");
}
}
function showSearch() {
if ($("#searchInput").css("display") == "none") {
$("#searchInput").css("display","block");
} else {
$("#searchInput").css("display","none");
}
}
// Parsing functions
function parseLocationHash() {
var hash = window.location.search.replace(/\?/, "");
if (hash) {
if (hash.match(/(.{20})/)) {
console.log("Matched! " + hash);
return {matched: true, hash: hash};
} else {
return {matched: false};
}
} else {
return {matched: false};
}
}