- /*var datas = [
- {date: "2018-09-09", value: 10},
- {date: "2018-09-08", value: 5},
- {date: "2018-09-07", value: 8},
- {date: "2018-09-07", value: 7},
- {date: "2018-09-06", value: 9},
- {date: "2018-09-05", value: 6},
- {date: "2018-09-04", value: 5},
- {date: "2018-09-03", value: 4},
- {date: "2018-09-02", value: 7},
- {date: "2018-08-02", value: 1}
- ]
-
- /*
- var parser = function(data) {
- var stats = {};
- console.log(data);
- for (var d in data) {
- console.log(data[d].date);
- var dateTime = new Date(data[d].date).toISOString();
- var timestamp = Math.floor(dateTime / 1000);
- let toTimestamp = strDate => Date.parse(data[d].date);
- stats[timestamp] = data[d].value;
- }
- console.log(stats);
- return stats;
-
- };
- */
- /*
- var stats = {};
- var parser = function(data) {
- //console.log(data);
- for (var d in data) {
- //console.log("date:"+data[d]);
- //var date = new Date(data[d].date); // Date of activity
- //var sec = date.getTime()/1000; // Convert to sec
- stats[d] = data[d];
- //cal.subDomainTextFormat(d, data[d]);
- }
-
- return stats;
- };
- */
- //var mainurl = "http://localhost:8080/test.json";
- var mainurl = "/?action=main_cal";
- var cal = new CalHeatMap();
-
- setUpCal();
- function setUpCal(){
- var datethen = new Date();
- datethen.setMonth(datethen.getMonth() - 9);
-
- cal.init({
- //data: "http://localhost:8080/index.php?action=main_cal",
- data: mainurl,
- dataType: "json",
- //afterLoadData: parser,
- start: datethen,
- id : "graph",
- domain : "month", // Group data by month
- subDomain : "day", // Split each month by days
- cellSize: 15,
- cellPadding: 2,
- cellRadius: 3,
- domainGutter:2,
- subDomainTitleFormat: {
- empty: "{date}",
- filled: "{count} - {date}"
- },
- //itemSelector: "#cal-heatmap",
- range : 10, // Just display 10 months
- legend: [2,3,4,5,6,7,8,9], // Custom threshold for the scale
- tooltip: false,
- displayLegend: false,
- label: {
- position: "bottom",
- align: "center",
- offset: {x:0,y:-10},
- },
- considerMissingDataAsZero: true,
- legendHorizontalPosition: "center",
- legendColors: {
- min: "#6b6de5",
- max: "#46e260",
- },
- subDomainTextFormat: function(date ,value) {
- var origdate = ""+date;
- date = origdate.split('00:00:00')[0]+'00:00:00';
- var date = parseInt((new Date(date).getTime() /1000 ).toFixed(0))
- var finval = 0;
-
- /***
- * All of this cos of a damn bug!
- * https://github.com/wa0x6e/cal-heatmap/issues/257
- */
- $.ajax({
- url: "/?action=date_average&date="+date,
- async: false,
- dataType: 'html',
- success: function (response) {
- //console.log(response);
- finval = response;
- console.log("origdate: "+origdate+" date: "+date+" val:"+response);
- }
- });
-
- if (finval == 0){finval = "";}
- return finval;
- },
- highlight: "now",
- onClick: function(date, count) {
- //alert("Oh gosh, " + count + " things occured on " + date.toISOString());
- updateDayLog(date.toISOString().slice(0, 19).replace('T', ' '));
- }
- });
- }
-
-
- function checkStats(timestamp){
- var finval = 0;
- $.getJSON('test.json', function (data) {
- $.each(data, function(i, field){
- //console.log(field);
- if(timestamp == i){
- console.log("found: "+field);
- finval = field;
- }
-
- });
- //if (finval == 0){finval = "";}
- return finval;
- });
-
- }
-
- var availableTags = [
- "-ansa",
- "+phishing",
- "+chill",
- "@home",
- ];
- $( function() {
-
- $( "#tags" ).autocomplete({
- source: availableTags
- });
- } );
-
- function updateTime() {
- var now = new Date();
- var datestring = now.getFullYear() + "-" + ("0"+(now.getMonth()+1)).slice(-2) + "-" + ("0" + now.getDate()).slice(-2) + " " + ("0" + now.getHours()).slice(-2) + ":" + ("0" + now.getMinutes()).slice(-2);
- $("span.date").html(datestring.toString());
- }
-
- $("#happyval").on("input change", function() {
- $(".hpyno").html("[" + $("#happyval").val() + "]");
- });
- removespan = function(span) {
- span.remove();
- }
-
- /***
- * filter adding and removing code
- */
- var filter = 0;
- function addFilterPos(word){
- filter++;
- $('.filtitle').after('<span class="filpos" onclick="removefilter(this);">'+word+'</span>');
- $(".filter").show();
- }
- function addFilterNeg(word){
- filter++;
- $('.filtitle').after('<span class="filneg" onclick="removefilter(this);">'+word+'</span>');
- $(".filter").show();
- }
- function addFilterCon(word){
- filter++;
- $('.filtitle').after('<span class="filcon" onclick="removefilter(this);">'+word+'</span>');
- $(".filter").show();
- }
- removefilter = function(span) {
- filter--;
- span.remove();
- if(filter == 0){
- $(".filter").hide();
- updateCal("/?action=main_cal");
- }
- }
-
- /***
- * updating description
- */
- function updatePretext(){
- var toadd = $("#tags").val();
- switch(toadd.charAt(0)){
- case "+":
- $('.pretext').after('<span class="prepos" onclick="removespan(this);">'+toadd+'</span>');
- break;
- case "-":
- $('.pretext').after('<span class="preneg" onclick="removespan(this);">'+toadd+'</span>');
- break;
- case "@":
- $('.preview').append('<span class="precon" onclick="removespan(this);">'+toadd+'</span>');
- break;
- default:
- $(".pretext").html( toadd );
- }
- $("#tags").val("");
- //$(".pretext").html( );
- }
-
- function submitMood(){
- var toadd = $(".preview").text();
-
- console.log(toadd);
- }
-
- /***
- * update main calendar values
- */
- function updateCal(url){ //updateCal("/?action=main_cal");
- mainurl = url;
- cal.destroy(function(){
- cal = new CalHeatMap();
- setUpCal();
- });
- }
-
- /***
- * update specific day log
- */
- function updateDayLog(date){
- $.ajax({
- url: "/?action=date&date="+date,
- async: true,
- dataType: 'html',
- success: function (response) {
- //console.log(response);
- if(response != "0"){
- $(".previewhist").html(response);
- }
- }
- });
- }
-
- /***
- * update latest tags
- */
- function updateLatest(tag){
- var string = "";
- switch(tag) {
- case "pos":
- string = "<div class=\"sub-header\">Latest 5 positive</div>";
- break;
- case "neg":
- string = "<div class=\"sub-header\">Latest 5 negative</div>";
- break;
- case "con":
- string = "<div class=\"sub-header\">Latest 5 context</div>";
- break;
- }
- $.ajax({
- url: "/?action=latest&tag="+tag,
- async: true,
- dataType: 'json',
- success: function (response) {
- if(response != "0"){
-
- $.each(response, function(i, item) {
- var ntag = tag.charAt(0).toUpperCase() + tag.substr(1);
- string += "<span onclick=\"addFilter"+ntag+"($(this).text());\">"+item+"</span><br />";
- });
-
- //console.log(response);
- $(".lat"+tag).html(string);
- }
- }
- });
- }
-
- /***
- * update top tags
- */
- function updateTop(tag){
- var string = "";
- switch(tag) {
- case "pos":
- string = "<div class=\"sub-header\">Top 5 positive</div>";
- break;
- case "neg":
- string = "<div class=\"sub-header\">Top 5 negative</div>";
- break;
- case "con":
- string = "<div class=\"sub-header\">Top 5 context</div>";
- break;
- }
- $.ajax({
- url: "/?action=top&tag="+tag,
- async: true,
- dataType: 'json',
- success: function (response) {
- if(response != "0"){
-
- $.each(response, function(i, item) {
- //console.log(item);
- var ntag = tag.charAt(0).toUpperCase() + tag.substr(1);
- string += "<span onclick=\"addFilter"+ntag+"($(this).text());\">"+item+"</span><br />";
- });
-
- //console.log(response);
- $(".top"+tag).html(string);
- }
- }
- });
- }
-
- /***
- * update available tags
- */
- function updateAvailable(){
- $.ajax({
- url: "/?action=alltag",
- async: true,
- dataType: 'json',
- success: function (response) {
- if(response != "0"){
- availableTags = response;
- $( "#tags" ).autocomplete({
- source: availableTags
- });
- }
- }
- });
- }
-
- /***
- * display filtered calendar
- */
- function applyFilters(){
- var filttstring ="";
- $(".filter").find("span").each(function() {
- filttstring += $(this).html();
- });
- filttstring = filttstring.slice(8);
- filttstring = filttstring.replace(/\s+/g, '');
- filttstring = filttstring.replace(/\+/g , "%2B");
- //filttstring = encodeURIComponent(filttstring);
- //console.log(filttstring);
- updateCal("/?action=filtered_cal&filter="+filttstring);
- }
-
- /***
- * save to moodlog
- */
- function submitMood(){
- var filttstring = [];
- var sendString = "";
- $(".preview").find("span").each(function() {
- filttstring.push( $(this).html() );
- });
-
- filttstring.shift(); // remove date
- filttstring[0] = filttstring[0].replace(/\[/g, ''); //fix no.
- filttstring[0] = filttstring[0].replace(/\]/g, '|'); //fix no.
-
- for (var i = 0, len = filttstring.length; i < len; i++) { //tidy for URL
- filttstring[i] = filttstring[i].replace(/\+/g , "%2B");
- filttstring[i] = filttstring[i].replace(/ /g , "%20");
- sendString += filttstring[i]
- }
-
- console.log(sendString);
- $.ajax({
- url: "/?action=save&log="+sendString,
- async: true,
- dataType: 'html',
- success: function (response) {
- if(response != "0"){
-
- var now = new Date();
- var datestring = now.getFullYear() + "-" + ("0"+(now.getMonth()+1)).slice(-2) + "-" + ("0" + now.getDate()).slice(-2);
- var fulldatestring = now.getFullYear() + "-" + ("0"+(now.getMonth()+1)).slice(-2) + "-" + ("0" + now.getDate()).slice(-2) + " " + ("0" + now.getHours()).slice(-2) + ":" + ("0" + now.getMinutes()).slice(-2);
-
- $(".preview").html("<span class=\"date\">"+fulldatestring+"</span><span class=\"hpyno\">[5]</span><span class=\"pretext\" onclick=\"updatePretext();\"></span></div>")
- $("#happyval").val(5);
- updateCal("/?action=main_cal");
- updateDayLog(datestring);
- updateTop("pos");
- updateTop("neg");
- updateTop("con");
- updateLatest("pos");
- updateLatest("neg");
- updateLatest("con");
- }
- }
- });
-
-
- }
-
- /***
- * once app loaded
- */
- $(document).ready(function(){
-
- $('#tags').keypress(function(e){
- if(e.keyCode==13)
- updatePretext();
- });
-
- updateTop("pos");
- updateTop("neg");
- updateTop("con");
- updateLatest("pos");
- updateLatest("neg");
- updateLatest("con");
-
- updateAvailable();
-
- updateTime();
- setInterval(updateTime, 5000); // 5 * 1000 miliseconds
- var interval1 = setInterval(function () { updateCal(mainurl); }, 300000); // update every 5 mins (300000)
-
- var interval1 = setInterval(function () { updateTop("pos"); }, 300000);
- var interval1 = setInterval(function () { updateTop("neg"); }, 300000);
- var interval1 = setInterval(function () { updateTop("con"); }, 300000);
- var interval1 = setInterval(function () { updateLatest("pos"); }, 300000);
- var interval1 = setInterval(function () { updateLatest("neg"); }, 300000);
- var interval1 = setInterval(function () { updateLatest("con"); }, 300000);
- });