github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/views/components/JSON.html (about) 1 <!-- Content managed by Project Forge, see [projectforge.md] for details. --> 2 {% import ( 3 "github.com/kyleu/dbaudit/app/controller/cutil" 4 "github.com/kyleu/dbaudit/app/util" 5 ) %} 6 7 {% func JSONModal(key string, title string, item any, indent int) %}{% stripspace %} 8 <div id="modal-{%s key %}" class="modal" style="display: none;"> 9 <a class="backdrop" href="#"></a> 10 <div class="modal-content"> 11 <div class="modal-header"> 12 <a href="#" class="modal-close">×</a> 13 <h2>{%s title %}</h2> 14 </div> 15 <div class="modal-body"> 16 <div id="modal-{%s key %}-data" hidden="hidden" style="display:none;">{%s util.ToJSON(item) %}</div> 17 <button onclick="clip('{%s key %}');">Copy to clipboard</button> 18 {%= JSON(item) %} 19 </div> 20 </div> 21 </div> 22 <script> 23 function clip(k) { 24 if (!navigator.clipboard) { 25 return; 26 } 27 const el = document.getElementById("modal-" + k + "-data"); 28 navigator.clipboard.writeText(el.innerText); 29 } 30 </script> 31 {% endstripspace %}{% endfunc %} 32 33 {% func JSON(v any) %}{% stripspace %} 34 {% code 35 b, ok := v.([]byte) 36 if ok { 37 _ = util.FromJSON(b, &v) 38 } 39 %} 40 {% code json := util.ToJSONBytes(v, true) %} 41 {% if len(json) > (1024 * 256) %} 42 <div class="mt"><em>This JSON is too large (<strong>{%s util.ByteSizeSI(int64(len(json))) %}</strong>), highlighting is disabled</em></div> 43 <div class="mt overflow full-width"> 44 <pre>{%s string(json) %}</pre> 45 </div> 46 {% else %} 47 {% code out, err := cutil.FormatLang(string(json), "json") %} 48 {% if err == nil %} 49 <div class="mt overflow full-width">{%s= out %}</div> 50 {% else %} 51 <div class="mt error">{%s err.Error() %}</div> 52 {% endif %} 53 {% endif %} 54 {% endstripspace %}{% endfunc %}