@dualbox/editor 1.0.35 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -103812,267 +103812,271 @@ var jsplumb_6 = jsplumb.jsPlumb;
103812
103812
  * Main class of the Graph Editor
103813
103813
  */
103814
103814
  class DualboxEditor {
103815
- // div: selector or jquery div
103816
- constructor(div, attrs) {
103817
- this.div = $(div);
103818
- this.div.addClass("main-editor-div");
103819
- this.attrs = attrs;
103820
-
103821
- // export itself top window
103822
- window.dualboxEditor = this;
103823
-
103824
- this.rootAppName = attrs.name ? attrs.name : "Application";
103825
-
103826
- // MVC model
103827
- this.m = new GraphModel(this);
103828
- this.v = new GraphView(this, div, attrs);
103829
- this.c = new GraphController(this);
103830
-
103831
- // bind links
103832
- this.v.m = this.c.m = this.m;
103833
- this.v.c = this.m.c = this.c;
103834
- this.c.v = this.m.v = this.v;
103835
-
103836
- // cache for packages
103837
- this.packages = {};
103838
- this.DualBox = null; // local dualbox, for editor use
103839
-
103840
- // the function to search for modules
103841
- // attr.search signature: search( text, cb )
103842
- // callback signature: cb( err, packages ), packages is an array of package.json
103843
- this.search = (text, type) => {
103844
- return new Promise((resolve, reject) => {
103845
- attrs.search(text, (err, packages) => {
103846
- if (err) {
103847
- reject(err);
103848
- } else {
103849
- // cache packages in this.packages
103850
- lodash.each(packages, r => {
103851
- if (!this.packages[r.name]) {
103852
- this.packages[r.name] = r;
103853
- }
103815
+ // div: selector or jquery div
103816
+ constructor(div, attrs) {
103817
+ this.div = $(div);
103818
+ this.div.addClass("main-editor-div");
103819
+ this.attrs = attrs;
103820
+
103821
+ // export itself top window
103822
+ window.dualboxEditor = this;
103823
+
103824
+ this.rootAppName = attrs.name ? attrs.name : "Application";
103825
+
103826
+ // MVC model
103827
+ this.m = new GraphModel(this);
103828
+ this.v = new GraphView(this, div, attrs);
103829
+ this.c = new GraphController(this);
103830
+
103831
+ // bind links
103832
+ this.v.m = this.c.m = this.m;
103833
+ this.v.c = this.m.c = this.c;
103834
+ this.c.v = this.m.v = this.v;
103835
+
103836
+ // cache for packages
103837
+ this.packages = {};
103838
+ this.DualBox = null; // local dualbox, for editor use
103839
+
103840
+ // the function to search for modules
103841
+ // attr.search signature: search( text, cb )
103842
+ // callback signature: cb( err, packages ), packages is an array of package.json
103843
+ this.search = (text, type) => {
103844
+ return new Promise((resolve, reject) => {
103845
+ attrs.search(text, (err, packages) => {
103846
+ if (err) {
103847
+ reject(err);
103848
+ } else {
103849
+ // cache packages in this.packages
103850
+ lodash.each(packages, r => {
103851
+ if (!this.packages[r.name]) {
103852
+ this.packages[r.name] = r;
103853
+ }
103854
+ });
103855
+
103856
+ if (type) {
103857
+ packages = lodash.filter(packages, o => {
103858
+ return (
103859
+ o.name.startsWith("@dualbox/dualbox-" + type) ||
103860
+ (type == "module" && o.name.startsWith("dualbox-core"))
103861
+ );
103862
+ });
103863
+ }
103864
+ resolve(packages);
103865
+ }
103866
+ });
103854
103867
  });
103868
+ };
103855
103869
 
103856
- if (type) {
103857
- packages = lodash.filter(packages, o => {
103858
- return (
103859
- o.name.startsWith("@dualbox/dualbox-" + type) ||
103860
- (type == "module" && o.name.startsWith("dualbox-core"))
103861
- );
103862
- });
103870
+ this.types = [];
103871
+ this.getRootTypes = function () {
103872
+ if (lodash.isEmpty(this.types)) {
103873
+ return new Promise((resolve, reject) => {
103874
+ this.search("dualbox-type-")
103875
+ .then(packages => {
103876
+ this.types = lodash.filter(packages, p => {
103877
+ var isType = p.name.startsWith("@dualbox/dualbox-type");
103878
+ var hasTypeDef = lodash.get(p, "dualbox.type");
103879
+
103880
+ if (isType && !hasTypeDef) {
103881
+ console.error(
103882
+ "Type package " +
103883
+ p.name +
103884
+ " has no dualbox Type description.\n" +
103885
+ "Type was not imported"
103886
+ );
103887
+ }
103888
+ return isType && hasTypeDef;
103889
+ });
103890
+ resolve(this.types);
103891
+ })
103892
+ .catch(e => {
103893
+ reject(e);
103894
+ });
103895
+ });
103896
+ } else {
103897
+ return Promise.resolve(this.types);
103863
103898
  }
103864
- resolve(packages);
103865
- }
103866
- });
103867
- });
103868
- };
103899
+ };
103869
103900
 
103870
- this.types = [];
103871
- this.getRootTypes = function() {
103872
- if (lodash.isEmpty(this.types)) {
103873
- return new Promise((resolve, reject) => {
103874
- this.search("dualbox-type-")
103875
- .then(packages => {
103876
- this.types = lodash.filter(packages, p => {
103877
- var isType = p.name.startsWith("@dualbox/dualbox-type");
103878
- var hasTypeDef = lodash.get(p, "dualbox.type");
103879
-
103880
- if (isType && !hasTypeDef) {
103881
- console.error(
103882
- "Type package " +
103883
- p.name +
103884
- " has no dualbox Type description.\n" +
103885
- "Type was not imported"
103886
- );
103887
- }
103888
- return isType && hasTypeDef;
103889
- });
103890
- resolve(this.types);
103891
- })
103892
- .catch(e => {
103893
- reject(e);
103894
- });
103895
- });
103896
- } else {
103897
- return Promise.resolve(this.types);
103898
- }
103899
- };
103901
+ this.getAvailableTypes = async function () {
103902
+ var types = await this.getRootTypes();
103903
+ return lodash.map(types, t => lodash.get(t, "dualbox.type.name"));
103904
+ };
103900
103905
 
103901
- this.getAvailableTypes = async function() {
103902
- var types = await this.getRootTypes();
103903
- return lodash.map(types, t => lodash.get(t, "dualbox.type.name"));
103904
- };
103906
+ // the function to find module by name and version (with cache)
103907
+ // signature: find( name, version ), return a Promise which, when resolved, is a package.json
103908
+ this.loadPackage = (name, version) => {
103909
+ version = version || "*";
103910
+
103911
+ return new Promise((resolve, reject) => {
103912
+ if (name.startsWith("dualbox-core")) {
103913
+ var pkg = null;
103914
+ lodash.each(this.DualBox.core, corePackage => {
103915
+ if (corePackage.name == name) {
103916
+ pkg = corePackage;
103917
+ return false;
103918
+ }
103919
+ });
103905
103920
 
103906
- // the function to find module by name and version (with cache)
103907
- // signature: find( name, version ), return a Promise which, when resolved, is a package.json
103908
- this.loadPackage = (name, version) => {
103909
- version = version || "*";
103921
+ if (pkg) {
103922
+ this.packages[name] = pkg;
103923
+ resolve(pkg);
103924
+ } else {
103925
+ reject("Couldn't resolve core package: " + name);
103926
+ }
103927
+ return;
103928
+ }
103910
103929
 
103911
- return new Promise((resolve, reject) => {
103912
- if (name.startsWith("dualbox-core")) {
103913
- var pkg = null;
103914
- lodash.each(this.DualBox.core, corePackage => {
103915
- if (corePackage.name == name) {
103916
- pkg = corePackage;
103917
- return false;
103918
- }
103919
- });
103930
+ var loadScript = (pkg, cb) => {
103931
+ if (attrs.load) {
103932
+ attrs.load(pkg.name, pkg.version, cb);
103933
+ } else {
103934
+ // Local editor. Bind the callback on the local script load
103935
+ var script = $('script[data-pkg="' + pkg.name + '"]');
103936
+ if (!script) {
103937
+ throw "Couldn't find script in the current document: " +
103938
+ pkg.name +
103939
+ ". Did you forget gulp link-editor ?";
103940
+ }
103920
103941
 
103921
- if (pkg) {
103922
- this.packages[name] = pkg;
103923
- resolve(pkg);
103924
- } else {
103925
- reject("Couldn't resolve core package: " + name);
103926
- }
103927
- return;
103928
- }
103942
+ if (script.attr("data-loaded") === "true") {
103943
+ cb();
103944
+ return;
103945
+ } else {
103946
+ script[0].addEventListener("error", function () {
103947
+ throw "Failed to load script " + pkg.name;
103948
+ });
103929
103949
 
103930
- var loadScript = (pkg, cb) => {
103931
- if (attrs.load) {
103932
- attrs.load(pkg.name, pkg.version, cb);
103933
- } else {
103934
- // Local editor. Bind the callback on the local script load
103935
- var script = $('script[data-pkg="' + pkg.name + '"]');
103936
- if (!script) {
103937
- throw "Couldn't find script in the current document: " +
103938
- pkg.name +
103939
- ". Did you forget gulp link-editor ?";
103940
- }
103950
+ var timeout = setTimeout(function () {
103951
+ cb("Failed to load " + pkg.name + " after 5 seconds");
103952
+ }, 5000);
103953
+ script[0].addEventListener("load", () => {
103954
+ clearTimeout(timeout);
103955
+ cb();
103956
+ });
103957
+ }
103958
+ }
103959
+ };
103941
103960
 
103942
- if (script.attr("data-loaded") === "true") {
103943
- cb();
103944
- return;
103945
- } else {
103946
- script[0].addEventListener("error", function() {
103947
- throw "Failed to load script " + pkg.name;
103948
- });
103949
-
103950
- var timeout = setTimeout(function() {
103951
- cb("Failed to load " + pkg.name + " after 5 seconds");
103952
- }, 5000);
103953
- script[0].addEventListener("load", () => {
103954
- clearTimeout(timeout);
103955
- cb();
103956
- });
103957
- }
103958
- }
103961
+ var onLoadedScript = err => {
103962
+ if (err) {
103963
+ reject(err);
103964
+ } else {
103965
+ window.require(name); // make sure the package is required at least once
103966
+ resolve(this.packages[name]);
103967
+ }
103968
+ };
103969
+
103970
+ // we need to find the package.json and make sure the script is loaded
103971
+ if (this.packages[name]) {
103972
+ try {
103973
+ loadScript(this.packages[name], onLoadedScript);
103974
+ } catch (e) {
103975
+ reject(e);
103976
+ }
103977
+ } else {
103978
+ // mark the package as beeing resolved
103979
+ attrs.find(name, version, (err, result) => {
103980
+ if (err) {
103981
+ reject(err);
103982
+ } else {
103983
+ this.packages[name] = result; // cache result
103984
+ loadScript(this.packages[name], onLoadedScript);
103985
+ }
103986
+ });
103987
+ }
103988
+ });
103959
103989
  };
103960
103990
 
103961
- var onLoadedScript = err => {
103962
- if (err) {
103963
- reject(err);
103964
- } else {
103965
- require(name); // make sure the package is required at least once
103966
- resolve(this.packages[name]);
103967
- }
103991
+ // the function to require a dualbox module
103992
+ this.require = (name, version, cb) => {
103993
+ return require(name); // TODO: local only for now
103968
103994
  };
103969
103995
 
103970
- // we need to find the package.json and make sure the script is loaded
103971
- if (this.packages[name]) {
103972
- try {
103973
- loadScript(this.packages[name], onLoadedScript);
103974
- } catch (e) {
103975
- reject(e);
103976
- }
103977
- } else {
103978
- // mark the package as beeing resolved
103979
- attrs.find(name, version, (err, result) => {
103980
- if (err) {
103981
- reject(err);
103982
- } else {
103983
- this.packages[name] = result; // cache result
103984
- loadScript(this.packages[name], onLoadedScript);
103985
- }
103986
- });
103996
+ // Safety when the user leave the page,
103997
+ // if not already defined externally
103998
+ if (!window.onbeforeunload) {
103999
+ window.onbeforeunload = function () {
104000
+ return "Unsaved changes will be lost, are you sure you want to leave the editor?";
104001
+ };
103987
104002
  }
103988
- });
103989
- };
103990
-
103991
- // the function to require a dualbox module
103992
- this.require = (name, version, cb) => {
103993
- return require(name); // TODO: local only for now
103994
- };
103995
-
103996
- // Safety when the user leave the page,
103997
- // if not already defined externally
103998
- if (!window.onbeforeunload) {
103999
- window.onbeforeunload = function() {
104000
- return "Unsaved changes will be lost, are you sure you want to leave the editor?";
104001
- };
104002
- }
104003
104003
 
104004
- this.loadCorePackages();
104004
+ this.loadCorePackages();
104005
104005
 
104006
- if (attrs.json) {
104007
- this.onReady(() => {
104008
- var p = this.setApp(attrs.json);
104009
- if (attrs.onLoaded) {
104010
- p.then(attrs.onLoaded);
104006
+ if (attrs.json) {
104007
+ this.onReady(() => {
104008
+ var p = this.setApp(attrs.json);
104009
+ if (attrs.onLoaded) {
104010
+ p.then(attrs.onLoaded);
104011
+ }
104012
+ });
104011
104013
  }
104012
- });
104013
104014
  }
104014
- }
104015
104015
 
104016
- // call cb when the editor is initialized
104017
- onReady(cb) {
104018
- this.v.onReady(cb);
104019
- }
104016
+ // call cb when the editor is initialized
104017
+ onReady(cb) {
104018
+ this.v.onReady(cb);
104019
+ }
104020
104020
 
104021
- async loadCorePackages() {
104021
+ async loadCorePackages() {
104022
104022
 
104023
- await this.loadPackage("@dualbox/dualbox");
104024
- this.DualBox = window.DualBox = this.require("@dualbox/dualbox");
104025
- lodash.each(this.DualBox.core, async corePackage => {
104026
- await this.loadPackage(corePackage.name);
104027
- });
104028
- }
104023
+ await this.loadPackage("@dualbox/dualbox");
104024
+ if (!window.DualBox && window.DualBox.start) {
104025
+ this.DualBox = window.DualBox = this.require("@dualbox/dualbox");
104026
+ } else {
104027
+ this.DualBox = window.DualBox;
104028
+ }
104029
+ lodash.each(this.DualBox.core, async corePackage => {
104030
+ await this.loadPackage(corePackage.name);
104031
+ });
104032
+ }
104029
104033
 
104030
- setApp(json) {
104031
- return this.c.load(json);
104032
- }
104034
+ setApp(json) {
104035
+ return this.c.load(json);
104036
+ }
104033
104037
 
104034
- getApp() {
104035
- this.m.get();
104036
- }
104038
+ getApp() {
104039
+ this.m.get();
104040
+ }
104037
104041
 
104038
- // load all packages needed in the json
104039
- // return a Promise
104040
- loadPackages(json) {
104041
- var promises = []; // array of promises
104042
- this.promises = promises;
104042
+ // load all packages needed in the json
104043
+ // return a Promise
104044
+ loadPackages(json) {
104045
+ var promises = []; // array of promises
104046
+ this.promises = promises;
104043
104047
 
104044
- var parser = new AppParser_1(json);
104045
- parser.eachComponent((name, version) => {
104046
- promises.push(this.loadPackage(name, version));
104047
- });
104048
+ var parser = new AppParser_1(json);
104049
+ parser.eachComponent((name, version) => {
104050
+ promises.push(this.loadPackage(name, version));
104051
+ });
104048
104052
 
104049
- /*
104050
- // also load core nodes
104051
- promises.push(this.loadPackage('dualbox-core-if', '*'));
104052
- promises.push(this.loadPackage('dualbox-core-value', '*'));
104053
- promises.push(this.loadPackage('dualbox-core-from-json', '*'));
104054
- promises.push(this.loadPackage('dualbox-core-to-json', '*'));
104055
- promises.push(this.loadPackage('dualbox-core-script', '*'));
104056
- promises.push(this.loadPackage('dualbox-core-switch', '*'));
104057
- */
104053
+ /*
104054
+ // also load core nodes
104055
+ promises.push(this.loadPackage('dualbox-core-if', '*'));
104056
+ promises.push(this.loadPackage('dualbox-core-value', '*'));
104057
+ promises.push(this.loadPackage('dualbox-core-from-json', '*'));
104058
+ promises.push(this.loadPackage('dualbox-core-to-json', '*'));
104059
+ promises.push(this.loadPackage('dualbox-core-script', '*'));
104060
+ promises.push(this.loadPackage('dualbox-core-switch', '*'));
104061
+ */
104058
104062
 
104059
- return Promise.all(promises);
104060
- }
104063
+ return Promise.all(promises);
104064
+ }
104061
104065
 
104062
- // sync version of this.loadPackage. Will crash if the package has not been loaded yet
104063
- getPackage(name, version) {
104064
- if (this.packages[name] === undefined) {
104065
- throw "Package " + name + " has not been loaded yet.";
104066
+ // sync version of this.loadPackage. Will crash if the package has not been loaded yet
104067
+ getPackage(name, version) {
104068
+ if (this.packages[name] === undefined) {
104069
+ throw "Package " + name + " has not been loaded yet.";
104070
+ }
104071
+ return this.packages[name];
104066
104072
  }
104067
- return this.packages[name];
104068
- }
104069
104073
  }
104070
104074
 
104071
104075
  // try to export in window if in browser context
104072
104076
  try {
104073
- window.DualboxEditor = DualboxEditor;
104077
+ window.DualboxEditor = DualboxEditor;
104074
104078
  } catch (e) {
104075
- console.log("Could not export to window: " + e);
104079
+ console.log("Could not export to window: " + e);
104076
104080
  }
104077
104081
 
104078
104082
  //export default DualboxEditor;