From ff7ab2d62ae1592134ff109f03940d703fa24325 Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Mon, 27 Jul 2009 20:47:50 -0500 Subject: [PATCH] Bug 3056 - replace span hacking for terms in C4::Search Reimplemented using jquery, added OSS plugin to both interfaces. This is another case where having a /common directory used by both OPAC and STAFF would increase runtime efficiency and cut development time. Removed unused variables. I changed the OPAC .term color to match the staff interface, rather than being a second shade of blue. The highlight/unhighlight link is currently a bit bolder than its neighbors, but my styling choices are provisional. I expect the final tweaking of CSS to come from another more capable designer, like Owen. Note this patch may rely on previously submitted "Search.pm minor cleanup" patch. Signed-off-by: Galen Charlton --- C4/Search.pm | 55 +------- .../prog/en/css/staff-global.css | 5 +- .../lib/jquery/plugins/jquery.highlight-3.js | 53 +++++++ .../prog/en/modules/catalogue/results.tmpl | 73 +++++++--- koha-tmpl/opac-tmpl/prog/en/css/opac.css | 10 +- .../lib/jquery/plugins/jquery.highlight-3.js | 53 +++++++ .../prog/en/modules/opac-opensearch.tmpl | 9 +- .../prog/en/modules/opac-results-grouped.tmpl | 26 +++- .../prog/en/modules/opac-results.tmpl | 131 ++++++++++++++++-- 9 files changed, 320 insertions(+), 95 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/lib/jquery/plugins/jquery.highlight-3.js create mode 100644 koha-tmpl/opac-tmpl/prog/en/lib/jquery/plugins/jquery.highlight-3.js diff --git a/C4/Search.pm b/C4/Search.pm index 0191ac7b1e..a80c79fd02 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -175,7 +175,6 @@ for my $i (0..$hits) { my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,''); #build the hash for the template. - $resultsloop{highlight} = ($i % 2)?(1):(0); $resultsloop{title} = $biblio->{'title'}; $resultsloop{subtitle} = $biblio->{'subtitle'}; $resultsloop{biblionumber} = $biblio->{'biblionumber'}; @@ -1126,23 +1125,13 @@ Format results in a form suitable for passing to the template sub searchResults { my ( $searchdesc, $hits, $results_per_page, $offset, $scan, @marcresults ) = @_; my $dbh = C4::Context->dbh; - my $even = 1; my @newresults; - # add search-term highlighting via s on the search terms - my $span_terms_hashref; - for my $span_term ( split( / /, $searchdesc ) ) { - $span_term =~ s/(.*=|\)|\(|\+|\.|\*)//g; - $span_terms_hashref->{$span_term}++; - } - #Build branchnames hash #find branchname #get branch information..... my %branches; - my $bsth = - $dbh->prepare("SELECT branchcode,branchname FROM branches") - ; # FIXME : use C4::Koha::GetBranches + my $bsth =$dbh->prepare("SELECT branchcode,branchname FROM branches"); # FIXME : use C4::Branch::GetBranches $bsth->execute(); while ( my $bdata = $bsth->fetchrow_hashref ) { $branches{ $bdata->{'branchcode'} } = $bdata->{'branchname'}; @@ -1254,48 +1243,6 @@ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g; $oldbiblio->{summary} = $summary; } - # save an author with no tag, for the > link - $oldbiblio->{'author_nospan'} = $oldbiblio->{'author'}; - $oldbiblio->{'title_nospan'} = $oldbiblio->{'title'}; - $oldbiblio->{'subtitle_nospan'} = $oldbiblio->{'subtitle'}; - # Add search-term highlighting to the whole record where they match using s - if (C4::Context->preference("OpacHighlightedWords")){ - my $searchhighlightblob; - for my $highlight_field ( $marcrecord->fields ) { - - # FIXME: need to skip title, subtitle, author, etc., as they are handled below - next if $highlight_field->tag() =~ /(^00)/; # skip fixed fields - for my $subfield ($highlight_field->subfields()) { - my $match; - next if $subfield->[0] eq '9'; - my $field = $subfield->[1]; - for my $term ( keys %$span_terms_hashref ) { - if ( ( $field =~ /$term/i ) && (( length($term) > 3 ) || ($field =~ / $term /i)) ) { - $field =~ s/$term/$&<\/span>/gi; - $match++; - } - } - $searchhighlightblob .= $field . " ... " if $match; - } - - } - $searchhighlightblob = ' ... '.$searchhighlightblob if $searchhighlightblob; - $oldbiblio->{'searchhighlightblob'} = $searchhighlightblob; - } - - # Add search-term highlighting to the title, subtitle, etc. fields - for my $term ( keys %$span_terms_hashref ) { - my $old_term = $term; - if ( length($term) > 3 ) { - $term =~ s/(.*=|\)|\(|\+|\.|\?|\[|\]|\\|\*)//g; - foreach(qw(title subtitle author publishercode place pages notes size)) { - $oldbiblio->{$_} =~ s/$term/$&<\/span>/gi; - } - } - } - - ($i % 2) and $oldbiblio->{'toggle'} = 1; - # Pull out the items fields my @fields = $marcrecord->field($itemtag); diff --git a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css index 858e1d19b3..ce264d2044 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css +++ b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css @@ -407,6 +407,9 @@ a.yuimenuitemlabel-disabled, #disabled a { a.yuimenuitemlabel-disabled:hover, #disabled a:hover { color : #999; } +a.highlight_toggle { + display : none; +} ul.toolbar li { display : inline; @@ -1199,9 +1202,7 @@ h1#logo { } h1#logo { background:transparent url(../../img/koha-logo-medium.gif) no-repeat scroll 0%; - border: 0; margin:0.75em .3em 0.75em .7em; - padding:0; } *html h1#logo { diff --git a/koha-tmpl/intranet-tmpl/prog/en/lib/jquery/plugins/jquery.highlight-3.js b/koha-tmpl/intranet-tmpl/prog/en/lib/jquery/plugins/jquery.highlight-3.js new file mode 100644 index 0000000000..69bb52d3b1 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/lib/jquery/plugins/jquery.highlight-3.js @@ -0,0 +1,53 @@ +/* + +highlight v3 + +Highlights arbitrary terms. + + + +MIT license. + +Johann Burkard + + + +*/ + +jQuery.fn.highlight = function(pat) { + function innerHighlight(node, pat) { + var skip = 0; + if (node.nodeType == 3) { + var pos = node.data.toUpperCase().indexOf(pat); + if (pos >= 0) { + var spannode = document.createElement('span'); + spannode.className = 'term'; + var middlebit = node.splitText(pos); + var endbit = middlebit.splitText(pat.length); + var middleclone = middlebit.cloneNode(true); + spannode.appendChild(middleclone); + middlebit.parentNode.replaceChild(spannode, middlebit); + skip = 1; + } + } + else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) { + for (var i = 0; i < node.childNodes.length; ++i) { + i += innerHighlight(node.childNodes[i], pat); + } + } + return skip; + } + return this.each(function() { + innerHighlight(this, pat.toUpperCase()); + }); +}; + +jQuery.fn.removeHighlight = function() { + return this.find("span.term").each(function() { + this.parentNode.firstChild.nodeName; + with (this.parentNode) { + replaceChild(this.firstChild, this); + normalize(); + } + }).end(); +}; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl index e6b36292a5..a3ea38415f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl @@ -1,12 +1,13 @@ Koha › Catalog › <!-- TMPL_IF NAME="searchdesc" -->Results of Search <!-- TMPL_IF NAME="query_desc" -->for '<!-- TMPL_VAR NAME="query_desc" -->'<!-- /TMPL_IF --><!-- TMPL_IF NAME="limit_desc" --> with limit(s): '<!-- TMPL_VAR NAME="limit_desc" -->'<!-- /TMPL_IF --><!-- TMPL_ELSE -->You did not specify any search criteria<!-- /TMPL_IF --> - + @@ -159,6 +181,8 @@ $(document).ready(function(){
@@ -188,7 +212,7 @@ $(document).ready(function(){ " title="View details for this title"> " title="View details for this title"> - by " title="Search for works by this author"> + by " title="Search for works by this author">  

- - diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl index b217aeb62a..c33a668ea8 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl @@ -10,8 +10,89 @@ +