Skip to content

Commit 88631bc

Browse files
committed
Refactored code
Moved File logic into its own module and refactored Sass code.
1 parent b214388 commit 88631bc

File tree

15 files changed

+637
-609
lines changed

15 files changed

+637
-609
lines changed

resources/source/js/application/app.js

Lines changed: 8 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,35 @@ import * as C from "../support/constants.js";
44
import * as Content from "./content.js";
55
import * as Controls from "./controls.js";
66
import * as Examples from "../dataPixels/examples.js";
7+
import * as File from "./file.js";
78
import * as Layout from "./layout.js";
89
import * as Main from "../main.js";
910
import * as Popups from "./popups.js";
10-
import * as Utils from "../support/utils.js";
1111
import DataPixelsCodeFactory from "../dataPixels/DataPixelsCodeFactory.js";
1212

1313
/**
1414
* @description The <strong>app.js</strong> module contains properties and functions pertaining to the initialization and control of the application.
1515
* @requires constants
1616
* @requires content
1717
* @requires controls
18-
* @requires dataPixelsCodeFactory
18+
* @requires examples
19+
* @requires file
1920
* @requires layout
21+
* @requires main
2022
* @requires popups
2123
* @requires sharedStates
22-
* @requires utils
2324
* @module
2425
*
2526
*/
2627
export {
2728

2829
displayAboutDialog,
2930
writeExampleCode,
31+
displayCode,
3032
displaySettingsDialog,
3133
executeCode,
32-
imageSizeCancelHandler,
33-
imageSizeOKHandler,
3434
init,
3535
loadDataPixelsClassCode,
36-
readImageFile,
3736
setErrorMessage,
3837
setFrameViewHasImage,
3938
setMode,
@@ -47,8 +46,6 @@ export {
4746
* <ul>
4847
* <li> DataPixelsClassCode </li>
4948
* <li> DataPixelsClassCodeInternal </li>
50-
* <li> File </li>
51-
* <li> Image </li>
5249
* <li> IsExecuting </li>
5350
* </ul>
5451
*
@@ -60,8 +57,6 @@ const M = {
6057

6158
DataPixelsClassCode: undefined,
6259
DataPixelsClassCodeInternal: undefined,
63-
File: undefined,
64-
Image: undefined,
6560
IsExecuting: undefined
6661
};
6762

@@ -74,10 +69,10 @@ const M = {
7469
function init() {
7570

7671
readStates();
77-
initFileManagement();
78-
72+
7973
Content.init();
8074
Controls.init();
75+
File.init();
8176
}
8277

8378
/**
@@ -205,7 +200,7 @@ function toggleLayout() {
205200
* @description Displays either manual or automatically generated program code in the Code Editor.
206201
* @param {string} code - The code to display in the Code Editor.
207202
* @param {boolean} [autoMode = true] - Sets the application mode based on how the supplied code was produced.
208-
* @private
203+
* @public
209204
* @function
210205
*
211206
*/
@@ -387,222 +382,6 @@ function XHRErrorHandler(event) {
387382
setErrorMessage(`${C.Label.ERROR} ${C.Label.FILE_READ}`);
388383
}
389384

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-
606385
/**
607386
* @description Updates the format and indentation of the automatically generates program code.
608387
* @public

0 commit comments

Comments
 (0)