lexgui 0.1.28 → 0.1.29

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.
package/build/lexgui.js CHANGED
@@ -12,12 +12,16 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
12
12
  */
13
13
 
14
14
  var LX = global.LX = {
15
- version: "0.1.28",
15
+ version: "0.1.29",
16
16
  ready: false,
17
17
  components: [], // specific pre-build components
18
18
  signals: {} // events and triggers
19
19
  };
20
20
 
21
+ LX.MOUSE_LEFT_CLICK = 0;
22
+ LX.MOUSE_MIDDLE_CLICK = 1;
23
+ LX.MOUSE_RIGHT_CLICK = 2;
24
+
21
25
  LX.MOUSE_DOUBLE_CLICK = 2;
22
26
  LX.MOUSE_TRIPLE_CLICK = 3;
23
27
 
@@ -79,27 +83,31 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
79
83
 
80
84
  LX.getBase64Image = getBase64Image;
81
85
 
82
- function hexToRgb(string) {
83
- const red = parseInt(string.substring(1, 3), 16) / 255;
84
- const green = parseInt(string.substring(3, 5), 16) / 255;
85
- const blue = parseInt(string.substring(5, 7), 16) / 255;
86
- return [red, green, blue];
86
+ function hexToRgb( hexStr ) {
87
+ const red = parseInt( hexStr.substring( 1, 3 ), 16 ) / 255;
88
+ const green = parseInt( hexStr.substring( 3, 5 ), 16 ) / 255;
89
+ const blue = parseInt( hexStr.substring( 5, 7 ), 16 ) / 255;
90
+ return [ red, green, blue ];
87
91
  }
88
-
89
- function rgbToHex(rgb) {
92
+
93
+ LX.hexToRgb = hexToRgb;
94
+
95
+ function rgbToHex( rgb ) {
90
96
  let hex = "#";
91
- for(let c of rgb) {
92
- c = Math.floor(c * 255);
93
- hex += c.toString(16);
97
+ for( let c of rgb ) {
98
+ c = Math.floor( c * 255 );
99
+ hex += c.toString( 16 );
94
100
  }
95
101
  return hex;
96
102
  }
103
+
104
+ LX.rgbToHex = rgbToHex;
97
105
 
98
106
  function simple_guidGenerator() {
99
107
  var S4 = function() {
100
108
  return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
101
109
  };
102
- return (S4()+"-"+S4());
110
+ return (S4()+"-"+S4()+"-"+S4());
103
111
  }
104
112
 
105
113
  // Timer that works everywhere (from litegraph.js)
@@ -144,6 +152,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
144
152
  sub ( v, v0 = new vec2() ) { v0.set( this.x - v.x, this.y - v.y ); return v0; }
145
153
  mul ( v, v0 = new vec2() ) { if( v.constructor == Number ) { v = new vec2( v ) } v0.set( this.x * v.x, this.y * v.y ); return v0; }
146
154
  div ( v, v0 = new vec2() ) { if( v.constructor == Number ) { v = new vec2( v ) } v0.set( this.x / v.x, this.y / v.y ); return v0; }
155
+ abs ( v0 = new vec2() ) { v0.set( Math.abs( this.x ), Math.abs( this.y ) ); return v0; }
147
156
  };
148
157
 
149
158
  LX.vec2 = vec2;
@@ -156,46 +165,58 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
156
165
  let offsetY;
157
166
  let currentTarget = null;
158
167
  let targetClass = options.targetClass;
159
-
160
- const moveFunc = (e) => {
161
- if(!currentTarget) return;
168
+
169
+ let id = LX.UTILS.uidGenerator();
170
+ domEl[ 'draggable-id' ] = id;
171
+
172
+ const defaultMoveFunc = e => {
173
+ if( !currentTarget ) return;
162
174
  let left = e.clientX - offsetX;
163
175
  let top = e.clientY - offsetY;
164
- if(left > 3 && (left + domEl.offsetWidth + 6) <= window.innerWidth)
176
+ if( left > 3 && ( left + domEl.offsetWidth + 6 ) <= window.innerWidth )
165
177
  domEl.style.left = left + 'px';
166
- if(top > 3 && (top + domEl.offsetHeight + 6) <= window.innerHeight)
178
+ if( top > 3 && ( top + domEl.offsetHeight + 6 ) <= window.innerHeight )
167
179
  domEl.style.top = top + 'px';
168
180
  };
169
-
170
- let onMove = options.onMove ?? moveFunc;
171
181
 
172
- domEl.setAttribute('draggable', true);
173
- domEl.addEventListener("mousedown", function(e) {
182
+ const customMoveFunc = e => {
183
+ if( !currentTarget ) return;
184
+ if( options.onMove )
185
+ options.onMove( currentTarget );
186
+ };
187
+
188
+ let onMove = options.onMove ? customMoveFunc : defaultMoveFunc;
189
+ let onDragStart = options.onDragStart;
190
+
191
+ domEl.setAttribute( 'draggable', true );
192
+ domEl.addEventListener( "mousedown", function( e ) {
174
193
  currentTarget = (e.target.classList.contains(targetClass) || !targetClass) ? e.target : null;
175
- });
194
+ } );
176
195
 
177
- domEl.addEventListener("dragstart", function(e) {
196
+ domEl.addEventListener( "dragstart", function( e ) {
178
197
  e.preventDefault();
179
198
  e.stopPropagation();
180
199
  e.stopImmediatePropagation();
181
- if(!currentTarget) return;
200
+ if( !currentTarget ) return;
182
201
  // Remove image when dragging
183
202
  var img = new Image();
184
203
  img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';
185
- e.dataTransfer.setDragImage(img, 0, 0);
204
+ e.dataTransfer.setDragImage( img, 0, 0 );
186
205
  e.dataTransfer.effectAllowed = "move";
187
206
  const rect = e.target.getBoundingClientRect();
188
207
  offsetX = e.clientX - rect.x;
189
208
  offsetY = e.clientY - rect.y;
190
- document.addEventListener("mousemove", onMove );
191
- }, false);
209
+ document.addEventListener( "mousemove", onMove );
210
+ if( onDragStart )
211
+ onDragStart( currentTarget, e );
212
+ }, false );
192
213
 
193
- document.addEventListener('mouseup', () => {
194
- if(currentTarget) {
214
+ document.addEventListener( 'mouseup', () => {
215
+ if( currentTarget ) {
195
216
  currentTarget = null;
196
- document.removeEventListener("mousemove", onMove );
217
+ document.removeEventListener( "mousemove", onMove );
197
218
  }
198
- });
219
+ } );
199
220
  }
200
221
 
201
222
  LX.makeDraggable = makeDraggable;
@@ -3447,103 +3468,109 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
3447
3468
 
3448
3469
  addText( name, value, callback, options = {} ) {
3449
3470
 
3450
- let widget = this.create_widget(name, Widget.TEXT, options);
3471
+ let widget = this.create_widget( name, Widget.TEXT, options );
3451
3472
  widget.onGetValue = () => {
3452
3473
  return wValue.value;
3453
3474
  };
3454
- widget.onSetValue = (new_value) => {
3455
- this.disabled ? wValue.innerText = new_value : wValue.value = new_value;
3456
- Panel._dispatch_event(wValue, "focusout");
3475
+ widget.onSetValue = newValue => {
3476
+ this.disabled ? wValue.innerText = newValue : wValue.value = newValue;
3477
+ Panel._dispatch_event( wValue, "focusout" );
3457
3478
  };
3458
-
3479
+
3459
3480
  let element = widget.domEl;
3460
-
3481
+
3461
3482
  // Add reset functionality
3462
- if(widget.name && !(options.skipReset ?? false)) {
3463
- Panel._add_reset_property(element.domName, function() {
3483
+ if( widget.name && !( options.skipReset ?? false ) ) {
3484
+ Panel._add_reset_property( element.domName, function() {
3464
3485
  wValue.value = wValue.iValue;
3465
3486
  this.style.display = "none";
3466
- Panel._dispatch_event(wValue, "focusout");
3467
- });
3487
+ Panel._dispatch_event( wValue, "focusout" );
3488
+ } );
3468
3489
  }
3469
3490
 
3470
3491
  // Add widget value
3471
-
3472
- let container = document.createElement('div');
3473
- container.className = "lextext" + (options.warning ? " lexwarning" : "");
3492
+
3493
+ let container = document.createElement( 'div' );
3494
+ container.className = "lextext" + ( options.warning ? " lexwarning" : "" );
3474
3495
  container.style.width = options.inputWidth || "calc( 100% - " + LX.DEFAULT_NAME_WIDTH + " )";
3475
3496
  container.style.display = "flex";
3476
-
3477
- this.disabled = (options.disabled || options.warning) ?? ( options.url ? true : false );
3497
+
3498
+ this.disabled = ( options.disabled || options.warning ) ?? ( options.url ? true : false );
3478
3499
  let wValue = null;
3479
-
3500
+
3480
3501
  if( !this.disabled )
3481
3502
  {
3482
- wValue = document.createElement('input');
3503
+ wValue = document.createElement( 'input' );
3483
3504
  wValue.type = options.type || "";
3484
3505
  wValue.value = wValue.iValue = value || "";
3485
3506
  wValue.style.width = "100%";
3486
3507
  wValue.style.textAlign = options.float ?? "";
3487
3508
 
3488
- if(options.placeholder) wValue.setAttribute("placeholder", options.placeholder);
3509
+ if( options.placeholder )
3510
+ wValue.setAttribute( "placeholder", options.placeholder );
3489
3511
 
3490
- var resolve = (function(val, event) {
3491
- let btn = element.querySelector(".lexwidgetname .lexicon");
3492
- if(btn) btn.style.display = (val != wValue.iValue ? "block" : "none");
3493
- this._trigger( new IEvent(name, val, event), callback );
3494
- }).bind(this);
3512
+ var resolve = ( function( val, event ) {
3513
+ let btn = element.querySelector( ".lexwidgetname .lexicon" );
3514
+ if( btn ) btn.style.display = ( val != wValue.iValue ? "block" : "none" );
3515
+ this._trigger( new IEvent( name, val, event ), callback );
3516
+ }).bind( this );
3495
3517
 
3496
3518
  const trigger = options.trigger ?? 'default';
3497
3519
 
3498
- if(trigger == 'default')
3520
+ if( trigger == 'default' )
3499
3521
  {
3500
- wValue.addEventListener("keyup", function(e){
3522
+ wValue.addEventListener( "keyup", function( e ){
3501
3523
  if(e.key == 'Enter')
3502
- resolve(e.target.value, e);
3524
+ resolve( e.target.value, e );
3503
3525
  });
3504
- wValue.addEventListener("focusout", function(e){
3505
- resolve(e.target.value, e);
3526
+ wValue.addEventListener( "focusout", function( e ){
3527
+ resolve( e.target.value, e );
3506
3528
  });
3507
3529
  }
3508
- else if(trigger == 'input')
3530
+ else if( trigger == 'input' )
3509
3531
  {
3510
- wValue.addEventListener("input", function(e){
3511
- resolve(e.target.value, e);
3532
+ wValue.addEventListener("input", function( e ){
3533
+ resolve( e.target.value, e );
3512
3534
  });
3513
3535
  }
3514
3536
 
3515
- if(options.icon)
3537
+ wValue.addEventListener( "mousedown", function( e ){
3538
+ e.stopImmediatePropagation();
3539
+ e.stopPropagation();
3540
+ });
3541
+
3542
+ if( options.icon )
3516
3543
  {
3517
- let icon = document.createElement('a');
3544
+ let icon = document.createElement( 'a' );
3518
3545
  icon.className = "inputicon " + options.icon;
3519
- container.appendChild(icon);
3546
+ container.appendChild( icon );
3520
3547
  }
3521
-
3548
+
3522
3549
  } else
3523
3550
  {
3524
- wValue = document.createElement(options.url ? 'a' : 'div');
3525
- if(options.url)
3551
+ wValue = document.createElement( options.url ? 'a' : 'div' );
3552
+ if( options.url )
3526
3553
  {
3527
3554
  wValue.href = options.url;
3528
3555
  wValue.target = "_blank";
3529
3556
  }
3530
3557
  const icon = options.warning ? '<i class="fa-solid fa-triangle-exclamation"></i>' : '';
3531
- wValue.innerHTML = (icon + value) || "";
3558
+ wValue.innerHTML = ( icon + value ) || "";
3532
3559
  wValue.style.width = "100%";
3533
3560
  wValue.style.textAlign = options.float ?? "";
3534
3561
  }
3535
-
3536
- Object.assign(wValue.style, options.style ?? {});
3537
-
3538
- container.appendChild(wValue);
3539
- element.appendChild(container);
3562
+
3563
+ Object.assign( wValue.style, options.style ?? {} );
3564
+
3565
+ container.appendChild( wValue );
3566
+ element.appendChild( container );
3540
3567
 
3541
3568
  // Remove branch padding and margins
3542
- if(!widget.name) {
3569
+ if( !widget.name ) {
3543
3570
  element.className += " noname";
3544
3571
  container.style.width = "100%";
3545
3572
  }
3546
-
3573
+
3547
3574
  return widget;
3548
3575
  }
3549
3576
 
@@ -3961,10 +3988,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
3961
3988
  delete list.unfocus_event;
3962
3989
  return;
3963
3990
  }
3964
- let container = selectedOption.parentElement.parentElement.parentElement.parentElement; // there must be a nicer way...
3965
- let rect = event.currentTarget.getBoundingClientRect();
3966
- let y_pos = container.classList.contains('lexdialog') ? rect.top - 5 + rect.height : rect.y + rect.height - 5;
3967
- element.querySelector(".lexoptions").style.top = y_pos + 'px';
3991
+ element.querySelector(".lexoptions").style.top = (selectedOption.offsetTop + selectedOption.offsetHeight) + 'px';
3968
3992
  element.querySelector(".lexoptions").style.width = (event.currentTarget.clientWidth) + 'px';
3969
3993
  element.querySelector(".lexoptions").toggleAttribute('hidden');
3970
3994
  list.focus();
@@ -4892,6 +4916,8 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
4892
4916
  document.body.classList.add('nocursor');
4893
4917
  document.body.classList.add('noevents');
4894
4918
  drag_icon.classList.remove('hidden');
4919
+ e.stopImmediatePropagation();
4920
+ e.stopPropagation();
4895
4921
  }
4896
4922
 
4897
4923
  function inner_mousemove(e) {
@@ -5064,6 +5090,8 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5064
5090
  document.body.classList.add('nocursor');
5065
5091
  document.body.classList.add('noevents');
5066
5092
  drag_icon.classList.remove('hidden');
5093
+ e.stopImmediatePropagation();
5094
+ e.stopPropagation();
5067
5095
  }
5068
5096
 
5069
5097
  function inner_mousemove(e) {
@@ -5248,11 +5276,11 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5248
5276
 
5249
5277
  addFile( name, callback, options = { } ) {
5250
5278
 
5251
- if(!name) {
5252
- throw("Set Widget Name!");
5279
+ if( !name ) {
5280
+ throw( "Set Widget Name!" );
5253
5281
  }
5254
5282
 
5255
- let widget = this.create_widget(name, Widget.FILE, options);
5283
+ let widget = this.create_widget( name, Widget.FILE, options );
5256
5284
  let element = widget.domEl;
5257
5285
 
5258
5286
  let local = options.local ?? true;
@@ -5260,31 +5288,36 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5260
5288
  let read = options.read ?? true;
5261
5289
 
5262
5290
  // Create hidden input
5263
- let input = document.createElement('input');
5291
+ let input = document.createElement( 'input' );
5264
5292
  input.style.width = "calc( 100% - " + LX.DEFAULT_NAME_WIDTH + " - 10%)";
5265
5293
  input.type = 'file';
5266
- if(options.placeholder)
5294
+
5295
+ if( options.placeholder )
5267
5296
  input.placeholder = options.placeholder;
5268
5297
 
5269
- input.addEventListener('change', function(e) {
5298
+ input.addEventListener( 'change', function( e ) {
5299
+
5270
5300
  const files = e.target.files;
5271
5301
  if( !files.length ) return;
5272
- if(read)
5302
+ if( read )
5273
5303
  {
5304
+ if( options.onBeforeRead )
5305
+ options.onBeforeRead();
5306
+
5274
5307
  const reader = new FileReader();
5275
5308
 
5276
- if(type === 'text') reader.readAsText(files[0]);
5277
- else if(type === 'buffer') reader.readAsArrayBuffer(files[0]);
5278
- else if(type === 'bin') reader.readAsBinaryString(files[0]);
5279
- else if(type === 'url') reader.readAsDataURL(files[0]);
5309
+ if( type === 'text' ) reader.readAsText( files[ 0 ] );
5310
+ else if( type === 'buffer' ) reader.readAsArrayBuffer( files[ 0 ] );
5311
+ else if( type === 'bin' ) reader.readAsBinaryString( files[ 0 ] );
5312
+ else if( type === 'url' ) reader.readAsDataURL( files[ 0 ] );
5280
5313
 
5281
- reader.onload = (e) => { callback.call( this, e.target.result, files[0] ) } ;
5314
+ reader.onload = e => { callback.call( this, e.target.result, files[ 0 ] ) } ;
5282
5315
  }
5283
- else callback(files[0]);
5284
-
5316
+ else
5317
+ callback( files[ 0 ] );
5285
5318
  });
5286
5319
 
5287
- element.appendChild(input);
5320
+ element.appendChild( input );
5288
5321
 
5289
5322
  this.queue( element );
5290
5323
 
@@ -5292,9 +5325,9 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5292
5325
  {
5293
5326
  this.addButton(null, "<a style='margin-top: 0px;' class='fa-solid fa-gear'></a>", () => {
5294
5327
 
5295
- new Dialog("Load Settings", p => {
5296
- p.addDropdown("Type", ['text', 'buffer', 'bin', 'url'], type, v => { type = v } );
5297
- p.addButton(null, "Reload", v => { input.dispatchEvent( new Event('change') ) } );
5328
+ new Dialog( "Load Settings", p => {
5329
+ p.addDropdown( "Type", [ 'text', 'buffer', 'bin', 'url' ], type, v => { type = v } );
5330
+ p.addButton( null, "Reload", v => { input.dispatchEvent( new Event( 'change' ) ) } );
5298
5331
  });
5299
5332
 
5300
5333
  }, { className: "micro", skipInlineCount: true });
@@ -5848,6 +5881,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5848
5881
  }
5849
5882
 
5850
5883
  destroy() {
5884
+
5851
5885
  this.root.remove();
5852
5886
  }
5853
5887
 
@@ -5862,6 +5896,14 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5862
5896
  this.root.style.left = x + "px";
5863
5897
  this.root.style.top = y + "px";
5864
5898
  }
5899
+
5900
+ setTitle( title ) {
5901
+
5902
+ const titleDOM = this.root.querySelector( '.lexdialogtitle' );
5903
+ if( !titleDOM )
5904
+ return;
5905
+ titleDOM.innerText = title;
5906
+ }
5865
5907
  }
5866
5908
 
5867
5909
  LX.Dialog = Dialog;
@@ -5891,8 +5933,10 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5891
5933
 
5892
5934
  // Custom
5893
5935
  this.root.classList.add( "pocket" );
5894
- this.root.style.left = "calc(100% - " + (this.root.offsetWidth + 6) + "px)";
5895
- this.root.style.top = "0px";
5936
+ if( !options.position ) {
5937
+ this.root.style.left = "calc(100% - " + (this.root.offsetWidth + 6) + "px)";
5938
+ this.root.style.top = "0px";
5939
+ }
5896
5940
  this.panel.root.style.width = "calc( 100% - 12px )";
5897
5941
  this.panel.root.style.height = "calc( 100% - 40px )";
5898
5942
  this.dock_pos = PocketDialog.TOP;
@@ -5931,7 +5975,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
5931
5975
  this.root.style.top = "calc(100% - " + (this.root.offsetHeight + 6) + "px)";
5932
5976
  break;
5933
5977
  case 'l':
5934
- this.root.style.left = "0px";
5978
+ this.root.style.left = options.position ? options.position[ 1 ] : "0px";
5935
5979
  break;
5936
5980
  }
5937
5981
  }
@@ -6036,7 +6080,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
6036
6080
  contextmenu.style.marginTop = 3.5 - c.offsetHeight + "px";
6037
6081
 
6038
6082
  // Set final width
6039
- contextmenu.style.width = contextmenu.offsetWidth + "px";
6083
+ // contextmenu.style.width = contextmenu.offsetWidth + "px";
6040
6084
  this._adjust_position( contextmenu, 6, true );
6041
6085
  }
6042
6086
 
@@ -7484,9 +7528,15 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
7484
7528
  configurable: true
7485
7529
  });
7486
7530
 
7487
- Element.prototype.insertChildAtIndex = function(child, index = Infinity) {
7488
- if (index >= this.children.length) this.appendChild(child);
7489
- else this.insertBefore(child, this.children[index]);
7531
+ Element.prototype.insertChildAtIndex = function( child, index = Infinity ) {
7532
+ if ( index >= this.children.length ) this.appendChild( child );
7533
+ else this.insertBefore( child, this.children[index] );
7534
+ }
7535
+
7536
+ Element.prototype.hasClass = function( list ) {
7537
+ list = [].concat( list );
7538
+ var r = list.filter( v => this.classList.contains( v ) );
7539
+ return !!r.length;
7490
7540
  }
7491
7541
 
7492
7542
  Element.prototype.getComputedSize = function() {
@@ -7502,7 +7552,7 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
7502
7552
  compareThreshold( v, p, n, t ) { return Math.abs(v - p) >= t || Math.abs(v - n) >= t },
7503
7553
  compareThresholdRange( v0, v1, t0, t1 ) { return v0 >= t0 && v0 <= t1 || v1 >= t0 && v1 <= t1 || v0 <= t0 && v1 >= t1},
7504
7554
  clamp (num, min, max) { return Math.min(Math.max(num, min), max) },
7505
-
7555
+ uidGenerator: simple_guidGenerator,
7506
7556
  getControlPoints( x0, y0, x1, y1, x2, y2 ,t ) {
7507
7557
 
7508
7558
  // x0,y0,x1,y1 are the coordinates of the end (knot) pts of this segment
@@ -7527,7 +7577,6 @@ console.warn( 'Script "build/lexgui.js" is depracated and will be removed soon.
7527
7577
 
7528
7578
  return [p1x,p1y,p2x,p2y]
7529
7579
  },
7530
-
7531
7580
  drawSpline( ctx, pts, t ) {
7532
7581
 
7533
7582
  ctx.save();