bfccdf97c10cac4920d7243d9aa8e93b73c41573
[ccsdk/features.git] /
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 ut = app.ns("ut");
20
21         ui.IndexOverview = ui.Page.extend({
22                 defaults: {
23                         cluster: null
24                 },
25                 init: function() {
26                         this._super();
27                         this.cluster = this.config.cluster;
28                         this._clusterState = this.config.clusterState;
29                         this._clusterState.on("data", this._refresh_handler );
30                         this.el = $(this._main_template());
31                         this._refresh_handler();
32                 },
33                 remove: function() {
34                         this._clusterState.removeObserver( "data", this._refresh_handler );
35                 },
36                 _refresh_handler: function() {
37                         var state = this._clusterState;
38                         var view = {
39                                 indices: acx.eachMap( state.status.indices, function( name, index ) {
40                                         return {
41                                                 name: name,
42                                                 state: index
43                                         };
44                                 }).sort( function( a, b ) {
45                                         return a.name < b.name ? -1 : 1;
46                                 })
47                         };
48                         this._indexViewEl && this._indexViewEl.remove();
49                         this._indexViewEl = $( this._indexTable_template( view ) );
50                         this.el.find(".uiIndexOverview-table").append( this._indexViewEl );
51                 },
52                 _newIndex_handler: function() {
53                         var fields = new app.ux.FieldCollection({
54                                 fields: [
55                                         new ui.TextField({ label: i18n.text("ClusterOverView.IndexName"), name: "_name", require: true }),
56                                         new ui.TextField({
57                                                 label: i18n.text("ClusterOverview.NumShards"),
58                                                 name: "number_of_shards",
59                                                 value: "5",
60                                                 require: function( val ) { return parseInt( val, 10 ) >= 1; }
61                                         }),
62                                         new ui.TextField({
63                                                 label: i18n.text("ClusterOverview.NumReplicas"),
64                                                 name: "number_of_replicas",
65                                                 value: "1",
66                                                 require: function( val ) { return parseInt( val, 10 ) >= 0; }
67                                         })
68                                 ]
69                         });
70                         var dialog = new ui.DialogPanel({
71                                 title: i18n.text("ClusterOverview.NewIndex"),
72                                 body: new ui.PanelForm({ fields: fields }),
73                                 onCommit: function(panel, args) {
74                                         if(fields.validate()) {
75                                                 var data = fields.getData();
76                                                 var name = data["_name"];
77                                                 delete data["_name"];
78                                                 this.config.cluster.put( encodeURIComponent( name ), JSON.stringify({ settings: { index: data } }), function(d) {
79                                                         dialog.close();
80                                                         alert(JSON.stringify(d));
81                                                         this._clusterState.refresh();
82                                                 }.bind(this) );
83                                         }
84                                 }.bind(this)
85                         }).open();
86                 },
87                 _indexTable_template: function( view ) { return (
88                         { tag: "TABLE", cls: "table", children: [
89                                 { tag: "THEAD", children: [
90                                         { tag: "TR", children: [
91                                                 { tag: "TH" },
92                                                 { tag: "TH", children: [
93                                                         { tag: "H3", text: "Size" }
94                                                 ] },
95                                                 { tag: "TH", children: [
96                                                         { tag: "H3", text: "Docs" }
97                                                 ] }
98                                         ] }
99                                 ] },
100                                 { tag: "TBODY", cls: "striped", children: view.indices.map( this._index_template, this ) }
101                         ] }
102                 ); },
103
104                 _index_template: function( index ) { return (
105                         { tag: "TR", children: [
106                                 { tag: "TD", children: [
107                                         { tag: "H3", text: index.name }
108                                 ] },
109                                 { tag: "TD", text: ut.byteSize_template( index.state.primaries.store.size_in_bytes ) + "/" + ut.byteSize_template( index.state.total.store.size_in_bytes ) },
110                                 { tag: "TD", text: ut.count_template( index.state.primaries.docs.count ) }
111                         ] }
112                 ); },
113                 _main_template: function() {
114                         return { tag: "DIV", id: this.id(), cls: "uiIndexOverview", children: [
115                                 new ui.Toolbar({
116                                         label: i18n.text("IndexOverview.PageTitle"),
117                                         left: [
118                                                 new ui.Button({
119                                                         label: i18n.text("ClusterOverview.NewIndex"),
120                                                         onclick: this._newIndex_handler
121                                                 }),
122                                         ]
123                                 }),
124                                 { tag: "DIV", cls: "uiIndexOverview-table", children: this._indexViewEl }
125                         ] };
126                 }
127
128         });
129
130 })( this.jQuery, this.app, this.i18n );