app.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  28. beforeMount: ->
  29. globalThis.AppDB = new (require 'app/utils/AppDB')()
  30. await globalThis.AppDB.init()
  31. globalThis._ = @
  32. computed:
  33. currentLanguage: ->
  34. @appState.currentLanguage
  35. watch:
  36. currentLanguage:
  37. handler: (newDoc) ->
  38. debug.dir newDoc
  39. @loadDocumentForPath(window.location.pathname)
  40. immediate: true
  41. methods:
  42. parse: (md)->
  43. marked.parse md
  44. initializeApp: ->
  45. # Определяем язык из URL или браузера
  46. @detectLanguage()
  47. # Загружаем документ для текущего пути
  48. @loadDocumentForPath(window.location.pathname)
  49. detectLanguage: ->
  50. # Простая логика определения языка
  51. pathLang = window.location.pathname.split('/')[1]
  52. if pathLang in @appState.availableLanguages
  53. @appState.currentLanguage = pathLang
  54. else
  55. browserLang = navigator.language.split('-')[0]
  56. if browserLang in @appState.availableLanguages
  57. @appState.currentLanguage = browserLang
  58. loadDocumentForPath: (path) ->
  59. try
  60. @appState.loading = true
  61. debug.log ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  62. doc = await AppDB.getDocumentByPath(path, AppDB.currentLanguage)
  63. debug.dir doc
  64. debug.log "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  65. @appState.currentDocument = doc
  66. @appState.loading = false
  67. # Устанавливаем заголовок страницы
  68. if doc?.title
  69. document.head.insertAdjacentHTML('beforeend', '<title>' + doc.title + '</title>')
  70. catch error
  71. @appState.error = "Ошибка загрузки документа: " + error
  72. @appState.loading = false
  73. render: (new Function '_ctx', '_cache', renderFns['app/app.pug'])()
  74. components: {
  75. 'hero-section': require('app/shared/HeroSection')
  76. 'image-gallery': require('app/shared/ImageGallery')
  77. }
  78. # Создаем и настраиваем роутер
  79. router = VueRouter.createRouter({
  80. routes: routes
  81. history: VueRouter.createWebHistory()
  82. scrollBehavior: (to, from, savedPosition) ->
  83. if savedPosition
  84. return savedPosition
  85. else
  86. return { x: 0, y: 0 }
  87. })
  88. app.use(router)
  89. app.mount('body')