From 5ca47989d1117d760cfef9e8f37f6f548b0f8e6a Mon Sep 17 00:00:00 2001 From: Paoda Date: Fri, 8 Feb 2019 21:29:40 -0600 Subject: [PATCH] Completed Table Deletion and Regeneration --- src/components/Body.js | 80 ++----------------------- src/components/Body/Table.js | 80 +++++++++++++++++++++++++ src/components/Modal.js | 4 ++ src/components/Modal/SettingsManager.js | 47 ++++++++++++++- 4 files changed, 132 insertions(+), 79 deletions(-) diff --git a/src/components/Body.js b/src/components/Body.js index 36bc8d4..f9a8923 100644 --- a/src/components/Body.js +++ b/src/components/Body.js @@ -1,10 +1,6 @@ import React from "react"; -import Table from "./Body/Table"; -import Song from "../melodii/Song"; -import Filepath from "../melodii/Filepath"; -import Misc from "./MiscMethods"; +import Table, { generate, saveTable } from "./Body/Table"; import Emitter from "../melodii/Events"; -import Modal from "./Modal"; const Settings = window.require("electron-settings"); @@ -66,25 +62,11 @@ export default class Body extends React.Component { }; if (!Settings.has("tableJSON")) { - let tableJSON = await this.generate(template); + let tableJSON = await generate("C:\\Users\\Paoda\\Downloads", template); - this.setState({ - table: tableJSON - }); + this.setState({ table: tableJSON }); + saveTable(tableJSON); - let timestamp = Date.now(); - Settings.set("tableJSON", { - data: tableJSON, - timestamp: timestamp - }); - - let date = new Date(timestamp); - console.log( - "Table data created at: " + - date.toDateString() + - " at " + - date.toTimeString() - ); } else { console.log("Data Loaded from Persistent Storage Space"); @@ -101,58 +83,6 @@ export default class Body extends React.Component { ); } } - - /** - * Generates Table From Scratch - * @param {Array} template - * @return {Promise<} - * @async - */ - async generate(template) { - let filepath = new Filepath("C:\\Users\\Paoda\\Downloads"); - let list = await filepath.getValidFiles(); - console.log("Found " + list.length + "valid files."); - return new Promise(async (res, rej) => { - let temp = await this.generateBody( - template, - list, - 0, - list.length - 1 - ); - res(temp); - }); - } - - /** - * Generates Body of Table from Scratch - * @param {Object} tableArg Table Element - * @param {Array} arr Array of Valid Song Files - * @param {Number} start of Array - * @param {Number} end of Array - * @return {Object} Table Object with Body Completely parsed. - * @async - */ - async generateBody(tableArg, arr, start, end) { - let table = tableArg; - let t1 = performance.now(); - let dom = document.getElementById("bad-search-syntax"); - for (let i = 0; i <= end; i++) { - let song = new Song(arr[i]); - song = await Song.getMetadata(song); - table.tbody.push(Misc.formatMetadata(song, song.metadata)); - dom.innerHTML = "Creating Table Data: " + ~~((i / end) * 100) + "%"; - } - let t2 = performance.now(); - console.log( - "Time Taken (Table Data Creation): " + - Math.floor(t2 - t1) / 1000 + - "s" - ); - - return new Promise((res, rej) => { - res(table); - }); - } /** * Handles Key Presses @@ -229,8 +159,6 @@ export default class Body extends React.Component { } openSettings() { - const settings = document.querySelector('.settings-window'); Emitter.emit('loadModal'); - } } diff --git a/src/components/Body/Table.js b/src/components/Body/Table.js index 2c5161f..74a1ae4 100644 --- a/src/components/Body/Table.js +++ b/src/components/Body/Table.js @@ -3,6 +3,8 @@ import Song from "../../melodii/Song"; import MusicPlayer from "../../melodii/MusicPlayer"; import Misc from "../MiscMethods"; import Emitter from "../../melodii/Events"; +import Filepath from "../../melodii/Filepath"; +import Settings from 'electron-settings'; /** @type {HTMLElement} */ var active = document.createElement("tr"); @@ -177,3 +179,81 @@ export default class Table extends React.Component { } } } + + +/** + * Generates Table From Scratch + * @param {Array} template + * @return {Promise<} + * @async + */ +export async function generate(path, template) { + const filepath = new Filepath(path); + const list = await filepath.getValidFiles(); + + console.log("Found " + list.length + " valid files."); + + return new Promise(async (res, rej) => { + let temp = await generateBody( + template, + list, + 0, + list.length - 1 + ); + res(temp); + }); + + + /** + * Generates Body of Table from Scratch + * @param {Object} tableArg Table Element + * @param {Array} arr Array of Valid Song Files + * @param {Number} start of Array + * @param {Number} end of Array + * @return {Object} Table Object with Body Completely parsed. + * @async + */ + + async function generateBody(tableArg, arr, start, end) { + let table = tableArg; + const t1 = performance.now(); + let dom = document.getElementById("bad-search-syntax"); + + for (let i = 0; i <= end; i++) { + let song = new Song(arr[i]); + song= await Song.getMetadata(song); + + table.tbody.push(Misc.formatMetadata(song, song.metadata)); + dom.innerHTML = "Creating Table Data: " + ~~((i / end) * 100) + "%"; + } + + const t2 = performance.now(); + console.log( + "Time Taken (Table Data Creation): " + + Math.floor(t2 - t1) / 1000 + + "s" + ); + + return new Promise((res, rej) => { + res(table); + }); + } +} + + +export function saveTable(tableJSON) { + const stamp = Date.now(); + + Settings.set("tableJSON", { + data: tableJSON, + timestamp: stamp + }); + + let date = new Date(stamp); + console.log( + "Table data created at: " + + date.toDateString() + + " at " + + date.toTimeString() + ); +} diff --git a/src/components/Modal.js b/src/components/Modal.js index dcecae1..56cf474 100644 --- a/src/components/Modal.js +++ b/src/components/Modal.js @@ -1,6 +1,10 @@ import React from 'react'; import Emitter from '../melodii/Events'; +/** + * Brarebones Template for a Modal in Melodii + * - toggles between visible and not in order to dissapear and reappear. + */ export default class Modal extends React.Component { constructor(props) { super(props); diff --git a/src/components/Modal/SettingsManager.js b/src/components/Modal/SettingsManager.js index 786938a..6e4b389 100644 --- a/src/components/Modal/SettingsManager.js +++ b/src/components/Modal/SettingsManager.js @@ -1,24 +1,65 @@ import React from 'react'; import Modal from '../Modal'; +import Emitter from '../../melodii/Events'; +import { generate, saveTable } from '../Body/Table'; const Settings = window.require("electron-settings"); +/** + * The Extension of the Modal Class which is responsible for + * dealing with the settings, and therefore named SettingsManager + */ export default class SettingsManager extends Modal { constructor() { - super(); + super(); + this.template = { + thead: { + tr: ["Artist", "Title", "Album", "Year", "Genre", "Time"] //contains strings + }, + tbody: [] + }; + this.JSX =

Settings

-
; } + render() { return( ); } + + deleteTable() { + if (Settings.has("tableJSON")) Settings.delete("tableJSON"); + else console.warn("There was no Table Saved to Delete!"); + + Emitter.emit("newTable", this.template) + + } + + + /** + * Regenerates a Table + * TODO: Make this a function w/ the one inside of Body.js + */ + async regenTable() { + if (Settings.has("tableJSON")) Settings.delete("tableJSON"); + else console.warn("there was no Table Saved to Delete!") + + let tableJSON = await generate("C:\\Users\\Paoda\\Downloads", this.template); + + Emitter.emit("newTable", tableJSON); + + saveTable(tableJSON); + + } + } \ No newline at end of file