word/edit: search input form in hierarchy navigation
[sheet.git] / word / edit.plp
index d8de60249c7a54945d048885acc4fb747d87357a..0f212b86702d05e211cbc2edea6fa52c414e8e31 100644 (file)
@@ -1,6 +1,8 @@
 <(../common.inc.plp)><:
 
+my $editorurl = '/word/edit';
 s{\Aedit(/|\z)}{} for $Request // ();
+
 Html({
        title => 'words cheat sheet admin',
        version => '1.0',
@@ -8,6 +10,8 @@ Html({
        raw => <<'EOT',
 <link rel="stylesheet" type="text/css" media="all" href="/word/editor.css" />
 <script src="/word/editor.js"></script>
+<script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.min.js"></script>
+<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.min.css" />
 EOT
 });
 
@@ -21,7 +25,6 @@ my $db = eval {
 } or Abort('Database error', 501, $@);
 
 my $user = eval {
-       my $rootpath = '/word/edit';
        if (defined $post{username}) {
                $cookie{login} = EncodeURI(join ':', @post{qw( username pass )});
        }
@@ -30,7 +33,7 @@ my $user = eval {
                if (AddCookie(CGI::Cookie->new(
                        -name    => 'login',
                        -value   => '',
-                       -path    => $rootpath,
+                       -path    => $editorurl,
                        -expires => 'now',
                )->as_string)) {
                        delete $cookie{login};
@@ -50,7 +53,7 @@ my $user = eval {
                my $httpcookie = CGI::Cookie->new(
                        -name    => 'login',
                        -value   => join(':', @{$found}{qw( username pass )}),
-                       -path    => $rootpath,
+                       -path    => $editorurl,
                ) or die "prepared object is empty\n";
                AddCookie($httpcookie->as_string);
        } or Abort(["Unable to create login cookie", $@], 403);
@@ -71,9 +74,10 @@ my $user = eval {
 };
 
 my %lang = (
+       '' => ['(reference)'],
        nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
        en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
-       eo => ['<span style="color:green">★</span>', 'esperanto'],
+       eo => [qq'<span style="color:green">\N{BLACK STAR}</span>', 'esperanto'],
        ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
        zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'],
        la => ["\N{PUSHPIN}", 'latin'],
@@ -88,7 +92,9 @@ my %wordcol = (
        prio    => [
                {-label => 'Level', -select => sub {
                        my ($row) = @_;
-                       my @enum = qw[ essential basic common distinctive optional invisible ];
+                       my @enum = qw[
+                               essential ubiquitous basic common distinctive specialised rare invisible
+                       ];
                        return {
                                ('' => 'parent') x (defined $row->{ref}),
                                map { $_ => $enum[$_] } 0 .. $#enum
@@ -105,17 +111,19 @@ my %wordcol = (
                return "data/word/org/$_[0]->{id}.jpg";
        }},
        convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
-               return "data/word/en/$_[0]->{id}.jpg";
+               return "data/word/32/$_[0]->{id}.jpg";
        }},
+       crop32  => {-label => 'Crop 3:2', -json => 'image'},
        story   => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
 );
 
 if (my $search = $fields{q}) {
-       my %filter = (form => {ilike => '%'.$search.'%'});
+       my %filter = $search eq '^' ? (cat => undef, ref => undef) :
+               (form => {ilike => '%'.parseinput($search).'%'});
        my $results = $db->select(word => '*', \%filter);
        say '<h1>Search</h1><ul>';
        printf("<li><small>%s</small> %s %s</li>\n",
-               $_->{id}, showlink($_->{form}, "/writer/$_->{id}"),
+               $_->{id}, showlink($_->{form}, "$editorurl/$_->{id}"),
                sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{convert}->{-src}->($_)) x defined $_->{image}
        ) for $results->hashes;
        say "</ul>\n";
@@ -156,7 +164,7 @@ elsif (defined $post{form}) {{
        my $imagecol = $row->{image};  # backup image subcolumns
        ref $_ eq 'HASH' and $_ = encode_json($_) for values %{$row};
 
-       if (!$row->{form}) {
+       if (!$row->{form} and $row->{lang}) {
                if ($row->{ref} ne 'delete') {
                        Alert("Empty title",
                                "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
@@ -213,7 +221,7 @@ elsif (defined $post{form}) {{
        $reimage++ if $fields{rethumb};  # force refresh
        if ($reimage) {
                eval {
-                       $image->convert($wordcol{convert}->{-src}->($row), $imagecol->{convert});
+                       $image->generate($wordcol{convert}->{-src}->($row), $imagecol);
                } or do {
                        my ($warn, @details) = ref $@ ? @{$@} : $@;
                        Alert([ "Thumbnail image not generated", $warn ], @details);
@@ -221,13 +229,13 @@ elsif (defined $post{form}) {{
        }
 }}
 else {
-       $row->{lang} //= $user->{editlang}->[0];
+       $row->{lang} //= $user->{editlang}->[0] unless exists $row->{lang};
        $row->{$_} = $get{$_} for keys %get;
-       $row->{prio} = defined $row->{ref} ? undef : 1 unless exists $row->{prio};
+       $row->{prio} = defined $row->{ref} ? undef : 4 unless exists $row->{prio};
 }
 
 eval {
-       my $imagerow = $row->{image} && decode_json(delete $row->{image}) || {};
+       my $imagerow = $row->{image} && JSON->new->decode(delete $row->{image}) || {};
        while (my ($col, $val) = each %{$imagerow}) {
                $row->{$col} = $val;
        }
@@ -264,7 +272,7 @@ for my $col (@wordcols) {
 }
 
 if (not $row->{ref}) {
-       printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
+       printf '<li><label for="%s">%s</label><div><ul class="inline multiinput" id="%1$s">',
                'trans', 'Translations';
        my @children = !$row->{id} ? () :
                $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
@@ -283,7 +291,7 @@ if (not $row->{ref}) {
                printf(
                        $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
                        '<input id="%s" name="%1$s" value="%3$s" />',
-                       "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form} // ''),
+                       "trans-$ref->{lang}", "$editorurl/$ref->{id}", Entity($ref->{form} // ''),
                );
        }
        say '</ul></div></li>';
@@ -292,7 +300,7 @@ if (not $row->{ref}) {
 </ul>
 <p>
        <input type="submit" value="Save" />
-       <input type="submit" value="New" formaction="/writer?copy=cat" />
+       <input type="submit" value="New" formaction="<:= $editorurl :>?copy=cat" />
 </p>
 </form>
 
@@ -306,20 +314,24 @@ if ($row->{id}) {
 say '<ul>';
 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
 while (my $ref = $parents->hash) {
-       printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
+       printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
 }
 say "<li><strong>$_</strong></li>" for Entity($row->{form});
 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
 while (my $ref = $children->hash) {
-       printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
+       printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
 }
 :>
-<li><form action="/writer">
+<li><form action="<:= $editorurl :>">
        <input type="hidden" name="cat" value="<:= $row->{id} :>" />
        <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
        <input type="submit" value="Add" />
 </form></li>
 </ul>
+
+<form id="search">
+       <input type="search" name="q" value="" placeholder="search" /><button type="submit">🔍</button>
+</form>
 </section>
 <:
 }