From b36269ea857ea81cbfa94eee26b7b2ba67b5a02e 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 | 62 +++++++++++++------ koha-tmpl/opac-tmpl/prog/en/css/opac.css | 10 ++- .../lib/jquery/plugins/jquery.highlight-3.js | 53 ++++++++++++++++ .../prog/en/modules/opac-opensearch.tmpl | 6 +- .../prog/en/modules/opac-results-grouped.tmpl | 26 +++++++- .../prog/en/modules/opac-results.tmpl | 29 ++++++++- 9 files changed, 214 insertions(+), 85 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 e838ee28e5..bae6aff491 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'}; @@ -1130,23 +1129,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'}; @@ -1248,48 +1237,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 a2f122a732..cc354ce918 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; @@ -1205,9 +1208,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 def42f378a..4acf2c164d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl @@ -1,6 +1,7 @@ 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 aba7c155e8..eb995f18b2 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl @@ -10,6 +10,7 @@ +