module_template.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package modules
  2. import (
  3. "html"
  4. "io/ioutil"
  5. "net/url"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "golang-fave/assets"
  10. "golang-fave/consts"
  11. "golang-fave/engine/builder"
  12. "golang-fave/engine/wrapper"
  13. "golang-fave/utils"
  14. )
  15. func (this *Modules) template_GetThemeFiles(wrap *wrapper.Wrapper) []string {
  16. var result []string
  17. files, err := ioutil.ReadDir(wrap.DTemplate)
  18. if err == nil {
  19. for _, file := range files {
  20. if len(file.Name()) > 0 && file.Name()[0] == '.' {
  21. continue
  22. }
  23. if len(file.Name()) > 0 && strings.ToLower(file.Name()) == "robots.txt" {
  24. continue
  25. }
  26. result = append(result, file.Name())
  27. }
  28. }
  29. return result
  30. }
  31. func (this *Modules) RegisterModule_Template() *Module {
  32. return this.newModule(MInfo{
  33. WantDB: false,
  34. Mount: "template",
  35. Name: "Template",
  36. Order: 802,
  37. System: true,
  38. Icon: assets.SysSvgIconView,
  39. Sub: &[]MISub{
  40. {Mount: "default", Name: "Editor", Show: true, Icon: assets.SysSvgIconEdit},
  41. },
  42. }, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
  43. content := ""
  44. sidebar := ""
  45. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  46. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  47. {Name: "Editor"},
  48. })
  49. files := this.template_GetThemeFiles(wrap)
  50. if len(files) > 0 {
  51. selected_file, _ := url.QueryUnescape(wrap.R.URL.Query().Get("file"))
  52. if !(selected_file != "" && utils.InArrayString(files, selected_file)) {
  53. selected_file = files[0]
  54. }
  55. list_of_files := ``
  56. for _, file := range files {
  57. selected := ""
  58. if file == selected_file {
  59. selected = " selected"
  60. }
  61. list_of_files += `<option value="` + html.EscapeString(file) +
  62. `"` + selected + `>` + html.EscapeString(file) + `</option>`
  63. }
  64. fcont := []byte(``)
  65. fcont, _ = ioutil.ReadFile(wrap.DTemplate + string(os.PathSeparator) + selected_file)
  66. fext := filepath.Ext(selected_file)
  67. if len(fext) > 2 {
  68. fext = fext[1:]
  69. }
  70. content += builder.DataForm(wrap, []builder.DataFormField{
  71. {
  72. Kind: builder.DFKHidden,
  73. Name: "action",
  74. Value: "template-edit-theme-file",
  75. },
  76. {
  77. Kind: builder.DFKText,
  78. Caption: "Theme file",
  79. Name: "file",
  80. Value: "0",
  81. CallBack: func(field *builder.DataFormField) string {
  82. return `<div class="form-group n1">` +
  83. `<div class="row">` +
  84. `<div class="col-9">` +
  85. `<div>` +
  86. `<select class="form-control ignore-lost-data" id="lbl_file" name="file" onchange="setTimeout(function(){$('#lbl_file').val('` + selected_file + `')},500);document.location='/cp/` + wrap.CurrModule + `/?file='+encodeURI(this.value);">` +
  87. list_of_files +
  88. `</select>` +
  89. `</div>` +
  90. `</div>` +
  91. `<div class="col-3">` +
  92. `<button type="button" class="btn btn-danger" onclick="return fave.ActionRestoreThemeFile('template-restore-file','` + selected_file + `','Are you sure want to restore theme file?');">Restore</button>` +
  93. `</div>` +
  94. `</div>` +
  95. `</div>`
  96. },
  97. },
  98. {
  99. Kind: builder.DFKText,
  100. CallBack: func(field *builder.DataFormField) string {
  101. return `<div class="form-group last"><div class="row"><div class="col-12"><textarea class="form-control tmpl-editor" id="lbl_content" name="content" data-emode="` + fext + `" placeholder="" autocomplete="off">` + html.EscapeString(string(fcont)) + `</textarea></div></div></div>`
  102. },
  103. },
  104. {
  105. Kind: builder.DFKMessage,
  106. CallBack: func(field *builder.DataFormField) string {
  107. return `<div class="row"><div class="col-12"><div class="sys-messages"></div></div></div>`
  108. },
  109. },
  110. {
  111. Kind: builder.DFKSubmit,
  112. CallBack: func(field *builder.DataFormField) string {
  113. return `<div class="row d-lg-none"><div class="col-12"><button type="submit" class="btn btn-primary" data-target="add-edit-button">Save</button></div></div>`
  114. },
  115. },
  116. })
  117. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  118. } else {
  119. content += `<div class="sys-messages">
  120. <div class="alert alert-warning" role="alert">
  121. <strong>Error!</strong> No any file found in theme folder
  122. </div>
  123. </div>`
  124. }
  125. }
  126. return this.getSidebarModules(wrap), content, sidebar
  127. })
  128. }