| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- document.head.insertAdjacentHTML('beforeend','<style type="text/css">'+stylFns['app/pages/Admin/Blog/index.styl']+'</style>')
- PouchDB = require 'app/utils/pouch'
- module.exports =
- name: 'AdminBlog'
-
- render: (new Function '_ctx', '_cache', renderFns['app/pages/Admin/Blog/index.pug'])()
-
- data: ->
- return {
- articles: []
- searchQuery: ''
- selectedStatus: ''
- showArticleModal: false
- editingArticle: null
- availableDomains: []
- articleForm: {
- title: ''
- slug: ''
- excerpt: ''
- image: ''
- author: ''
- content: ''
- published: false
- domains: []
- }
- }
-
- computed:
- filteredArticles: ->
- articles = @articles
-
- # Фильтр по поиску
- if @searchQuery
- query = @searchQuery.toLowerCase()
- articles = articles.filter (article) =>
- article.title?.toLowerCase().includes(query) ||
- article.excerpt?.toLowerCase().includes(query)
-
- # Фильтр по статусу
- if @selectedStatus == 'published'
- articles = articles.filter (article) => article.published
- else if @selectedStatus == 'draft'
- articles = articles.filter (article) => !article.published
-
- return articles
-
- methods:
- loadArticles: ->
- PouchDB.queryView('admin', 'blog_articles', { include_docs: true })
- .then (result) =>
- @articles = result.rows.map (row) -> row.doc
- # Сортируем по дате создания (новые сначала)
- @articles.sort (a, b) -> new Date(b.createdAt) - new Date(a.createdAt)
- .catch (error) =>
- console.error 'Ошибка загрузки статей:', error
- @showNotification 'Ошибка загрузки статей', 'error'
-
- loadDomains: ->
- PouchDB.queryView('admin', 'domain_settings', { include_docs: true })
- .then (result) =>
- @availableDomains = result.rows.map (row) -> row.doc
- .catch (error) =>
- console.error 'Ошибка загрузки доменов:', error
-
- editArticle: (article) ->
- @editingArticle = article
- @articleForm = {
- title: article.title || ''
- slug: article.slug || ''
- excerpt: article.excerpt || ''
- image: article.image || ''
- author: article.author || ''
- content: article.content || ''
- published: article.published || false
- domains: article.domains || []
- }
- @showArticleModal = true
-
- saveArticle: ->
- if !@articleForm.title
- @showNotification 'Введите заголовок статьи', 'error'
- return
-
- if !@articleForm.slug
- @showNotification 'Введите URL slug', 'error'
- return
-
- articleData = {
- type: 'blog_article'
- ...@articleForm
- updatedAt: new Date().toISOString()
- }
-
- if @editingArticle
- articleData._id = @editingArticle._id
- articleData._rev = @editingArticle._rev
- articleData.createdAt = @editingArticle.createdAt
- else
- articleData._id = "blog_article:#{Date.now()}"
- articleData.createdAt = new Date().toISOString()
-
- PouchDB.saveToRemote(articleData)
- .then (result) =>
- @showArticleModal = false
- @resetArticleForm()
- @loadArticles()
- @showNotification 'Статья успешно сохранена'
- .catch (error) =>
- console.error 'Ошибка сохранения статьи:', error
- @showNotification 'Ошибка сохранения статьи', 'error'
-
- deleteArticle: (articleId) ->
- if confirm('Вы уверены, что хотите удалить эту статью?')
- PouchDB.getDocument(articleId)
- .then (doc) ->
- PouchDB.saveToRemote({ ...doc, _deleted: true })
- .then (result) =>
- @loadArticles()
- @showNotification 'Статья удалена'
- .catch (error) =>
- console.error 'Ошибка удаления статьи:', error
- @showNotification 'Ошибка удаления статьи', 'error'
-
- toggleArticleStatus: (article) ->
- updatedArticle = {
- ...article
- published: !article.published
- updatedAt: new Date().toISOString()
- }
-
- PouchDB.saveToRemote(updatedArticle)
- .then (result) =>
- @loadArticles()
- @showNotification 'Статус статьи обновлен'
- .catch (error) =>
- console.error 'Ошибка обновления статуса:', error
- @showNotification 'Ошибка обновления статуса', 'error'
-
- resetArticleForm: ->
- @editingArticle = null
- @articleForm = {
- title: ''
- slug: ''
- excerpt: ''
- image: ''
- author: ''
- content: ''
- published: false
- domains: []
- }
-
- formatDate: (dateString) ->
- return '' if !dateString
- date = new Date(dateString)
- date.toLocaleDateString('ru-RU', {
- year: 'numeric'
- month: 'long'
- day: 'numeric'
- })
-
- getStatusClass: (isPublished) ->
- baseClass = 'admin-blog__item-status'
- if isPublished
- return "#{baseClass} admin-blog__item-status--published"
- else
- return "#{baseClass} admin-blog__item-status--draft"
-
- getToggleBtnClass: (isPublished) ->
- baseClass = 'admin-blog__btn'
- if isPublished
- return "#{baseClass} admin-blog__btn--secondary"
- else
- return "#{baseClass} admin-blog__btn--primary"
-
- showNotification: (message, type = 'success') ->
- @$root.showNotification?(message, type) || debug.log("#{type}: #{message}")
-
- mounted: ->
- @loadArticles()
- @loadDomains()
|