2 * Copyright 2010-2013 Ben Birch
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this software except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 (function( $, app, i18n ) {
18 var ui = app.ns("ui");
19 var data = app.ns("data");
21 var StructuredQuery = ui.AbstractWidget.extend({
23 cluster: null // (required) instanceof app.services.Cluster
25 _baseCls: "uiStructuredQuery",
26 init: function(parent) {
28 this.selector = new ui.IndexSelector({
29 onIndexChanged: this._indexChanged_handler,
30 cluster: this.config.cluster
32 this.el = $(this._main_template());
33 this.out = this.el.find("DIV.uiStructuredQuery-out");
34 this.attach( parent );
37 _indexChanged_handler: function( index ) {
38 this.filter && this.filter.remove();
39 this.filter = new ui.FilterBrowser({
40 cluster: this.config.cluster,
42 onStartingSearch: function() { this.el.find("DIV.uiStructuredQuery-out").text( i18n.text("General.Searching") ); this.el.find("DIV.uiStructuredQuery-src").hide(); }.bind(this),
43 onSearchSource: this._searchSource_handler,
44 onResults: this._results_handler
46 this.el.find(".uiStructuredQuery-body").append(this.filter);
49 _results_handler: function( filter, event ) {
51 "json": this._jsonResults_handler,
52 "table": this._tableResults_handler,
53 "csv": this._csvResults_handler
55 typeMap[ event.type ].call( this, event.data, event.metadata );
57 _jsonResults_handler: function( results ) {
58 this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.JsonPretty({ obj: results }));
60 _csvResults_handler: function( results ) {
61 this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.CSVTable({ results: results }));
63 _tableResults_handler: function( results, metadata ) {
64 // hack up a QueryDataSourceInterface so that StructuredQuery keeps working without using a Query object
65 var qdi = new data.QueryDataSourceInterface({ metadata: metadata, query: new data.Query() });
66 var tab = new ui.Table( {
69 width: this.out.innerWidth()
70 } ).attach(this.out.empty());
71 qdi._results_handler(qdi.config.query, results);
74 _showRawJSON : function() {
75 if($("#rawJsonText").length === 0) {
76 var hiddenButton = $("#showRawJSON");
77 var jsonText = $({tag: "P", type: "p", id: "rawJsonText"});
78 jsonText.text(hiddenButton[0].value);
79 hiddenButton.parent().append(jsonText);
83 _searchSource_handler: function(src) {
84 var searchSourceDiv = this.el.find("DIV.uiStructuredQuery-src");
85 searchSourceDiv.empty().append(new app.ui.JsonPretty({ obj: src }));
86 if(typeof JSON !== "undefined") {
87 var showRawJSON = $({ tag: "BUTTON", type: "button", text: i18n.text("StructuredQuery.ShowRawJson"), id: "showRawJSON", value: JSON.stringify(src), onclick: this._showRawJSON });
88 searchSourceDiv.append(showRawJSON);
90 searchSourceDiv.show();
93 _main_template: function() {
94 return { tag: "DIV", cls: this._baseCls, children: [
96 { tag: "DIV", cls: "uiStructuredQuery-body" },
97 { tag: "DIV", cls: "uiStructuredQuery-src", css: { display: "none" } },
98 { tag: "DIV", cls: "uiStructuredQuery-out" }
103 ui.StructuredQuery = ui.Page.extend({
105 this.q = new StructuredQuery( this.config );
110 })( this.jQuery, this.app, this.i18n );