@@ -4,36 +4,35 @@ import * as C from "../support/constants.js";
4
4
import * as Content from "./content.js" ;
5
5
import * as Controls from "./controls.js" ;
6
6
import * as Examples from "../dataPixels/examples.js" ;
7
+ import * as File from "./file.js" ;
7
8
import * as Layout from "./layout.js" ;
8
9
import * as Main from "../main.js" ;
9
10
import * as Popups from "./popups.js" ;
10
- import * as Utils from "../support/utils.js" ;
11
11
import DataPixelsCodeFactory from "../dataPixels/DataPixelsCodeFactory.js" ;
12
12
13
13
/**
14
14
* @description The <strong>app.js</strong> module contains properties and functions pertaining to the initialization and control of the application.
15
15
* @requires constants
16
16
* @requires content
17
17
* @requires controls
18
- * @requires dataPixelsCodeFactory
18
+ * @requires examples
19
+ * @requires file
19
20
* @requires layout
21
+ * @requires main
20
22
* @requires popups
21
23
* @requires sharedStates
22
- * @requires utils
23
24
* @module
24
25
*
25
26
*/
26
27
export {
27
28
28
29
displayAboutDialog ,
29
30
writeExampleCode ,
31
+ displayCode ,
30
32
displaySettingsDialog ,
31
33
executeCode ,
32
- imageSizeCancelHandler ,
33
- imageSizeOKHandler ,
34
34
init ,
35
35
loadDataPixelsClassCode ,
36
- readImageFile ,
37
36
setErrorMessage ,
38
37
setFrameViewHasImage ,
39
38
setMode ,
@@ -47,8 +46,6 @@ export {
47
46
* <ul>
48
47
* <li> DataPixelsClassCode </li>
49
48
* <li> DataPixelsClassCodeInternal </li>
50
- * <li> File </li>
51
- * <li> Image </li>
52
49
* <li> IsExecuting </li>
53
50
* </ul>
54
51
*
@@ -60,8 +57,6 @@ const M = {
60
57
61
58
DataPixelsClassCode : undefined ,
62
59
DataPixelsClassCodeInternal : undefined ,
63
- File : undefined ,
64
- Image : undefined ,
65
60
IsExecuting : undefined
66
61
} ;
67
62
@@ -74,10 +69,10 @@ const M = {
74
69
function init ( ) {
75
70
76
71
readStates ( ) ;
77
- initFileManagement ( ) ;
78
-
72
+
79
73
Content . init ( ) ;
80
74
Controls . init ( ) ;
75
+ File . init ( ) ;
81
76
}
82
77
83
78
/**
@@ -205,7 +200,7 @@ function toggleLayout() {
205
200
* @description Displays either manual or automatically generated program code in the Code Editor.
206
201
* @param {string } code - The code to display in the Code Editor.
207
202
* @param {boolean } [autoMode = true] - Sets the application mode based on how the supplied code was produced.
208
- * @private
203
+ * @public
209
204
* @function
210
205
*
211
206
*/
@@ -387,222 +382,6 @@ function XHRErrorHandler(event) {
387
382
setErrorMessage ( `${ C . Label . ERROR } ${ C . Label . FILE_READ } ` ) ;
388
383
}
389
384
390
- /**
391
- * @description Initializes input file control and file drop functionality with applicable event handlers.
392
- * @private
393
- * @function
394
- *
395
- */
396
- function initFileManagement ( ) {
397
-
398
- const dropTarget = document . body ;
399
-
400
- dropTarget . addEventListener ( C . Event . DRAG_ENTER , ( event ) => {
401
-
402
- event . stopPropagation ( ) ;
403
- event . preventDefault ( ) ;
404
-
405
- event . dataTransfer . effectAllowed = C . HTML . COPY ;
406
- } ) ;
407
-
408
- dropTarget . addEventListener ( C . Event . DRAG_OVER , ( event ) => {
409
-
410
- event . stopPropagation ( ) ;
411
- event . preventDefault ( ) ;
412
-
413
- event . dataTransfer . dropEffect = C . HTML . COPY ;
414
- } ) ;
415
-
416
- dropTarget . addEventListener ( C . Event . DROP , ( event ) => {
417
-
418
- event . stopPropagation ( ) ;
419
- event . preventDefault ( ) ;
420
-
421
- validateDroppedFileType ( event . dataTransfer . files [ 0 ] ) ;
422
- } ) ;
423
- }
424
-
425
- /**
426
- * @description Validates the file type of the dropped file as one of the application's supported image file types.
427
- * @param {Object } file - The dropped file.
428
- * @private
429
- * @function
430
- *
431
- */
432
- function validateDroppedFileType ( file ) {
433
-
434
- if ( file ) {
435
-
436
- setMode ( C . Mode . MANUAL ) ;
437
-
438
- M . File = file ;
439
- const fileType = M . File . type ;
440
-
441
- if ( fileType !== C . Type . MIME_IMAGE_GIF && fileType !== C . Type . MIME_IMAGE_JPG && fileType !== C . Type . MIME_IMAGE_PNG ) {
442
-
443
- Popups . display ( C . Dialog . FILE_TYPE_ERROR ) ;
444
-
445
- return ;
446
- }
447
-
448
- setMode ( C . Mode . AUTO ) ;
449
- readImageFile ( file ) ;
450
- }
451
- }
452
-
453
- /**
454
- * @description Instantiates a FireReader object to read a dropped or opened image file
455
- * @param {Object } blob - The Blob object read by the FileReader.
456
- * @param {string } [fileName = null] - Name of the opened image file assigned to the name property of the M.File object.
457
- * @public
458
- * @function
459
- *
460
- */
461
- function readImageFile ( blob , fileName = null ) {
462
-
463
- if ( fileName ) {
464
-
465
- M . File = { name : fileName } ;
466
- }
467
-
468
- const fileReader = new FileReader ( ) ;
469
- fileReader . readAsDataURL ( blob ) ;
470
- fileReader . addEventListener ( C . Event . LOAD , fileReaderLoadHandler ) ;
471
- fileReader . addEventListener ( C . Event . ERROR , fileReaderErrorHandler ) ;
472
- }
473
-
474
- /**
475
- * @description Event handler called when the FileReader has finished loading the image file's contents.
476
- * @param {Object } event - The event object.
477
- * @private
478
- * @function
479
- *
480
- */
481
- function fileReaderLoadHandler ( event ) {
482
-
483
- const fileReader = event . target ;
484
- fileReader . removeEventListener ( C . Event . LOAD , fileReaderLoadHandler ) ;
485
- fileReader . removeEventListener ( C . Event . ERROR , fileReaderErrorHandler ) ;
486
-
487
- const image = new Image ( ) ;
488
- image . src = fileReader . result ;
489
- image . addEventListener ( C . Event . LOAD , imageLoadHandler ) ;
490
- image . addEventListener ( C . Event . ERROR , imageErrorHandler ) ;
491
- }
492
-
493
- /**
494
- * @description Event handler called when the FileReader has encountered an error.
495
- * @param {Object } event - The event object.
496
- * @private
497
- * @function
498
- *
499
- */
500
- function fileReaderErrorHandler ( event ) {
501
-
502
- const fileReader = event . target ;
503
- fileReader . removeEventListener ( C . Event . LOAD , fileReaderLoadHandler ) ;
504
- fileReader . removeEventListener ( C . Event . ERROR , fileReaderErrorHandler ) ;
505
-
506
- setErrorMessage ( `${ C . Label . ERROR } ${ C . Label . FILE_READ } ` ) ;
507
- setMode ( C . Mode . MANUAL ) ;
508
- }
509
-
510
- /**
511
- * @description Event handler called when the HTMLImageElement with dimension attributes has finished loading.
512
- * @param {Object } event - The event object.
513
- * @private
514
- * @function
515
- *
516
- */
517
- function imageLoadHandler ( event ) {
518
-
519
- const image = event . target ;
520
- image . removeEventListener ( C . Event . LOAD , imageLoadHandler ) ;
521
- image . removeEventListener ( C . Event . ERROR , imageErrorHandler ) ;
522
-
523
- if ( image . width * image . height > C . Measurement . IMAGE_MAX_AREA ) {
524
-
525
- M . Image = image ;
526
-
527
- Popups . display ( C . Dialog . IMAGE_SIZE_WARNING ) ;
528
- }
529
- else {
530
-
531
- createAutoCode ( image ) ;
532
- }
533
- }
534
-
535
- /**
536
- * @description Event handler called when the OK button of the Image Size Dialog is clicked.
537
- * @public
538
- * @function
539
- *
540
- */
541
- function imageSizeOKHandler ( ) {
542
-
543
- createAutoCode ( M . Image ) ;
544
-
545
- M . Image = null ;
546
- }
547
-
548
- /**
549
- * @description Event handler called when the Cancel button of the Image Size Dialog is clicked.
550
- * @public
551
- * @function
552
- *
553
- */
554
- function imageSizeCancelHandler ( ) {
555
-
556
- setMode ( C . Mode . MANUAL ) ;
557
-
558
- M . Image = null ;
559
- }
560
-
561
- /**
562
- * @description Automatically generates program code by parsing an image file.
563
- * @param {Object } image - The image file to be parsed.
564
- * @private
565
- * @function
566
- *
567
- */
568
- function createAutoCode ( image ) {
569
-
570
- setErrorMessage ( null ) ;
571
-
572
- const canvas = document . createElement ( C . HTML . CANVAS ) ;
573
- canvas . width = image . width ;
574
- canvas . height = image . height ;
575
-
576
- const context = canvas . getContext ( C . HTML . CANVAS_RENDERING_CONTEXT_2D ) ;
577
- context . drawImage ( image , 0 , 0 ) ;
578
-
579
- const variableName = Utils . cleanFileName ( M . File . name , "pixelData" ) ;
580
- const imageData = context . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
581
-
582
- S . AutoCode = new DataPixelsCodeFactory ( variableName , imageData ) ;
583
- S . AutoCode . formatCode ( S . Alignment , S . Description ) ;
584
- S . AutoCode . updateIndentation ( S . Indentation ) ;
585
-
586
- displayCode ( S . AutoCode . output ) ;
587
- }
588
-
589
- /**
590
- * @description Event handler called when the HTMLImageElement has encountered an error.
591
- * @param {Object } event - The event object.
592
- * @private
593
- * @function
594
- *
595
- */
596
- function imageErrorHandler ( event ) {
597
-
598
- const image = event . target ;
599
- image . removeEventListener ( C . Event . LOAD , imageLoadHandler ) ;
600
- image . removeEventListener ( C . Event . ERROR , imageErrorHandler ) ;
601
-
602
- setErrorMessage ( `${ C . Label . ERROR } ${ C . Label . FILE_CORRUPT } ` ) ;
603
- setMode ( C . Mode . MANUAL ) ;
604
- }
605
-
606
385
/**
607
386
* @description Updates the format and indentation of the automatically generates program code.
608
387
* @public
0 commit comments