Index: trunk/extensions/SemanticMediaWiki/specials/SearchTriple/SMW_SpecialSearchByProperty.php |
— | — | @@ -399,14 +399,72 @@ |
400 | 400 | * @return string HTML for the query form |
401 | 401 | */ |
402 | 402 | private function queryForm() { |
| 403 | + self::addAutoComplete(); |
403 | 404 | $spectitle = SpecialPage::getTitleFor( 'SearchByProperty' ); |
404 | 405 | $html = '<form name="searchbyproperty" action="' . $spectitle->escapeLocalURL() . '" method="get">' . "\n" . |
405 | 406 | '<input type="hidden" name="title" value="' . $spectitle->getPrefixedText() . '"/>' ; |
406 | | - $html .= wfMsg( 'smw_sbv_property' ) . ' <input type="text" name="property" value="' . htmlspecialchars( $this->propertystring ) . '" />' . "   \n"; |
| 407 | + $html .= wfMsg( 'smw_sbv_property' ) . ' <input type="text" id="property_box" name="property" value="' . htmlspecialchars( $this->propertystring ) . '" />' . "   \n"; |
407 | 408 | $html .= wfMsg( 'smw_sbv_value' ) . ' <input type="text" name="value" value="' . htmlspecialchars( $this->valuestring ) . '" />' . "\n"; |
408 | 409 | $html .= '<input type="submit" value="' . wfMsg( 'smw_sbv_submit' ) . "\"/>\n</form>\n"; |
409 | 410 | |
410 | 411 | return $html; |
411 | 412 | } |
412 | 413 | |
| 414 | + /** |
| 415 | + * Creates the JS needed for adding auto-completion to queryForm(). Uses the |
| 416 | + * MW API to fetch suggestions. |
| 417 | + * |
| 418 | + */ |
| 419 | + protected static function addAutoComplete() { |
| 420 | + global $wgOut, $smwgScriptPath, $smwgJQueryIncluded, $smwgJQueryUIIncluded; |
| 421 | + |
| 422 | + // Add CSS and JavaScript for jQuery and jQuery UI. |
| 423 | + $wgOut->addExtensionStyle( "$smwgScriptPath/skins/jquery-ui/base/jquery.ui.all.css" ); |
| 424 | + |
| 425 | + $scripts = array(); |
| 426 | + |
| 427 | + if ( !$smwgJQueryIncluded ) { |
| 428 | + $realFunction = array( 'OutputPage', 'includeJQuery' ); |
| 429 | + if ( is_callable( $realFunction ) ) { |
| 430 | + $wgOut->includeJQuery(); |
| 431 | + } else { |
| 432 | + $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js"; |
| 433 | + } |
| 434 | + |
| 435 | + $smwgJQueryIncluded = true; |
| 436 | + } |
| 437 | + |
| 438 | + if ( !$smwgJQueryUIIncluded ) { |
| 439 | + $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.core.min.js"; |
| 440 | + $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.widget.min.js"; |
| 441 | + $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.position.min.js"; |
| 442 | + $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.autocomplete.min.js"; |
| 443 | + $smwgJQueryUIIncluded = true; |
| 444 | + } |
| 445 | + |
| 446 | + foreach ( $scripts as $js ) { |
| 447 | + $wgOut->addScriptFile( $js ); |
| 448 | + } |
| 449 | + |
| 450 | + $javascript_autocomplete_text = <<<END |
| 451 | +<script type="text/javascript"> |
| 452 | +jQuery(document).ready(function(){ |
| 453 | + jQuery("#property_box").autocomplete({ |
| 454 | + minLength: 2, |
| 455 | + source: function(request, response) { |
| 456 | + jQuery.getJSON(wgScriptPath+'/api.php?action=opensearch&limit=10&namespace=102&form&search='+request.term, function(data){ |
| 457 | + //remove the word 'Property:' from returned data |
| 458 | + for(i=0;i<data[1].length;i++) data[1][i]= data[1][i].substr(9); // 9 because "Property:".length==9 |
| 459 | + response(data[1]); |
| 460 | + }); |
| 461 | + } |
| 462 | + }); |
| 463 | +}); |
| 464 | +</script> |
| 465 | + |
| 466 | +END; |
| 467 | + |
| 468 | + $wgOut->addScript( $javascript_autocomplete_text ); |
| 469 | + } |
| 470 | + |
413 | 471 | } |