CalculatorUI.js
22.6 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* Project : AMDA-NG4
* Name : CalculatorUI.js
* @class amdaDesktop.ExplorerModule
* @extends Ext.util.Observable
* @brief Calculator Plugin used in SearchUI and ParameterUI
* @author elena
*/
/**
* @plugin calculator
* @extends Ext.util.Observable
* @ptype calculator
*/
var CalculatorData = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '(', ')', '[', ']', '+', '-', '*', '/', '^', '.', '>', '>=', '=', '!=', '<=', '<', '&', '|'];
Ext.define('amdaUI.CalculatorUI', {
extend: 'Ext.util.Observable',
requires: [
'amdaModel.Constant',
'amdaModel.Function'
],
alias: 'plugin.calculator',
statics: {
constantStore: null,
functionStore: null
},
win: null,
constructor: function (config) {
Ext.apply(this, config);
this.callParent(arguments);
},
init: function (cmp)
{
this.hostCmp = cmp;
this.hostCmp.on({
scope: this,
added: function () {
this.hostCmp.ownerCt.on({
render: this.onRender,
show: this.onShow,
hide: this.onHide,
scope: this});
}
});
},
onRender: function ()
{
this.win = new Ext.Window({
width: 350,
height: 170,
x: 380, y: 0,
baseCls: 'x-panel',
title: 'Tools For ' + this.context + ' Construction',
layout: 'fit',
closable: false,
collapsible: true,
constrain: true,
floating: true,
ghost: false,
renderTo: this.hostCmp.id,
items: this.getFormConfig(),
listeners: {
boxready: function (w)
{
if (w.y + w.height > myDesktopApp.desktop.el.getHeight())
w.el.setY((myDesktopApp.desktop.el.getHeight() - w.height > 0) ? myDesktopApp.desktop.el.getHeight() - w.height : 0);
}
},
getConstrainVector: function (constrainTo) {
var me = this;
if (me.constrain || me.constrainHeader) {
constrainTo = constrainTo || (me.floatParent && me.floatParent.getTargetEl()) || me.container || me.el.getScopeParent();
return (me.constrainHeader ? me.header.el : me.el).getConstrainVector(constrainTo);
}
}
});
//load constants store
if (!amdaUI.CalculatorUI.constantStore)
{
amdaUI.CalculatorUI.constantStore = Ext.create('Ext.data.Store', {model: 'amdaModel.Constant'});
amdaUI.CalculatorUI.constantStore.load({
scope: this,
callback: function (records, operation, success)
{
this.createAllConstantBtns();
}
});
} else
this.createAllConstantBtns();
//load functions store
if (!amdaUI.CalculatorUI.functionStore)
{
amdaUI.CalculatorUI.functionStore = Ext.create('Ext.data.Store', {model: 'amdaModel.Function'});
amdaUI.CalculatorUI.functionStore.load({
scope: this,
callback: function (records, operation, success)
{
this.createAllBtns();
}
});
} else
this.createAllBtns();
},
onShow: function () {
this.win.show();
var parentWin = this.hostCmp.findParentByType('window');
if (parentWin.getId() === myDesktopApp.dynamicModules.param.id) {
this.win.setPosition(335, 10);
} else {
var posX = parentWin.getWidth() - this.win.getWidth() - 30;
var posY = parentWin.getHeight() - this.win.getHeight() - 110 - 30/*20*/;
this.win.setPosition(posX, posY);//(420,290);
}
},
onHide: function ()
{
this.win.hide();
},
getFormConfig: function () {
return {
xtype: 'tabpanel',
border: false, frame: true, plain: true,
enableTabScroll: true,
defaults: {frame: true, border: false, plain: true, autoScroll: true},
activeTab: 0,
items: [{
title: 'Calculator', layout: 'column',
defaults: {xtype: 'button', columnWidth: .11},
items: this.getItems('Calculator')
}, {
title: 'Constants', xtype: 'tabpanel', //iconCls: 'tabs',
enableTabScroll: true, tabPosition: 'bottom',
defaults: {frame: true, border: false, plain: true, layout: 'column', autoScroll: true},
activeTab: 0,
id: 'calc_tab_const_id'
}, {
title: 'Math', xtype: 'tabpanel', //iconCls: 'tabs',
enableTabScroll: true, tabPosition: 'bottom',
defaults: {frame: true, border: false, plain: true, layout: 'column', autoScroll: true},
activeTab: 0,
id: 'calc_tab_math_id'
},
{
title: 'Statistics', xtype: 'tabpanel', //iconCls: 'tabs',
enableTabScroll: true, tabPosition: 'bottom',
defaults: {frame: true, border: false, plain: true, layout: 'column', autoScroll: true},
activeTab: 0,
id: 'calc_tab_stat_id'
},
{
title: 'Space', xtype: 'tabpanel', //iconCls: 'tabs',
enableTabScroll: true, tabPosition: 'bottom',
defaults: {frame: true, border: false, plain: true, layout: 'column', autoScroll: true},
activeTab: 0,
id: 'calc_tab_func_id'
}
]
};
},
/**
* Prompt any argument to user before processing Formula
* @param sel selected text
* @param currentBtn calculator button pressed
* @param params array of parameters
*/
preProcessFormula: function (sel, currentBtn, params) {
if (currentBtn.initialConfig.args != 0 && currentBtn.initialConfig.prompt != "") {
// Prompt for user precision and process the result using a callback
Ext.Msg.prompt('Argument', currentBtn.initialConfig.prompt, function (bt2, text) {
if (bt2 === 'ok') {
var afterParamsText = text + ")";
//TODO: more than one args and prompt
this.processFormula(sel, currentBtn, params, afterParamsText);
}
}, this);
} else {
if ( currentBtn.initialConfig.prompt_param != "" ) {
Ext.Msg.alert('Input Parameters', currentBtn.initialConfig.prompt_param);
}
this.processFormula(sel, currentBtn, params, ")");
}// endif prompt
},
processFormula: function (sel, currentBtn, params, afterParamsText) {
// calculation of the required number of parameters
var nbParams = currentBtn.initialConfig.args;
var remainingParams = nbParams - params.length;
var fnText = currentBtn.text.split('(');
var newConstruction = sel.beforeText + fnText[0] + "(";
// if there at least one parameter selected
if (params.length) {
for (var i = 0; i < params.length; i++) {
if (i > 0) {
newConstruction += ",";
}
newConstruction += params[i];
}
}
// we keep position
var afterParameterPos = newConstruction.length;
for (var i = 0; i < remainingParams-1; i++) {
newConstruction += ',';
}
if(afterParamsText != ")")
newConstruction += ','
newConstruction += afterParamsText;
var caretPos = newConstruction.length;
newConstruction += sel.afterText;
this.hostCmp.constructionField.setValue(newConstruction);
// If we haven't the right number of selected parameters
if (remainingParams > 0) {
var stringParamRequired = (remainingParams) + " parameter(s)";
Ext.Msg.alert('Caution', 'you\'ll have to add ' + stringParamRequired + ' to apply this function',
function () {
// set Caret Position at placement of required parameter in function
this.hostCmp.constructionField.setCaretPosition(afterParameterPos);
}, this
);
} else if (remainingParams < 0) {
var stringParamRequired = (-remainingParams) + " parameter(s)";
Ext.Msg.show({
title: 'Caution',
msg: 'you\'ll have to remove ' + stringParamRequired + ' to apply this function',
buttons: Ext.Msg.OK,
// animEl: 'elId',
icon: Ext.MessageBox.WARNING,
fn:function () {
// set Caret Position at placement of required parameter in function
this.hostCmp.constructionField.setCaretPosition(afterParameterPos);
},
scope: this
});
} else {
// set Caret Position after inserted Text
this.hostCmp.constructionField.setCaretPosition(caretPos);
}
},
/**
* This method construct an array of arguments into selected text
* @param selectedText the selection to parse
* @param parseIndex the index to start parsing
* @return the arguments array
*/
parseArgsInFormula: function (selectedText, parseIndex) {
if (!selectedText || selectedText == "") {
return [];
} else {
var params = [];
var startIndex = parseIndex;
var curIndex = parseIndex;
var openBrace = 0;
var sep = 0;
var closeBrace = 0;
// while there is a separator
while (sep != -1) {
openBrace = selectedText.indexOf("(", curIndex);
sep = selectedText.indexOf(",", curIndex);
closeBrace = selectedText.indexOf(")", curIndex);
// if there's an open bracket and no close bracket or inversely
if (openBrace != -1 && closeBrace == -1 || openBrace == -1 && closeBrace != -1) {
// invalid selection
return -1;
}
// if there's a separator and opening brackets into selection
if (sep != -1 && openBrace != -1) {
// if brace is before separator
if (openBrace < sep) {
curIndex = this.getEndBracket(selectedText, openBrace + 1);
if (curIndex === -1) {
return -1;
}
} else {// else separator is before brace
params.push(selectedText.substring(startIndex, sep));
startIndex = curIndex = sep + 1;
}
// if there's only separators into selection
} else if (sep != -1) {
params.push(selectedText.substring(startIndex, sep));
startIndex = curIndex = sep + 1;
}
}
params.push(selectedText.substring(startIndex, selectedText.length));
return params;
}
},
getEndBracket: function (string, indOpenBrace) {
// we search for the corresponding end brace (after open bracket)
var currentIndex = indOpenBrace;
var nextCloseBrace = 0;
var nextOpenBrace = 0;
var braceLevel = 1;
while (nextCloseBrace !== -1 && braceLevel !== 0) {
// get index of next opening bracket
nextOpenBrace = string.indexOf("(", currentIndex);
// get index of next closing bracket
nextCloseBrace = string.indexOf(")", currentIndex);
// if both exist
if (nextOpenBrace != -1 && nextCloseBrace != -1) {
// if opening bracket is before closing one
if (nextOpenBrace < nextCloseBrace) {
currentIndex = nextOpenBrace + 1;
braceLevel++;
} else { // if closing bracket is before opening one
currentIndex = nextCloseBrace + 1;
braceLevel--;
}
// if there's only a next opening bracket
} else if (nextOpenBrace != -1 && nextCloseBrace == -1) {
currentIndex = nextOpenBrace + 1;
braceLevel++;
// if there's only a next closing bracket
} else if (nextOpenBrace == -1 && nextCloseBrace != -1) {
currentIndex = nextCloseBrace + 1;
braceLevel--;
}
}
// if no level imbrication left return index after closing bracket of block else -1
return braceLevel == 0 ? currentIndex : -1;
},
getCalculatorBtn: function ()
{
var btns = [];
Ext.each(CalculatorData, function (c) {
btns.push({
text: c,
scope: this,
handler: function (b, e) {
// keep selection into construction field
var selection = this.hostCmp.constructionField.getSelection();
// the new value of construction field
var newConstruction = "";
// replacement of selection into construction field by text of clicked button
newConstruction = selection.beforeText + b.text;
var caretPos = newConstruction.length;
newConstruction += selection.afterText;
this.hostCmp.constructionField.setValue(newConstruction);
// set Caret Position after inserted Text
this.hostCmp.constructionField.setCaretPosition(caretPos);
}
})
},
this
);
return btns;
},
createAllBtns: function ()
{
// group math
amdaUI.CalculatorUI.functionStore.filter('group', 'math');
this.createFunctionBtns('MathFunctions', 'Simple Maths','#calc_tab_math_id');
this.createFunctionBtns('VectorFunctions', 'Vector Functions', '#calc_tab_math_id');
amdaUI.CalculatorUI.functionStore.clearFilter();
// group space
amdaUI.CalculatorUI.functionStore.filter('group', 'space');
this.createFunctionBtns('PhysicsFunctions', 'Derived', '#calc_tab_func_id');
this.createFunctionBtns('ModelFunctions', 'Models', '#calc_tab_func_id');
this.createFunctionBtns('AmdaFunctions', 'TimeShift', '#calc_tab_func_id');
amdaUI.CalculatorUI.functionStore.clearFilter();
// group stat
amdaUI.CalculatorUI.functionStore.filter('group', 'stat');
this.createFunctionBtns('TimeFunctions', 'Statistics', '#calc_tab_stat_id');
this.createFunctionBtns('FunctionsSliding', 'Statistics/Sliding','#calc_tab_stat_id');
amdaUI.CalculatorUI.functionStore.clearFilter();
},
createAllConstantBtns: function ()
{
this.createConstantBtns('Space', 'Planets Constants');
this.createConstantBtns('Physics', 'Physics Constants');
this.createConstantBtns('Units', 'Units Conversion');
},
createConstantBtns: function (item, tabTitle)
{
var constTab = this.win.query('#calc_tab_const_id');
if (constTab.length < 1)
return;
switch (item)
{
case 'Space' :
amdaUI.CalculatorUI.constantStore.filter('kind', 'space');
break;
case 'Physics' :
amdaUI.CalculatorUI.constantStore.filter('kind', 'physics');
break;
case 'Units' :
amdaUI.CalculatorUI.constantStore.filter('kind', 'units');
break;
}
var crtTab = constTab[0].add(
{
title: tabTitle,
defaults: {xtype: 'button', columnWidth: .20}
});
amdaUI.CalculatorUI.constantStore.each(function (c) {
crtTab.add(
{
text: c.get('name'),
tooltip: c.get('units') == '' ? c.get('info') + '<br/>' + c.get('value') :
c.get('info') + '<br/>' + c.get('value') + ' ' + c.get('units'),
scope: this,
handler: function (b, e) {
// keep selection into construction field
var selection = this.hostCmp.constructionField.getSelection();
// the new value of construction field
var newConstruction = "";
// replacement of selection into construction field by text of clicked button
newConstruction = selection.beforeText + '@' + b.text;
var caretPos = newConstruction.length;
newConstruction += selection.afterText;
this.hostCmp.constructionField.setValue(newConstruction);
// set Caret Position after inserted Text
this.hostCmp.constructionField.setCaretPosition(caretPos);
}
});
}, this);
//clear filter
amdaUI.CalculatorUI.constantStore.clearFilter();
},
createFunctionBtns: function (item, tabTitle, tabID)
{
var funcTab = this.win.query(tabID);
if (funcTab.length < 1)
return;
var width = .20;
switch (item)
{
case 'MathFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'math');
break;
case 'AmdaFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'amda');
break;
case 'TimeFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'time');
break;
case 'FunctionsSliding' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'sliding');
break;
case 'VectorFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'vector');
break;
case 'ModelFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'model');
width = .45;
break;
case 'PhysicsFunctions' :
amdaUI.CalculatorUI.functionStore.filter('kind', 'physics');
width = .25;
break;
}
var crtTab = funcTab[0].add(
{
title: tabTitle,
defaults: {xtype: 'button', columnWidth: width}
});
amdaUI.CalculatorUI.functionStore.each(function (f) {
crtTab.add(
{
text: f.get('name'),
args: f.get('args'),
defaultArgs: f.get('default_args'),
prompt: f.get('prompt'),
prompt_param: f.get('prompt_param'),
tooltip: f.get('info_brief'),
scope: this,
handler: function (b, e) {
var selection = this.hostCmp.constructionField.getSelection();
var selectedText = selection && selection.text != "" ? Ext.util.Format.trim(selection.text) : null;
if (selectedText && selectedText !== "") {
selectedText.replace("[", "(");
selectedText.replace("{", "(");
selectedText.replace("}", ")");
selectedText.replace("]", ")");
}
var params = [];
var raw_args = b.initialConfig.defaultArgs;
if (raw_args && raw_args != "") {
params = b.initialConfig.defaultArgs.split(',');
}
// Formula Parsing for arguments
// var params = this.parseArgsInFormula(selectedText, 0);
// var params = selectedText ? selectedText.split(',') : [];
if (params === -1) {
Ext.Msg.alert("Invalid Selection", "Action aborted");
} else {
// calculation of the required number of parameters
var nbParams = b.initialConfig.args;
this.preProcessFormula(selection, b, params);
/**
if (params.length > nbParams)
{
// Show a dialog using config options:
Ext.Msg.show({
title: 'Caution',
msg: 'you have selected more than ' + nbParams + ' parameter(s) to apply this function<br>Only the first will be kept, others will be deleted',
buttons: Ext.Msg.OKCANCEL,
// animEl: 'elId',
icon: Ext.MessageBox.WARNING,
fn: function (bt1) {
if (bt1 === 'ok') {
this.preProcessFormula(selection, b, params);
}
},
scope: this
});
} else
{
this.preProcessFormula(selection, b, params);
}*/
}
}
});
},
this
);
//clear filter
amdaUI.CalculatorUI.functionStore.clearFilter();
},
getItems: function (item)
{
switch (item)
{
case 'Calculator':
return this.getCalculatorBtn();
default:
break;
}
return [];
}
});