app.js 3.8 KB

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