page-categories.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. let app = new Vue({
  2. el: "#app",
  3. data: {
  4. categories: []
  5. },
  6. mounted: function() {
  7. this.getCategories();
  8. $("#categoriesDropdown").dropdown("setting", "onChange", function(index, text, $choice) {
  9. app.selectedCategory = index;
  10. let cat = app.categories[index];
  11. if (cat.parent != null) {
  12. console.log("parent not null");
  13. for (let i = 0; i < app.categories.length; i++) {
  14. if (app.categories[i].name == cat.parent.name) {
  15. $("#categoriesParentDropdown").dropdown("set selected", i);
  16. break;
  17. }
  18. }
  19. } else {
  20. console.log("parent null");
  21. $("#categoriesParentDropdown").dropdown("clear");
  22. }
  23. });
  24. $("#categoriesParentDropdown").dropdown("setting", "onChange", function(index, text, $choice) {
  25. app.selectedCategory = index;
  26. });
  27. },
  28. updated: function() {},
  29. methods: {
  30. getCategories: function() {
  31. let data = {
  32. category: "all"
  33. };
  34. $.ajax({
  35. url: "/api/blob/",
  36. method: "GET",
  37. data: data,
  38. success: function(data) {
  39. app.categories = data.categories;
  40. },
  41. error: toast.defaultAjaxError
  42. });
  43. },
  44. createNewCategory: function(event, index, parent) {
  45. let parentId = parent == null ? -1 : parent.id;
  46. let formData = new FormData();
  47. formData.append("name", "New Category");
  48. formData.append("parentId", parentId);
  49. $.ajax({
  50. url: "/api/category/",
  51. method: "POST",
  52. data: formData,
  53. cache: false,
  54. contentType: false,
  55. processData: false,
  56. success: function(data) {
  57. app.categories.push(data);
  58. toast.success(null, "Category created: " + data.name, false);
  59. },
  60. error: toast.defaultAjaxError
  61. });
  62. },
  63. getChildCount: function(category) {
  64. let count = 0;
  65. for (let i = 0; i < this.categories.length; i++) {
  66. if (this.categories[i].parent != null && this.categories[i].parent.id == category.id) {
  67. count++;
  68. }
  69. }
  70. return count;
  71. },
  72. mouseEnterCategory: function(event) {
  73. $(event.target)
  74. .children("div")
  75. .removeClass("transition hidden");
  76. },
  77. mouseLeaveCategory: function(event) {
  78. $(event.target)
  79. .children("div")
  80. .addClass("transition hidden");
  81. },
  82. mouseLeaveCategoryTextField: function(event) {
  83. $(event.target).addClass("transition hidden");
  84. $(event.target)
  85. .parent()
  86. .children("div")
  87. .first()
  88. .removeClass("transition hidden");
  89. },
  90. editCategory: function(event) {
  91. $(event.target)
  92. .parent()
  93. .addClass("transition hidden");
  94. $(event.target)
  95. .parent()
  96. .parent()
  97. .children("div:nth-child(2)")
  98. .last()
  99. .removeClass("transition hidden");
  100. },
  101. updateSelectedCategory: function(category, index) {
  102. let formData = new FormData();
  103. formData.append("id", category.id);
  104. formData.append("name", category.name);
  105. formData.append("parentId", category.parent == null ? -1 : category.parent.id);
  106. $.ajax({
  107. url: "/api/category/",
  108. method: "PUT",
  109. data: formData,
  110. cache: false,
  111. contentType: false,
  112. processData: false,
  113. success: function(data) {
  114. app.categories[index] = data;
  115. toast.success(null, "Category updated: " + data.name, false);
  116. },
  117. error: toast.defaultAjaxError
  118. });
  119. },
  120. deleteCategory: function(category, index) {
  121. let formData = new FormData();
  122. formData.append("id", category.id);
  123. $.ajax({
  124. url: "/api/category/",
  125. method: "DELETE",
  126. data: formData,
  127. cache: false,
  128. contentType: false,
  129. processData: false,
  130. success: function(data) {
  131. app.categories.splice(index, 1);
  132. $("#categoriesDropdown").dropdown("clear");
  133. toast.success(null, "Category deleted: " + data.name, false);
  134. },
  135. error: toast.defaultAjaxError
  136. });
  137. }
  138. }
  139. });