app.js 4.2 KB

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