index.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. document.head.insertAdjacentHTML('beforeend','<style type="text/tailwindcss">'+stylFns['app/pages/Admin/Settings/index.styl']+'</style>')
  2. PouchDB = require 'app/utils/pouch'
  3. module.exports =
  4. name: 'AdminSettings'
  5. render: (new Function '_ctx', '_cache', renderFns['app/pages/Admin/Settings/index.pug'])()
  6. data: ->
  7. return {
  8. activeTab: 'brand'
  9. tabs: [
  10. { id: 'brand', name: 'Бренд' }
  11. { id: 'favicon', name: 'Фавикон' }
  12. { id: 'domains', name: 'Домены' }
  13. { id: 'languages', name: 'Языки' }
  14. ]
  15. brandSettings: {
  16. logo: ''
  17. favicon: ''
  18. companyName: ''
  19. description: ''
  20. email: ''
  21. phone: ''
  22. }
  23. domains: []
  24. currentDomain: window.location.hostname
  25. }
  26. methods:
  27. getTabClass: (tab) ->
  28. baseClass = 'admin-settings__tab'
  29. isActive = @activeTab == tab.id
  30. if isActive
  31. return "#{baseClass} admin-settings__tab--active"
  32. else
  33. return "#{baseClass} admin-settings__tab--inactive"
  34. loadBrandSettings: ->
  35. PouchDB.getDocument("domain_settings:#{@currentDomain}")
  36. .then (settings) =>
  37. if settings
  38. @brandSettings = { ...@brandSettings, ...settings }
  39. .catch (error) =>
  40. debug.log 'Настройки бренда не найдены'
  41. onLogoUpload: (event) ->
  42. file = event.target.files[0]
  43. if file
  44. # В реальном приложении здесь будет загрузка файла на сервер
  45. # и получение пути к изображению
  46. reader = new FileReader()
  47. reader.onload = (e) =>
  48. # Временное решение - используем data URL
  49. # В продакшене нужно загружать на сервер и получать путь /d/[db]/[doc]/[filename]
  50. @brandSettings.logo = e.target.result
  51. reader.readAsDataURL(file)
  52. removeLogo: ->
  53. @brandSettings.logo = ''
  54. onFaviconUpload: (event) ->
  55. file = event.target.files[0]
  56. if file
  57. reader = new FileReader()
  58. reader.onload = (e) =>
  59. @brandSettings.favicon = e.target.result
  60. reader.readAsDataURL(file)
  61. removeFavicon: ->
  62. @brandSettings.favicon = ''
  63. saveBrandSettings: ->
  64. domainDoc = {
  65. _id: "domain_settings:#{@currentDomain}"
  66. type: 'domain_settings'
  67. domain: @currentDomain
  68. ...@brandSettings
  69. updatedAt: new Date().toISOString()
  70. }
  71. # Проверяем существование документа
  72. PouchDB.getDocument(domainDoc._id)
  73. .then (existingDoc) =>
  74. domainDoc._rev = existingDoc._rev
  75. return PouchDB.saveToRemote(domainDoc)
  76. .catch (error) =>
  77. if error.status == 404
  78. return PouchDB.saveToRemote(domainDoc)
  79. else
  80. throw error
  81. .then (result) =>
  82. @showNotification 'Настройки бренда сохранены'
  83. # Обновляем данные в главном приложении
  84. @$root.loadDomainSettings?()
  85. .catch (error) =>
  86. console.error 'Ошибка сохранения настроек бренда:', error
  87. @showNotification 'Ошибка сохранения настроек бренда', 'error'
  88. showNotification: (message, type = 'success') ->
  89. @$root.showNotification?(message, type) || debug.log("#{type}: #{message}")
  90. mounted: ->
  91. @loadBrandSettings()