index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var app = new Vue({
  2. el: "#app",
  3. data: {
  4. search: {
  5. categoryId: -1,
  6. title: "",
  7. tag: "",
  8. author: "",
  9. orderBy: "updated",
  10. direction: "descending",
  11. page: 0
  12. },
  13. // results: [],
  14. searchResult: {},
  15. searched: false
  16. },
  17. mounted: function() {
  18. // pull data from the url path
  19. $(".tooltip").popup();
  20. let searchOnLoad = false;
  21. let searchParams = new URLSearchParams(window.location.search);
  22. if (searchParams.has("categoryId")) {
  23. this.search.categoryId = parseInt(searchParams.get("categoryId"));
  24. searchOnLoad = true;
  25. }
  26. if (searchParams.has("title")) {
  27. this.search.title = searchParams.get("title");
  28. searchOnLoad = true;
  29. }
  30. if (searchParams.has("tag")) {
  31. this.search.tag = searchParams.get("tag");
  32. searchOnLoad = true;
  33. }
  34. if (searchParams.has("author")) {
  35. this.search.author = searchParams.get("author");
  36. searchOnLoad = true;
  37. }
  38. if (searchParams.has("orderBy")) {
  39. this.search.orderBy = searchParams.get("orderBy");
  40. searchOnLoad = true;
  41. }
  42. if (searchParams.has("direction")) {
  43. this.search.direction = searchParams.get("direction");
  44. searchOnLoad = true;
  45. }
  46. let vue = this;
  47. // update the props for the dropdowns when they're changed.
  48. $("#categoriesDropdown").dropdown("setting", "onChange", function(index, text, choice) {
  49. vue.search.categoryId = index;
  50. });
  51. $("#orderByDropdown").dropdown("setting", "onChange", function(index, text, choice) {
  52. vue.search.orderBy = index;
  53. });
  54. $("#directionDropdown").dropdown("setting", "onChange", function(index, text, choice) {
  55. vue.search.direction = index;
  56. });
  57. // set the data.
  58. // category
  59. $("#categoriesDropdown").dropdown("set selected", this.search.categoryId);
  60. $("#orderByDropdown").dropdown("set selected", this.search.orderBy);
  61. $("#directionDropdown").dropdown("set selected", this.search.direction);
  62. // start searching on load if required
  63. if (searchOnLoad) {
  64. this.beginSearch();
  65. }
  66. },
  67. methods: {
  68. beginSearch() {
  69. this.searched = false;
  70. $.ajax({
  71. url: "/api/search/",
  72. method: "GET",
  73. data: this.search,
  74. success: function(data) {
  75. app.searchResult = data;
  76. app.searched = true;
  77. // var params = jQuery.param(app.search);
  78. // window.history.pushState(null, null, "/search/?" + params);
  79. },
  80. error: toast.defaultAjaxError
  81. });
  82. }
  83. }
  84. });