Skip to content

Commit

Permalink
Added Typeahead support for quick search using "qsearch" DokuWiki AJA…
Browse files Browse the repository at this point in the history
…X service and fixed action items functions
  • Loading branch information
giterlizzi committed Jan 15, 2018
1 parent 940a755 commit 99899af
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 91 deletions.
1 change: 1 addition & 0 deletions assets/typeahead/bootstrap3-typeahead.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions css/template.less
Expand Up @@ -129,9 +129,11 @@ header {
outline: 0;
}

#qsearch__out {
overflow-y: auto;
max-height: 400px;
/* Typeahead plugin */
.typeahead a.dropdown-item {
font-size: 85%;
text-overflow:ellipsis;
overflow:hidden
}

}
Expand Down
49 changes: 49 additions & 0 deletions script.js
Expand Up @@ -32,6 +32,55 @@ jQuery(document).ready(function() {
jQuery(document).trigger('bootstrap3:toc-resize');
});


// Add typeahead support for quick seach
jQuery("#qsearch__in").typeahead({

source: function(query, process) {

return jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', {
call: 'qsearch',
q: encodeURI(query)
},

function(data) {

var results = [];

jQuery(data).find('a').each(function(){

var page = jQuery(this);

results.push({
name : page.text(),
href : page.attr('href'),
title : page.attr('title'),
});

});

return process(results);

});
},

itemLink: function (item) {
return item.href;
},

itemTitle: function (item) {
return item.title;
},

followLinkOnSelect : true,
autoSelect : false,
items : 50,
fitToElement : true,
delay : 500,

});


