1 (function( $, app, i18n ) {
4 var data = app.ns("data");
6 var StructuredQuery = ui.AbstractWidget.extend({
8 cluster: null // (required) instanceof app.services.Cluster
10 _baseCls: "uiStructuredQuery",
11 init: function(parent) {
13 this.selector = new ui.IndexSelector({
14 onIndexChanged: this._indexChanged_handler,
15 cluster: this.config.cluster
17 this.el = $(this._main_template());
18 this.out = this.el.find("DIV.uiStructuredQuery-out");
19 this.attach( parent );
22 _indexChanged_handler: function( index ) {
23 this.filter && this.filter.remove();
24 this.filter = new ui.FilterBrowser({
25 cluster: this.config.cluster,
27 onStartingSearch: function() { this.el.find("DIV.uiStructuredQuery-out").text( i18n.text("General.Searching") ); this.el.find("DIV.uiStructuredQuery-src").hide(); }.bind(this),
28 onSearchSource: this._searchSource_handler,
29 onResults: this._results_handler
31 this.el.find(".uiStructuredQuery-body").append(this.filter);
34 _results_handler: function( filter, event ) {
36 "json": this._jsonResults_handler,
37 "table": this._tableResults_handler,
38 "csv": this._csvResults_handler
40 typeMap[ event.type ].call( this, event.data, event.metadata );
42 _jsonResults_handler: function( results ) {
43 this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.JsonPretty({ obj: results }));
45 _csvResults_handler: function( results ) {
46 this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.CSVTable({ results: results }));
48 _tableResults_handler: function( results, metadata ) {
49 // hack up a QueryDataSourceInterface so that StructuredQuery keeps working without using a Query object
50 var qdi = new data.QueryDataSourceInterface({ metadata: metadata, query: new data.Query() });
51 var tab = new ui.Table( {
54 width: this.out.innerWidth()
55 } ).attach(this.out.empty());
56 qdi._results_handler(qdi.config.query, results);
59 _showRawJSON : function() {
60 if($("#rawJsonText").length === 0) {
61 var hiddenButton = $("#showRawJSON");
62 var jsonText = $({tag: "P", type: "p", id: "rawJsonText"});
63 jsonText.text(hiddenButton[0].value);
64 hiddenButton.parent().append(jsonText);
68 _searchSource_handler: function(src) {
69 var searchSourceDiv = this.el.find("DIV.uiStructuredQuery-src");
70 searchSourceDiv.empty().append(new app.ui.JsonPretty({ obj: src }));
71 if(typeof JSON !== "undefined") {
72 var showRawJSON = $({ tag: "BUTTON", type: "button", text: i18n.text("StructuredQuery.ShowRawJson"), id: "showRawJSON", value: JSON.stringify(src), onclick: this._showRawJSON });
73 searchSourceDiv.append(showRawJSON);
75 searchSourceDiv.show();
78 _main_template: function() {
79 return { tag: "DIV", cls: this._baseCls, children: [
81 { tag: "DIV", cls: "uiStructuredQuery-body" },
82 { tag: "DIV", cls: "uiStructuredQuery-src", css: { display: "none" } },
83 { tag: "DIV", cls: "uiStructuredQuery-out" }
88 ui.StructuredQuery = ui.Page.extend({
90 this.q = new StructuredQuery( this.config );
95 })( this.jQuery, this.app, this.i18n );