index.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # app/pages/Admin/Routes/index.coffee
  2. document.head.insertAdjacentHTML('beforeend','<style type="text/tailwindcss">'+stylFns['app/pages/Admin/Routes/index.styl']+'</style>')
  3. CouchDB = require 'app/utils/couch'
  4. module.exports =
  5. name: 'AdminRoutes'
  6. render: (new Function '_ctx', '_cache', renderFns['app/pages/Admin/Routes/index.pug'])()
  7. data: ->
  8. routes: []
  9. showRouteModal: false
  10. currentRoute: {
  11. path: ''
  12. name: ''
  13. component: ''
  14. metaTitle: ''
  15. metaDescription: ''
  16. active: true
  17. }
  18. mounted: ->
  19. @loadRoutes()
  20. methods:
  21. loadRoutes: ->
  22. @$root.couchRequest('find', {
  23. selector: { type: 'route' }
  24. sort: [{path: 'asc'}]
  25. })
  26. .then (result) =>
  27. @routes = result.docs
  28. .catch (error) =>
  29. console.error 'Ошибка загрузки маршрутов:', error
  30. @$root.showNotification 'Ошибка загрузки маршрутов', 'error'
  31. editRoute: (route) ->
  32. @currentRoute = { ...route }
  33. @showRouteModal = true
  34. saveRoute: ->
  35. routeData = {
  36. type: 'route'
  37. ...@currentRoute
  38. updatedAt: new Date().toISOString()
  39. }
  40. if !routeData._id
  41. routeData.createdAt = new Date().toISOString()
  42. @$root.couchRequest('put', routeData)
  43. .then (result) =>
  44. if routeData._id
  45. index = @routes.findIndex (r) -> r._id == routeData._id
  46. if index != -1
  47. @routes.splice(index, 1, { ...routeData, _id: routeData._id, _rev: result.rev })
  48. else
  49. @routes.push({ ...routeData, _id: result.id, _rev: result.rev })
  50. @showRouteModal = false
  51. @resetCurrentRoute()
  52. @$root.showNotification 'Маршрут успешно сохранен'
  53. # Обновляем роутер
  54. @updateRouter()
  55. .catch (error) =>
  56. console.error 'Ошибка сохранения маршрута:', error
  57. @$root.showNotification 'Ошибка сохранения маршрута', 'error'
  58. deleteRoute: (routeId) ->
  59. if confirm('Вы уверены, что хотите удалить этот маршрут?')
  60. route = @routes.find (r) -> r._id == routeId
  61. @$root.couchRequest('put', {
  62. _id: routeId
  63. _rev: route._rev
  64. _deleted: true
  65. })
  66. .then =>
  67. @routes = @routes.filter (r) -> r._id != routeId
  68. @$root.showNotification 'Маршрут успешно удален'
  69. @updateRouter()
  70. .catch (error) =>
  71. console.error 'Ошибка удаления маршрута:', error
  72. @$root.showNotification 'Ошибка удаления маршрута', 'error'
  73. resetCurrentRoute: ->
  74. @currentRoute = {
  75. path: ''
  76. name: ''
  77. component: ''
  78. metaTitle: ''
  79. metaDescription: ''
  80. active: true
  81. }
  82. getRouteStatusClass: (isActive) ->
  83. if isActive
  84. return 'px-2 py-1 text-xs rounded-full bg-green-100 text-green-800'
  85. else
  86. return 'px-2 py-1 text-xs rounded-full bg-red-100 text-red-800'
  87. updateRouter: ->
  88. # Здесь можно добавить логику для динамического обновления роутера
  89. console.log 'Роутер должен быть обновлен'