Update of /cvsroot/php-blog/serendipity/plugins/serendipity_event_livesearch
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11256/plugins/serendipity_event_livesearch
Added Files:
serendipity_event_livesearch.js
serendipity_event_livesearch.php
Log Message:
as jannis and sterlin want this, here it is. go play with it. we can still
kick it if there are serious issues with it. after all, it's just a plugin
and nobody is forced to use it ;)
--- NEW FILE: serendipity_event_livesearch.js ---
/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License"); |
// | you may not use this file except in compliance with the License. |
// | You may obtain a copy of the License at |
// | http://www.apache.org/licenses/LICENSE-2.0 |
// | Unless required by applicable law or agreed to in writing, software |
// | distributed under the License is distributed on an "AS IS" BASIS, |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
// | implied. See the License for the specific language governing |
// | permissions and limitations under the License. |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <de...@bi...> |
// +----------------------------------------------------------------------+
*/
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
liveSearchReq = new XMLHttpRequest();
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function liveSearchInit() {
if (navigator.userAgent.indexOf("Safari") > 0) {
document.getElementById('serendipityQuickSearchTermField').addEventListener("keydown", liveSearchKeyPress,false);
document.getElementById('searchform').addEventListener("submit", liveSearchSubmit,false);
} else if (navigator.product == "Gecko") {
document.getElementById('serendipityQuickSearchTermField').addEventListener("keypress", liveSearchKeyPress,false);
document.getElementById('searchform').addEventListener("submit", liveSearchSubmit,false);
} else {
document.getElementById('serendipityQuickSearchTermField').attachEvent("onkeydown", liveSearchKeyPress);
document.getElementById('searchform').attachEvent("onsubmit", liveSearchSubmit);
isIE = true;
}
document.getElementById('serendipityQuickSearchTermField').style.border = '1px solid green';
}
function liveSearchKeyPress(event) {
if (event.keyCode == 40 ) { //KEY DOWN
highlight = document.getElementById("LSHighlight");
if (!highlight) {
highlight = document.getElementById("LSResult").firstChild.firstChild.firstChild;
} else {
highlight.removeAttribute("id");
highlight = highlight.nextSibling;
}
if (highlight) {
highlight.setAttribute("id","LSHighlight");
}
if (!isIE) { event.preventDefault(); }
}
//KEY UP
else if (event.keyCode == 38 ) {
highlight = document.getElementById("LSHighlight");
if (!highlight) {
highlight = document.getElementById("LSResult").firstChild.firstChild.lastChild;
}
else {
highlight.removeAttribute("id");
highlight = highlight.previousSibling;
}
if (highlight) {
highlight.setAttribute("id","LSHighlight");
}
if (!isIE) { event.preventDefault(); }
}
//ESC
else if (event.keyCode == 27) {
highlight = document.getElementById("LSHighlight");
if (highlight) {
highlight.removeAttribute("id");
}
document.getElementById("LSResult").style.display = "none";
} else {
liveSearchStart();
}
}
function liveSearchStart() {
if (t) {
window.clearTimeout(t);
}
t = window.setTimeout("liveSearchDoSearch()",200);
}
function liveSearchDoSearch() {
v = document.getElementById('serendipityQuickSearchTermField').value;
if (liveSearchLast != v && v.length > 3) {
if (liveSearchReq && liveSearchReq.readyState < 4) {
liveSearchReq.abort();
}
if (v == "") {
document.getElementById("LSResult").style.display = "none";
highlight = document.getElementById("LSHighlight");
if (highlight) {
highlight.removeAttribute("id");
}
return false;
}
if (window.XMLHttpRequest) {
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
}
document.getElementById('LSResult').style.display = "block";
document.getElementById('LSResult').firstChild.innerHTML = '<div class="serendipity_livesearch_result">' + waittext + '</div>';
liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
liveSearchReq.open("GET", lsbase + "?s=" + v);
liveSearchLast = v;
liveSearchReq.send(null);
}
}
function liveSearchProcessReqChange() {
if (liveSearchReq.readyState == 4) {
var res = document.getElementById("LSResult");
res.style.display = "block";
res.firstChild.innerHTML = liveSearchReq.responseText;
}
}
function liveSearchSubmit() {
var highlight = document.getElementById("LSHighlight");
if (highlight && highlight.firstChild) {
window.location = highlight.firstChild.getAttribute("href");
return false;
}
else {
return true;
}
}
// s9y is missing easy onload() adding, so call it from here:
addLoadEvent(liveSearchInit);
--- NEW FILE: serendipity_event_livesearch.php ---
<?php # $Id: serendipity_event_livesearch.php,v 1.1 2004/07/13 16:25:26 garvinhicking Exp $
switch ($serendipity['lang']) {
case 'de':
@define('PLUGIN_EVENT_LIVESEARCH_NAME', 'LiveSearch');
@define('PLUGIN_EVENT_LIVESEARCH_DESC', 'Erweitert die Suchfunktion mit einer live-aktualisierung mit dem Datenbestand zur sofortigen Anzeige (JavaScript)');
@define('PLUGIN_EVENT_LIVESEARCH_WAIT', 'Suche aktiviert, bitte warten...');
break;
case 'en':
case 'es':
default:
@define('PLUGIN_EVENT_LIVESEARCH_NAME', 'LiveSearch');
@define('PLUGIN_EVENT_LIVESEARCH_DESC', 'Enhances the Search function of your blog with live-updates on keypress (JavaScript)');
@define('PLUGIN_EVENT_LIVESEARCH_WAIT', 'Wait, sending request...');
break;
}
class serendipity_event_livesearch extends serendipity_event
{
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_EVENT_LIVESEARCH_NAME);
$propbag->add('description', PLUGIN_EVENT_LIVESEARCH_DESC);
$propbag->add('event_hooks', array(
'external_plugin' => true,
'frontend_pagebottom' => true,
'css' => true
));
}
function generate_content(&$title) {
$title = PLUGIN_EVENT_LIVESEARCH_NAME;
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'css':
if (stristr('.serendipity_livesearch_row', $addData)) {
// class exists in CSS, so a user has customized it and we don't need default
return true;
}
?>
#LSResult {
position: absolute;
margin-left: 4px;
margin-top: 4px;
}
#LSShadow {
position: relative;
bottom: 1px;
right: 1px;
color: inherit;
border-right: 2px solid #ccc;
border-bottom: 2px solid #ccc;
}
#LSHighlight {
background-color: lightgreen;
color: black;
}
.serendipity_livesearch_row {
margin: 0px;
line-height: 1.2em;
padding-top: 0.2em;
padding-bottom: 0.2em;
text-indent: -1em;
padding-left: 1em;
line-height: 1.2em;
padding-right: 1em;
}
.serendipity_livesearch_result {
position: relative;
bottom: 2px;
right: 2px;
border: 1px solid black;
padding: 2px;
}
<?php
break;
case 'frontend_pagebottom':
echo '<script type="text/javascript">
lsbase = "' . $serendipity['baseURL'] . 'plugin/ls";
waittext = "' . PLUGIN_EVENT_LIVESEARCH_WAIT . '";
</script>';
echo '<script type="text/javascript" src="' . $serendipity['baseURL'] . 'plugins/serendipity_event_livesearch/serendipity_event_livesearch.js"></script>';
break;
case 'external_plugin':
$parts = explode('_', preg_replace('@^([^\?]+).*$@', '\1', $eventData));
if (!empty($parts[1])) {
$param = (int) $parts[1];
} else {
$param = null;
}
switch($parts[0]) {
case 'ls':
$res = serendipity_searchEntries($_REQUEST['s']);
if (is_array($res) && count($res) > 0) {
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo '<div class="serendipity_livesearch_result">';
foreach($res AS $id => $entry) {
echo '<div class="serendipity_livesearch_row"><a href="' . serendipity_archiveURL($entry['id'], $entry['title'], 'baseURL', false) . '">' . htmlspecialchars($entry['title']) . '</a></div>';
}
echo '</div>';
}
break;
}
return true;
break;
default:
return false;
break;
}
} else {
return false;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */
?>
|