Skip to content

Commit 480a85f

Browse files
author
minjk-bl
committed
Add inplace operationo to Frame
1 parent 4229cf6 commit 480a85f

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

html/m_apps/frame.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<div>
3939
<label for="vp_feReturn">Allocate to</label>
4040
<input type="text" class="vp-input" id="vp_feReturn" placeholder="Variable name" />
41+
<label><input type="checkbox" class="vp-state" id="inplace" /><span>Inplace</span></label>
4142
</div>
4243
</div>
4344
<div class="vp-fe-toolbar">

js/m_apps/Frame.js

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ define([
3636
originObj: '',
3737
tempObj: '_vp',
3838
returnObj: '_vp',
39+
inplace: false,
3940
columnList: [],
4041
indexList: [],
4142
selected: [],
@@ -105,7 +106,13 @@ define([
105106
// initialize state values
106107
that.state.originObj = origin;
107108
that.state.tempObj = '_vp';
109+
that.state.returnObj = that.state.tempObj;
110+
that.state.inplace = false;
108111
that.initState();
112+
113+
// reset return obj
114+
$(that.wrapSelector('#vp_feReturn')).val(that.state.tempObj);
115+
$(that.wrapSelector('#inplace')).prop('checked', false);
109116

110117
// reset table
111118
$(that.wrapSelector('.' + VP_FE_TABLE)).replaceWith(function() {
@@ -133,10 +140,33 @@ define([
133140
if (returnVariable == '') {
134141
returnVariable = that.state.tempObj;
135142
}
143+
// check if it's same with origin obj
144+
if (returnVariable === that.state.originObj) {
145+
$(that.wrapSelector('#inplace')).prop('checked', true);
146+
that.state.inplace = true;
147+
} else {
148+
$(that.wrapSelector('#inplace')).prop('checked', false);
149+
that.state.inplace = false;
150+
}
151+
152+
// show preview with new return variable
153+
that.state.returnObj = returnVariable;
154+
that.setPreview(that.getCurrentCode());
155+
});
156+
157+
// check/uncheck inplace
158+
$(this.wrapSelector('#inplace')).on('change', function() {
159+
let checked = $(this).prop('checked');
160+
let returnVariable = '_vp';
161+
if (checked === true) {
162+
returnVariable = that.state.originObj;
163+
}
164+
$(that.wrapSelector('#vp_feReturn')).val(returnVariable);
165+
136166
// show preview with new return variable
137-
var newCode = that.state.steps[that.state.steps.length - 1];
138-
that.setPreview(newCode.replaceAll(that.state.tempObj, returnVariable));
167+
that.state.inplace = checked;
139168
that.state.returnObj = returnVariable;
169+
that.setPreview(that.getCurrentCode());
140170
});
141171

142172
// menu on column
@@ -483,22 +513,24 @@ define([
483513
render() {
484514
super.render();
485515

486-
this.loadVariableList();
487-
488516
var {
489-
originObj,
490517
returnObj,
518+
inplace,
491519
steps
492520
} = this.state;
521+
522+
this.loadVariableList();
493523

494-
$(this.wrapSelector('#vp_feVariable')).val(originObj);
524+
$(this.wrapSelector('#vp_feVariable')).val(this.state.originObj);
495525

496526
$(this.wrapSelector('#vp_feReturn')).val(returnObj);
527+
528+
$(this.wrapSelector('#inplace')).prop('checked', inplace);
497529

498530
// execute all steps
499531
if (steps && steps.length > 0) {
500532
var code = steps.join('\n');
501-
this.state.steps = [];
533+
// this.state.steps = [];
502534
this.loadCode(code);
503535
}
504536
}
@@ -537,13 +569,42 @@ define([
537569
return tag.toString();
538570
}
539571

572+
/**
573+
* Get last code to set preview
574+
* @returns
575+
*/
576+
getCurrentCode() {
577+
let { inplace, steps, tempObj, returnObj } = this.state;
578+
let codeList = steps;
579+
if (inplace === true) {
580+
codeList = steps.slice(1, steps.length);
581+
}
582+
583+
// get last code
584+
let currentCode = codeList[codeList.length - 1];
585+
if (currentCode && currentCode != '') {
586+
currentCode = currentCode.replaceAll(tempObj, returnObj);
587+
} else {
588+
currentCode = '';
589+
}
590+
return currentCode;
591+
}
592+
540593
generateCode() {
541-
var code = this.state.steps.join('\n');
594+
var code = '';
595+
// if inplace is true, join steps without .copy()
596+
if (this.state.inplace === true) {
597+
code = this.state.steps.slice(1).join('\n');
598+
} else {
599+
code = this.state.steps.join('\n');
600+
}
542601
var returnVariable = $(this.wrapSelector('#vp_feReturn')).val();
543602
if (returnVariable != '') {
544603
code = code.replaceAll(this.state.tempObj, returnVariable);
545604

546-
code += '\n' + returnVariable;
605+
if (code != '') {
606+
code += '\n' + returnVariable;
607+
}
547608
} else {
548609
code += '\n' + this.state.tempObj;
549610
}
@@ -570,8 +631,11 @@ define([
570631

571632
setPreview(previewCodeStr) {
572633
// get only last line of code
573-
var previewCodeLines = previewCodeStr.split('\n');
574-
var previewCode = previewCodeLines.pop();
634+
var previewCode = previewCodeStr;
635+
if (previewCodeStr.includes('\n') === true) {
636+
let previewCodeLines = previewCodeStr.split('\n');
637+
previewCode = previewCodeLines.pop();
638+
}
575639
this.setCmValue('previewCode', previewCode);
576640
}
577641

@@ -1273,12 +1337,11 @@ define([
12731337
}
12741338

12751339
var that = this;
1276-
var tempObj = this.state.tempObj;
1277-
var lines = this.state.lines;
1340+
let { tempObj, lines, indexList } = this.state;
12781341
var prevLines = 0;
12791342
var scrollPos = -1;
12801343
if (more) {
1281-
prevLines = that.state.indexList.length;
1344+
prevLines = indexList.length;
12821345
scrollPos = $(this.wrapSelector('.vp-fe-table')).scrollTop();
12831346
}
12841347

0 commit comments

Comments
 (0)