word edit: dynamically aligned form styling (emulate table)
[sheet.git] / writer.plp
index 11754e8a72d4b1834aa904126ad158cda5fee5f4..9d67520bdd1230d6d14412d3c009986ace370cb1 100644 (file)
@@ -6,27 +6,39 @@ Html({
        nocache => 1,
        raw => <<'EOT',
 <style>
-dd > input {
+dl {
+       display: inline-grid;
+       grid: auto-flow / min-content repeat(10, auto);
+}
+
+form > ul {
+       display: table;
+       border-spacing: 0 2px;
+}
+form > ul li {
+       display: table-row;
+}
+form > ul li > * {
+       display: table-cell;
+       padding-right: .5em;
+}
+form > ul li > label {
+       /* th */
+       text-align: right;
+}
+form > ul li > label + * {
+       /* td */
        width: 32em;
-       max-width: 100%;
-       padding: 1ex;
-       font-family: monospace;
 }
-dl > dt, dl > dd {
-       float: none;
-       display: inline-block;
+
+form > ul li > p input {
        box-sizing: border-box;
-       width: 50%;
-       margin: 0;
-       line-height: 4ex;
-       vertical-align: text-top;
+       width: 100%;
+       padding: .4rem;
+       font-family: monospace;
 }
-dd > img {
+form > ul li img {
        max-width: 300px;
-       display: block;
-}
-dd input ~ button {
-       margin-left: -5em;
 }
 
 ul.popup {
@@ -44,16 +56,29 @@ ul.popup {
        border: 1px solid #CCC;
 }
 
-section {
-       position: absolute;
-       top: 7ex;
-       right: 1em;
+h1 {
+       margin-bottom: 1ex;
+}
+.inline {
+       display: inline-flex;
+       align-items: start;
+       margin: 0 -1ex; /* inner gap */
+}
+.inline > * {
+       margin: 0 1ex;
+}
+.inline .inline {
+       display: flex;
 }
-section > ul {
-       margin-top: 1ex;
+
+#nav {
+       -margin-left: 1em; /* flex gap */
 }
-section > ul strong, section form {
-       line-height: 2;
+#nav > ul,
+#nav > ul strong,
+#nav form {
+       margin: 1ex 0;
+       display: inline-block;
 }
 </style>
 
@@ -65,7 +90,9 @@ document.addEventListener('DOMContentLoaded', () => {
        wpbutton.append('Copy');
        wpbutton.onclick = () => {
                let wptitle = wpinput.value || document.getElementById('form').value;
-               let wppage = 'https://en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&prop=text&page='+wptitle;
+               let wplang = document.getElementById('lang').value.substr(0, 2); // crude iso-639-3→2
+               let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
+               let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&page='+wptitle;
                fetch(wppage).then(res => res.json()).then(json => {
                        if (json.error) throw `error returned: ${json.error.info}`;
                        wpinput.value = json.parse.title;
@@ -108,13 +135,15 @@ my $db = eval {
 } or Abort('Database error', 501, $@);
 
 my @wordcols = (
+       lang    => 'Language',
+       cat     => 'Category',
        form    => 'Translation',
+       alt     => 'Synonyms',
        wptitle => 'Wikipedia',
-       ref     => 'Reference',
-       cat     => 'Category',
-       lang    => 'Language',
        source  => 'Image URL',
        thumb   => 'Convert options',
+       prio    => 'Level',
+       ref     => 'Reference',
 );
 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
 
@@ -125,9 +154,9 @@ if ($find) {
 }
 
 if (exists $get{copy}) {
-       $row = {%{$row}{ qw(lang cat) }};
+       $row = {%{$row}{ qw(prio lang cat) }};
 }
-elsif ($ENV{REQUEST_METHOD} eq 'POST') {
+elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
        my $replace = $row;
        $row = {%post{ pairkeys @wordcols }};
        $_ = length ? $_ : undef for values %{$row};
@@ -137,7 +166,10 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {
                my $query = $find ? $db->update(word => $row, $find, \%res) :
                        $db->insert(word => $row, \%res);
                $row = $query->hash;
-       } or Alert("Entry could not be saved", $@);
+       } or do {
+               Alert("Entry could not be saved", $@);
+               next;
+       };
 
        my $imgpath = "data/word/org/$row->{id}.jpg";
        if (($row->{source} // '') ne ($replace->{source} // '')) {
@@ -182,8 +214,9 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {
                        unlink $thumbpath;
                }
        }
-}
+}}
 else {
+       $row->{prio} //= 1;
        $row->{$_} = $get{$_} for keys %get;
 }
 
@@ -191,25 +224,27 @@ my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
 :>
 <h1>Words <:= $title :></h1>
 
+<div class="inline">
+
 <form action="?" method="post">
 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
-<dl>
+<ul>
 <:
 
 for my $col (pairs @wordcols) {
        my $val = $row->{$col->key} // '';
        $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
-       printf '<dt><label for="%s">%s</label></dt>'
-               . '<dd><input id="%1$s" name="%1$s" value="%s" />',
+       printf '<li><label for="%s">%s</label><p><span class=inline>'
+               . '<input id="%1$s" name="%1$s" value="%s" />',
                $col->key, $col->value, Entity($val);
-       -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
+       -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
                $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
                $col->key eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" :
                ();
-       say '</dd>';
+       say '</span></p></li>';
 }
 :>
-</dl>
+</ul>
 <p>
        <input type="submit" value="Save" />
        <input type="submit" value="New" formaction="/writer?copy=cat" />
@@ -217,9 +252,9 @@ for my $col (pairs @wordcols) {
 </form>
 
 <:
-$row->{id} or exit;
+if ($row->{id}) {
 :>
-<section>
+<section id="nav">
 <h2>Hierarchy</h2>
 
 <:
@@ -241,3 +276,7 @@ while (my $ref = $children->hash) {
 </form></li>
 </ul>
 </section>
+<:
+}
+:>
+</div>