app.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Number.prototype.currency = function() { return '$' + this.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') };
  2. function seconds_to_time(s) {
  3. var duration = moment.duration(s, 'seconds');
  4. return duration.format("DD-hh:mm");
  5. }
  6. function ts_to_remaining(ts) {
  7. return moment.unix(ts).fromNow();
  8. }
  9. function log() {
  10. console.log('LOG >>>', arguments);
  11. }
  12. var app = new Vue({
  13. el: '#app',
  14. data: {
  15. title: "guntrader",
  16. queries: {},
  17. debug: false,
  18. account: false,
  19. new_account: "",
  20. search_visible: false,
  21. query: {
  22. url: null,
  23. search: null,
  24. maxprice: null,
  25. minprice: null,
  26. restricted: null,
  27. }
  28. },
  29. mounted: function () {
  30. // hide splash
  31. var self = this;
  32. var splash = document.getElementById("splash");
  33. splash.style.display = "none";
  34. // unhide main container after mount
  35. this.$el.style.display = "block";
  36. this.update();
  37. window.addEventListener('hashchange', function() {
  38. self.update()
  39. }, false);
  40. },
  41. computed: {
  42. query_url: {
  43. get() {
  44. if (this.query.search || this.query.maxprice || this.query.minprice || this.query.restricted) {
  45. var query = "http://www.egun.de/market/list_items.php?mode=qry&plusdescr=off";
  46. if (this.query.search) {query += "&query=" + this.query.search.replace(" ", "+");}
  47. if (this.query.restricted) {query += "&ewb=1";}
  48. if (this.query.maxprice) {query += "&maxprice=" + this.query.maxprice;}
  49. if (this.query.minprice) {query += "&minprice=" + this.query.minprice;}
  50. return query;
  51. } else {
  52. return this.query.url
  53. }
  54. },
  55. set(newVal){
  56. //this function will run whenever the input changes
  57. this.query.url = newVal;
  58. }
  59. }
  60. },
  61. methods: {
  62. update: function () {
  63. this.account = window.location.hash.slice(1)
  64. if (this.account) {
  65. console.log(this.account);
  66. this.load_account();
  67. }
  68. },
  69. load_account: function () {
  70. var self = this;
  71. fetch('load/' + self.account)
  72. .then(function(response) {
  73. return response.json();
  74. })
  75. .then(function(myJson) {
  76. self.queries = myJson;
  77. });
  78. },
  79. display_query: function (queryname) {
  80. let q = queryname.split("query=")[1].split("&")[0].replace(/[+]/g, " ")
  81. return q
  82. },
  83. add_query: function () {
  84. this.$set(this.queries, this.query_url, {});
  85. },
  86. delete_query: function (queryname) {
  87. this.$delete(this.queries, queryname);
  88. },
  89. save_account: function () {
  90. let account = {}
  91. account[this.account] = {queries: this.queries};
  92. console.log(JSON.stringify(account) );
  93. var self = this;
  94. console.log("Saving");
  95. fetch("http://localhost:8000/save", {
  96. method: "POST",
  97. body: JSON.stringify(account)
  98. }).then(res => {
  99. console.log("Request complete! response:", res);
  100. });
  101. }
  102. },
  103. watch: {
  104. },
  105. });