Completed Table Deletion and Regeneration

This commit is contained in:
Paoda 2019-02-08 21:29:40 -06:00
parent c92956854d
commit 5ca47989d1
4 changed files with 132 additions and 79 deletions

View File

@ -1,10 +1,6 @@
import React from "react"; import React from "react";
import Table from "./Body/Table"; import Table, { generate, saveTable } from "./Body/Table";
import Song from "../melodii/Song";
import Filepath from "../melodii/Filepath";
import Misc from "./MiscMethods";
import Emitter from "../melodii/Events"; import Emitter from "../melodii/Events";
import Modal from "./Modal";
const Settings = window.require("electron-settings"); const Settings = window.require("electron-settings");
@ -66,25 +62,11 @@ export default class Body extends React.Component {
}; };
if (!Settings.has("tableJSON")) { if (!Settings.has("tableJSON")) {
let tableJSON = await this.generate(template); let tableJSON = await generate("C:\\Users\\Paoda\\Downloads", template);
this.setState({ this.setState({ table: tableJSON });
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 { } else {
console.log("Data Loaded from Persistent Storage Space"); 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<String>} 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<String>} 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 * Handles Key Presses
@ -229,8 +159,6 @@ export default class Body extends React.Component {
} }
openSettings() { openSettings() {
const settings = document.querySelector('.settings-window');
Emitter.emit('loadModal'); Emitter.emit('loadModal');
} }
} }

View File

@ -3,6 +3,8 @@ import Song from "../../melodii/Song";
import MusicPlayer from "../../melodii/MusicPlayer"; import MusicPlayer from "../../melodii/MusicPlayer";
import Misc from "../MiscMethods"; import Misc from "../MiscMethods";
import Emitter from "../../melodii/Events"; import Emitter from "../../melodii/Events";
import Filepath from "../../melodii/Filepath";
import Settings from 'electron-settings';
/** @type {HTMLElement} */ /** @type {HTMLElement} */
var active = document.createElement("tr"); var active = document.createElement("tr");
@ -177,3 +179,81 @@ export default class Table extends React.Component {
} }
} }
} }
/**
* Generates Table From Scratch
* @param {Array<String>} 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<String>} 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()
);
}

View File

@ -1,6 +1,10 @@
import React from 'react'; import React from 'react';
import Emitter from '../melodii/Events'; 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 { export default class Modal extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);

View File

@ -1,24 +1,65 @@
import React from 'react'; import React from 'react';
import Modal from '../Modal'; import Modal from '../Modal';
import Emitter from '../../melodii/Events';
import { generate, saveTable } from '../Body/Table';
const Settings = window.require("electron-settings"); 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 { export default class SettingsManager extends Modal {
constructor() { constructor() {
super(); super();
this.template = {
thead: {
tr: ["Artist", "Title", "Album", "Year", "Genre", "Time"] //contains <th> strings
},
tbody: []
};
this.JSX = this.JSX =
<div className='settings-window'> <div className='settings-window'>
<form> <form>
<h3>Settings</h3> <h3>Settings</h3>
<label for="table-data"> <label htmlFor="table-data">
Delete Table Data: <input type="button" value="Delete" name="delete-table" id="delete-table-btn" /> Delete Table Data: <input onClick={this.deleteTable.bind(this)} type="button" value="Delete" name="delete-table" id="delete-table-btn" />
Regnerate Table Data: <input onClick={this.regenTable.bind(this)} type="button" value="Regenerate" name="regen-table" id="regen-table-btn" />
</label> </label>
</form> </form>
</div>; </div>;
} }
render() { render() {
return( return(
<Modal content={this.JSX} /> <Modal content={this.JSX} />
); );
} }
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);
}
} }