Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GCI] Standardised UI code comments #1345

Merged
merged 14 commits into from
Jan 1, 2020
61 changes: 34 additions & 27 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var defaultHtmlSequencerUi = require('./lib/defaultHtmlSequencerUi.js'),
insertPreview = require('./lib/insertPreview.js');

window.onload = function () {
sequencer = ImageSequencer();
sequencer = ImageSequencer(); // Set the global sequencer variable.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved

function refreshOptions() {
// Load information of all modules (Name, Inputs, Outputs)
// Load information (Name, Inputs, Outputs) of all modules.
var modulesInfo = sequencer.modulesInfo();

var addStepSelect = $('#addStep select');
addStepSelect.html('');

// Add modules to the addStep dropdown
// Add modules to the addStep dropdown.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Add modules to the addStep dropdown.
// Add modules to the addStep dropdown

for (var m in modulesInfo) {
if (modulesInfo[m] && modulesInfo[m].name)
addStepSelect.append(
Expand All @@ -32,6 +32,9 @@ window.onload = function () {

$(window).on('scroll', scrollFunction);

/**
* @description Method to toggle the scroll-up arro
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
*/
function scrollFunction() {
var shouldDisplay = $('body').scrollTop() > 20 || $(':root').scrollTop() > 20;

Expand All @@ -41,6 +44,9 @@ window.onload = function () {
}


/**
* @description Method to scroll to the top of the page.
*/
function topFunction() {
$('body').animate({scrollTop: 0});
$(':root').animate({scrollTop: 0});
Expand All @@ -55,7 +61,7 @@ window.onload = function () {
// UI for the overall demo:
var ui = defaultHtmlSequencerUi(sequencer);

// find any `src` parameters in URL hash and attempt to source image from them and run the sequencer
// Load image data from URL `src` parameter.
SidharthBansal marked this conversation as resolved.
Show resolved Hide resolved
if (urlHash.getUrlHashParameter('src')) {
sequencer.loadImage(urlHash.getUrlHashParameter('src'), ui.onLoad);
} else {
Expand All @@ -72,18 +78,23 @@ window.onload = function () {
$('#addStep #add-step-btn').on('click', ui.addStepUi);
$('#resetButton').on('click', resetSequence);

//Module button radio selection
// Module Selector quick buttons click handler.
$('.radio-group .radio').on('click', function () {
$(this).parent().find('.radio').removeClass('selected');
$(this).addClass('selected');
newStep = $(this).attr('data-value');
//$("#addStep option[value=" + newStep + "]").attr('selected', 'selected');

$('#addStep select').val(newStep);
ui.selectNewStepUi(newStep);
ui.addStepUi(newStep);
$(this).removeClass('selected');
});

/**
* @method displayMessageOnSaveSequence
* @description When a sequence is saved to a browser, notification is displayed.
* @returns {Null}
*/
function displayMessageOnSaveSequence() {
$('.savesequencemsg').fadeIn();
setTimeout(function () {
Expand All @@ -103,7 +114,7 @@ window.onload = function () {
}
}
$('#saveButton').on('click', function () {
// different handlers triggered for different dropdown options
// Different handlers triggered for different dropdown options.

let dropDownValue = $('#selectSaveOption option:selected').val();

Expand All @@ -120,9 +131,8 @@ window.onload = function () {

let isWorkingOnGifGeneration = false;

$('.js-view-as-gif').on('click', function (event) {
/* Prevent user from triggering generation multiple times*/
if (isWorkingOnGifGeneration) return;
$('.js-view-as-gif').on('click', function (event) { // GIF generation and display.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (isWorkingOnGifGeneration) return; // Prevent multiple button clicks.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved

isWorkingOnGifGeneration = true;

Expand All @@ -131,12 +141,12 @@ window.onload = function () {
button.innerHTML = '<i class="fa fa-circle-o-notch fa-spin"></i>';

try {
/* Get gif resources of previous steps */
// Get GIF resources from previous steps.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
let options = getGifResources();

gifshot.createGIF(options, function (obj) { // gif generation
gifshot.createGIF(options, function (obj) { // GIF generation.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (!obj.error) {
// Final gif encoded with base64 format
// Final gif encoded with base64 format.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
var image = obj.image;
var animatedImage = document.createElement('img');

Expand All @@ -146,22 +156,19 @@ window.onload = function () {
let modal = $('#js-download-gif-modal');

$('#js-download-as-gif-button').one('click', function () {
// Trigger download
downloadGif(image);
// Close modal
downloadGif(image); // Trigger GIF download.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
modal.modal('hide');
});

var gifContainer = document.getElementById('js-download-modal-gif-container');

// Clear previous results
// Clear previous results.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
gifContainer.innerHTML = '';

// Insert image
// Insert image.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
gifContainer.appendChild(animatedImage);


// Open modal
// Open modal.
modal.modal();

button.disabled = false;
Expand All @@ -179,16 +186,16 @@ window.onload = function () {
});

function getGifResources() {
/* Returns an object with specific gif options */
// Returns an object with specific gif options.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
let imgs = document.getElementsByClassName('step-thumbnail');
var imgSrcs = [];

// Pushes image sources of all the modules in dom
// Pushes image sources of all the modules in the DOM.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < imgs.length; i++) {
imgSrcs.push(imgs[i].src);
}

var options = { // gif frame options
var options = { // GIF frame options.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
'gifWidth': imgs[0].width,
'gifHeight': imgs[0].height,
'images': imgSrcs,
Expand All @@ -208,10 +215,10 @@ window.onload = function () {
}

function downloadGif(image) {
download(image, 'index.gif', 'image/gif');// downloadjs library function
download(image, 'index.gif', 'image/gif'); // Downloadjs library function.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
}

// image selection and drag/drop handling from examples/lib/imageSelection.js
// Image selection and drag/drop handling from examples/lib/imageSelection.js
sequencer.setInputStep({
dropZoneSelector: '#dropzone',
fileInputSelector: '#fileInput',
Expand Down Expand Up @@ -244,9 +251,9 @@ window.onload = function () {

setupCache();

if (urlHash.getUrlHashParameter('src')) {
if (urlHash.getUrlHashParameter('src')) { // Gets the sequence from the URL.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
insertPreview.updatePreviews(urlHash.getUrlHashParameter('src'), '#addStep');
} else {
insertPreview.updatePreviews('images/tulips.png', '#addStep');
}
};
};
64 changes: 42 additions & 22 deletions examples/lib/defaultHtmlStepUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const intermediateHtmlStepUi = require('./intermediateHtmlStepUi.js'),

function DefaultHtmlStepUi(_sequencer, options) {
let $step, $stepAll;

options = options || {};
var stepsEl = options.stepsEl || document.querySelector('#steps');
var selectStepSel = options.selectStepSel = options.selectStepSel || '#selectStep';
Expand All @@ -26,7 +25,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
if (step.options && step.options.description)
step.description = step.options.description;

step.ui =
step.ui = // Basic UI markup for the step.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
'\
<div class="container-fluid step-container">\
<div class="panel panel-default">\
Expand Down Expand Up @@ -68,21 +67,21 @@ function DefaultHtmlStepUi(_sequencer, options) {
var util = intermediateHtmlStepUi(_sequencer, step);

var parser = new DOMParser();
step.ui = parser.parseFromString(step.ui, 'text/html');
step.ui = parser.parseFromString(step.ui, 'text/html'); // Convert the markup string to a DOM node.
step.ui = step.ui.querySelector('div.container-fluid');

$step = scopeQuery.scopeSelector(step.ui);
$step = scopeQuery.scopeSelector(step.ui); // Shorthand methods for scoped DOM queries. Read the docs [CONTRIBUTING.md](https://github.com/publiclab/image-sequencer/blob/main/CONTRIBUTING.md) for more info.
$stepAll = scopeQuery.scopeSelectorAll(step.ui);
step.ui.$step = $step;
step.ui.$stepAll = $stepAll;

step.linkElements = step.ui.querySelectorAll('a');
step.imgElement = $step('a img.img-thumbnail')[0];
step.linkElements = step.ui.querySelectorAll('a'); // All the anchor tags in the step UI.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
step.imgElement = $step('a img.img-thumbnail')[0]; // The output image.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved

if (_sequencer.modulesInfo().hasOwnProperty(step.name)) {
var inputs = _sequencer.modulesInfo(step.name).inputs;
var outputs = _sequencer.modulesInfo(step.name).outputs;
var merged = Object.assign(inputs, outputs); // combine outputs w inputs
var merged = Object.assign(inputs, outputs); // Combine outputs with inputs.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved

for (var paramName in merged) {
var isInput = inputs.hasOwnProperty(paramName);
Expand All @@ -102,7 +101,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
else {
let paramVal = step.options[paramName] || inputDesc.default;

if (inputDesc.id == 'color-picker') { // separate input field for color-picker
if (inputDesc.id == 'color-picker') { // Separate input field for color-picker.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
html +=
'<div id="color-picker" class="input-group colorpicker-component">' +
'<input class="form-control target" type="' +
Expand All @@ -113,7 +112,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
paramVal + '">' + '<span class="input-group-addon"><i></i></span>' +
'</div>';
}
else { // use this if the the field isn't color-picker
else { // Non color-picker input types.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
html =
'<input class="form-control target" type="' +
inputDesc.type +
Expand Down Expand Up @@ -155,10 +154,10 @@ function DefaultHtmlStepUi(_sequencer, options) {
</div>';
$step('div.details').append(div);
}
$step('div.panel-footer').append(
$step('div.panel-footer').append( // Save button.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
'<div class="cal collapse in"><button type="submit" class="btn btn-sm btn-default btn-save" disabled = "true" >Apply</button> <small style="padding-top:2px;">Press apply to see changes</small></div>'
);
$step('div.panel-footer').prepend(
$step('div.panel-footer').prepend( // Markup for tools: download and insert step buttons.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
'<button class="pull-right btn btn-default btn-sm insert-step" >\
<span class="insert-text"><i class="fa fa-plus"></i> Insert Step</span><span class="no-insert-text" style="display:none">Close</span></button>\
<button class="pull-right btn btn-default btn-sm download-btn" style="margin-right:2px" >\
Expand All @@ -173,9 +172,9 @@ function DefaultHtmlStepUi(_sequencer, options) {
parser.parseFromString(tools, 'text/html').querySelector('div')
);

$stepAll('.remove').on('click', function() {notify('Step Removed', 'remove-notification');});
$stepAll('.insert-step').on('click', function() { util.insertStep(step.ID); });
// Insert the step's UI in the right place
$stepAll('.remove').on('click', function() {notify('Step Removed', 'remove-notification');}); // Notification on removal of a step.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
$stepAll('.insert-step').on('click', function() { util.insertStep(step.ID); }); // Insert a step in between the sequence.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved

if (stepOptions.index == _sequencer.steps.length) {
stepsEl.appendChild(step.ui);
$('#steps .step-container:nth-last-child(1) .insert-step').prop('disabled', true);
Expand All @@ -187,14 +186,14 @@ function DefaultHtmlStepUi(_sequencer, options) {
}
}
else {
$('#load-image').append(step.ui);
$('#load-image').append(step.ui); // Default UI without extra tools for the first step(load image).
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
}
$step('.toggle').on('click', () => {
$step('.toggle').on('click', () => { // Step container dropdown.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
$step('.toggleIcon').toggleClass('rotated');
$stepAll('.cal').collapse('toggle');
});

$(step.imgElement).on('mousemove', _.debounce(() => imageHover(step), 150));
$(step.imgElement).on('mousemove', _.debounce(() => imageHover(step), 150)); // Shows the pixel coordinates on hover.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
$(step.imgElement).on('click', (e) => {e.preventDefault(); });
$stepAll('#color-picker').colorpicker();

Expand All @@ -211,15 +210,23 @@ function DefaultHtmlStepUi(_sequencer, options) {
});
_sequencer.run({ index: step.index - 1 });

// modify the url hash
// Modify the URL hash.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
urlHash.setUrlHashParameter('steps', _sequencer.toString());
// disable the save button
// Disable the save button.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
$step('.btn-save').prop('disabled', true);
optionsChanged = false;
changedInputs = 0;
}
}

/**
* @method handleInputValueChange
* @description Enables the save button on input change.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} currentValue The current value of the input.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} initValue The initial/old value of the input.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {Boolean} hasChangedBefore Whether the input was changed before.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Boolean} True if the value has changed.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
*/
function handleInputValueChange(currentValue, initValue, hasChangedBefore) {
var inputChanged = !(isNaN(initValue) || isNaN(currentValue) ? currentValue === initValue : currentValue - initValue === 0);
changedInputs += hasChangedBefore ? inputChanged ? 0 : -1 : inputChanged ? 1 : 0;
Expand Down Expand Up @@ -276,7 +283,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
step.linkElements[index].href = step.imgElement.src;
}

// TODO: use a generalized version of this
// TODO: use a generalized version of this.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK. Whoever added the TODO must know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did! I meant that we might use a standard lib to determine file extensions, rather then a one-off function I made up 😄

function fileExtension(output) {
return output.split('/')[1].split(';')[0];
}
Expand All @@ -297,7 +304,7 @@ function DefaultHtmlStepUi(_sequencer, options) {
}
});

// fill inputs with stored step options
// Fill inputs with stored step options.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (_sequencer.modulesInfo().hasOwnProperty(step.name)) {
var inputs = _sequencer.modulesInfo(step.name).inputs;
var outputs = _sequencer.modulesInfo(step.name).outputs;
Expand Down Expand Up @@ -328,6 +335,12 @@ function DefaultHtmlStepUi(_sequencer, options) {
});
}

/**
* @method imageHover
* @description Handler to display image coordinates on hover.
* @param {Object} step Current step variable.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Null}
*/
function imageHover(step){

var img = $(step.imgElement);
Expand Down Expand Up @@ -357,6 +370,13 @@ function DefaultHtmlStepUi(_sequencer, options) {
return step.imgElement;
}

/**
* @method notify
* @description General purpose DOM toast notification.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} msg Message to be displayed.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} id A unique identifier for the notification.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Null}
*/
function notify(msg, id){
if ($('#' + id).length == 0) {
var notification = document.createElement('span');
Expand Down Expand Up @@ -388,4 +408,4 @@ if(typeof window === 'undefined'){
};
}

module.exports = DefaultHtmlStepUi;
module.exports = DefaultHtmlStepUi;
Loading