app.js 3.6 KB

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