aeb4b6b8ff7c000317d89d2a4083b96f1be6d4cc
[ccsdk/features.git] /
1 (function( $, app, i18n ) {
2
3         var ui = app.ns("ui");
4         var data = app.ns("data");
5
6         var StructuredQuery = ui.AbstractWidget.extend({
7                 defaults: {
8                         cluster: null  // (required) instanceof app.services.Cluster
9                 },
10                 _baseCls: "uiStructuredQuery",
11                 init: function(parent) {
12                         this._super();
13                         this.selector = new ui.IndexSelector({
14                                 onIndexChanged: this._indexChanged_handler,
15                                 cluster: this.config.cluster
16                         });
17                         this.el = $(this._main_template());
18                         this.out = this.el.find("DIV.uiStructuredQuery-out");
19                         this.attach( parent );
20                 },
21                 
22                 _indexChanged_handler: function( index ) {
23                         this.filter && this.filter.remove();
24                         this.filter = new ui.FilterBrowser({
25                                 cluster: this.config.cluster,
26                                 index: index,
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
30                         });
31                         this.el.find(".uiStructuredQuery-body").append(this.filter);
32                 },
33                 
34                 _results_handler: function( filter, event ) {
35                         var typeMap = {
36                                 "json": this._jsonResults_handler,
37                                 "table": this._tableResults_handler,
38                                 "csv": this._csvResults_handler
39                         };
40                         typeMap[ event.type ].call( this, event.data, event.metadata );
41                 },
42                 _jsonResults_handler: function( results ) {
43                         this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.JsonPretty({ obj: results }));
44                 },
45                 _csvResults_handler: function( results ) {
46                         this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.CSVTable({ results: results }));
47                 },
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( {
52                                 store: qdi,
53                                 height: 400,
54                                 width: this.out.innerWidth()
55                         } ).attach(this.out.empty());
56                         qdi._results_handler(qdi.config.query, results);
57                 },
58                 
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);
65                         }
66                 },
67                 
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);
74                         }
75                         searchSourceDiv.show();
76                 },
77                 
78                 _main_template: function() {
79                         return { tag: "DIV", cls: this._baseCls, children: [
80                                 this.selector,
81                                 { tag: "DIV", cls: "uiStructuredQuery-body" },
82                                 { tag: "DIV", cls: "uiStructuredQuery-src", css: { display: "none" } },
83                                 { tag: "DIV", cls: "uiStructuredQuery-out" }
84                         ]};
85                 }
86         });
87
88         ui.StructuredQuery = ui.Page.extend({
89                 init: function() {
90                         this.q = new StructuredQuery( this.config );
91                         this.el = this.q.el;
92                 }
93         });
94
95 })( this.jQuery, this.app, this.i18n );