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