app.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Подключение мета информации
  2. document.head.insertAdjacentHTML 'beforeend', '<meta charset="UTF-8">'
  3. document.head.insertAdjacentHTML 'beforeend', '<meta name="viewport" content="width=device-width, initial-scale=1.0">'
  4. # Настройка tailwind
  5. #tailwind.config = require 'tailwind.config.js'
  6. # Подключение основных стилей
  7. document.head.insertAdjacentHTML('beforeend', '<style type="text/tailwindcss" file="main.css">' + stylFns['main.css'] + '</style>')
  8. document.head.insertAdjacentHTML('beforeend', '<style type="text/tailwindcss" file="app/DesignTokens.styl">' + stylFns['app/DesignTokens.styl'] + '</style>')
  9. document.head.insertAdjacentHTML('beforeend', '<style type="text/tailwindcss" file="app/app.styl">' + stylFns['app/app.styl'] + '</style>')
  10. # Маршруты
  11. routes = [
  12. { path: '/', component: require 'app/pages/Home' }
  13. { path: '/:path*', component: require 'app/pages/DocumentPage' }
  14. ]
  15. globalThis._ = {}
  16. # Глобальное определение vuejs приложения
  17. globalThis.app = Vue.createApp
  18. name: 'app'
  19. data: () ->
  20. return {
  21. appState:
  22. currentDocument: null
  23. currentLanguage: 'ru'
  24. availableLanguages: ['ru', 'en', 'tj']
  25. loading: true
  26. error: null
  27. dbService: new (require('app/core/DB'))()
  28. }
  29. beforeMount: ->
  30. globalThis.AppDB = new (require 'app/utils/AppDB')()
  31. await globalThis.AppDB.init()
  32. globalThis._ = @
  33. computed:
  34. currentLanguage: ->
  35. @appState.currentLanguage
  36. watch:
  37. currentLanguage:
  38. handler: (newDoc) ->
  39. debug.dir newDoc
  40. @loadDocumentForPath(window.location.pathname)
  41. immediate: true
  42. methods:
  43. initializeApp: ->
  44. # Определяем язык из URL или браузера
  45. @detectLanguage()
  46. # Загружаем документ для текущего пути
  47. @loadDocumentForPath(window.location.pathname)
  48. detectLanguage: ->
  49. # Простая логика определения языка
  50. pathLang = window.location.pathname.split('/')[1]
  51. if pathLang in @appState.availableLanguages
  52. @appState.currentLanguage = pathLang
  53. else
  54. browserLang = navigator.language.split('-')[0]
  55. if browserLang in @appState.availableLanguages
  56. @appState.currentLanguage = browserLang
  57. loadDocumentForPath: (path) ->
  58. try
  59. @appState.loading = true
  60. doc = await AppDB.getDocumentByPath(path, AppDB.currentLanguage)
  61. @appState.currentDocument = doc
  62. @appState.loading = false
  63. # Устанавливаем заголовок страницы
  64. if doc?.title
  65. document.head.insertAdjacentHTML('beforeend', '<title>' + doc.title + '</title>')
  66. catch error
  67. @appState.error = "Ошибка загрузки документа: " + error
  68. @appState.loading = false
  69. render: (new Function '_ctx', '_cache', renderFns['app/app.pug'])()
  70. components: {
  71. 'hero-section': require('shared/HeroSection')
  72. 'image-gallery': require('shared/ImageGallery')
  73. }
  74. # Создаем и настраиваем роутер
  75. router = VueRouter.createRouter({
  76. routes: routes
  77. history: VueRouter.createWebHistory()
  78. scrollBehavior: (to, from, savedPosition) ->
  79. if savedPosition
  80. return savedPosition
  81. else
  82. return { x: 0, y: 0 }
  83. })
  84. app.use(router)
  85. app.mount('body')