Skip to content

Commit a4eff82

Browse files
author
minjk-bl
committed
Apps > Groupby update
1 parent c025bbe commit a4eff82

File tree

1 file changed

+109
-9
lines changed

1 file changed

+109
-9
lines changed

src/common/vpGroupby.js

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ define([
9191
return vpCommon.formatString('.{0}.{1} {2}', APP_PREFIX, this.uuid, query);
9292
}
9393

94-
_setPreview() {
95-
96-
}
97-
94+
/**
95+
* Load state and set values on components
96+
* @param {object} state
97+
*/
9898
_loadState(state) {
9999
var {
100100
variable, groupby, display, method, advanced, allocateTo, resetIndex,
@@ -125,6 +125,9 @@ define([
125125
});
126126
}
127127

128+
/**
129+
* Save now state of components
130+
*/
128131
_saveState() {
129132
// save input state
130133
$(this._wrapSelector('.vp-gb-adv-box input')).each(function () {
@@ -152,6 +155,10 @@ define([
152155
//====================================================================
153156
// External call function
154157
//====================================================================
158+
/**
159+
* Open this page with initializing
160+
* @param {object} config
161+
*/
155162
open(config={}) {
156163
this.config = {
157164
...this.config,
@@ -167,11 +174,18 @@ define([
167174
}
168175
}
169176

177+
/**
178+
* Close this page
179+
*/
170180
close() {
171181
this.unbindEvent();
172182
$(this._wrapSelector()).remove();
173183
}
174184

185+
/**
186+
* Initialize state, Render and Bind events
187+
* @param {object} state
188+
*/
175189
init(state = undefined) {
176190

177191
this.state = {
@@ -210,6 +224,9 @@ define([
210224
this.loadVariableList();
211225
}
212226

227+
/**
228+
* Render main page & frame
229+
*/
213230
render() {
214231
var page = new sb.StringBuilder();
215232
page.appendFormatLine('<div class="{0} {1}">', APP_PREFIX, this.uuid);
@@ -315,6 +332,11 @@ define([
315332
$(this._wrapSelector()).hide();
316333
}
317334

335+
/**
336+
* Render variable list (for dataframe)
337+
* @param {Array<object>} varList
338+
* @param {string} defaultValue previous value
339+
*/
318340
renderVariableList(varList, defaultValue='') {
319341
var tag = new sb.StringBuilder();
320342
tag.appendFormatLine('<select id="{0}">', 'vp_gbVariable');
@@ -330,6 +352,10 @@ define([
330352
return tag.toString();
331353
}
332354

355+
/**
356+
* Render advanced item and return it
357+
* @returns Advanced box's item
358+
*/
333359
renderAdvancedItem() {
334360
var page = new sb.StringBuilder();
335361
page.appendFormatLine('<div class="{0}">', 'vp-gb-adv-item');
@@ -358,6 +384,10 @@ define([
358384
return page.toString();
359385
}
360386

387+
/**
388+
* Render inner popup for selecting columns
389+
* @returns Inner popup page dom
390+
*/
361391
renderInnerPopup() {
362392
var page = new sb.StringBuilder();
363393
page.appendFormatLine('<div class="{0}" style="display: none; width: 400px; height: 300px;">', APP_POPUP_BOX);
@@ -380,10 +410,22 @@ define([
380410
return page.toString();
381411
}
382412

413+
/**
414+
* Render column selector using ColumnSelector module
415+
* @param {Array<string>} previousList previous selected columns
416+
* @param {Array<string>} includeList columns to include
417+
*/
383418
renderColumnSelector(previousList, includeList) {
384419
this.popup.ColSelector = new vpColumnSelector(this._wrapSelector('.' + APP_POPUP_BODY), this.state.variable, previousList, includeList);
385420
}
386421

422+
/**
423+
* Render naming box
424+
* @param {Array<string>} columns
425+
* @param {string} method
426+
* @param {Object} previousDict
427+
* @returns
428+
*/
387429
renderNamingBox(columns, method, previousDict) {
388430
var page = new sb.StringBuilder();
389431
page.appendFormatLine('<div class="{0}">', 'vp-gb-naming-box');
@@ -415,8 +457,12 @@ define([
415457
return page.toString();
416458
}
417459

418-
419-
460+
/**
461+
* Open Inner popup page for column selection
462+
* @param {Object} targetSelector
463+
* @param {string} title
464+
* @param {Array<string>} includeList
465+
*/
420466
openInnerPopup(targetSelector, title='Select columns', includeList=[]) {
421467
this.popup.type = 'column';
422468
this.popup.targetSelector = targetSelector;
@@ -429,10 +475,19 @@ define([
429475
$(this._wrapSelector('.' + APP_POPUP_BOX)).show();
430476
}
431477

478+
/**
479+
* Close Inner popup page
480+
*/
432481
closeInnerPopup() {
433482
$(this._wrapSelector('.' + APP_POPUP_BOX)).hide();
434483
}
435484

485+
/**
486+
* Open Naming popup page
487+
* @param {Object} targetSelector
488+
* @param {Array<string>} columns
489+
* @param {string} method
490+
*/
436491
openNamingPopup(targetSelector, columns, method) {
437492
this.popup.type = 'naming';
438493
this.popup.targetSelector = targetSelector;
@@ -445,6 +500,9 @@ define([
445500
$(this._wrapSelector('.' + APP_POPUP_BOX)).show();
446501
}
447502

503+
/**
504+
* Load variable list (dataframe)
505+
*/
448506
loadVariableList() {
449507
var that = this;
450508
// load using kernel
@@ -466,18 +524,33 @@ define([
466524
});
467525
}
468526

527+
/**
528+
* Unbind events
529+
*/
469530
unbindEvent() {
470531
$(document).unbind(vpCommon.formatString(".{0} .{1}", this.uuid, APP_BODY));
471532

472533
// user operation event
473534
$(document).off('change', this._wrapSelector('#vp_gbVariable'));
535+
$(document).off('click', this._wrapSelector('.vp-gb-df-refresh'));
536+
$(document).off('change', this._wrapSelector('#vp_gbBy'));
474537
$(document).off('click', this._wrapSelector('#vp_gbBySelect'));
538+
$(document).off('change', this._wrapSelector('#vp_gbDisplay'));
475539
$(document).off('click', this._wrapSelector('#vp_gbDisplaySelect'));
476540
$(document).off('change', this._wrapSelector('#vp_gbMethodSelect'));
477541
$(document).off('change', this._wrapSelector('#vp_gbAdvanced'));
478542
$(document).off('change', this._wrapSelector('#vp_gbAllocateTo'));
479543
$(document).off('change', this._wrapSelector('#vp_gbResetIndex'));
480544

545+
$(document).off('click', this._wrapSelector('#vp_gbAdvAdd'));
546+
$(document).off('change', this._wrapSelector('.vp-gb-adv-col'));
547+
$(document).off('click', this._wrapSelector('.vp-gb-adv-col-selector'));
548+
$(document).off('change', this._wrapSelector('.vp-gb-adv-method-selector'));
549+
$(document).off('click', this._wrapSelector('.vp-gb-adv-method-return'));
550+
$(document).off('change', this._wrapSelector('.vp-gb-adv-naming'));
551+
$(document).off('click', this._wrapSelector('.vp-gb-adv-naming-selector'));
552+
$(document).off('click', this._wrapSelector('.vp-gb-adv-item-delete'));
553+
481554
$(document).off('click', this._wrapSelector('.' + APP_CLOSE));
482555
$(document).off('click', this._wrapSelector('.' + APP_BUTTON_PREVIEW));
483556
$(document).off('click', this._wrapSelector('.' + APP_BUTTON_CANCEL));
@@ -495,6 +568,9 @@ define([
495568
$(document).off('click', this._wrapSelector('.' + APP_POPUP_CLOSE));
496569
}
497570

571+
/**
572+
* Bind events
573+
*/
498574
bindEvent() {
499575
var that = this;
500576
//====================================================================
@@ -758,6 +834,11 @@ define([
758834
});
759835
}
760836

837+
/**
838+
* Apply code to jupyter cell or as a block
839+
* @param {boolean} addCell
840+
* @param {boolean} runCell
841+
*/
761842
apply(addCell=false, runCell=false) {
762843
var code = this.generateCode();
763844

@@ -796,9 +877,17 @@ define([
796877
var {
797878
variable, groupby, display, method, advanced, allocateTo, resetIndex
798879
} = this.state;
880+
881+
//====================================================================
882+
// Allocation
883+
//====================================================================
799884
if (allocateTo && allocateTo != '') {
800885
code.appendFormat('{0} = ', allocateTo);
801886
}
887+
888+
//====================================================================
889+
// Dataframe variable & Groupby columns
890+
//====================================================================
802891
var byStr = '';
803892
if (groupby.length <= 1) {
804893
byStr = groupby.join('');
@@ -813,6 +902,9 @@ define([
813902
// variable & groupby columns & option
814903
code.appendFormat('{0}.groupby({1}{2})', variable, byStr, optStr);
815904

905+
//====================================================================
906+
// Display columns
907+
//====================================================================
816908
var colStr = '';
817909
if (display) {
818910
if (display.length == 1) {
@@ -824,9 +916,14 @@ define([
824916
}
825917
}
826918

919+
//====================================================================
920+
// Aggregation/Method code generation
921+
//====================================================================
827922
var methodStr = new sb.StringBuilder();
828923
if (advanced) {
829-
// aggregation
924+
//================================================================
925+
// Aggregation code generation
926+
//================================================================
830927
methodStr.append('agg(');
831928
// prepare variables for aggregation
832929
var advItemTags = $(this._wrapSelector('.vp-gb-adv-item'));
@@ -853,10 +950,9 @@ define([
853950
}
854951
}
855952

856-
console.log('advColumnDict', advColumnDict);
857-
858953
// if target columns not selected
859954
if (Object.keys(advColumnDict).length == 1) {
955+
// EX) .agg([('average', 'mean'), ('maximum value', max')])
860956
var noColList = advColumnDict['nothing'];
861957
if (noColList.length == 1) {
862958
// 1 method
@@ -878,6 +974,7 @@ define([
878974
methodStr.appendFormat("[{0}]", tmpList.join(', '));
879975
}
880976
} else {
977+
// EX) .agg({'col1':[('average', 'mean')], 'col2': 'max')})
881978
// apply method with empty column to all columns(display)
882979
var noColList = advColumnDict['nothing'];
883980
delete advColumnDict['nothing'];
@@ -918,6 +1015,9 @@ define([
9181015
}
9191016
methodStr.append(')');
9201017
} else {
1018+
//================================================================
1019+
// Method code generation
1020+
//================================================================
9211021
methodStr.appendFormat('{0}()', method);
9221022
}
9231023
// display columns

0 commit comments

Comments
 (0)