Skip to content

Commit 4a86abb

Browse files
harshkhandeparkarjywarren
authored andcommitted
[GCI] InsertStep functionality added for loadImage step (#1360)
* add markuo * add markupfix insertPreview * fix insertPreview test * make the new insertPreview work * fix disabled * small fix * fix comments * remove console log * fix enabling/disabling of insert-step btn
1 parent 426733f commit 4a86abb

File tree

6 files changed

+78
-43
lines changed

6 files changed

+78
-43
lines changed

Diff for: examples/demo.css

+6
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,10 @@ a.name-header{
313313
bottom: 7px;
314314
font-size: 16px;
315315
color: #444;
316+
}
317+
318+
/* Non float rightward alignment*/
319+
.right {
320+
margin-left: auto;
321+
display: block;
316322
}

Diff for: examples/demo.js

+27-28
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ window.onload = function () {
99
sequencer = ImageSequencer();
1010

1111
options = {
12-
sortField: 'text',
13-
openOnFocus: false,
14-
onInitialize: function () {
15-
this.$control.on("click", () => {
16-
this.ignoreFocusOpen = true;
17-
setTimeout(() => {
18-
// Trigger onFocus and open dropdown.
19-
this.ignoreFocusOpen = false;
20-
}, 50);
21-
});
22-
},
23-
// Open dropdown after timeout of onClick.
24-
onFocus: function () {
25-
if (!this.ignoreFocusOpen) {
26-
this.open();
27-
}
12+
sortField: 'text',
13+
openOnFocus: false,
14+
onInitialize: function () {
15+
this.$control.on('click', () => {
16+
this.ignoreFocusOpen = true;
17+
setTimeout(() => {
18+
// Trigger onFocus and open dropdown.
19+
this.ignoreFocusOpen = false;
20+
}, 50);
21+
});
22+
},
23+
// Open dropdown after timeout of onClick.
24+
onFocus: function () {
25+
if (!this.ignoreFocusOpen) {
26+
this.open();
2827
}
29-
}
28+
}
29+
};
3030

3131
function refreshOptions(options) {
3232
// Default options if parameter is empty.
@@ -239,8 +239,8 @@ window.onload = function () {
239239
function getLastImage() {
240240
// Get the image from the last step.
241241
let imgs = document.getElementsByClassName('step-thumbnail');
242-
let lastStepImage = imgs[imgs.length-1];
243-
return lastStepImage.getAttribute("src");
242+
let lastStepImage = imgs[imgs.length - 1];
243+
return lastStepImage.getAttribute('src');
244244
}
245245

246246
/**
@@ -255,16 +255,16 @@ window.onload = function () {
255255

256256
// Create a new pdf with the same dimensions as the image.
257257
const pdf = new jsPDF({
258-
orientation: pageHeight > pageWidth ? "portrait": "landscape",
259-
unit: "px",
258+
orientation: pageHeight > pageWidth ? 'portrait' : 'landscape',
259+
unit: 'px',
260260
format: [pageHeight, pageWidth]
261261
});
262262

263263
// Add the image to the pdf with dimensions equal to the internal dimensions of the page.
264264
pdf.addImage(imageDataURL, 0, 0, pdf.internal.pageSize.getWidth(), pdf.internal.pageSize.getHeight());
265265

266266
// Save the pdf with the filename specified here:
267-
pdf.save("index.pdf");
267+
pdf.save('index.pdf');
268268
});
269269
}
270270

@@ -301,8 +301,8 @@ window.onload = function () {
301301
step.options.step.imgElement.src = reader.result;
302302
else
303303
step.imgElement.src = reader.result;
304-
insertPreview.updatePreviews(reader.result, '#addStep');
305-
insertPreview.updatePreviews(sequencer.steps[0].imgElement.src, '.insertDiv');
304+
305+
insertPreview.updatePreviews(reader.result, document.querySelector('#addStep'));
306306
},
307307
onTakePhoto: function (url) {
308308
var step = sequencer.steps[0];
@@ -312,16 +312,15 @@ window.onload = function () {
312312
step.options.step.imgElement.src = url;
313313
else
314314
step.imgElement.src = url;
315-
insertPreview.updatePreviews(url, '#addStep');
316-
insertPreview.updatePreviews(sequencer.steps[0].imgElement.src, '.insertDiv');
315+
insertPreview.updatePreviews(url, document.querySelector('#addStep'));
317316
}
318317
});
319318

320319
setupCache();
321320

322321
if (urlHash.getUrlHashParameter('src')) {
323-
insertPreview.updatePreviews(urlHash.getUrlHashParameter('src'), '#addStep');
322+
insertPreview.updatePreviews(urlHash.getUrlHashParameter('src'), document.querySelector('#addStep'));
324323
} else {
325-
insertPreview.updatePreviews('images/tulips.png', '#addStep');
324+
insertPreview.updatePreviews('images/tulips.png', document.querySelector('#addStep'));
326325
}
327326
};

Diff for: examples/lib/defaultHtmlStepUi.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function DefaultHtmlStepUi(_sequencer, options) {
2020
var selectStepSel = options.selectStepSel = options.selectStepSel || '#selectStep';
2121

2222
function onSetup(step, stepOptions) {
23-
2423
if (step.options && step.options.description)
2524
step.description = step.options.description;
2625

@@ -169,7 +168,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
169168
);
170169

171170
$stepAll('.remove').on('click', function() {notify('Step Removed', 'remove-notification');});
172-
$stepAll('.insert-step').on('click', function() { util.insertStep(step.ID); });
171+
$step('.insert-step').on('click', function() { util.insertStep(step.ID); });
173172
// Insert the step's UI in the right place
174173
if (stepOptions.index == _sequencer.steps.length) {
175174
stepsEl.appendChild(step.ui);
@@ -180,9 +179,24 @@ function DefaultHtmlStepUi(_sequencer, options) {
180179
else {
181180
stepsEl.insertBefore(step.ui, $(stepsEl).children()[stepOptions.index]);
182181
}
182+
183+
// Enable the load-image insert-step button when there are steps after load-image
184+
// The logical operator is `> 0` because the number of steps is found before adding the step, actual logic is `steps.length + 1 > 1` which is later simplified.
185+
if (_sequencer.steps.length > 0) $('#load-image .insert-step').prop('disabled', false);
186+
else $('#load-image .insert-step').prop('disabled', true);
183187
}
184188
else {
185189
$('#load-image').append(step.ui);
190+
191+
192+
$step('div.panel-footer').prepend( `
193+
<button class="right btn btn-default btn-sm insert-step" disabled="true">
194+
<span class="insert-text"><i class="fa fa-plus"></i> Insert Step</span>
195+
<span class="no-insert-text" style="display:none">Close</span>
196+
</button>`
197+
);
198+
199+
$step('.insert-step').on('click', function() { util.insertStep(step.ID); });
186200
}
187201
$step('.toggle').on('click', () => {
188202
$step('.toggleIcon').toggleClass('rotated');
@@ -346,6 +360,12 @@ function DefaultHtmlStepUi(_sequencer, options) {
346360
function onRemove(step) {
347361
step.ui.remove();
348362
$('#steps .step-container:nth-last-child(1) .insert-step').prop('disabled', true);
363+
364+
// Enable the load-image insert-step button when there are steps after load-image
365+
// The logical operator is `> 2` because the number of steps is found before removing the step, actual logic is `steps.length - 1 > 1` which is later simplified.
366+
if (_sequencer.steps.length - 1 > 1) $('#load-image .insert-step').prop('disabled', false);
367+
else $('#load-image .insert-step').prop('disabled', true);
368+
349369
$('div[class*=imgareaselect-]').remove();
350370
}
351371

Diff for: examples/lib/insertPreview.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
function generatePreview(previewStepName, customValues, path, selector) {
2-
1+
function generatePreview(previewStepName, customValues, path, DomNode) {
32
var previewSequencer = ImageSequencer();
43
function insertPreview(src) {
54
var img = document.createElement('img');
@@ -8,9 +7,9 @@ function generatePreview(previewStepName, customValues, path, selector) {
87
img.src = src;
98
$(img).css('max-width', '200%');
109
$(img).css('transform', 'translateX(-20%)');
11-
$(selector + ' .radio-group').find('div').each(function() {
12-
if ($(this).find('div').attr('data-value') === previewStepName) {
13-
$(this).find('div').append(img);
10+
$(DomNode.querySelector('.radio-group')).find('.radio').each(function() {
11+
if ($(this).attr('data-value') === previewStepName) {
12+
$(this).append(img);
1413
}
1514
});
1615
}
@@ -29,8 +28,8 @@ function generatePreview(previewStepName, customValues, path, selector) {
2928
previewSequencer.loadImage(path, loadPreview);
3029
}
3130

32-
function updatePreviews(src, selector) {
33-
$(selector + ' img').remove();
31+
function updatePreviews(src, DomNode) {
32+
$(DomNode).find('img').remove();
3433

3534
var previewSequencerSteps = {
3635
'resize': '125%',
@@ -48,6 +47,7 @@ function updatePreviews(src, selector) {
4847
};
4948

5049
var img = new Image();
50+
5151
img.onload = function(){
5252
var height = img.height;
5353
var width = img.width;
@@ -62,7 +62,7 @@ function updatePreviews(src, selector) {
6262
this.addSteps('resize', {['resize']: percentage + '%'});
6363
this.run((src)=>{
6464
Object.keys(previewSequencerSteps).forEach(function (step, index) {
65-
generatePreview(step, Object.values(previewSequencerSteps)[index], src, selector);
65+
generatePreview(step, Object.values(previewSequencerSteps)[index], src, DomNode);
6666
});
6767
});
6868
});

Diff for: examples/lib/intermediateHtmlStepUi.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
8181
var parser = new DOMParser();
8282
var addStepUI = stepUI();
8383
addStepUI = parser.parseFromString(addStepUI, 'text/html').querySelector('div');
84+
8485
if ($step('.insertDiv').length > 0){
8586
toggleDiv($step);
8687
}
@@ -91,9 +92,11 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
9192
addStepUI
9293
);
9394
toggleDiv($step, function(){
94-
insertPreview.updatePreviews(step.output, '.insertDiv');
95+
if (step.name === 'load-image') insertPreview.updatePreviews(step.output.src, $step('.insertDiv').getDomElem());
96+
else insertPreview.updatePreviews(step.output, $step('.insertDiv').getDomElem());
9597
});
9698
}
99+
97100

98101
$step('.insertDiv .close-insert-box').off('click').on('click', function(){
99102
toggleDiv($step);
@@ -102,21 +105,25 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
102105

103106
var insertStepSelect = $step('.insert-step-select');
104107
insertStepSelect.html('');
108+
105109
// Add modules to the insertStep dropdown
106110
for (var m in modulesInfo) {
107111
if (modulesInfo[m] && modulesInfo[m].name)
108112
insertStepSelect.append(
109113
'<option value="' + m + '">' + modulesInfo[m].name + '</option>'
110114
);
111115
}
116+
112117
insertStepSelect.selectize({
113118
sortField: 'text'
114119
});
120+
115121
$('.insertDiv .radio-group .radio').on('click', function () {
116122
var newStepName = $(this).attr('data-value');
117123
id = $($step('.insertDiv').parents()[3]).prevAll().length;
118124
insert(id, $step, newStepName);
119125
});
126+
120127
$step('.insertDiv .add-step-btn').on('click', function () {
121128
var newStepName = insertStepSelect.val();
122129
id = $($step('.insertDiv').parents()[3]).prevAll().length;

Diff for: test/ui/spec/insertPreview.spec.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ describe('Preview UI HTML', function() {
55
var insertPreview;
66
var options = { brightness: 50 };
77

8+
// TODO: Add rigorous tests with virtual DOM Nodes
9+
const DomNode = {};
10+
811
beforeEach(()=>{
912
insertPreview = InsertPreview;
1013

1114
spyOn(insertPreview, 'generatePreview');
1215
spyOn(insertPreview, 'updatePreviews');
1316

14-
insertPreview.generatePreview('brightness', options, 'src', 'selector');
15-
insertPreview.updatePreviews('src', 'selector');
17+
insertPreview.generatePreview('brightness', options, 'src', DomNode);
18+
insertPreview.updatePreviews('src', DomNode);
1619
});
1720

1821
it('generate preview ui', function() {
19-
expect(insertPreview.generatePreview).toHaveBeenCalledWith('brightness', options, 'src', 'selector');
22+
expect(insertPreview.generatePreview).toHaveBeenCalledWith('brightness', options, 'src', DomNode);
2023
});
2124

2225
it('update preview ui', function() {
23-
expect(insertPreview.updatePreviews).toHaveBeenCalledWith('src', 'selector');
26+
expect(insertPreview.updatePreviews).toHaveBeenCalledWith('src', DomNode);
2427
});
2528

2629
});

0 commit comments

Comments
 (0)