Skip to content

Commit

Permalink
Url params 🎉 (#276)
Browse files Browse the repository at this point in the history
* basic implementation

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* fix bug

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* fix everything

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* add test and refactor

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* update demo

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* fix comma issue

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* update test

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* now doing with native encode

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
  • Loading branch information
tech4GT authored and jywarren committed Jun 3, 2018
1 parent ac18320 commit 871453b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 104 deletions.
123 changes: 79 additions & 44 deletions dist/image-sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47528,23 +47528,23 @@ else {var isBrowser = false}
require('./util/getStep.js')

ImageSequencer = function ImageSequencer(options) {

var sequencer = (this.name == "ImageSequencer")?this:this.sequencer;
options = options || {};
options.inBrowser = options.inBrowser || isBrowser;
options.sequencerCounter = 0;

function objTypeOf(object){
return Object.prototype.toString.call(object).split(" ")[1].slice(0,-1)
}

function log(color,msg) {
if(options.ui!="none") {
if(arguments.length==1) console.log(arguments[0]);
else if(arguments.length==2) console.log(color,msg);
}
}

function copy(a) {
if (!typeof(a) == "object") return a;
if (objTypeOf(a) == "Array") return a.slice();
Expand All @@ -47557,11 +47557,11 @@ ImageSequencer = function ImageSequencer(options) {
}
return a;
}

function makeArray(input) {
return (objTypeOf(input)=="Array")?input:[input];
}

var image,
steps = [],
modules = require('./Modules'),
Expand All @@ -47570,27 +47570,27 @@ ImageSequencer = function ImageSequencer(options) {
inputlog = [],
events = require('./ui/UserInterface')(),
fs = require('fs');

// if in browser, prompt for an image
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);

function addSteps(){
var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
var args = (this.name == "ImageSequencer")?[]:[this.images];
var json_q = {};
for(var arg in arguments){args.push(copy(arguments[arg]));}
json_q = formatInput.call(this_,args,"+");

inputlog.push({method:"addSteps", json_q:copy(json_q)});

for (var i in json_q)
for (var j in json_q[i])
require("./AddStep")(this_,i,json_q[i][j].name,json_q[i][j].o);

return this;
}

function removeStep(image,index) {
//remove the step from images[image].steps and redraw remaining images
if(index>0) {
Expand All @@ -47600,16 +47600,16 @@ ImageSequencer = function ImageSequencer(options) {
}
//tell the UI a step has been removed
}

function removeSteps(image,index) {
var run = {}, indices;
var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
var args = (this.name == "ImageSequencer")?[]:[this.images];
for(var arg in arguments) args.push(copy(arguments[arg]));

var json_q = formatInput.call(this_,args,"-");
inputlog.push({method:"removeSteps", json_q:copy(json_q)});

for (var img in json_q) {
indices = json_q[img].sort(function(a,b){return b-a});
run[img] = indices[indices.length-1];
Expand All @@ -47619,16 +47619,16 @@ ImageSequencer = function ImageSequencer(options) {
// this.run(run); // This is creating problems
return this;
}

function insertSteps(image, index, name, o) {
var run = {};
var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
var args = (this.name == "ImageSequencer")?[]:[this.images];
for (var arg in arguments) args.push(arguments[arg]);

var json_q = formatInput.call(this_,args,"^");
inputlog.push({method:"insertSteps", json_q:copy(json_q)});

for (var img in json_q) {
var details = json_q[img];
details = details.sort(function(a,b){return b.index-a.index});
Expand All @@ -47639,39 +47639,39 @@ ImageSequencer = function ImageSequencer(options) {
// this.run(run); // This is Creating issues
return this;
}

function run(spinnerObj,t_image,t_from) {
let progressObj;
if(arguments[0] != 'test'){
progressObj = spinnerObj
delete arguments['0']
delete arguments['0']
}

var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
var args = (this.name == "ImageSequencer")?[]:[this.images];
for (var arg in arguments) args.push(copy(arguments[arg]));

var callback = function() {};
for (var arg in args)
if(objTypeOf(args[arg]) == "Function")
callback = args.splice(arg,1)[0];

var json_q = formatInput.call(this_,args,"r");

require('./Run')(this_, json_q, callback,progressObj);

return true;
}

function loadImages() {
var args = [];
var sequencer = this;
for (var arg in arguments) args.push(copy(arguments[arg]));
var json_q = formatInput.call(this,args,"l");

inputlog.push({method:"loadImages", json_q:copy(json_q)});
var loadedimages = this.copy(json_q.loadedimages);

var ret = {
name: "ImageSequencer Wrapper",
sequencer: this,
Expand All @@ -47683,7 +47683,7 @@ ImageSequencer = function ImageSequencer(options) {
setUI: this.setUI,
images: loadedimages
};

function load(i) {
if(i==loadedimages.length) {
json_q.callback.call(ret);
Expand All @@ -47694,24 +47694,24 @@ ImageSequencer = function ImageSequencer(options) {
load(++i);
});
}

load(0);
}

function replaceImage(selector,steps,options) {
options = options || {};
options.callback = options.callback || function() {};
return require('./ReplaceImage')(this,selector,steps,options);
}

function setUI(UI) {
this.events = require('./ui/UserInterface')(UI);
}

var exportBin = function(dir,basic) {
return require('./ExportBin')(dir,this,basic);
}

function modulesInfo(name) {
var modulesdata = {}
if(name == "load-image") return {};
Expand All @@ -47722,25 +47722,58 @@ ImageSequencer = function ImageSequencer(options) {
else modulesdata = modules[name][1];
return modulesdata;
}


// Strigifies the current sequence
function toString(step) {
if(step) {
return stepToString(step);
} else {
return copy(this.images.image1.steps).map(stepToString).slice(1).join(',');
}
}


// Stringifies one step of the sequence
function stepToString(step) {
let inputs = copy(modulesInfo(step.options.name).inputs);
inputs = inputs || {};

for(let input in inputs) inputs[input] = step.options[input] || inputs[input].default;


for(let input in inputs) {
inputs[input] = step.options[input] || inputs[input].default;
inputs[input] = encodeURIComponent(inputs[input]);
}

var configurations = Object.keys(inputs).map(key=>key + ':' + inputs[key]).join(',');
return `${step.options.name}(${configurations})`;
}


// Coverts stringified sequence into JSON
function importStringtoJson(str){
let steps = str.split('),');
steps.push(steps.splice(-1)[0].slice(0,-1));
return steps.map(importStringtoJsonStep);
}
// Converts one stringified step into JSON
function importStringtoJsonStep(str){
str = [
str.substr(0,str.indexOf('(')),
str.slice(str.indexOf('(')+1)
];

str[1] = str[1].split(',').reduce(function(acc,cur,i){
cur = [
cur.substr(0,cur.indexOf(':')),
decodeURIComponent(cur.substr(cur.indexOf(':') + 1))
];
if(!!cur[0]) acc[cur[0]] = cur[1];
return acc;
},{});

return {
name : str[0],
options:str[1]
}
}

return {
//literals and objects
name: "ImageSequencer",
Expand All @@ -47749,7 +47782,7 @@ ImageSequencer = function ImageSequencer(options) {
modules: modules,
images: images,
events: events,

//user functions
loadImages: loadImages,
loadImage: loadImages,
Expand All @@ -47763,15 +47796,17 @@ ImageSequencer = function ImageSequencer(options) {
modulesInfo: modulesInfo,
toString: toString,
stepToString: stepToString,

importStringtoJson: importStringtoJson,
importStringtoJsonStep: importStringtoJsonStep,

//other functions
log: log,
objTypeOf: objTypeOf,
copy: copy,

setInputStep: require('./ui/SetInputStep')(sequencer)
}

}
module.exports = ImageSequencer;

Expand Down
2 changes: 1 addition & 1 deletion dist/image-sequencer.min.js

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions examples/lib/defaultHtmlSequencerUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function DefaultHtmlSequencerUi(_sequencer, options) {
var hash = getUrlHashParameter("steps");

if (hash) {
var stepsFromHash = hash.split(",");
stepsFromHash.forEach(function eachStep(step) {
_sequencer.addSteps(step);
var stepsFromHash = _sequencer.importStringtoJson(hash);
stepsFromHash.forEach(function eachStep(stepObj) {
_sequencer.addSteps(stepObj.name,stepObj.options);
});
_sequencer.run();
}
setUrlHashParameter("steps", sequencer.toString());
}

function selectNewStepUi() {
Expand All @@ -31,23 +32,19 @@ function DefaultHtmlSequencerUi(_sequencer, options) {
var index = $(removeStepSel).index(this) + 1;
sequencer.removeSteps(index).run();
// remove from URL hash too
var urlHash = getUrlHashParameter("steps").split(",");
urlHash.splice(index - 1, 1);
setUrlHashParameter("steps", urlHash.join(","));
setUrlHashParameter("steps", sequencer.toString());
}

function addStepUi() {
if ($(addStepSel + " select").val() == "none") return;

// add to URL hash too
var hash = getUrlHashParameter("steps") || "";
if (hash != "") hash += ",";
setUrlHashParameter("steps", hash + $(addStepSel + " select").val());

var newStepName = $(addStepSel + " select").val();
_sequencer
.addSteps(newStepName, options)
.run(null);
.addSteps(newStepName, options)
.run(null);

// add to URL hash too
setUrlHashParameter("steps", _sequencer.toString());
}

return {
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/defaultHtmlStepUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ function DefaultHtmlStepUi(_sequencer, options) {
step.options[$(input).attr("name")] = input.value;
});
_sequencer.run();
// modify the url hash
setUrlHashParameter("steps", _sequencer.toString());
}

saveOptions();

// on clicking Save in the details pane of the step
$(step.ui.querySelector("div.details .btn-save")).click(saveOptions);
}
Expand Down
Loading

0 comments on commit 871453b

Please sign in to comment.