index.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. document.head.insertAdjacentHTML('beforeend','<style type="text/css">'+stylFns['app/pages/Admin/Blog/index.styl']+'</style>')
  2. PouchDB = require 'app/utils/pouch'
  3. module.exports =
  4. name: 'AdminBlog'
  5. render: (new Function '_ctx', '_cache', renderFns['app/pages/Admin/Blog/index.pug'])()
  6. data: ->
  7. return {
  8. articles: []
  9. searchQuery: ''
  10. selectedStatus: ''
  11. showArticleModal: false
  12. editingArticle: null
  13. availableDomains: []
  14. articleForm: {
  15. title: ''
  16. slug: ''
  17. excerpt: ''
  18. image: ''
  19. author: ''
  20. content: ''
  21. published: false
  22. domains: []
  23. }
  24. }
  25. computed:
  26. filteredArticles: ->
  27. articles = @articles
  28. # Фильтр по поиску
  29. if @searchQuery
  30. query = @searchQuery.toLowerCase()
  31. articles = articles.filter (article) =>
  32. article.title?.toLowerCase().includes(query) ||
  33. article.excerpt?.toLowerCase().includes(query)
  34. # Фильтр по статусу
  35. if @selectedStatus == 'published'
  36. articles = articles.filter (article) => article.published
  37. else if @selectedStatus == 'draft'
  38. articles = articles.filter (article) => !article.published
  39. return articles
  40. methods:
  41. loadArticles: ->
  42. PouchDB.queryView('admin', 'blog_articles', { include_docs: true })
  43. .then (result) =>
  44. @articles = result.rows.map (row) -> row.doc
  45. # Сортируем по дате создания (новые сначала)
  46. @articles.sort (a, b) -> new Date(b.createdAt) - new Date(a.createdAt)
  47. .catch (error) =>
  48. console.error 'Ошибка загрузки статей:', error
  49. @showNotification 'Ошибка загрузки статей', 'error'
  50. loadDomains: ->
  51. PouchDB.queryView('admin', 'domain_settings', { include_docs: true })
  52. .then (result) =>
  53. @availableDomains = result.rows.map (row) -> row.doc
  54. .catch (error) =>
  55. console.error 'Ошибка загрузки доменов:', error
  56. editArticle: (article) ->
  57. @editingArticle = article
  58. @articleForm = {
  59. title: article.title || ''
  60. slug: article.slug || ''
  61. excerpt: article.excerpt || ''
  62. image: article.image || ''
  63. author: article.author || ''
  64. content: article.content || ''
  65. published: article.published || false
  66. domains: article.domains || []
  67. }
  68. @showArticleModal = true
  69. saveArticle: ->
  70. if !@articleForm.title
  71. @showNotification 'Введите заголовок статьи', 'error'
  72. return
  73. if !@articleForm.slug
  74. @showNotification 'Введите URL slug', 'error'
  75. return
  76. articleData = {
  77. type: 'blog_article'
  78. ...@articleForm
  79. updatedAt: new Date().toISOString()
  80. }
  81. if @editingArticle
  82. articleData._id = @editingArticle._id
  83. articleData._rev = @editingArticle._rev
  84. articleData.createdAt = @editingArticle.createdAt
  85. else
  86. articleData._id = "blog_article:#{Date.now()}"
  87. articleData.createdAt = new Date().toISOString()
  88. PouchDB.saveToRemote(articleData)
  89. .then (result) =>
  90. @showArticleModal = false
  91. @resetArticleForm()
  92. @loadArticles()
  93. @showNotification 'Статья успешно сохранена'
  94. .catch (error) =>
  95. console.error 'Ошибка сохранения статьи:', error
  96. @showNotification 'Ошибка сохранения статьи', 'error'
  97. deleteArticle: (articleId) ->
  98. if confirm('Вы уверены, что хотите удалить эту статью?')
  99. PouchDB.getDocument(articleId)
  100. .then (doc) ->
  101. PouchDB.saveToRemote({ ...doc, _deleted: true })
  102. .then (result) =>
  103. @loadArticles()
  104. @showNotification 'Статья удалена'
  105. .catch (error) =>
  106. console.error 'Ошибка удаления статьи:', error
  107. @showNotification 'Ошибка удаления статьи', 'error'
  108. toggleArticleStatus: (article) ->
  109. updatedArticle = {
  110. ...article
  111. published: !article.published
  112. updatedAt: new Date().toISOString()
  113. }
  114. PouchDB.saveToRemote(updatedArticle)
  115. .then (result) =>
  116. @loadArticles()
  117. @showNotification 'Статус статьи обновлен'
  118. .catch (error) =>
  119. console.error 'Ошибка обновления статуса:', error
  120. @showNotification 'Ошибка обновления статуса', 'error'
  121. resetArticleForm: ->
  122. @editingArticle = null
  123. @articleForm = {
  124. title: ''
  125. slug: ''
  126. excerpt: ''
  127. image: ''
  128. author: ''
  129. content: ''
  130. published: false
  131. domains: []
  132. }
  133. formatDate: (dateString) ->
  134. return '' if !dateString
  135. date = new Date(dateString)
  136. date.toLocaleDateString('ru-RU', {
  137. year: 'numeric'
  138. month: 'long'
  139. day: 'numeric'
  140. })
  141. getStatusClass: (isPublished) ->
  142. baseClass = 'admin-blog__item-status'
  143. if isPublished
  144. return "#{baseClass} admin-blog__item-status--published"
  145. else
  146. return "#{baseClass} admin-blog__item-status--draft"
  147. getToggleBtnClass: (isPublished) ->
  148. baseClass = 'admin-blog__btn'
  149. if isPublished
  150. return "#{baseClass} admin-blog__btn--secondary"
  151. else
  152. return "#{baseClass} admin-blog__btn--primary"
  153. showNotification: (message, type = 'success') ->
  154. @$root.showNotification?(message, type) || debug.log("#{type}: #{message}")
  155. mounted: ->
  156. @loadArticles()
  157. @loadDomains()