YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / resources / elasticsearch / plugins / head / src / app / ui / structuredQuery / structuredQuery.js
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 (function( $, app, i18n ) {
17
18         var ui = app.ns("ui");
19         var data = app.ns("data");
20
21         var StructuredQuery = ui.AbstractWidget.extend({
22                 defaults: {
23                         cluster: null  // (required) instanceof app.services.Cluster
24                 },
25                 _baseCls: "uiStructuredQuery",
26                 init: function(parent) {
27                         this._super();
28                         this.selector = new ui.IndexSelector({
29                                 onIndexChanged: this._indexChanged_handler,
30                                 cluster: this.config.cluster
31                         });
32                         this.el = $(this._main_template());
33                         this.out = this.el.find("DIV.uiStructuredQuery-out");
34                         this.attach( parent );
35                 },
36                 
37                 _indexChanged_handler: function( index ) {
38                         this.filter && this.filter.remove();
39                         this.filter = new ui.FilterBrowser({
40                                 cluster: this.config.cluster,
41                                 index: index,
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
45                         });
46                         this.el.find(".uiStructuredQuery-body").append(this.filter);
47                 },
48                 
49                 _results_handler: function( filter, event ) {
50                         var typeMap = {
51                                 "json": this._jsonResults_handler,
52                                 "table": this._tableResults_handler,
53                                 "csv": this._csvResults_handler
54                         };
55                         typeMap[ event.type ].call( this, event.data, event.metadata );
56                 },
57                 _jsonResults_handler: function( results ) {
58                         this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.JsonPretty({ obj: results }));
59                 },
60                 _csvResults_handler: function( results ) {
61                         this.el.find("DIV.uiStructuredQuery-out").empty().append( new ui.CSVTable({ results: results }));
62                 },
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( {
67                                 store: qdi,
68                                 height: 400,
69                                 width: this.out.innerWidth()
70                         } ).attach(this.out.empty());
71                         qdi._results_handler(qdi.config.query, results);
72                 },
73                 
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);
80                         }
81                 },
82                 
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);
89                         }
90                         searchSourceDiv.show();
91                 },
92                 
93                 _main_template: function() {
94                         return { tag: "DIV", cls: this._baseCls, children: [
95                                 this.selector,
96                                 { tag: "DIV", cls: "uiStructuredQuery-body" },
97                                 { tag: "DIV", cls: "uiStructuredQuery-src", css: { display: "none" } },
98                                 { tag: "DIV", cls: "uiStructuredQuery-out" }
99                         ]};
100                 }
101         });
102
103         ui.StructuredQuery = ui.Page.extend({
104                 init: function() {
105                         this.q = new StructuredQuery( this.config );
106                         this.el = this.q.el;
107                 }
108         });
109
110 })( this.jQuery, this.app, this.i18n );