Newer
Older
moodlog_web / script.js
root on 7 May 2019 10 KB Initial commit
  1. /*var datas = [
  2. {date: "2018-09-09", value: 10},
  3. {date: "2018-09-08", value: 5},
  4. {date: "2018-09-07", value: 8},
  5. {date: "2018-09-07", value: 7},
  6. {date: "2018-09-06", value: 9},
  7. {date: "2018-09-05", value: 6},
  8. {date: "2018-09-04", value: 5},
  9. {date: "2018-09-03", value: 4},
  10. {date: "2018-09-02", value: 7},
  11. {date: "2018-08-02", value: 1}
  12. ]
  13.  
  14. /*
  15. var parser = function(data) {
  16. var stats = {};
  17. console.log(data);
  18. for (var d in data) {
  19. console.log(data[d].date);
  20. var dateTime = new Date(data[d].date).toISOString();
  21. var timestamp = Math.floor(dateTime / 1000);
  22. let toTimestamp = strDate => Date.parse(data[d].date);
  23. stats[timestamp] = data[d].value;
  24. }
  25. console.log(stats);
  26. return stats;
  27.  
  28. };
  29. */
  30. /*
  31. var stats = {};
  32. var parser = function(data) {
  33. //console.log(data);
  34. for (var d in data) {
  35. //console.log("date:"+data[d]);
  36. //var date = new Date(data[d].date); // Date of activity
  37. //var sec = date.getTime()/1000; // Convert to sec
  38. stats[d] = data[d];
  39. //cal.subDomainTextFormat(d, data[d]);
  40. }
  41.  
  42. return stats;
  43. };
  44. */
  45. //var mainurl = "http://localhost:8080/test.json";
  46. var mainurl = "/?action=main_cal";
  47. var cal = new CalHeatMap();
  48.  
  49. setUpCal();
  50. function setUpCal(){
  51. var datethen = new Date();
  52. datethen.setMonth(datethen.getMonth() - 9);
  53.  
  54. cal.init({
  55. //data: "http://localhost:8080/index.php?action=main_cal",
  56. data: mainurl,
  57. dataType: "json",
  58. //afterLoadData: parser,
  59. start: datethen,
  60. id : "graph",
  61. domain : "month", // Group data by month
  62. subDomain : "day", // Split each month by days
  63. cellSize: 15,
  64. cellPadding: 2,
  65. cellRadius: 3,
  66. domainGutter:2,
  67. subDomainTitleFormat: {
  68. empty: "{date}",
  69. filled: "{count} - {date}"
  70. },
  71. //itemSelector: "#cal-heatmap",
  72. range : 10, // Just display 10 months
  73. legend: [2,3,4,5,6,7,8,9], // Custom threshold for the scale
  74. tooltip: false,
  75. displayLegend: false,
  76. label: {
  77. position: "bottom",
  78. align: "center",
  79. offset: {x:0,y:-10},
  80. },
  81. considerMissingDataAsZero: true,
  82. legendHorizontalPosition: "center",
  83. legendColors: {
  84. min: "#6b6de5",
  85. max: "#46e260",
  86. },
  87. subDomainTextFormat: function(date ,value) {
  88. var origdate = ""+date;
  89. date = origdate.split('00:00:00')[0]+'00:00:00';
  90. var date = parseInt((new Date(date).getTime() /1000 ).toFixed(0))
  91. var finval = 0;
  92.  
  93. /***
  94. * All of this cos of a damn bug!
  95. * https://github.com/wa0x6e/cal-heatmap/issues/257
  96. */
  97. $.ajax({
  98. url: "/?action=date_average&date="+date,
  99. async: false,
  100. dataType: 'html',
  101. success: function (response) {
  102. //console.log(response);
  103. finval = response;
  104. console.log("origdate: "+origdate+" date: "+date+" val:"+response);
  105. }
  106. });
  107.  
  108. if (finval == 0){finval = "";}
  109. return finval;
  110. },
  111. highlight: "now",
  112. onClick: function(date, count) {
  113. //alert("Oh gosh, " + count + " things occured on " + date.toISOString());
  114. updateDayLog(date.toISOString().slice(0, 19).replace('T', ' '));
  115. }
  116. });
  117. }
  118.  
  119.  
  120. function checkStats(timestamp){
  121. var finval = 0;
  122. $.getJSON('test.json', function (data) {
  123. $.each(data, function(i, field){
  124. //console.log(field);
  125. if(timestamp == i){
  126. console.log("found: "+field);
  127. finval = field;
  128. }
  129.  
  130. });
  131. //if (finval == 0){finval = "";}
  132. return finval;
  133. });
  134.  
  135. }
  136.  
  137. var availableTags = [
  138. "-ansa",
  139. "+phishing",
  140. "+chill",
  141. "@home",
  142. ];
  143. $( function() {
  144.  
  145. $( "#tags" ).autocomplete({
  146. source: availableTags
  147. });
  148. } );
  149.  
  150. function updateTime() {
  151. var now = new Date();
  152. 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);
  153. $("span.date").html(datestring.toString());
  154. }
  155.  
  156. $("#happyval").on("input change", function() {
  157. $(".hpyno").html("[" + $("#happyval").val() + "]");
  158. });
  159. removespan = function(span) {
  160. span.remove();
  161. }
  162.  
  163. /***
  164. * filter adding and removing code
  165. */
  166. var filter = 0;
  167. function addFilterPos(word){
  168. filter++;
  169. $('.filtitle').after('<span class="filpos" onclick="removefilter(this);">'+word+'</span>');
  170. $(".filter").show();
  171. }
  172. function addFilterNeg(word){
  173. filter++;
  174. $('.filtitle').after('<span class="filneg" onclick="removefilter(this);">'+word+'</span>');
  175. $(".filter").show();
  176. }
  177. function addFilterCon(word){
  178. filter++;
  179. $('.filtitle').after('<span class="filcon" onclick="removefilter(this);">'+word+'</span>');
  180. $(".filter").show();
  181. }
  182. removefilter = function(span) {
  183. filter--;
  184. span.remove();
  185. if(filter == 0){
  186. $(".filter").hide();
  187. updateCal("/?action=main_cal");
  188. }
  189. }
  190.  
  191. /***
  192. * updating description
  193. */
  194. function updatePretext(){
  195. var toadd = $("#tags").val();
  196. switch(toadd.charAt(0)){
  197. case "+":
  198. $('.pretext').after('<span class="prepos" onclick="removespan(this);">'+toadd+'</span>');
  199. break;
  200. case "-":
  201. $('.pretext').after('<span class="preneg" onclick="removespan(this);">'+toadd+'</span>');
  202. break;
  203. case "@":
  204. $('.preview').append('<span class="precon" onclick="removespan(this);">'+toadd+'</span>');
  205. break;
  206. default:
  207. $(".pretext").html( toadd );
  208. }
  209. $("#tags").val("");
  210. //$(".pretext").html( );
  211. }
  212.  
  213. function submitMood(){
  214. var toadd = $(".preview").text();
  215.  
  216. console.log(toadd);
  217. }
  218.  
  219. /***
  220. * update main calendar values
  221. */
  222. function updateCal(url){ //updateCal("/?action=main_cal");
  223. mainurl = url;
  224. cal.destroy(function(){
  225. cal = new CalHeatMap();
  226. setUpCal();
  227. });
  228. }
  229.  
  230. /***
  231. * update specific day log
  232. */
  233. function updateDayLog(date){
  234. $.ajax({
  235. url: "/?action=date&date="+date,
  236. async: true,
  237. dataType: 'html',
  238. success: function (response) {
  239. //console.log(response);
  240. if(response != "0"){
  241. $(".previewhist").html(response);
  242. }
  243. }
  244. });
  245. }
  246.  
  247. /***
  248. * update latest tags
  249. */
  250. function updateLatest(tag){
  251. var string = "";
  252. switch(tag) {
  253. case "pos":
  254. string = "<div class=\"sub-header\">Latest 5 positive</div>";
  255. break;
  256. case "neg":
  257. string = "<div class=\"sub-header\">Latest 5 negative</div>";
  258. break;
  259. case "con":
  260. string = "<div class=\"sub-header\">Latest 5 context</div>";
  261. break;
  262. }
  263. $.ajax({
  264. url: "/?action=latest&tag="+tag,
  265. async: true,
  266. dataType: 'json',
  267. success: function (response) {
  268. if(response != "0"){
  269.  
  270. $.each(response, function(i, item) {
  271. var ntag = tag.charAt(0).toUpperCase() + tag.substr(1);
  272. string += "<span onclick=\"addFilter"+ntag+"($(this).text());\">"+item+"</span><br />";
  273. });
  274.  
  275. //console.log(response);
  276. $(".lat"+tag).html(string);
  277. }
  278. }
  279. });
  280. }
  281.  
  282. /***
  283. * update top tags
  284. */
  285. function updateTop(tag){
  286. var string = "";
  287. switch(tag) {
  288. case "pos":
  289. string = "<div class=\"sub-header\">Top 5 positive</div>";
  290. break;
  291. case "neg":
  292. string = "<div class=\"sub-header\">Top 5 negative</div>";
  293. break;
  294. case "con":
  295. string = "<div class=\"sub-header\">Top 5 context</div>";
  296. break;
  297. }
  298. $.ajax({
  299. url: "/?action=top&tag="+tag,
  300. async: true,
  301. dataType: 'json',
  302. success: function (response) {
  303. if(response != "0"){
  304.  
  305. $.each(response, function(i, item) {
  306. //console.log(item);
  307. var ntag = tag.charAt(0).toUpperCase() + tag.substr(1);
  308. string += "<span onclick=\"addFilter"+ntag+"($(this).text());\">"+item+"</span><br />";
  309. });
  310.  
  311. //console.log(response);
  312. $(".top"+tag).html(string);
  313. }
  314. }
  315. });
  316. }
  317.  
  318. /***
  319. * update available tags
  320. */
  321. function updateAvailable(){
  322. $.ajax({
  323. url: "/?action=alltag",
  324. async: true,
  325. dataType: 'json',
  326. success: function (response) {
  327. if(response != "0"){
  328. availableTags = response;
  329. $( "#tags" ).autocomplete({
  330. source: availableTags
  331. });
  332. }
  333. }
  334. });
  335. }
  336.  
  337. /***
  338. * display filtered calendar
  339. */
  340. function applyFilters(){
  341. var filttstring ="";
  342. $(".filter").find("span").each(function() {
  343. filttstring += $(this).html();
  344. });
  345. filttstring = filttstring.slice(8);
  346. filttstring = filttstring.replace(/\s+/g, '');
  347. filttstring = filttstring.replace(/\+/g , "%2B");
  348. //filttstring = encodeURIComponent(filttstring);
  349. //console.log(filttstring);
  350. updateCal("/?action=filtered_cal&filter="+filttstring);
  351. }
  352.  
  353. /***
  354. * save to moodlog
  355. */
  356. function submitMood(){
  357. var filttstring = [];
  358. var sendString = "";
  359. $(".preview").find("span").each(function() {
  360. filttstring.push( $(this).html() );
  361. });
  362. filttstring.shift(); // remove date
  363. filttstring[0] = filttstring[0].replace(/\[/g, ''); //fix no.
  364. filttstring[0] = filttstring[0].replace(/\]/g, '|'); //fix no.
  365.  
  366. for (var i = 0, len = filttstring.length; i < len; i++) { //tidy for URL
  367. filttstring[i] = filttstring[i].replace(/\+/g , "%2B");
  368. filttstring[i] = filttstring[i].replace(/ /g , "%20");
  369. sendString += filttstring[i]
  370. }
  371.  
  372. console.log(sendString);
  373. $.ajax({
  374. url: "/?action=save&log="+sendString,
  375. async: true,
  376. dataType: 'html',
  377. success: function (response) {
  378. if(response != "0"){
  379.  
  380. var now = new Date();
  381. var datestring = now.getFullYear() + "-" + ("0"+(now.getMonth()+1)).slice(-2) + "-" + ("0" + now.getDate()).slice(-2);
  382. 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);
  383.  
  384. $(".preview").html("<span class=\"date\">"+fulldatestring+"</span><span class=\"hpyno\">[5]</span><span class=\"pretext\" onclick=\"updatePretext();\"></span></div>")
  385. $("#happyval").val(5);
  386. updateCal("/?action=main_cal");
  387. updateDayLog(datestring);
  388. updateTop("pos");
  389. updateTop("neg");
  390. updateTop("con");
  391. updateLatest("pos");
  392. updateLatest("neg");
  393. updateLatest("con");
  394. }
  395. }
  396. });
  397.  
  398. }
  399.  
  400. /***
  401. * once app loaded
  402. */
  403. $(document).ready(function(){
  404.  
  405. $('#tags').keypress(function(e){
  406. if(e.keyCode==13)
  407. updatePretext();
  408. });
  409.  
  410. updateTop("pos");
  411. updateTop("neg");
  412. updateTop("con");
  413. updateLatest("pos");
  414. updateLatest("neg");
  415. updateLatest("con");
  416.  
  417. updateAvailable();
  418.  
  419. updateTime();
  420. setInterval(updateTime, 5000); // 5 * 1000 miliseconds
  421. var interval1 = setInterval(function () { updateCal(mainurl); }, 300000); // update every 5 mins (300000)
  422.  
  423. var interval1 = setInterval(function () { updateTop("pos"); }, 300000);
  424. var interval1 = setInterval(function () { updateTop("neg"); }, 300000);
  425. var interval1 = setInterval(function () { updateTop("con"); }, 300000);
  426. var interval1 = setInterval(function () { updateLatest("pos"); }, 300000);
  427. var interval1 = setInterval(function () { updateLatest("neg"); }, 300000);
  428. var interval1 = setInterval(function () { updateLatest("con"); }, 300000);
  429. });
Buy Me A Coffee