uil 4.0.5 → 4.0.7

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.
@@ -1698,6 +1698,238 @@ T.setText();
1698
1698
 
1699
1699
  const Tools = T;
1700
1700
 
1701
+ ///https://wicg.github.io/file-system-access/#api-filesystemfilehandle-getfile
1702
+
1703
+
1704
+ class Files {
1705
+
1706
+ //-----------------------------
1707
+ // FILE TYPE
1708
+ //-----------------------------
1709
+
1710
+ static autoTypes( type ) {
1711
+
1712
+ let t = [];
1713
+
1714
+ switch( type ){
1715
+ case 'json':
1716
+ t = [ { accept: { 'image/svg+xml': '.svg'} }, ];
1717
+ break;
1718
+ case 'text':
1719
+ t = [ { description: 'Text Files', accept: { 'text/plain': ['.txt', '.text'], 'text/html': ['.html', '.htm'] } }, ];
1720
+ break;
1721
+ case 'json':
1722
+ t = [ { description: 'JSON Files', accept: { 'text/plain': ['.json'] } }, ];
1723
+ break;
1724
+ case 'image':
1725
+ t = [ { description: 'Images', accept: { 'image/*': ['.png', '.gif', '.jpeg', '.jpg'] } }, ];
1726
+ break;
1727
+
1728
+ }
1729
+
1730
+ return t
1731
+
1732
+ }
1733
+
1734
+
1735
+ //-----------------------------
1736
+ // LOAD
1737
+ //-----------------------------
1738
+
1739
+ static async load( o = {} ) {
1740
+
1741
+ if (typeof window.showOpenFilePicker !== 'function') {
1742
+ window.showOpenFilePicker = this.showOpenFilePickerPolyfill;
1743
+ }
1744
+
1745
+ try {
1746
+
1747
+ let type = o.type || '';
1748
+
1749
+ const options = {
1750
+ excludeAcceptAllOption: type ? true : false,
1751
+ multiple: false,
1752
+ //startIn:'./assets'
1753
+ };
1754
+
1755
+ options.types = this.autoTypes( type );
1756
+
1757
+ // create a new handle
1758
+ const handle = await window.showOpenFilePicker( options );
1759
+ const file = await handle[0].getFile();
1760
+ //let content = await file.text()
1761
+
1762
+ if( !file ) return null
1763
+
1764
+ let fname = file.name;
1765
+ let ftype = fname.substring( fname.lastIndexOf('.')+1, fname.length );
1766
+
1767
+ const dataUrl = [ 'png', 'jpg', 'jpeg', 'mp4', 'webm', 'ogg', 'mp3' ];
1768
+ const dataBuf = [ 'sea', 'z', 'hex', 'bvh', 'BVH', 'glb', 'gltf' ];
1769
+ const reader = new FileReader();
1770
+
1771
+ if( dataUrl.indexOf( ftype ) !== -1 ) reader.readAsDataURL( file );
1772
+ else if( dataBuf.indexOf( ftype ) !== -1 ) reader.readAsArrayBuffer( file );
1773
+ else reader.readAsText( file );
1774
+
1775
+ reader.onload = function(e) {
1776
+
1777
+ let content = e.target.result;
1778
+
1779
+ if( type === 'image' ){
1780
+ var img = new Image;
1781
+ img.onload = function() {
1782
+ if( o.callback ) o.callback( img, fname );
1783
+ };
1784
+ img.src = content;
1785
+ } else {
1786
+ if( o.callback ) o.callback( content, fname );
1787
+ }
1788
+
1789
+ };
1790
+
1791
+ } catch(e) {
1792
+
1793
+ console.log(e);
1794
+
1795
+ }
1796
+
1797
+ }
1798
+
1799
+ static showOpenFilePickerPolyfill( options ) {
1800
+ return new Promise((resolve) => {
1801
+ const input = document.createElement("input");
1802
+ input.type = "file";
1803
+ input.multiple = options.multiple;
1804
+ input.accept = options.types
1805
+ .map((type) => type.accept)
1806
+ .flatMap((inst) => Object.keys(inst).flatMap((key) => inst[key]))
1807
+ .join(",");
1808
+
1809
+ input.addEventListener("change", () => {
1810
+ resolve(
1811
+ [...input.files].map((file) => {
1812
+ return {
1813
+ getFile: async () =>
1814
+ new Promise((resolve) => {
1815
+ resolve(file);
1816
+ }),
1817
+ };
1818
+ })
1819
+ );
1820
+ });
1821
+
1822
+ input.click();
1823
+ })
1824
+ }
1825
+
1826
+
1827
+ //-----------------------------
1828
+ // SAVE
1829
+ //-----------------------------
1830
+
1831
+ static async save( o = {} ) {
1832
+
1833
+ this.usePoly = false;
1834
+
1835
+ if (typeof window.showSaveFilePicker !== 'function') {
1836
+ window.showSaveFilePicker = this.showSaveFilePickerPolyfill;
1837
+ this.usePoly = true;
1838
+ }
1839
+
1840
+ try {
1841
+
1842
+ let type = o.type || '';
1843
+
1844
+ const options = {
1845
+ suggestedName: o.name || 'hello',
1846
+ data: o.data || ''
1847
+ };
1848
+
1849
+
1850
+ options.types = this.autoTypes( type );
1851
+ options.finalType = Object.keys(options.types[0].accept )[0];
1852
+ options.suggestedName += options.types[0].accept[options.finalType][0];
1853
+
1854
+
1855
+ // create a new handle
1856
+ const handle = await window.showSaveFilePicker( options );
1857
+
1858
+ if( this.usePoly ) return
1859
+
1860
+ // create a FileSystemWritableFileStream to write to
1861
+ const file = await handle.createWritable();
1862
+
1863
+ let blob = new Blob([ options.data ], { type: option.finalType });
1864
+
1865
+ // write our file
1866
+ await file.write(blob);
1867
+
1868
+ // close the file and write the contents to disk.
1869
+ await file.close();
1870
+
1871
+ } catch(e) {
1872
+
1873
+ console.log(e);
1874
+
1875
+ }
1876
+
1877
+ }
1878
+
1879
+ static showSaveFilePickerPolyfill( options ) {
1880
+ return new Promise((resolve) => {
1881
+ const a = document.createElement("a");
1882
+ a.download = options.suggestedName || "my-file.txt";
1883
+ let blob = new Blob([ options.data ], { type:options.finalType });
1884
+ a.href = URL.createObjectURL( blob );
1885
+
1886
+ a.addEventListener("click", () => {
1887
+ resolve(
1888
+ setTimeout( () => URL.revokeObjectURL(a.href), 1000 )
1889
+ );
1890
+ });
1891
+ a.click();
1892
+ })
1893
+ }
1894
+
1895
+
1896
+ //-----------------------------
1897
+ // FOLDER not possible in poly
1898
+ //-----------------------------
1899
+
1900
+ static async getFolder() {
1901
+
1902
+ try {
1903
+
1904
+ const handle = await window.showDirectoryPicker();
1905
+ const files = [];
1906
+ for await (const entry of handle.values()) {
1907
+ const file = await entry.getFile();
1908
+ files.push(file);
1909
+ }
1910
+
1911
+ console.log(files);
1912
+ return files;
1913
+
1914
+ } catch(e) {
1915
+
1916
+ console.log(e);
1917
+
1918
+ }
1919
+
1920
+ }
1921
+
1922
+
1923
+
1924
+
1925
+
1926
+
1927
+
1928
+
1929
+
1930
+
1931
+ }
1932
+
1701
1933
  class V2 {
1702
1934
 
1703
1935
  constructor( x = 0, y = 0 ) {
@@ -2398,6 +2630,8 @@ class Proto {
2398
2630
 
2399
2631
  handleEvent( e ) {
2400
2632
 
2633
+ //if(!this.s) return false
2634
+
2401
2635
  if( this.lock ) return
2402
2636
 
2403
2637
  if( this.neverlock ) Roots.lock = false;
@@ -2710,12 +2944,13 @@ class Button extends Proto {
2710
2944
 
2711
2945
  }
2712
2946
 
2713
- if( !o.value || !o.values ){
2714
- if( this.c[1] !== undefined ) this.c[1].textContent = '';
2715
- this.p = o.p !== undefined ? o.p : 0;
2716
- } else {
2717
- if( !this.txt ) this.p = 0;
2718
- }
2947
+ if( !o.value && !o.values ){
2948
+ if( this.c[1] !== undefined ) {
2949
+ this.txt = '';
2950
+ this.c[1].textContent = '';
2951
+ }
2952
+ }
2953
+ if( !this.txt ) this.p = 0;
2719
2954
 
2720
2955
  //
2721
2956
 
@@ -2827,6 +3062,8 @@ class Button extends Proto {
2827
3062
 
2828
3063
  mode ( n, id ) {
2829
3064
 
3065
+ //if(!this.s) return false
3066
+
2830
3067
  let change = false;
2831
3068
  let cc = this.colors, s = this.s;
2832
3069
  let i = id+2;
@@ -2907,6 +3144,21 @@ class Button extends Proto {
2907
3144
 
2908
3145
  }
2909
3146
 
3147
+ addLoader( n, callbackLoad ){
3148
+
3149
+ this.callbackLoad = callbackLoad;
3150
+
3151
+ let l = this.dom( 'input', this.css.basic +'top:0px; opacity:0; height:100%; width:100%; pointer-events:auto; cursor:pointer;' );//
3152
+ l.name = 'loader';
3153
+ l.type = "file";
3154
+ l.addEventListener( 'change', function(e){ this.fileSelect( e.target.files[0] ); }.bind(this), false );
3155
+
3156
+ this.c[n].appendChild( l );
3157
+
3158
+ return this
3159
+
3160
+ }
3161
+
2910
3162
  initLoader () {
2911
3163
 
2912
3164
  this.c[3] = this.dom( 'input', this.css.basic +'top:0px; opacity:0; height:'+(this.h)+'px; pointer-events:auto; cursor:pointer;' );//
@@ -2952,8 +3204,10 @@ class Button extends Proto {
2952
3204
  //else reader.readAsText( file );
2953
3205
 
2954
3206
  reader.onload = function (e) {
3207
+
3208
+ if( this.callbackLoad ) this.callbackLoad( e.target.result, fname, type );
2955
3209
 
2956
- if( this.callback ) this.callback( e.target.result, fname, type );
3210
+ //if( this.callback ) this.callback( e.target.result, fname, type );
2957
3211
  //this.c[3].type = "file";
2958
3212
  //this.send( e.target.result );
2959
3213
  }.bind(this);
@@ -7807,8 +8061,8 @@ const add = function () {
7807
8061
 
7808
8062
  ref = true;
7809
8063
  if( a[2] === undefined ) [].push.call(a, {});
7810
-
7811
- type = a[2].type ? a[2].type : 'slide';//autoType.apply( this, a );
8064
+
8065
+ type = a[2].type ? a[2].type : autoType( a[0][a[1]], a[2] );
7812
8066
 
7813
8067
  o = a[2];
7814
8068
  o.name = a[1];
@@ -7817,13 +8071,15 @@ const add = function () {
7817
8071
 
7818
8072
  }
7819
8073
 
8074
+
8075
+
7820
8076
  let name = type.toLowerCase();
7821
8077
 
7822
8078
  if( name === 'group' ) o.add = add;
7823
8079
 
7824
8080
  switch( name ){
7825
8081
 
7826
- case 'bool': n = new Bool(o); break;
8082
+ case 'bool': case 'boolean': n = new Bool(o); break;
7827
8083
  case 'button': n = new Button(o); break;
7828
8084
  case 'circular': n = new Circular(o); break;
7829
8085
  case 'color': n = new Color(o); break;
@@ -7853,6 +8109,37 @@ const add = function () {
7853
8109
 
7854
8110
  }
7855
8111
 
8112
+ };
8113
+
8114
+ const autoType = function ( v, o ) {
8115
+
8116
+ let type = 'slide';
8117
+
8118
+ if( typeof v === 'boolean' ) type = 'bool';
8119
+ else if( typeof v === 'string' ){
8120
+
8121
+ if( v.substring(0,1) === '#' ) type = 'color';
8122
+ else type = 'string';
8123
+
8124
+ } else if( typeof v === 'number' ){
8125
+
8126
+ if( o.ctype ) type = 'color';
8127
+ else type = 'slide';
8128
+
8129
+ } else if( typeof v === 'array' && v instanceof Array ){
8130
+
8131
+ if( typeof v[0] === 'number' ) type = 'number';
8132
+ else if( typeof v[0] === 'string' ) type = 'list';
8133
+
8134
+ } else if( typeof v === 'object' && v instanceof Object ){
8135
+
8136
+ if( v.x !== undefined ) type = 'number';
8137
+ else type = 'list';
8138
+
8139
+ }
8140
+
8141
+ return type
8142
+
7856
8143
  };
7857
8144
 
7858
8145
  /**
@@ -8171,9 +8458,12 @@ class Gui {
8171
8458
  clearTarget () {
8172
8459
 
8173
8460
  if( this.current === -1 ) return false;
8174
- //if(!this.target) return;
8175
- this.target.uiout();
8176
- this.target.reset();
8461
+ if( this.target.s ){
8462
+ // if no s target is delete !!
8463
+ this.target.uiout();
8464
+ this.target.reset();
8465
+ }
8466
+
8177
8467
  this.target = null;
8178
8468
  this.current = -1;
8179
8469
 
@@ -8359,8 +8649,8 @@ class Gui {
8359
8649
  else {
8360
8650
  a[2].isUI = true;
8361
8651
  a[2].main = this;
8362
-
8363
- ontop = a[1].ontop ? a[1].ontop : false;
8652
+ //ontop = a[1].ontop ? a[1].ontop : false;
8653
+ ontop = a[2].ontop ? a[2].ontop : false;
8364
8654
  }
8365
8655
 
8366
8656
  }
@@ -8654,6 +8944,6 @@ class Gui {
8654
8944
 
8655
8945
  }
8656
8946
 
8657
- const REVISION = '4.0.5';
8947
+ const REVISION = '4.0.7';
8658
8948
 
8659
- export { Gui, Proto, REVISION, Tools, add };
8949
+ export { Files, Gui, Proto, REVISION, Tools, add };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uil",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "JavaScript gui library",
5
5
  "main": "build/uil.js",
6
6
  "repository": "lo-th/uil",