github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/www/service-make-new-page.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package www
     4  
     5  import (
     6  	"bytes"
     7  	"strings"
     8  	"text/template"
     9  	"unsafe"
    10  )
    11  
    12  type MakeNewPageReq struct {
    13  	Domain      string
    14  	ScopeName   string
    15  	PageVarName string
    16  }
    17  
    18  type MakeNewPageRes struct {
    19  	JS, HTML, CSS, JSON []byte
    20  }
    21  
    22  // MakeNewPage generate 4 files for a ui page.
    23  func MakeNewPage(req *MakeNewPageReq) (res *MakeNewPageRes, err error) {
    24  	if req.PageVarName == "" {
    25  		req.PageVarName = strings.ReplaceAll(strings.Title(req.ScopeName), "-", " ")
    26  	}
    27  
    28  	var jsBuf = new(bytes.Buffer)
    29  	err = jsPageFile.Execute(jsBuf, req)
    30  	if err != nil {
    31  		return
    32  	}
    33  	res = &MakeNewPageRes{
    34  		JS:   jsBuf.Bytes(),
    35  		HTML: (*(*[]byte)(unsafe.Pointer(&htmlPageFile))),
    36  		CSS:  (*(*[]byte)(unsafe.Pointer(&cssPageFile))),
    37  		JSON: (*(*[]byte)(unsafe.Pointer(&jsonPageFile))),
    38  	}
    39  
    40  	return
    41  }
    42  
    43  var jsPageFile = template.Must(template.New("jsPageFile").Parse(`/* For license and copyright information please see LEGAL file in repository */
    44  
    45  import '../libjs/widget-notification/force-leave-page.js'
    46  
    47  const page{{.PageVarName}} = {
    48      URN: {
    49  		URN: "urn:giti:{{.Domain}}:page:{{.ScopeName}}",
    50  		ID: "",
    51          Domain: "{{.Domain}}",
    52          Scope: "page",
    53  		Name: "{{.ScopeName}}",
    54  	},
    55      Icon: "{{.ScopeName}}",
    56      Info: {
    57          Name: "LocaleText[0]",
    58          ShortName: "LocaleText[1]",
    59          Tagline: "LocaleText[2]",
    60          Slogan: "LocaleText[3]",
    61          Description: "LocaleText[4]",
    62          Tags: ["LocaleText[5]"]
    63      },
    64      Robots: "all",
    65      Related: ["", ""],
    66      HTML: () => ` + "``," + `
    67      CSS: '',
    68      Templates: {},
    69      Options: {},
    70  	acceptedConditions: {
    71  		"id": "",
    72  		editable: true,
    73  		offset: 0,
    74  	},
    75  	activeState: null,
    76  }
    77  Application.RegisterPage(page{{.PageVarName}})
    78  
    79  // function init() {
    80  
    81  // }
    82  
    83  page{{.PageVarName}}.ActivatePage = async function (state) {
    84      // this.activeState = state
    85  	// TODO::: Do any logic before page render
    86  	window.document.body.innerHTML = this.HTML()
    87  	// TODO::: Do any logic after page render
    88  }
    89  
    90  page{{.PageVarName}}.DeactivatePage = async function () {
    91  	if (this.newList) {
    92          var forceLeave = await forceLeavePageWidget.ConnectedCallback()
    93      }
    94      if (forceLeave) {
    95  		this.newList = null
    96  		// call any widgets DisconnectedCallback e.g. barcodeCameraScannerWidget.DisconnectedCallback()
    97      }
    98      return forceLeave
    99  }
   100  
   101  page{{.PageVarName}}.OtherAction = async function () {}
   102  `))
   103  
   104  var htmlPageFile = `<!-- For license and copyright information please see LEGAL file in repository -->
   105  
   106  <header>
   107  </header>
   108  
   109  <main>
   110      <header>
   111      </header>
   112  
   113      <footer>
   114      </footer>
   115  </main>`
   116  
   117  var cssPageFile = `/* For license and copyright information please see LEGAL file in repository */
   118  
   119  `
   120  
   121  var jsonPageFile = `{
   122      "en": [
   123          "",
   124          "",
   125          "",
   126          "",
   127          "",
   128          "",
   129          ""
   130      ],
   131      "fa": [
   132          "",
   133          "",
   134          "",
   135          "",
   136          "",
   137          "",
   138          ""
   139      ]
   140  }`