index.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Добавление стилей страницы
  2. if globalThis.stylFns and globalThis.stylFns['app/pages/Admin/Categories/index.styl']
  3. styleElement = document.createElement('style')
  4. styleElement.type = 'text/css'
  5. styleElement.textContent = globalThis.stylFns['app/pages/Admin/Categories/index.styl']
  6. document.head.appendChild(styleElement)
  7. else
  8. log '⚠️ Стили страницы категорий не найдены'
  9. CategoryService = require 'app/services/CategoryService'
  10. CategoryNode = require 'app/components/Admin/CategoryNode/index.coffee'
  11. MediaUpload = require 'app/components/Admin/MediaUpload/index.coffee'
  12. module.exports = {
  13. components: {
  14. 'category-node': CategoryNode
  15. 'media-upload': MediaUpload
  16. }
  17. data: ->
  18. {
  19. loading: false
  20. saving: false
  21. categories: []
  22. editingCategory: null
  23. creatingNew: false
  24. categoryForm: {
  25. name: ''
  26. slug: ''
  27. parent: ''
  28. order: 0
  29. image: ''
  30. description: ''
  31. }
  32. }
  33. computed:
  34. hierarchicalCategories: ->
  35. @buildHierarchy(@categories)
  36. flatCategories: ->
  37. @flattenCategories(@hierarchicalCategories)
  38. methods:
  39. loadCategories: ->
  40. @loading = true
  41. try
  42. @categories = await CategoryService.getAllCategories()
  43. log '✅ Категории загружены: '+@categories.length
  44. catch error
  45. log '❌ Ошибка загрузки категорий: '+error.message
  46. @$emit('show-notification', 'Ошибка загрузки категорий', 'error')
  47. finally
  48. @loading = false
  49. buildHierarchy: (categories, parentId = null) ->
  50. hierarchy = []
  51. categories
  52. .filter (cat) -> cat.parent == parentId
  53. .sort (a, b) -> a.order - b.order
  54. .forEach (category) =>
  55. children = @buildHierarchy(categories, category._id)
  56. if children.length > 0
  57. category.children = children
  58. hierarchy.push(category)
  59. return hierarchy
  60. flattenCategories: (hierarchical, level = 0) ->
  61. flat = []
  62. hierarchical.forEach (category) =>
  63. flat.push({
  64. ...category
  65. level: level
  66. name: '→ '.repeat(level) + category.name
  67. })
  68. if category.children
  69. flat = flat.concat(@flattenCategories(category.children, level + 1))
  70. return flat
  71. createCategory: ->
  72. @editingCategory = null
  73. @creatingNew = true
  74. @resetForm()
  75. editCategory: (category) ->
  76. @editingCategory = category
  77. @creatingNew = false
  78. @categoryForm = {
  79. name: category.name
  80. slug: category.slug
  81. parent: category.parent || ''
  82. order: category.order || 0
  83. image: category.image || ''
  84. description: category.description || ''
  85. }
  86. cancelEdit: ->
  87. @editingCategory = null
  88. @creatingNew = false
  89. @resetForm()
  90. resetForm: ->
  91. @categoryForm = {
  92. name: ''
  93. slug: ''
  94. parent: ''
  95. order: 0
  96. image: ''
  97. description: ''
  98. }
  99. saveCategory: ->
  100. if not @categoryForm.name.trim()
  101. @$emit('show-notification', 'Введите название категории', 'error')
  102. return
  103. @saving = true
  104. try
  105. categoryData = {
  106. ...@categoryForm
  107. type: 'category'
  108. domains: [window.location.hostname]
  109. active: true
  110. }
  111. if @editingCategory
  112. categoryData._id = @editingCategory._id
  113. categoryData._rev = @editingCategory._rev
  114. else
  115. categoryData._id = 'category:'+Date.now()
  116. if not categoryData.slug
  117. categoryData.slug = @slugify(categoryData.name)
  118. await CategoryService.saveCategory(categoryData)
  119. @$emit('show-notification',
  120. if @editingCategory then 'Категория обновлена' else 'Категория создана',
  121. 'success'
  122. )
  123. @cancelEdit()
  124. @loadCategories()
  125. catch error
  126. log '❌ Ошибка сохранения категории: '+error.message
  127. @$emit('show-notification', 'Ошибка сохранения категории', 'error')
  128. finally
  129. @saving = false
  130. deleteCategory: (category) ->
  131. if not confirm('Удалить категорию "'+category.name+'"?')
  132. return
  133. try
  134. await CategoryService.deleteCategory(category._id)
  135. @$emit('show-notification', 'Категория удалена', 'success')
  136. @loadCategories()
  137. catch error
  138. log '❌ Ошибка удаления категории: '+error.message
  139. @$emit('show-notification', 'Ошибка удаления категории', 'error')
  140. moveCategory: (categoryId, newParentId, newIndex) ->
  141. log 'Перемещение категории: '+categoryId+' в '+newParentId+' на позицию '+newIndex
  142. # Здесь будет логика перемещения категорий через drag&drop
  143. onImageSelected: (imageUrl) ->
  144. @categoryForm.image = imageUrl
  145. onImageRemoved: ->
  146. @categoryForm.image = ''
  147. expandAll: ->
  148. # Логика раскрытия всех узлов дерева
  149. document.querySelectorAll('.category-node').forEach (node) ->
  150. node.classList.add('expanded')
  151. collapseAll: ->
  152. # Логика сворачивания всех узлов дерева
  153. document.querySelectorAll('.category-node').forEach (node) ->
  154. node.classList.remove('expanded')
  155. slugify: (text) ->
  156. text.toString().toLowerCase()
  157. .replace(/\s+/g, '-')
  158. .replace(/[^\w\-]+/g, '')
  159. .replace(/\-\-+/g, '-')
  160. .replace(/^-+/, '')
  161. .replace(/-+$/, '')
  162. mounted: ->
  163. @loadCategories()
  164. log '📂 Страница управления категориями загружена'
  165. render: (new Function '_ctx', '_cache', globalThis.renderFns['app/pages/Admin/Categories/index.pug'])()
  166. }