Inyono has uploaded a new change for review. https://gerrit.wikimedia.org/r/274109
Change subject: Create `TableResultBrower` and remove `getResultAsTable` ...................................................................... Create `TableResultBrower` and remove `getResultAsTable` Change-Id: I193b06dea6d0c53ef42164425430d80dd18066af --- M index.html M wikibase/queryService/api/Sparql.js M wikibase/queryService/ui/App.js M wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js M wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js A wikibase/queryService/ui/resultBrowser/TableResultBrowser.js 6 files changed, 132 insertions(+), 94 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/gui refs/changes/09/274109/1 diff --git a/index.html b/index.html index c35cccf..8326cc8 100644 --- a/index.html +++ b/index.html @@ -195,6 +195,7 @@ <script src="wikibase/queryService/ui/QueryExampleDialog.js"></script> <script src="wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js"></script> <script src="wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js"></script> + <script src="wikibase/queryService/ui/resultBrowser/TableResultBrowser.js"></script> <script src="wikibase/queryService/api/Sparql.js"></script> <script src="wikibase/queryService/api/QuerySamples.js"></script> diff --git a/wikibase/queryService/api/Sparql.js b/wikibase/queryService/api/Sparql.js index 7e70971..0d19d46 100644 --- a/wikibase/queryService/api/Sparql.js +++ b/wikibase/queryService/api/Sparql.js @@ -157,25 +157,6 @@ }; /** - * Produce abbreviation of the URI. - * - * @param {string} uri - * @returns {string} - */ - SELF.prototype.abbreviateUri = function( uri ) { - var nsGroup, ns, NAMESPACE_SHORTCUTS = wikibase.queryService.RdfNamespaces.NAMESPACE_SHORTCUTS; - - for ( nsGroup in NAMESPACE_SHORTCUTS ) { - for ( ns in NAMESPACE_SHORTCUTS[nsGroup] ) { - if ( uri.indexOf( NAMESPACE_SHORTCUTS[nsGroup][ns] ) === 0 ) { - return uri.replace( NAMESPACE_SHORTCUTS[nsGroup][ns], ns + ':' ); - } - } - } - return '<' + uri + '>'; - }; - - /** * Get execution time in seconds of the submitted query * * @return {int} @@ -209,75 +190,6 @@ */ SELF.prototype.getQueryUri = function() { return this._queryUri; - }; - - /** - * Get the result as table - * - * @return {jQuery} - */ - SELF.prototype.getResultAsTable = function() { - var data = this._rawData, thead, tr, td, binding, i, - table = $( '<table>' ).attr( 'class', 'table' ); - - if ( typeof data.boolean !== 'undefined' ) { - // ASK query - table.append( '<tr><td>' + data.boolean + '</td></tr>' ).addClass( 'boolean' ); - return; - } - - thead = $( '<thead>' ).appendTo( table ); - tr = $( '<tr>' ); - for ( i = 0; i < data.head.vars.length; i++ ) { - tr.append( '<th>' + data.head.vars[i] + '</th>' ); - } - thead.append( tr ); - table.append( thead ); - - for (i = 0; i < this._resultLength; i++ ) { - tr = $( '<tr>' ) ; - for ( var j = 0; j < data.head.vars.length; j++ ) { - td = $( '<td>' ) ; - if ( data.head.vars[j] in data.results.bindings[i] ) { - binding = data.results.bindings[i][data.head.vars[j]]; - var text = binding.value; - if ( binding.type === 'uri' ) { - text = this.abbreviateUri( text ); - } - var linkText = $( '<pre>' ).text( text.trim() ); - if ( binding.type === 'typed-literal' ) { - td.attr( { - 'class': 'literal', - 'data-datatype': binding.datatype - } ).append( linkText ); - } else { - td.attr( 'class', binding.type ); - if ( binding.type === 'uri' ) { - td.append( $( '<a>' ) - .attr( 'href', binding.value ) - .append( linkText ) - ); - } else { - td.append( linkText ); - } - - if ( binding['xml:lang'] ) { - td.attr( { - 'data-lang': binding['xml:lang'], - title: binding.value + '@' + binding['xml:lang'] - } ); - } - } - } else { - // no binding - td.attr( 'class', 'unbound' ); - } - tr.append( td ); - } - table.append( tr ); - } - - return table; }; /** diff --git a/wikibase/queryService/ui/App.js b/wikibase/queryService/ui/App.js index 2c8f7bd..5b8d29a 100644 --- a/wikibase/queryService/ui/App.js +++ b/wikibase/queryService/ui/App.js @@ -64,7 +64,7 @@ SELF.prototype._editor = null; /** - * @property {bool} + * @property {boolean} * @private **/ SELF.prototype._autoExecuteQuery = false; @@ -359,7 +359,13 @@ $( '#total-results' ).text( api.getResultLength() ); $( '#query-time' ).text( api.getExecutionTime() ); $( '.query-total' ).show(); - $( '#query-result' ).html( api.getResultAsTable() ).show(); + + var $queryResult = $( '#query-result' ); + var tableBrowser = new wikibase.queryService.ui.resultBrowser.TableResultBrowser(); + tableBrowser.setResult( api.getResultRawData() ); + tableBrowser.draw( $queryResult ); + $queryResult.show(); + $( '.actionMessage' ).hide(); $( '#query-error' ).hide(); diff --git a/wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js b/wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js index 0b1aee3..a49bacf 100644 --- a/wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js +++ b/wikibase/queryService/ui/resultBrowser/AbstractResultBrowser.js @@ -35,7 +35,7 @@ /** * Checks whether the result browser can draw the given result - * @return {bool} + * @return {boolean} **/ SELF.prototype.isDrawable = function() { jQuery.error( 'Method isDrawable() needs to be implemented!' ); diff --git a/wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js b/wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js index d9ef097..bb3a015 100644 --- a/wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js +++ b/wikibase/queryService/ui/resultBrowser/ImageResultBrowser.js @@ -18,8 +18,6 @@ * * @author Jonas Kress * @constructor - * - * @param {Object} result set */ function SELF() { } @@ -59,7 +57,7 @@ /** * Checks whether the browser can draw the given result - * @return {bool} + * @return {boolean} **/ SELF.prototype.isDrawable = function() { diff --git a/wikibase/queryService/ui/resultBrowser/TableResultBrowser.js b/wikibase/queryService/ui/resultBrowser/TableResultBrowser.js new file mode 100644 index 0000000..06cada9 --- /dev/null +++ b/wikibase/queryService/ui/resultBrowser/TableResultBrowser.js @@ -0,0 +1,121 @@ +var wikibase = wikibase || {}; +wikibase.queryService = wikibase.queryService || {}; +wikibase.queryService.ui = wikibase.queryService.ui || {}; +wikibase.queryService.ui.resultBrowser = wikibase.queryService.ui.resultBrowser || {}; +window.mediaWiki = window.mediaWiki || {}; + +wikibase.queryService.ui.resultBrowser.TableResultBrowser = ( function( $ ) { + "use strict"; + + /** + * A result browser for tables + * + * @class wikibase.queryService.ui.resultBrowser.TableResultBrowser + * @licence GNU GPL v2+ + * + * @author Jonas Kress + * @constructor + */ + function SELF() { + } + + SELF.prototype = new wikibase.queryService.ui.resultBrowser.AbstractResultBrowser(); + + /** + * Draw browser to the given element + * @param {jQuery} $element to draw at + **/ + SELF.prototype.draw = function( $element ) { + var data = this._result, thead, tr, td, binding, i, + table = $( '<table>' ).attr( 'class', 'table' ); + + if ( typeof data.boolean !== 'undefined' ) { + // ASK query + table.append( '<tr><td>' + data.boolean + '</td></tr>' ).addClass( 'boolean' ); + return; + } + + thead = $( '<thead>' ).appendTo( table ); + tr = $( '<tr>' ); + for ( i = 0; i < data.head.vars.length; i++ ) { + tr.append( '<th>' + data.head.vars[i] + '</th>' ); + } + thead.append( tr ); + table.append( thead ); + + for (i = 0; i < data.results.bindings.length; i++ ) { + tr = $( '<tr>' ) ; + for ( var j = 0; j < data.head.vars.length; j++ ) { + td = $( '<td>' ) ; + if ( data.head.vars[j] in data.results.bindings[i] ) { + binding = data.results.bindings[i][data.head.vars[j]]; + var text = binding.value; + if ( binding.type === 'uri' ) { + text = this.abbreviateUri( text ); + } + var linkText = $( '<pre>' ).text( text.trim() ); + if ( binding.type === 'typed-literal' ) { + td.attr( { + 'class': 'literal', + 'data-datatype': binding.datatype + } ).append( linkText ); + } else { + td.attr( 'class', binding.type ); + if ( binding.type === 'uri' ) { + td.append( $( '<a>' ) + .attr( 'href', binding.value ) + .append( linkText ) + ); + } else { + td.append( linkText ); + } + + if ( binding['xml:lang'] ) { + td.attr( { + 'data-lang': binding['xml:lang'], + title: binding.value + '@' + binding['xml:lang'] + } ); + } + } + } else { + // no binding + td.attr( 'class', 'unbound' ); + } + tr.append( td ); + } + table.append( tr ); + } + + $( $element ).html( table ); + }; + + /** + * Checks whether the browser can draw the given result + * @return {boolean} + **/ + SELF.prototype.isDrawable = function() { + return true; + }; + + /** + * Produce abbreviation of the URI. + * + * @private + * @param {string} uri + * @returns {string} + */ + SELF.prototype.abbreviateUri = function( uri ) { + var nsGroup, ns, NAMESPACE_SHORTCUTS = wikibase.queryService.RdfNamespaces.NAMESPACE_SHORTCUTS; + + for ( nsGroup in NAMESPACE_SHORTCUTS ) { + for ( ns in NAMESPACE_SHORTCUTS[nsGroup] ) { + if ( uri.indexOf( NAMESPACE_SHORTCUTS[nsGroup][ns] ) === 0 ) { + return uri.replace( NAMESPACE_SHORTCUTS[nsGroup][ns], ns + ':' ); + } + } + } + return '<' + uri + '>'; + }; + + return SELF; +}( jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/274109 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I193b06dea6d0c53ef42164425430d80dd18066af Gerrit-PatchSet: 1 Gerrit-Project: wikidata/query/gui Gerrit-Branch: master Gerrit-Owner: Inyono <jo...@keinholz.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits