[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
This commit is contained in:
Harsh Khandeparkar
2019-12-16 18:42:15 +00:00
committed by Jeffrey Warren
parent 426733f625
commit 4a86abbafd
6 changed files with 78 additions and 43 deletions

View File

@@ -313,4 +313,10 @@ a.name-header{
bottom: 7px; bottom: 7px;
font-size: 16px; font-size: 16px;
color: #444; color: #444;
}
/* Non float rightward alignment*/
.right {
margin-left: auto;
display: block;
} }

View File

@@ -9,24 +9,24 @@ window.onload = function () {
sequencer = ImageSequencer(); sequencer = ImageSequencer();
options = { options = {
sortField: 'text', sortField: 'text',
openOnFocus: false, openOnFocus: false,
onInitialize: function () { onInitialize: function () {
this.$control.on("click", () => { this.$control.on('click', () => {
this.ignoreFocusOpen = true; this.ignoreFocusOpen = true;
setTimeout(() => { setTimeout(() => {
// Trigger onFocus and open dropdown. // Trigger onFocus and open dropdown.
this.ignoreFocusOpen = false; this.ignoreFocusOpen = false;
}, 50); }, 50);
}); });
}, },
// Open dropdown after timeout of onClick. // Open dropdown after timeout of onClick.
onFocus: function () { onFocus: function () {
if (!this.ignoreFocusOpen) { if (!this.ignoreFocusOpen) {
this.open(); this.open();
}
} }
} }
};
function refreshOptions(options) { function refreshOptions(options) {
// Default options if parameter is empty. // Default options if parameter is empty.
@@ -239,8 +239,8 @@ window.onload = function () {
function getLastImage() { function getLastImage() {
// Get the image from the last step. // Get the image from the last step.
let imgs = document.getElementsByClassName('step-thumbnail'); let imgs = document.getElementsByClassName('step-thumbnail');
let lastStepImage = imgs[imgs.length-1]; let lastStepImage = imgs[imgs.length - 1];
return lastStepImage.getAttribute("src"); return lastStepImage.getAttribute('src');
} }
/** /**
@@ -255,8 +255,8 @@ window.onload = function () {
// Create a new pdf with the same dimensions as the image. // Create a new pdf with the same dimensions as the image.
const pdf = new jsPDF({ const pdf = new jsPDF({
orientation: pageHeight > pageWidth ? "portrait": "landscape", orientation: pageHeight > pageWidth ? 'portrait' : 'landscape',
unit: "px", unit: 'px',
format: [pageHeight, pageWidth] format: [pageHeight, pageWidth]
}); });
@@ -264,7 +264,7 @@ window.onload = function () {
pdf.addImage(imageDataURL, 0, 0, pdf.internal.pageSize.getWidth(), pdf.internal.pageSize.getHeight()); pdf.addImage(imageDataURL, 0, 0, pdf.internal.pageSize.getWidth(), pdf.internal.pageSize.getHeight());
// Save the pdf with the filename specified here: // Save the pdf with the filename specified here:
pdf.save("index.pdf"); pdf.save('index.pdf');
}); });
} }
@@ -301,8 +301,8 @@ window.onload = function () {
step.options.step.imgElement.src = reader.result; step.options.step.imgElement.src = reader.result;
else else
step.imgElement.src = reader.result; step.imgElement.src = reader.result;
insertPreview.updatePreviews(reader.result, '#addStep');
insertPreview.updatePreviews(sequencer.steps[0].imgElement.src, '.insertDiv'); insertPreview.updatePreviews(reader.result, document.querySelector('#addStep'));
}, },
onTakePhoto: function (url) { onTakePhoto: function (url) {
var step = sequencer.steps[0]; var step = sequencer.steps[0];
@@ -312,16 +312,15 @@ window.onload = function () {
step.options.step.imgElement.src = url; step.options.step.imgElement.src = url;
else else
step.imgElement.src = url; step.imgElement.src = url;
insertPreview.updatePreviews(url, '#addStep'); insertPreview.updatePreviews(url, document.querySelector('#addStep'));
insertPreview.updatePreviews(sequencer.steps[0].imgElement.src, '.insertDiv');
} }
}); });
setupCache(); setupCache();
if (urlHash.getUrlHashParameter('src')) { if (urlHash.getUrlHashParameter('src')) {
insertPreview.updatePreviews(urlHash.getUrlHashParameter('src'), '#addStep'); insertPreview.updatePreviews(urlHash.getUrlHashParameter('src'), document.querySelector('#addStep'));
} else { } else {
insertPreview.updatePreviews('images/tulips.png', '#addStep'); insertPreview.updatePreviews('images/tulips.png', document.querySelector('#addStep'));
} }
}; };

View File

@@ -20,7 +20,6 @@ function DefaultHtmlStepUi(_sequencer, options) {
var selectStepSel = options.selectStepSel = options.selectStepSel || '#selectStep'; var selectStepSel = options.selectStepSel = options.selectStepSel || '#selectStep';
function onSetup(step, stepOptions) { function onSetup(step, stepOptions) {
if (step.options && step.options.description) if (step.options && step.options.description)
step.description = step.options.description; step.description = step.options.description;
@@ -169,7 +168,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
); );
$stepAll('.remove').on('click', function() {notify('Step Removed', 'remove-notification');}); $stepAll('.remove').on('click', function() {notify('Step Removed', 'remove-notification');});
$stepAll('.insert-step').on('click', function() { util.insertStep(step.ID); }); $step('.insert-step').on('click', function() { util.insertStep(step.ID); });
// Insert the step's UI in the right place // Insert the step's UI in the right place
if (stepOptions.index == _sequencer.steps.length) { if (stepOptions.index == _sequencer.steps.length) {
stepsEl.appendChild(step.ui); stepsEl.appendChild(step.ui);
@@ -180,9 +179,24 @@ function DefaultHtmlStepUi(_sequencer, options) {
else { else {
stepsEl.insertBefore(step.ui, $(stepsEl).children()[stepOptions.index]); stepsEl.insertBefore(step.ui, $(stepsEl).children()[stepOptions.index]);
} }
// Enable the load-image insert-step button when there are steps after load-image
// 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.
if (_sequencer.steps.length > 0) $('#load-image .insert-step').prop('disabled', false);
else $('#load-image .insert-step').prop('disabled', true);
} }
else { else {
$('#load-image').append(step.ui); $('#load-image').append(step.ui);
$step('div.panel-footer').prepend( `
<button class="right btn btn-default btn-sm insert-step" disabled="true">
<span class="insert-text"><i class="fa fa-plus"></i> Insert Step</span>
<span class="no-insert-text" style="display:none">Close</span>
</button>`
);
$step('.insert-step').on('click', function() { util.insertStep(step.ID); });
} }
$step('.toggle').on('click', () => { $step('.toggle').on('click', () => {
$step('.toggleIcon').toggleClass('rotated'); $step('.toggleIcon').toggleClass('rotated');
@@ -346,6 +360,12 @@ function DefaultHtmlStepUi(_sequencer, options) {
function onRemove(step) { function onRemove(step) {
step.ui.remove(); step.ui.remove();
$('#steps .step-container:nth-last-child(1) .insert-step').prop('disabled', true); $('#steps .step-container:nth-last-child(1) .insert-step').prop('disabled', true);
// Enable the load-image insert-step button when there are steps after load-image
// 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.
if (_sequencer.steps.length - 1 > 1) $('#load-image .insert-step').prop('disabled', false);
else $('#load-image .insert-step').prop('disabled', true);
$('div[class*=imgareaselect-]').remove(); $('div[class*=imgareaselect-]').remove();
} }

View File

@@ -1,5 +1,4 @@
function generatePreview(previewStepName, customValues, path, selector) { function generatePreview(previewStepName, customValues, path, DomNode) {
var previewSequencer = ImageSequencer(); var previewSequencer = ImageSequencer();
function insertPreview(src) { function insertPreview(src) {
var img = document.createElement('img'); var img = document.createElement('img');
@@ -8,9 +7,9 @@ function generatePreview(previewStepName, customValues, path, selector) {
img.src = src; img.src = src;
$(img).css('max-width', '200%'); $(img).css('max-width', '200%');
$(img).css('transform', 'translateX(-20%)'); $(img).css('transform', 'translateX(-20%)');
$(selector + ' .radio-group').find('div').each(function() { $(DomNode.querySelector('.radio-group')).find('.radio').each(function() {
if ($(this).find('div').attr('data-value') === previewStepName) { if ($(this).attr('data-value') === previewStepName) {
$(this).find('div').append(img); $(this).append(img);
} }
}); });
} }
@@ -29,8 +28,8 @@ function generatePreview(previewStepName, customValues, path, selector) {
previewSequencer.loadImage(path, loadPreview); previewSequencer.loadImage(path, loadPreview);
} }
function updatePreviews(src, selector) { function updatePreviews(src, DomNode) {
$(selector + ' img').remove(); $(DomNode).find('img').remove();
var previewSequencerSteps = { var previewSequencerSteps = {
'resize': '125%', 'resize': '125%',
@@ -48,6 +47,7 @@ function updatePreviews(src, selector) {
}; };
var img = new Image(); var img = new Image();
img.onload = function(){ img.onload = function(){
var height = img.height; var height = img.height;
var width = img.width; var width = img.width;
@@ -62,7 +62,7 @@ function updatePreviews(src, selector) {
this.addSteps('resize', {['resize']: percentage + '%'}); this.addSteps('resize', {['resize']: percentage + '%'});
this.run((src)=>{ this.run((src)=>{
Object.keys(previewSequencerSteps).forEach(function (step, index) { Object.keys(previewSequencerSteps).forEach(function (step, index) {
generatePreview(step, Object.values(previewSequencerSteps)[index], src, selector); generatePreview(step, Object.values(previewSequencerSteps)[index], src, DomNode);
}); });
}); });
}); });

View File

@@ -81,6 +81,7 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
var parser = new DOMParser(); var parser = new DOMParser();
var addStepUI = stepUI(); var addStepUI = stepUI();
addStepUI = parser.parseFromString(addStepUI, 'text/html').querySelector('div'); addStepUI = parser.parseFromString(addStepUI, 'text/html').querySelector('div');
if ($step('.insertDiv').length > 0){ if ($step('.insertDiv').length > 0){
toggleDiv($step); toggleDiv($step);
} }
@@ -91,9 +92,11 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
addStepUI addStepUI
); );
toggleDiv($step, function(){ toggleDiv($step, function(){
insertPreview.updatePreviews(step.output, '.insertDiv'); if (step.name === 'load-image') insertPreview.updatePreviews(step.output.src, $step('.insertDiv').getDomElem());
else insertPreview.updatePreviews(step.output, $step('.insertDiv').getDomElem());
}); });
} }
$step('.insertDiv .close-insert-box').off('click').on('click', function(){ $step('.insertDiv .close-insert-box').off('click').on('click', function(){
toggleDiv($step); toggleDiv($step);
@@ -102,6 +105,7 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
var insertStepSelect = $step('.insert-step-select'); var insertStepSelect = $step('.insert-step-select');
insertStepSelect.html(''); insertStepSelect.html('');
// Add modules to the insertStep dropdown // Add modules to the insertStep dropdown
for (var m in modulesInfo) { for (var m in modulesInfo) {
if (modulesInfo[m] && modulesInfo[m].name) if (modulesInfo[m] && modulesInfo[m].name)
@@ -109,14 +113,17 @@ function IntermediateHtmlStepUi(_sequencer, step, options) {
'<option value="' + m + '">' + modulesInfo[m].name + '</option>' '<option value="' + m + '">' + modulesInfo[m].name + '</option>'
); );
} }
insertStepSelect.selectize({ insertStepSelect.selectize({
sortField: 'text' sortField: 'text'
}); });
$('.insertDiv .radio-group .radio').on('click', function () { $('.insertDiv .radio-group .radio').on('click', function () {
var newStepName = $(this).attr('data-value'); var newStepName = $(this).attr('data-value');
id = $($step('.insertDiv').parents()[3]).prevAll().length; id = $($step('.insertDiv').parents()[3]).prevAll().length;
insert(id, $step, newStepName); insert(id, $step, newStepName);
}); });
$step('.insertDiv .add-step-btn').on('click', function () { $step('.insertDiv .add-step-btn').on('click', function () {
var newStepName = insertStepSelect.val(); var newStepName = insertStepSelect.val();
id = $($step('.insertDiv').parents()[3]).prevAll().length; id = $($step('.insertDiv').parents()[3]).prevAll().length;

View File

@@ -5,22 +5,25 @@ describe('Preview UI HTML', function() {
var insertPreview; var insertPreview;
var options = { brightness: 50 }; var options = { brightness: 50 };
// TODO: Add rigorous tests with virtual DOM Nodes
const DomNode = {};
beforeEach(()=>{ beforeEach(()=>{
insertPreview = InsertPreview; insertPreview = InsertPreview;
spyOn(insertPreview, 'generatePreview'); spyOn(insertPreview, 'generatePreview');
spyOn(insertPreview, 'updatePreviews'); spyOn(insertPreview, 'updatePreviews');
insertPreview.generatePreview('brightness', options, 'src', 'selector'); insertPreview.generatePreview('brightness', options, 'src', DomNode);
insertPreview.updatePreviews('src', 'selector'); insertPreview.updatePreviews('src', DomNode);
}); });
it('generate preview ui', function() { it('generate preview ui', function() {
expect(insertPreview.generatePreview).toHaveBeenCalledWith('brightness', options, 'src', 'selector'); expect(insertPreview.generatePreview).toHaveBeenCalledWith('brightness', options, 'src', DomNode);
}); });
it('update preview ui', function() { it('update preview ui', function() {
expect(insertPreview.updatePreviews).toHaveBeenCalledWith('src', 'selector'); expect(insertPreview.updatePreviews).toHaveBeenCalledWith('src', DomNode);
}); });
}); });