Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Apr 11 16:21:27 2019 +0000
Revision:
91:9a125082f53c
Parent:
90:9cc77a16b6c5
Child:
92:9ce59a5b6032
Added tab to main content to nav bar for keyboard users

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:f8998d10763e 1 #include <stdio.h>
andrewboyson 30:6a08abbe6301 2 #include "http.h"
andrewboyson 30:6a08abbe6301 3 #include "page.h"
andrewboyson 30:6a08abbe6301 4 #include "page-derived.h"
andrewboyson 30:6a08abbe6301 5 #include "mac.h"
andrewboyson 0:f8998d10763e 6 #include "ip4addr.h"
andrewboyson 0:f8998d10763e 7 #include "ip6addr.h"
andrewboyson 0:f8998d10763e 8
andrewboyson 2:14de8c14afd9 9 void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
andrewboyson 0:f8998d10763e 10 {
andrewboyson 0:f8998d10763e 11 HttpAddText("<!DOCTYPE html>\r\n"
andrewboyson 0:f8998d10763e 12 "<html>\r\n"
andrewboyson 0:f8998d10763e 13 "<head>\r\n");
andrewboyson 2:14de8c14afd9 14 if (site)
andrewboyson 0:f8998d10763e 15 {
andrewboyson 0:f8998d10763e 16 HttpAddText(" <title>");
andrewboyson 2:14de8c14afd9 17 HttpAddText(site);
andrewboyson 2:14de8c14afd9 18 if (title)
andrewboyson 2:14de8c14afd9 19 {
andrewboyson 2:14de8c14afd9 20 HttpAddText(" - ");
andrewboyson 2:14de8c14afd9 21 HttpAddText(title);
andrewboyson 2:14de8c14afd9 22 }
andrewboyson 0:f8998d10763e 23 HttpAddText("</title>\r\n");
andrewboyson 0:f8998d10763e 24 }
andrewboyson 0:f8998d10763e 25 HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 26 if (style)
andrewboyson 0:f8998d10763e 27 {
andrewboyson 0:f8998d10763e 28 HttpAddText(" <link rel='stylesheet' href='/");
andrewboyson 0:f8998d10763e 29 HttpAddText(style);
andrewboyson 0:f8998d10763e 30 HttpAddText("' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 31 }
andrewboyson 0:f8998d10763e 32 if (script)
andrewboyson 0:f8998d10763e 33 {
andrewboyson 0:f8998d10763e 34 HttpAddText(" <script src='/");
andrewboyson 0:f8998d10763e 35 HttpAddText(script);
andrewboyson 0:f8998d10763e 36 HttpAddText("' type='text/javascript'></script>\r\n");
andrewboyson 0:f8998d10763e 37 }
andrewboyson 0:f8998d10763e 38 HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
andrewboyson 0:f8998d10763e 39 " <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n"
andrewboyson 0:f8998d10763e 40 "</head>\r\n"
andrewboyson 0:f8998d10763e 41 "<body>\r\n");
andrewboyson 0:f8998d10763e 42
andrewboyson 0:f8998d10763e 43 }
andrewboyson 12:237a0f75b4d0 44 void PageAddH1(const char* site, const char* pageName)
andrewboyson 12:237a0f75b4d0 45 {
andrewboyson 91:9a125082f53c 46 HttpAddText("<h1 id='main-content'>");
andrewboyson 12:237a0f75b4d0 47 HttpAddText(site);
andrewboyson 12:237a0f75b4d0 48 HttpAddText(" - ");
andrewboyson 12:237a0f75b4d0 49 HttpAddText(pageName);
andrewboyson 12:237a0f75b4d0 50 HttpAddText("</h1>\r\n");
andrewboyson 12:237a0f75b4d0 51 }
andrewboyson 12:237a0f75b4d0 52 void PageAddH2(const char* text)
andrewboyson 12:237a0f75b4d0 53 {
andrewboyson 12:237a0f75b4d0 54 HttpAddText("<h2>");
andrewboyson 12:237a0f75b4d0 55 HttpAddText(text);
andrewboyson 12:237a0f75b4d0 56 HttpAddText("</h2>\r\n");
andrewboyson 12:237a0f75b4d0 57 }
andrewboyson 12:237a0f75b4d0 58 void PageAddEnd()
andrewboyson 12:237a0f75b4d0 59 {
andrewboyson 12:237a0f75b4d0 60 HttpAddText("</body>\r\n"
andrewboyson 12:237a0f75b4d0 61 "</html>\r\n");
andrewboyson 12:237a0f75b4d0 62 }
andrewboyson 0:f8998d10763e 63
andrewboyson 0:f8998d10763e 64 void PageAddNavItem(int highlight, char* href, char* title)
andrewboyson 0:f8998d10763e 65 {
andrewboyson 0:f8998d10763e 66 char *p;
andrewboyson 0:f8998d10763e 67 HttpAddText("<li ");
andrewboyson 0:f8998d10763e 68 if (highlight) p = "class='this'";
andrewboyson 0:f8998d10763e 69 else p = " ";
andrewboyson 0:f8998d10763e 70 HttpAddText(p);
andrewboyson 0:f8998d10763e 71 HttpAddText("><a href='");
andrewboyson 0:f8998d10763e 72 HttpAddText(href);
andrewboyson 0:f8998d10763e 73 HttpAddText("'>");
andrewboyson 0:f8998d10763e 74 HttpAddText(title);
andrewboyson 0:f8998d10763e 75 HttpAddText("</a></li>\r\n");
andrewboyson 0:f8998d10763e 76
andrewboyson 0:f8998d10763e 77 }
andrewboyson 30:6a08abbe6301 78 void PageAddNav(int page)
andrewboyson 30:6a08abbe6301 79 {
andrewboyson 91:9a125082f53c 80 HttpAddText("<a class='tab-shortcut' href='#main-content'>Skip to main content</a>\r\n");
andrewboyson 91:9a125082f53c 81
andrewboyson 30:6a08abbe6301 82 HttpAddText("<nav><ul>\r\n");
andrewboyson 30:6a08abbe6301 83 PageAddNavDerived(page);
andrewboyson 86:f3c9beec4ee7 84 PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock" );
andrewboyson 86:f3c9beec4ee7 85 PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault" );
andrewboyson 86:f3c9beec4ee7 86 PageAddNavItem(page == NET_PAGE, "/net", "Net" );
andrewboyson 86:f3c9beec4ee7 87 PageAddNavItem(page == NET4_PAGE, "/net4", "Net IPv4" );
andrewboyson 86:f3c9beec4ee7 88 PageAddNavItem(page == NET6_PAGE, "/net6", "Net IPv6" );
andrewboyson 86:f3c9beec4ee7 89 PageAddNavItem(page == TRACE_PAGE, "/trace", "Net Trace");
andrewboyson 86:f3c9beec4ee7 90 PageAddNavItem(page == LOG_PAGE, "/log", "Log" );
andrewboyson 86:f3c9beec4ee7 91 PageAddNavItem(page == FIRMWARE_PAGE, "/firmware", "Firmware" );
andrewboyson 30:6a08abbe6301 92 HttpAddText("</ul></nav>\r\n");
andrewboyson 30:6a08abbe6301 93 }
andrewboyson 0:f8998d10763e 94
andrewboyson 77:4689596a2f3f 95 void PageAddLabelledPrefixSuffix(char* label, char* prefix, char* text, char* suffix)
andrewboyson 0:f8998d10763e 96 {
andrewboyson 77:4689596a2f3f 97 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 98 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 99 HttpAddF (" <div>%s%s%s</div>\r\n", prefix, text, suffix);
andrewboyson 77:4689596a2f3f 100 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 101 }
andrewboyson 77:4689596a2f3f 102 void PageAddLabelledText(char* label, char* text)
andrewboyson 77:4689596a2f3f 103 {
andrewboyson 77:4689596a2f3f 104 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 105 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 106 HttpAddF (" <div>%s</div>\r\n", text);
andrewboyson 0:f8998d10763e 107 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 108 }
andrewboyson 0:f8998d10763e 109
andrewboyson 77:4689596a2f3f 110 void PageAddLabelledMac(char* label, char* mac)
andrewboyson 0:f8998d10763e 111 {
andrewboyson 77:4689596a2f3f 112 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 113 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 114 HttpAddText(" <div>"); MacHttp(mac); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 115 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 116 }
andrewboyson 77:4689596a2f3f 117
andrewboyson 77:4689596a2f3f 118 void PageAddLabelledIp4(char* label, uint32_t ip)
andrewboyson 77:4689596a2f3f 119 {
andrewboyson 77:4689596a2f3f 120 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 121 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 122 HttpAddText(" <div>"); Ip4AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 123 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 124 }
andrewboyson 0:f8998d10763e 125
andrewboyson 77:4689596a2f3f 126 void PageAddLabelledIp6(char* label, char* ip)
andrewboyson 0:f8998d10763e 127 {
andrewboyson 77:4689596a2f3f 128 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 129 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 130 HttpAddText(" <div>"); Ip6AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 131 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 132 }
andrewboyson 77:4689596a2f3f 133 void PageAddLabelledOnOff(char* label, bool value)
andrewboyson 0:f8998d10763e 134 {
andrewboyson 77:4689596a2f3f 135 if (value) PageAddLabelledText(label, "On");
andrewboyson 77:4689596a2f3f 136 else PageAddLabelledText(label, "Off");
andrewboyson 0:f8998d10763e 137 }
andrewboyson 77:4689596a2f3f 138 void PageAddLabelledInt(char* label, int value)
andrewboyson 0:f8998d10763e 139 {
andrewboyson 0:f8998d10763e 140 char text[30];
andrewboyson 11:84121d7b47e9 141 snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
andrewboyson 77:4689596a2f3f 142 PageAddLabelledText(label, text);
andrewboyson 0:f8998d10763e 143 }
andrewboyson 77:4689596a2f3f 144 void PageAddInputText(char* label, float inputwidth, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 145 {
andrewboyson 58:e5ab14ef6ea6 146 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 147 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 148 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 149 HttpAddF (" <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value);
andrewboyson 77:4689596a2f3f 150 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 151 HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n");
andrewboyson 58:e5ab14ef6ea6 152 HttpAddF ("</form>\r\n");
andrewboyson 0:f8998d10763e 153
andrewboyson 0:f8998d10763e 154 }
andrewboyson 77:4689596a2f3f 155 void PageAddInputInt(char* label, float inputwidth, int value, char* action, char* name)
andrewboyson 0:f8998d10763e 156 {
andrewboyson 0:f8998d10763e 157 char text[30];
andrewboyson 0:f8998d10763e 158 snprintf(text, sizeof(text), "%d", value);
andrewboyson 77:4689596a2f3f 159 PageAddInputText(label, inputwidth, text, action, name);
andrewboyson 0:f8998d10763e 160 }
andrewboyson 77:4689596a2f3f 161 void PageAddInputButton(char* label, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 162 {
andrewboyson 77:4689596a2f3f 163 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 164 HttpAddF ("<input type='hidden' name='%s'>\r\n", name);
andrewboyson 77:4689596a2f3f 165 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 166 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 167 HttpAddF (" <input type='submit' value='%s'>\r\n", value);
andrewboyson 77:4689596a2f3f 168 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 169 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 170 }
andrewboyson 77:4689596a2f3f 171 void PageAddAjaxInputToggle(char* label, char* id, char* name)
andrewboyson 0:f8998d10763e 172 {
andrewboyson 77:4689596a2f3f 173 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 174 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 90:9cc77a16b6c5 175 HttpAddF (" <div class='toggle' id='%s' tabindex='0' dir='ltr' onclick='AjaxRequest(\"%s=1\")' onkeydown='return event.keyCode != 13 || AjaxRequest(\"%s=1\")'>\r\n", id, name, name);
andrewboyson 58:e5ab14ef6ea6 176 HttpAddText(" <div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 58:e5ab14ef6ea6 177 HttpAddText(" </div>\r\n");
andrewboyson 0:f8998d10763e 178 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 179 }
andrewboyson 77:4689596a2f3f 180 void PageAddAjaxLed(char* label, char* id)
andrewboyson 58:e5ab14ef6ea6 181 {
andrewboyson 77:4689596a2f3f 182 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 183 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 184 HttpAddF (" <div class='led' id='%s' dir='ltr'></div>\r\n", id);
andrewboyson 0:f8998d10763e 185 HttpAddText("</div>\r\n");
andrewboyson 58:e5ab14ef6ea6 186 }
andrewboyson 77:4689596a2f3f 187 void PageAddAjaxInput(char* label, float inputwidth, char* id, char* name)
andrewboyson 58:e5ab14ef6ea6 188 {
andrewboyson 77:4689596a2f3f 189 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 190 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 191 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", inputwidth, id, name);
andrewboyson 0:f8998d10763e 192 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 193 }
andrewboyson 84:4ed751de613e 194 void PageAddAjaxInputSuffix(char* label, float inputwidth, char* id, char* name, char* suffix)
andrewboyson 84:4ed751de613e 195 {
andrewboyson 84:4ed751de613e 196 HttpAddText("<div class='line'>\r\n");
andrewboyson 84:4ed751de613e 197 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 84:4ed751de613e 198 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>%s\r\n", inputwidth, id, name, suffix);
andrewboyson 84:4ed751de613e 199 HttpAddText("</div>\r\n");
andrewboyson 84:4ed751de613e 200 }
andrewboyson 77:4689596a2f3f 201 void PageAddAjaxLabelled(char* label, char* id)
andrewboyson 57:8fa31ff4e773 202 {
andrewboyson 77:4689596a2f3f 203 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 204 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 205 HttpAddF (" <div id='%s'></div>\r\n", id);
andrewboyson 77:4689596a2f3f 206 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 207 }
andrewboyson 77:4689596a2f3f 208 void PageAddAjaxLabelledSuffix(char* label, char* id, char* suffix)
andrewboyson 77:4689596a2f3f 209 {
andrewboyson 77:4689596a2f3f 210 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 211 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 212 HttpAddF (" <div><span id='%s'></span>%s</div>\r\n", id, suffix);
andrewboyson 57:8fa31ff4e773 213 HttpAddText("</div>\r\n");
andrewboyson 57:8fa31ff4e773 214 }
andrewboyson 0:f8998d10763e 215
andrewboyson 0:f8998d10763e 216