r91793 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91792‎ | r91793 | r91794 >
Date:17:32, 9 July 2011
Author:devayon
Status:deferred (Comments)
Tags:
Comment:
added form boxes
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -12,7 +12,85 @@
1313 */
1414 abstract class SMWQueryUI extends SpecialPage {
1515 protected $m_ui_helper;
 16+ private $autocompleteenabled=false;
1617
 18+ protected function addAutocompletionJavascriptAndCSS(){
 19+ global $wgOut, $smwgScriptPath, $smwgJQueryIncluded, $smwgJQueryUIIncluded;
 20+ if($this->autocompleteenabled==false){
 21+ $wgOut->addExtensionStyle( "$smwgScriptPath/skins/jquery-ui/base/jquery.ui.all.css" );
 22+
 23+ $scripts = array();
 24+
 25+ if ( !$smwgJQueryIncluded ) {
 26+ $realFunction = array( 'OutputPage', 'includeJQuery' );
 27+ if ( is_callable( $realFunction ) ) {
 28+ $wgOut->includeJQuery();
 29+ } else {
 30+ $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
 31+ }
 32+
 33+ $smwgJQueryIncluded = true;
 34+ }
 35+
 36+ if ( !$smwgJQueryUIIncluded ) {
 37+ $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.core.min.js";
 38+ $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.widget.min.js";
 39+ $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.position.min.js";
 40+ $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.autocomplete.min.js";
 41+ $smwgJQueryUIIncluded = true;
 42+ }
 43+
 44+ foreach ( $scripts as $js ) {
 45+ $wgOut->addScriptFile( $js );
 46+ }
 47+ $javascript_autocomplete_text = <<<END
 48+<script type="text/javascript">
 49+function split(val) {
 50+ return val.split('\\n');
 51+}
 52+function extractLast(term) {
 53+ return split(term).pop();
 54+}
 55+function escapeQuestion(term){
 56+ if (term.substring(0, 1) == "?") {
 57+ return term.substring(1);
 58+ } else {
 59+ return term;
 60+ }
 61+}
 62+
 63+jQuery.noConflict();
 64+/* extending jQuery functions for custom highligting */
 65+jQuery.ui.autocomplete.prototype._renderItem = function( ul, item) {
 66+ var term_without_q = escapeQuestion(extractLast(this.term));
 67+ var re = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term_without_q.replace("/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi", "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
 68+ var loc = item.label.search(re);
 69+ if (loc >= 0) {
 70+ var t = item.label.substr(0, loc) + '<strong>' + item.label.substr(loc, term_without_q.length) + '</strong>' + item.label.substr(loc + term_without_q.length);
 71+ } else {
 72+ var t = item.label;
 73+ }
 74+ jQuery( "<li></li>" )
 75+ .data( "item.autocomplete", item )
 76+ .append( " <a>" + t + "</a>" )
 77+ .appendTo( ul );
 78+};
 79+
 80+///* extending jquery functions for custom autocomplete matching */
 81+jQuery.extend( jQuery.ui.autocomplete, {
 82+ filter: function(array, term) {
 83+ var matcher = new RegExp("\\\b" + jQuery.ui.autocomplete.escapeRegex(term), "i" );
 84+ return jQuery.grep( array, function(value) {
 85+ return matcher.test( value.label || value.value || value );
 86+ });
 87+ }
 88+});
 89+END;
 90+
 91+ $wgOut->addScript( $javascript_autocomplete_text );
 92+ $this->autocompleteenabled=true;
 93+ }
 94+ }
1795 protected function makeResults($p){
1896 /*
1997 * TODO: extract parameters from $p and decide:
@@ -161,16 +239,82 @@
162240 */
163241
164242 //$result="";
165 - //$result.= getQueryFormBox($content);
 243+ //$result.= getQueryFormBox($contents, $errors);
166244 //$result.= getPOFormBox($content, $enableAutoComplete);
167245 //$result.= getParamBox($content); //avoid ajax, load form elements in the UI by default
168246 $result="<br>Stub: The Form elements come here<br><br>";
169247 return $result;
170248 }
 249+ protected function getQueryFormBox($contents, $errors=""){
 250+ $result="";
 251+ $result= Html::element('textarea', array('name'=>'q', 'id'=>'querybox', 'rows' => '6'), $contents);
 252+ //TODO:enable/disable on checking for errors; perhaps show error messages right below the box
 253+ return $result;
 254+ }
171255
172256 /**
 257+ * A method which generates the form box for PrintOuts.
 258+ * UIs may overload this to change the form parameter or the html elements.
 259+ *
 260+ *
 261+ * @global OutputPage $wgOut
 262+ * @param string $content The content expected to appear in the box
 263+ * @param boolean $enableAutocomplete If set to true, adds the relevant JS and CSS to the page
 264+ * @return string The HTML code
 265+ */
 266+ protected function getPOFormBox($content, $enableAutocomplete=true){
 267+ if($enableAutocomplete){
 268+ global $wgOut;
 269+
 270+ if(!$this->autocompleteenabled) addAutocompletionJavascriptAndCSS();
 271+ $javascript_autocomplete_text = <<<END
 272+<script type="text/javascript">
 273+jQuery(document).ready(function(){
 274+ jQuery("#add_property").autocomplete({
 275+ minLength: 2,
 276+ source: function(request, response) {
 277+ request.term=request.term.substr(request.term.lastIndexOf("\\n")+1);
 278+ url=wgScriptPath+'/api.php?action=opensearch&limit=10&namespace='+wgNamespaceIds['property']+'&format=jsonfm&search=';
 279+
 280+ jQuery.getJSON(url+request.term, function(data){
 281+ //remove the namespace prefix 'Property:' from returned data and add prefix '?'
 282+ for(i=0;i<data[1].length;i++) data[1][i]="?"+data[1][i].substr(data[1][i].indexOf(':')+1);
 283+ response(jQuery.ui.autocomplete.filter(data[1], escapeQuestion(extractLast(request.term))));
 284+ });
 285+ },
 286+ focus: function() {
 287+ // prevent value inserted on focus
 288+ return false;
 289+ },
 290+ select: function(event, ui) {
 291+ var terms = split( this.value );
 292+ // remove the current input
 293+ terms.pop();
 294+ // add the selected item
 295+ terms.push( ui.item.value );
 296+ // add placeholder to get the comma-and-space at the end
 297+ terms.push("");
 298+ this.value = terms.join("\\n");
 299+ return false;
 300+ }
 301+ });
 302+});
 303+</script>
 304+END;
 305+
 306+ $wgOut->addScript( $javascript_autocomplete_text );
 307+
 308+ }
 309+ $result="";
 310+ $result=Html::element('textarea',array('id'=> 'add_property', 'name'=> 'po', 'cols'=>'20', 'rows'=> '6'),$content);
 311+ return $result;
 312+ }
 313+
 314+ /**
173315 * A method which generates the url parameters based on passed parameters.
174 - * UI implementations need to overload this if they use different form parameters
 316+ * UI implementations need to overload this if they use different form parameters.
 317+ *
 318+ * @return string An url-encoded string.
175319 */
176320 protected function getUrlTail() {
177321 $urltail = '&q=' . urlencode( $this->m_ui_helper->getQuerystring() );

Follow-up revisions

RevisionCommit summaryAuthorDate
r91836added documentation, follow up to r91793devayon16:00, 10 July 2011

Comments

#Comment by Devayon (talk | contribs)   17:51, 9 July 2011

a follow-up to r91789.