Jonas Kress (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250670

Change subject: Show Wikidata ID hint in SPARQL query editor
......................................................................

Show Wikidata ID hint in SPARQL query editor

Shows tooltip when hovering over wikibase entity

Bug: T114740
Change-Id: Icfda334a52973921ddb093c57962703b88bcc569
---
A gui/addon/tooltip/WikibaseRDFTooltip.js
M gui/gui.js
M gui/index.html
3 files changed, 145 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/rdf 
refs/changes/70/250670/1

diff --git a/gui/addon/tooltip/WikibaseRDFTooltip.js 
b/gui/addon/tooltip/WikibaseRDFTooltip.js
new file mode 100644
index 0000000..f389808
--- /dev/null
+++ b/gui/addon/tooltip/WikibaseRDFTooltip.js
@@ -0,0 +1,140 @@
+var WikibaseRDFTooltip = ( function( CodeMirror, $ ) {
+       "use strict";
+
+
+       /**
+        * Wikibase RDF tooltip for codemirror editor
+        *
+        * @class WikibaseRdfTooltip
+        * @licence GNU GPL v2+
+        * @author Jonas Kress
+        * @constructor
+        */
+       function SELF( editor ) {
+
+               this.editor = editor;
+               this._registerHandler();
+       }
+
+       SELF.prototype.editor = null;
+       SELF.prototype.tooltipTimeoutHandler = null;
+
+
+       var ENTITY_TYPES = { "http://www.wikidata.org/prop/direct/": "property",
+                       "http://www.wikidata.org/prop/": "property",
+                       "http://www.wikidata.org/prop/novalue/": "property",
+                       "http://www.wikidata.org/prop/statement/": "property",
+                       "http://www.wikidata.org/prop/statement/value/": 
"property",
+                       "http://www.wikidata.org/prop/qualifier/": "property",
+                       "http://www.wikidata.org/prop/qualifier/value/": 
"property",
+                       "http://www.wikidata.org/prop/reference/": "property",
+                       "http://www.wikidata.org/prop/reference/value/": 
"property",
+                       "http://www.wikidata.org/wiki/Special:EntityData/": 
"item",
+                       "http://www.wikidata.org/entity/": "item" };
+
+       var ENTITY_SEARCH_API_ENDPOINT = 
"https://www.wikidata.org/w/api.php?action=wbsearchentities&search={term}&format=json&language=en&uselang=en&type={entityType}&continue=0";;
+
+
+       SELF.prototype._registerHandler = function(){
+               CodeMirror.on(this.editor.getWrapperElement(), "mouseover", 
$.proxy(this._triggerTooltip, this));
+       };
+
+       SELF.prototype._triggerTooltip = function( e ){
+
+               clearTimeout( this.tooltipTimeoutHandler );
+               this._removeToolTip();
+
+               var self = this;
+               this.tooltipTimeoutHandler = setTimeout(function(){
+                       self._createTooltip( e );
+               }, 500);
+       };
+
+
+       SELF.prototype._createTooltip = function( e ){
+
+               var posX = e.clientX, posY = (e.clientY +  
$(window).scrollTop());
+
+               var token = 
this.editor.getTokenAt(this.editor.coordsChar({left: posX, top: posY})).string;
+               console.log(token);
+
+               if(!token.match(/.+\:(Q|P)[0-9]*/)){
+                       return;
+               }
+
+               var prefixes = this._extractPrefixes( 
this.editor.doc.getValue() );
+               var prefix = token.split( ":" ).shift();
+               var entityId = token.split( ":" ).pop();
+
+               if (! prefixes[ prefix ] ) {
+                       return;
+               }
+
+               var self = this;
+               this._searchEntities( entityId, prefixes[ prefix ] 
).done(function( list ){
+                       self._showToolTip( list.shift(), {x:posX, y:posY} );
+               });
+       };
+
+       SELF.prototype._removeToolTip = function(){
+               $(".wikibaseRDFtoolTip").remove();
+       };
+
+       SELF.prototype._showToolTip = function( text, pos ){
+               if( !text || !pos ){
+                       return;
+               }
+               $("<div/>")
+               .text(text)
+               .css( "position", "absolute" )
+               .css( "background-color", "white" )
+               .css( "z-index", "100" )
+               .css( "border", "1px solid grey" )
+               .css( "max-width", "200px" )
+               .css( "padding", "5px" )
+               .css( {top: pos.y + 2, left: pos.x + 2} )
+               .addClass( "wikibaseRDFtoolTip" )
+               .appendTo( "body" )
+               .fadeIn( "slow" );
+       };
+
+
+       SELF.prototype._extractPrefixes = function( text ) {
+               var prefixes = {},
+                       lines = text.split( "\n" ),
+                       matches;
+
+               $.each( lines, function ( index, line ) {
+                       // PREFIX wd: <http://www.wikidata.org/entity/>
+                       if ( matches = line.match( /(PREFIX) (\S+): <([^>]+)>/ 
) ) {
+                               if ( ENTITY_TYPES[ matches[ 3 ] ] ) {
+                                       prefixes[ matches[ 2 ] ] = 
ENTITY_TYPES[ matches[ 3 ] ];
+                               }
+                       }
+               } );
+
+               return prefixes;
+       };
+
+       SELF.prototype._searchEntities = function( term, type ) {
+               var entityList = [],
+                       deferred = $.Deferred();
+
+               $.ajax( {
+                       url: ENTITY_SEARCH_API_ENDPOINT.replace( "{term}", term 
).replace( "{entityType}", type ),
+                       dataType: "jsonp"
+               } ).done( function ( data ) {
+
+                       $.each( data.search, function ( key, value ) {
+                               entityList.push( value.label + " (" + value.id 
+ ")\n" + value.description );
+                       } );
+
+                       deferred.resolve( entityList );
+               } );
+
+               return deferred.promise();
+       };
+
+       return SELF;
+
+}( CodeMirror, jQuery ) );
diff --git a/gui/gui.js b/gui/gui.js
index 9a2716f..64b1354 100644
--- a/gui/gui.js
+++ b/gui/gui.js
@@ -321,6 +321,8 @@
                });
                EDITOR.addKeyMap({'Ctrl-Enter': submitQuery});
                EDITOR.focus();
+
+               new WikibaseRDFTooltip(EDITOR);
        }
 
        /**
diff --git a/gui/index.html b/gui/index.html
index 0e44f52..40df811 100644
--- a/gui/index.html
+++ b/gui/index.html
@@ -4,14 +4,15 @@
 <script src="jquery-1.11.3.js"></script>
 <script src="bootstrap.min.js"></script>
 <script src="codemirror.js"></script>
-<script src="gui.js"></script>
 <script src="sparql.js"></script>
 <script src="addon/hint/show-hint.js"></script>
 <script src="addon/hint/wikibase-sparql-hint.js"></script>
 <script src="addon/hint/wikibase-rdf-hint.js"></script>
+<script src="addon/tooltip/WikibaseRDFTooltip.js"></script>
+<script src="gui.js"></script>
+<script src="vis.js"></script>
 <script src="wdqs.js"></script>
 <script src="wdqs-explorer.js"></script>
-<script src="vis.js"></script>
 <link rel="stylesheet" href="bootstrap.min.css">
 <link rel="stylesheet" href="style.css">
 <link rel="stylesheet" href="codemirror.css">

-- 
To view, visit https://gerrit.wikimedia.org/r/250670
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icfda334a52973921ddb093c57962703b88bcc569
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/rdf
Gerrit-Branch: master
Gerrit-Owner: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to