// Replace ALL input[type=submit|reset|button] (with no events) to button[type=submit|reset|button] for CSS styling
jQuery.fn.extend({

Expand Down
183 changes: 95 additions & 88 deletions tpl_functions.php
Expand Up @@ -13,83 +13,6 @@
include_once(dirname(__FILE__) . '/inc/simple_html_dom.php');


function bootstrap3_action($type, $icon = '', $wrapper = false, $return = false) {

global $ACT;
global $ID;

$output = '';

$custom_actions = array('purge', 'discussion');

if (in_array($type, $custom_actions)) {

if ($wrapper) $output .= "<$wrapper>";

if ($type == 'purge') {

$link = wl($ID, array('purge' => 'true'));
$title = tpl_getLang('purge_cache_page');

$output .= sprintf('<a href="%s" class="action %s%s" title="%s">%s%s</a>',
$link,
$type,
(($ACT == $type) ? ' active': ''),
$title,
(($icon) ? "<i class='$icon'></i> ": ''),
$title);

}

if ($type == 'discussion') {

$discuss_page = str_replace('@ID@', $ID, tpl_getConf('discussionPage'));
$discuss_page_raw = str_replace('@ID@', '', tpl_getConf('discussionPage'));
$is_discussPage = strpos($ID, $discuss_page_raw) !== false;
$back_id = ':'.str_replace($discuss_page_raw, '', $ID);

if ($is_discussPage) {

$link = html_wikilink($back_id, tpl_getLang('back_to_article'));
$link = str_replace('title="', 'title="' . tpl_getLang('back_to_article') . ': ', $link);

} else {

$link = html_wikilink($discuss_page, tpl_getLang('discussion'));
$link = str_replace('title="', 'title="' . tpl_getLang('discussion') . ': ', $link);

}

$output .= str_replace(array('class="', 'wikilink1', 'wikilink2'),
array('class="action discussion ', '', ''), $link);

if ($icon) {
$output = preg_replace('/(<a (.*?)>)/m', '$1<i class="'.$icon.'"></i> ', $output);
}

}

if ($wrapper) $output .= "</$wrapper>";

} else {

$inner = '';

if ($icon) $inner = '<i class="'. $icon . '"></i>';

$output .= tpl_actionlink($type, '', '', $inner, true, $wrapper);

if ($type == $ACT) $output = str_replace('class="action ', 'class="action active ', $output);

}

if ($return) return $output;
echo $output;

}



/**
* copied from core (available since Detritus)
*/
Expand Down Expand Up @@ -139,6 +62,7 @@ function tpl_classes() {

}


/**
* copied from core (available since Detritus)
*/
Expand Down Expand Up @@ -172,6 +96,91 @@ function plugin_getRequestAdminPlugin(){
}


/**
* Create link for DokuWiki actions
*
* @param string $type action
* @param string $icon class
* @param boolean|string $wrapper
* @param boolean $return
*/
function bootstrap3_action($type, $icon = '', $wrapper = false, $return = false) {

global $ACT;
global $ID;
global $lang;

$output = '';

$custom_actions = array('purge', 'discussion');

if (in_array($type, $custom_actions)) {

if ($wrapper) $output .= "<$wrapper>";

if ($type == 'purge') {

$link = wl($ID, array('purge' => 'true'));
$title = tpl_getLang('purge_cache_page');

$output .= sprintf('<a href="%s" class="action %s%s" title="%s">%s%s</a>',
$link,
$type,
(($ACT == $type) ? ' active': ''),
$title,
(($icon) ? "<i class='$icon'></i> ": ''),
$title);

}

if ($type == 'discussion') {

$discuss_page = str_replace('@ID@', $ID, tpl_getConf('discussionPage'));
$discuss_page_raw = str_replace('@ID@', '', tpl_getConf('discussionPage'));
$is_discussPage = strpos($ID, $discuss_page_raw) !== false;
$back_id = ':'.str_replace($discuss_page_raw, '', $ID);

if ($is_discussPage) {

$link = html_wikilink($back_id, tpl_getLang('back_to_article'));
$link = str_replace('title="', 'title="' . tpl_getLang('back_to_article') . ': ', $link);

} else {

$link = html_wikilink($discuss_page, tpl_getLang('discussion'));
$link = str_replace('title="', 'title="' . tpl_getLang('discussion') . ': ', $link);

}

$output .= str_replace(array('class="', 'wikilink1', 'wikilink2'),
array('class="action discussion ', '', ''), $link);

if ($icon) {
$output = preg_replace('/(<a (.*?)>)/m', '$1<i class="'.$icon.'"></i> ', $output);
}

}

if ($wrapper) $output .= "</$wrapper>";

} else {

$inner = $lang['btn_' . $type];

if ($icon) $inner = '<i class="'. $icon . '"></i> ' . $inner;

$output .= tpl_actionlink($type, '', '', $inner, true, $wrapper);

if ($type == $ACT) $output = str_replace('class="action ', 'class="action active ', $output);

}

if ($return) return $output;
echo $output;

}


/**
* Create event for tools menus
*
Expand Down Expand Up @@ -323,8 +332,6 @@ function bootstrap3_sidebar_include($type) {
function bootstrap3_action_item($action, $icon = null, $return = false) {

global $ACT;
global $ID;


if ($action == 'purge') {

Expand Down Expand Up @@ -639,7 +646,7 @@ function bootstrap3_dropdown_page($page) {
* @param bool $autocomplete
* @return bool
*/
function bootstrap3_searchform($ajax = true, $autocomplete = true) {
function bootstrap3_searchform() {

global $lang;
global $ACT;
Expand All @@ -654,14 +661,11 @@ function bootstrap3_searchform($ajax = true, $autocomplete = true) {

print '<input ';
if ($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
if (!$autocomplete) print 'autocomplete="off" ';
print 'id="qsearch__in" type="search" placeholder="'.$lang['btn_search'].'" accesskey="f" name="id" class="form-control" title="[F]" />';
print 'id="qsearch__in" autocomplete="off" type="search" placeholder="'.$lang['btn_search'].'" accesskey="f" name="id" class="form-control" title="[F]" />';

print '<button type="submit" title="'.$lang['btn_search'].'"><i class="fa fa-fw fa-search"></i></button>';

print '<input type="hidden" name="do" value="search" />';

if ($ajax) print '<div id="qsearch__out" class="panel panel-default ajax_qsearch JSpopup"></div>';
print '</div></form>';

return true;
Expand Down Expand Up @@ -829,11 +833,11 @@ function bootstrap3_tools_menu($add_icons = true) {
*/
function bootstrap3_toolbar() {

$tools = _tpl_tools();
$tools = bootstrap3_tools();

foreach ($tools as $id => $menu) {
foreach ($menu['items'] as $action => $item) {
$tools[$id]['menu'][$action] = str_replace('class="action ', 'class="action btn btn-default ', bootstrap3_action_item($action, $item['icon'], 1));
foreach ($tools as $tool => $data) {
foreach($data['menu'] as $action => $item) {
$tools[$tool]['menu'][$action] = str_replace(array('class="action ', '<li>', '</li>'), array('class="action btn btn-default ', '', ''), $item);
}
}

Expand Down Expand Up @@ -1670,6 +1674,9 @@ function bootstrap3_metaheaders(Doku_Event &$event, $param) {
'type' => 'text/javascript',
'src' => tpl_basedir() . 'assets/anchorjs/anchor.min.js');

$event->data['script'][] = array(
'type' => 'text/javascript',
'src' => tpl_basedir() . 'assets/typeahead/bootstrap3-typeahead.min.js');

// Apply some FIX
if ($ACT || defined('DOKU_MEDIADETAIL')) {
Expand Down

0 comments on commit 99899af

Please sign in to comment.