Browse Source

Added template support for cogs and refactored basic form code

Wirecog 7 years ago
parent
commit
1785f6ecdd
4 changed files with 78 additions and 13 deletions
  1. 21
    8
      basicform.go
  2. 16
    5
      template.go
  3. 26
    0
      templatebundle.go
  4. 15
    0
      templateset.go

+ 21
- 8
basicform.go View File

10
 type BasicForm struct {
10
 type BasicForm struct {
11
 	formParams *FormParams
11
 	formParams *FormParams
12
 
12
 
13
-	autofillFields []string
14
-	fields         map[string]string
15
-	errors         map[string]string
13
+	prefillFields []string
14
+	fields        map[string]string
15
+	errors        map[string]string
16
 }
16
 }
17
 
17
 
18
-func (c *BasicForm) AutofillFields() []string {
19
-	return c.autofillFields
18
+func (c *BasicForm) PrefillFields() []string {
19
+	return c.prefillFields
20
 }
20
 }
21
 
21
 
22
 func (c *BasicForm) Fields() map[string]string {
22
 func (c *BasicForm) Fields() map[string]string {
32
 
32
 
33
 }
33
 }
34
 
34
 
35
-func (c *BasicForm) SetAutofillFields(autofillFields []string) {
36
-	c.autofillFields = autofillFields
35
+func (c *BasicForm) SetPrefillFields(prefillFields []string) {
36
+	c.prefillFields = prefillFields
37
 }
37
 }
38
 
38
 
39
 func (c *BasicForm) SetFields(fields map[string]string) {
39
 func (c *BasicForm) SetFields(fields map[string]string) {
57
 }
57
 }
58
 
58
 
59
 func (c *BasicForm) PopulateFields() {
59
 func (c *BasicForm) PopulateFields() {
60
-	for _, fieldName := range c.autofillFields {
60
+	for _, fieldName := range c.prefillFields {
61
 		c.fields[fieldName] = FormValue(c.FormParams(), fieldName)
61
 		c.fields[fieldName] = FormValue(c.FormParams(), fieldName)
62
 	}
62
 	}
63
 }
63
 }
70
 		}
70
 		}
71
 	}
71
 	}
72
 }
72
 }
73
+
74
+func (c *BasicForm) RegenerateErrors() {
75
+
76
+	c.errors = make(map[string]string)
77
+
78
+	if OperatingEnvironment() == WebBrowserEnvironment && c.formParams.FormElement != nil {
79
+		errorSpans := c.formParams.FormElement.QuerySelectorAll(".formError")
80
+		for _, v := range errorSpans {
81
+			v.SetInnerHTML(c.errors[strings.Replace(v.GetAttribute("id"), "Error", "", -1)])
82
+		}
83
+	}
84
+
85
+}

+ 16
- 5
template.go View File

40
 )
40
 )
41
 
41
 
42
 type RenderParams struct {
42
 type RenderParams struct {
43
-	Data        interface{}
44
-	Writer      io.Writer
45
-	Element     dom.Element
46
-	Disposition int8
47
-	Attributes  map[string]string
43
+	Data                          interface{}
44
+	Writer                        io.Writer
45
+	Element                       dom.Element
46
+	Disposition                   int8
47
+	Attributes                    map[string]string
48
+	ShouldPopulateRenderedContent bool
49
+	RenderedContent               string
50
+	ShouldSkipFinalRenderStep     bool
48
 }
51
 }
49
 
52
 
50
 func (t *Template) GetTemplateType() int8 {
53
 func (t *Template) GetTemplateType() int8 {
109
 		println("Error encountered when attempting to render template on client: ", err)
112
 		println("Error encountered when attempting to render template on client: ", err)
110
 	}
113
 	}
111
 
114
 
115
+	if params.ShouldPopulateRenderedContent == true {
116
+		params.RenderedContent = string(tpl.Bytes())
117
+	}
118
+
119
+	if params.ShouldSkipFinalRenderStep == true {
120
+		return
121
+	}
122
+
112
 	div := dom.GetWindow().Document().CreateElement("div").(*dom.HTMLDivElement)
123
 	div := dom.GetWindow().Document().CreateElement("div").(*dom.HTMLDivElement)
113
 	div.SetInnerHTML(string(tpl.Bytes()))
124
 	div.SetInnerHTML(string(tpl.Bytes()))
114
 
125
 

+ 26
- 0
templatebundle.go View File

54
 	return nil
54
 	return nil
55
 
55
 
56
 }
56
 }
57
+
58
+func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string, prefixName string, templateFileExtension string) error {
59
+
60
+	templateDirectory := filepath.Clean(templatesPath)
61
+	println("td: ", templateDirectory)
62
+	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
63
+		if strings.HasSuffix(path, templateFileExtension) {
64
+			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
65
+			name = prefixName + "/" + name
66
+			contents, err := ioutil.ReadFile(path)
67
+			t.items[name] = string(contents)
68
+
69
+			if err != nil {
70
+				fmt.Println("error encountered while walking directory: ", err)
71
+				return err
72
+			}
73
+
74
+		}
75
+		return nil
76
+	}); err != nil {
77
+		return err
78
+	}
79
+
80
+	return nil
81
+
82
+}

+ 15
- 0
templateset.go View File

121
 	t.bundle = bundle
121
 	t.bundle = bundle
122
 
122
 
123
 }
123
 }
124
+
125
+func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {
126
+
127
+	bundle := NewTemplateBundle()
128
+
129
+	templatesPath := cogTemplatePath
130
+	bundle.importTemplateFileContentsForCog(templatesPath, prefixName, templateFileExtension)
131
+	t.ImportTemplatesFromMap(bundle.Items())
132
+
133
+	for k, v := range bundle.Items() {
134
+		t.bundle.items[k] = v
135
+	}
136
+	//	t.bundle = bundle
137
+
138
+}