Skip to content

Commit cdc9779

Browse files
committed
A bit of refactoring & reorg.
1 parent e75efbc commit cdc9779

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

web/regex_demo.html

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@
107107
sub pp { Data::Dumper->new([@_])->Useqq(1)->Terse(1)->Pair('=>')
108108
->Sortkeys(1)->Quotekeys(0)->Indent(0)->Purity(1)->Dump }
109109

110-
$jq->('#perlinfo')->text("perl $^V");
111-
112110
sub sample_init {
113111
my $samp = shift;
114112
my $samptxt = $samp->children(".samptxt");
@@ -154,7 +152,7 @@
154152
}
155153
$addsamp->click(sub { newsamp('')->click });
156154

157-
our $re_debug=0;
155+
our $re_debug=0; #TODO Later: this is actually a parameter to update()
158156
my $ta_debugout = $jq->('#debugout');
159157
my $re_debug_hide = $jq->('#re_debug_hide');
160158
$jq->('#re_debug')->click(sub {
@@ -188,13 +186,14 @@
188186
$codecopy->hide;
189187
$sampcodebtn->text('Show Example Perl Code');
190188
}
191-
else {
192-
$samplecode_ta->show;
193-
$codecopy->show;
194-
$sampcodebtn->text('Hide Example Perl Code');
195-
update();
196-
}
189+
else { sampcode_show() }
197190
});
191+
sub sampcode_show {
192+
$samplecode_ta->show;
193+
$codecopy->show;
194+
$sampcodebtn->text('Hide Example Perl Code');
195+
update();
196+
}
198197
$codecopy->click(sub {
199198
$samplecode_ta->[0]->select;
200199
js(q{ document.execCommand("copy"); });
@@ -209,30 +208,42 @@
209208
$precode_ta->hide;
210209
$precodebtn->text("Add Preamble Code");
211210
}
212-
else {
213-
$precode_ta->show;
214-
$precodebtn->text("Disable Preamble Code");
215-
}
211+
else { precode_show() }
216212
update();
217213
});
214+
sub precode_show {
215+
$precode_ta->text(shift) if @_;
216+
$precode_ta->show;
217+
$precodebtn->text("Disable Preamble Code");
218+
}
218219
$precode_ta->hide;
219220
$precode_ta->change(\&update);
220221
$precode_ta->keyup( \&update);
221222

222223
my $thisurl_ta = $jq->("#thisurl");
224+
$jq->('#urlcopy')->click(sub {
225+
$thisurl_ta->[0]->select;
226+
js(q{ document.execCommand("copy"); });
227+
});
228+
229+
$jq->('#perlinfo')->text("perl $^V");
230+
223231
my $ta_regex = $jq->("#regex");
224232
my $ta_flags = $jq->("#flags");
225-
my $warnmsgs = $jq->("#warnmsgs");
226233
$ta_regex->change(\&update);
227234
$ta_regex->keyup( \&update);
228235
$ta_flags->change(\&update);
229236
$ta_flags->keyup( \&update);
230-
update();
237+
238+
js('$(window)')->on('hashchange',\&hashchange);
231239

232240
$ta_regex->on('input', sub {
233241
$ta_regex->height($ta_regex->[0]->{scrollHeight});
234242
});
235243

244+
hashchange();
245+
update();
246+
236247
sub run_code {
237248
my ($code,$inp) = @_;
238249
my @warns;
@@ -264,6 +275,7 @@
264275
." Here, a workaround is used so it acts as a true empty pattern.\n" unless length $regex;
265276
$warn .= "\\n is recommended over literal newlines\n" if $regex=~/\n/ && $flags!~/x/;
266277
$warn .= "\\t is recommended over literal tabs\n" if $regex=~/\t/ && $flags!~/x/;
278+
state $warnmsgs = $jq->("#warnmsgs");
267279
$warnmsgs->text($warn);
268280

269281
# apply regex to the samples and do highlighting
@@ -372,11 +384,6 @@
372384
$thisurl_ta->text( $baseurl . $hash );
373385
}
374386

375-
$jq->('#urlcopy')->click(sub {
376-
$thisurl_ta->[0]->select;
377-
js(q{ document.execCommand("copy"); });
378-
});
379-
380387
sub hashchange {
381388
my $hash = js('window.location')->{hash};
382389
return unless $hash=~/^#.*\bregex=/;
@@ -390,16 +397,8 @@
390397
$ta_regex->text($res{regex});
391398
$ta_regex->height($ta_regex->[0]->{scrollHeight});
392399
$ta_flags->text($res{flags});
393-
if ($res{showsampcode}) {
394-
$samplecode_ta->show;
395-
$codecopy->show;
396-
$sampcodebtn->text('Hide Example Perl Code');
397-
}
398-
if (exists $res{pre}) {
399-
$precode_ta->text($res{pre});
400-
$precode_ta->show;
401-
$precodebtn->text("Disable Preamble Code");
402-
}
400+
sampcode_show() if $res{showsampcode};
401+
precode_show($res{pre}) if exists $res{pre};
403402
if (exists $res{samp1}) {
404403
$jq->(".sample")->remove();
405404
for (my $i=1;exists $res{"samp$i"};$i++) {
@@ -410,8 +409,6 @@
410409
}
411410
else { js('window.location')->{hash}='' }
412411
}
413-
hashchange();
414-
js('$(window)')->on('hashchange',\&hashchange);
415412

416413
</script>
417414

0 commit comments

Comments
 (0)