app.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const MIN_DATE = dayjs("2014-01-01")
  2. const RANGE_MAX = dayjs().diff(MIN_DATE,"d")
  3. const coinTop50 = {
  4. data() {
  5. return {
  6. date :document.getElementById("startDate").innerText,
  7. items: [],
  8. rangeDate:RANGE_MAX,
  9. maxRange:RANGE_MAX,
  10. source:axios.CancelToken.source(),
  11. datepicker:null
  12. }
  13. },
  14. created(){
  15. this.requestData = _.debounce(this.requestData,200)
  16. },
  17. mounted() {
  18. this.requestData(this.date)
  19. const elems = document.querySelectorAll('.datepicker');
  20. this.datepicker = M.Datepicker.init(elems, {onClose:()=>{
  21. this.date = this.datepicker.date.toISOString().slice(0,10)
  22. }})[0];
  23. },
  24. watch:{
  25. date(newDate,oldDate){
  26. this.requestData(newDate)
  27. },
  28. rangeDate(newValue){
  29. this.date = MIN_DATE.add(newValue,"d").format("YYYY-MM-DD");
  30. }
  31. },
  32. methods:{
  33. requestData(sDate){
  34. const d = new Date(sDate)
  35. const url = `/top50json/${d.getDate()}-${("0"+(d.getMonth()+1)).slice(-2)}-${d.getFullYear()}/`;
  36. axios.get(url)
  37. .then(this.handleJsonUpdate)
  38. .catch(function (thrown) {
  39. if (axios.isCancel(thrown)) {
  40. console.log('Request canceled', thrown.message);
  41. } else {
  42. console.log(thrown)
  43. }
  44. });
  45. },
  46. handleJsonUpdate(response){
  47. this.items = response.data
  48. },
  49. displayRoundedPrice(price){
  50. var precision = -Math.floor( Math.log10(price) + 1);
  51. return price.toLocaleString();
  52. },
  53. getChart(id){
  54. window.location = "/coin/chart/"+id+"/";
  55. }
  56. }
  57. }
  58. Vue.createApp(coinTop50).mount('#app')