forked from agony-unleashed/fleet-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FleetsGrid.js
259 lines (243 loc) · 9.1 KB
/
FleetsGrid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
FleetsGrid = Ext.extend(Ext.grid.GridPanel, {
fleetStore: '',
initComponent:function() {
var eto = this;
// create the data store
this.fleetStore = new Ext.data.Store({
id: 'fleetStore',
proxy: new Ext.data.HttpProxy({
url: 'fleetConnection.php', // File to connect to
method: 'POST'
}),
baseParams:{task: "LISTING", "pilot": pilotName, "hoesAmount": hoesAmount}, // this parameter asks for listing
reader: new Ext.data.JsonReader({
// we tell the datastore where to get his data from
root: 'results',
totalProperty: 'total',
id: 'id'
},[
{name: 'id', type: 'int', mapping: 'id'},
{name: 'fleetOwner', type: 'string', mapping: 'fleetOwner'},
{name: 'fleetXO', type: 'string', mapping: 'fleetXO'},
{name: 'createdOn', mapping: 'createdOn'},
{name: 'about', type: 'string', mapping: 'about'},
{name: 'description', type: 'string', mapping: 'description'},
{name: 'memberCount', type: 'int', mapping: 'memberCount'},
{name: 'joined', type: 'boolean', mapping: 'joined'},
{name: 'isPublic', type: 'int', mapping: 'isPublic'},
]),
sortInfo: {
field: 'createdOn',
direction: 'ASC' // or 'DESC' (case sensitive for local sorting)
},
});
this.fleetStore.load();
var sm1 = new Ext.grid.RowSelectionModel({singleSelect:true});
var summaryRefreshHandler = function(button, event) {
if(event) {
eto.fleetStore.reload();
}
};
var summaryViewFleetHandler = function(button, event) {
if(event) {
var selectedRecord = sm1.getSelected();
var isFC = false;
if(selectedRecord) {
if(selectedRecord.get('fleetOwner') == pilotName) {
isFC = true;
}
FleetManagement.showFleetFittings(selectedRecord.get("id"), isFC);
}
}
};
var summaryNewFleetHandler = function(button, event) {
if(event) {
Ext.Msg.prompt('Description', 'Describe the purpose of the fleet:', function(btn, text){
if (btn == 'ok'){
var conn = new Ext.data.Connection();
conn.request({
url: 'fleetConnection.php',
method: 'POST',
params: {"task": "CREATEFLEET", "about": text, "fleetOwner": pilotName, "hoesAmount": hoesAmount},
failure: function(response, opts) {
Ext.Msg.alert('Error', 'Server Error: Failed Request. Try again or contact an administrator');
},
success: function(response, opts) {
if(response.responseText == "0") {
Ext.Msg.alert('Error', 'You cannot create a fleet...');
}
eto.fleetStore.reload();
}
});
}
});
}
};
var summaryRemoveFleetHandler = function(button, event) {
if(event) {
var selectedRecord = sm1.getSelected();
Ext.MessageBox.confirm('Delete Fleet?', 'Are you sure you want to delete this fleet?', function(btn) {
if(btn == "yes") {
var conn = new Ext.data.Connection();
conn.request({
url: 'fleetConnection.php',
method: 'POST',
params: {"task": "REMOVEFLEET", "id": selectedRecord.get("id"), "pilot": pilotName},
failure: function(response, opts) {
Ext.Msg.alert('Error', 'Server Error: Failed Request. Try again or contact an administrator');
},
success: function(response, opts) {
if(response.responseText == "0") {
Ext.Msg.alert('Error', 'You cannot remove this fleet...');
}
eto.fleetStore.reload();
}
});
}
});
}
};
var joinFleetHandler = function(button, event) {
if(event) {
Ext.Msg.prompt('Enter Ship Fitting URL', instructionPrompt, function(btn, text){
if (btn == 'ok'){
var selectedRecord = sm1.getSelected();
var conn = new Ext.data.Connection();
conn.request({
url: 'fleetFittings.php',
method: 'POST',
params: {"task": "JOIN", "fleetId": selectedRecord.get('id'), "pilot": pilotName, "shipDNA": text},
failure: function(response, opts) {
Ext.Msg.alert('Error', 'Server Error: Failed Request. Try again or contact an administrator');
},
success: function(response, opts) {
eto.fleetStore.reload();
}
});
}
});
}
};
var togglePrivacy = function(button, event) {
if(event) {
var selectedRecord = sm1.getSelected();
var conn = new Ext.data.Connection();
var opposite = 1;
if(selectedRecord.get('isPublic') == 1) {
opposite = 0;
}
conn.request({
url: 'fleetConnection.php',
method: 'POST',
params: {"task": "UPDATEPUBLIC", "id": selectedRecord.get('id'), "hoesAmount": hoesAmount, "isPublic": opposite},
failure: function(response, opts) {
Ext.Msg.alert('Error', 'Server Error: Failed Request. Try again or contact an administrator');
},
success: function(response, opts) {
if(response.responseText == "fagony") {
Ext.Msg.alert('Error', 'You cannot do this...');
}
eto.fleetStore.reload();
}
});
}
};
function joined (val){
if(val){
return '<span style="color:green;">' + "YES" + '</span>';
} else {
return '<span style="font-weight:bold; color:red;">' + "NO" + '</span>';
}
return val;
}
function private (val){
if(!val){
return '<span style="color:green;">' + "YES" + '</span>';
} else {
return '<span style="font-weight:bold; color:red;">' + "NO" + '</span>';
}
return val;
}
// create the Grid
Ext.apply(this, {
store: this.fleetStore,
tbar: [{
text: 'Create New Fleet',
iconCls: 'create',
handler: summaryNewFleetHandler,
},'-',{
text: 'View Fleet',
disabled: true,
iconCls: 'info',
handler: summaryViewFleetHandler,
},{
text: 'Toggle Privacy',
disabled: true,
iconCls: 'privacy',
handler: togglePrivacy,
},{
text: 'Join Fleet',
iconCls: 'join',
disabled: true,
handler: joinFleetHandler,
},'-',{
text: 'Refresh',
iconCls: 'refresh',
handler: summaryRefreshHandler,
},'->',{
text: 'Remove Fleet',
iconCls: 'delete',
disabled: true,
handler: summaryRemoveFleetHandler,
}],
columns: [
{header: 'Created On (GMT)', width: 150, sortable: true, dataIndex: 'createdOn'},
{header: 'Owner', width: 100, sortable: true, dataIndex: 'fleetOwner'},
{header: 'XO', width: 100, sortable: true, dataIndex: 'fleetXO'},
{header: 'Pilots', width: 50, sortable: true, dataIndex: 'memberCount'},
{header: 'Title', width: 250, sortable: false, dataIndex: 'about'},
{header: 'Joined', width: 46, sortable: true, dataIndex: 'joined', renderer: joined},
{header: 'Private', width: 46, sortable: true, dataIndex: 'isPublic', renderer: private},
],
stripeRows: true,
sm: sm1,
autoHeight: true,
frame: true,
height: 'auto',
width: 755,
title: 'Available Fleets',
});
this.on('rowdblclick', this.onRowDblClick, this);
sm1.on('rowselect', this.onRowSelect, this);
sm1.on('rowdeselect', this.onRowDeselect, this);
FleetsGrid.superclass.initComponent.call(this);
},
constructor: function() {
FleetsGrid.superclass.constructor.call(this);
},
onRowSelect: function(sm, rowIndex, r) {
this.getTopToolbar().getComponent(2).enable();
this.getTopToolbar().getComponent(3).enable();
// dont enable if user is already in the fleet
if(!sm.getSelected().get('joined')) {
this.getTopToolbar().getComponent(4).enable();
}
this.getTopToolbar().getComponent(8).enable();
},
onRowDeselect: function(sm, rowIndex, r) {
this.getTopToolbar().getComponent(2).disable();
this.getTopToolbar().getComponent(3).disable();
this.getTopToolbar().getComponent(4).disable();
this.getTopToolbar().getComponent(8).disable();
},
onRowDblClick: function(g, rowIndex, e) {
var selectedRecord = g.getStore().getAt(rowIndex);
//var isFC = false;
if(selectedRecord) {
//if(selectedRecord.get('fleetOwner') == pilotName) {
// isFC = true;
//}
FleetManagement.showFleetFittings(selectedRecord.get("id"));
}
},
});