2016-09-21 22:40:25 +02:00
|
|
|
import './index.html';
|
2016-09-22 02:18:54 +02:00
|
|
|
import './sass/main.scss';
|
2016-09-21 22:40:25 +02:00
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
2016-09-22 02:18:54 +02:00
|
|
|
import {Header} from './components/Header';
|
2016-09-24 15:59:06 +02:00
|
|
|
import {WordForm} from './components/WordForm';
|
2016-09-22 16:25:49 +02:00
|
|
|
import {Button} from './components/Button';
|
2016-09-22 02:18:54 +02:00
|
|
|
import {Dictionary} from './components/Dictionary';
|
2016-09-22 22:05:49 +02:00
|
|
|
|
|
|
|
import {dynamicSort} from './js/helpers';
|
2016-09-21 22:40:25 +02:00
|
|
|
|
|
|
|
class Lexiconga extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-09-22 02:18:54 +02:00
|
|
|
|
2016-09-22 22:05:49 +02:00
|
|
|
this.showConsoleMessages = this.props.showConsoleMessages || false;
|
|
|
|
|
2016-09-22 02:18:54 +02:00
|
|
|
this.state = {
|
|
|
|
scroll: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
2016-09-22 15:42:07 +02:00
|
|
|
},
|
|
|
|
|
2016-09-22 22:05:49 +02:00
|
|
|
details: {
|
2016-09-22 15:42:07 +02:00
|
|
|
name: "New",
|
|
|
|
description: "A new dictionary.",
|
|
|
|
createdBy: 'Someone',
|
|
|
|
nextWordId: 1,
|
|
|
|
externalID: 0
|
2016-09-22 22:05:49 +02:00
|
|
|
},
|
|
|
|
words: [],
|
|
|
|
settings: {
|
|
|
|
allowDuplicates: false,
|
|
|
|
caseSensitive: false,
|
|
|
|
partsOfSpeech: "Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction",
|
|
|
|
sortByEquivalent: false,
|
|
|
|
isComplete: false,
|
|
|
|
isPublic: false
|
2016-09-23 02:08:14 +02:00
|
|
|
}
|
2016-09-22 15:42:07 +02:00
|
|
|
};
|
2016-09-22 02:18:54 +02:00
|
|
|
|
2016-09-22 15:42:07 +02:00
|
|
|
this.defaultDictionaryJSON = JSON.stringify(this.state.dictionaryDetails); //Saves a stringifyed default dictionary.
|
2016-09-22 02:18:54 +02:00
|
|
|
this.previousDictionary = {};
|
2016-09-21 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 16:25:49 +02:00
|
|
|
changeDictionaryName() {
|
|
|
|
// To change elements within the dictionary object, you can set it to
|
|
|
|
// a variable, manipulate the variable, then use setState() to set
|
|
|
|
// the object equal to the changed variable.
|
|
|
|
let updateDictionary = this.state.currentDictionary;
|
|
|
|
updateDictionary.name = 'something else'
|
|
|
|
this.setState({
|
|
|
|
currentDictionary: updateDictionary
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:08:14 +02:00
|
|
|
sortWords(array) {
|
|
|
|
let sortMethod;
|
|
|
|
if (this.state.settings.sortByEquivalent) {
|
|
|
|
sortMethod = ['simpleDefinition', 'partOfSpeech'];
|
|
|
|
} else {
|
|
|
|
sortMethod = ['name', 'partOfSpeech'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array.sort(dynamicSort(sortMethod));
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:05:49 +02:00
|
|
|
addWord(wordObject) {
|
|
|
|
let newWord = {
|
|
|
|
name: wordObject.name || 'errorWord',
|
|
|
|
pronunciation: wordObject.pronunciation || '',
|
|
|
|
partOfSpeech: wordObject.partOfSpeech || '',
|
|
|
|
simpleDefinition: wordObject.simpleDefinition || '',
|
|
|
|
longDefinition: wordObject.longDefinition || '',
|
|
|
|
wordId: this.state.details.nextWordId
|
|
|
|
}
|
|
|
|
|
|
|
|
let updatedWords = this.state.words.concat([newWord]);
|
2016-09-23 02:08:14 +02:00
|
|
|
updatedWords = this.sortWords(updatedWords);
|
2016-09-22 22:05:49 +02:00
|
|
|
|
|
|
|
let updatedDetails = this.state.details;
|
2016-09-24 16:58:09 +02:00
|
|
|
updatedDetails.nextWordId += 1;
|
2016-09-22 22:05:49 +02:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
words: updatedWords,
|
|
|
|
details: updatedDetails
|
|
|
|
}, () => {
|
|
|
|
if (this.showConsoleMessages) {
|
|
|
|
console.log('New word ' + newWord.name + ' added successfully');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
firstIndexWordWithId(id) {
|
|
|
|
let resultIndex = -1;
|
2016-09-23 02:08:14 +02:00
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
for (let i = 0; i < this.state.words.length; i++) {
|
|
|
|
let word = this.state.words[i];
|
2016-09-23 02:08:14 +02:00
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
if (word.wordId === id) {
|
|
|
|
resultIndex = i;
|
|
|
|
break;
|
2016-09-23 02:08:14 +02:00
|
|
|
}
|
2016-09-24 16:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultIndex;
|
2016-09-22 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
updateWord(wordId, wordObject) {
|
|
|
|
let index = this.firstIndexWordWithId(wordId);
|
2016-09-22 22:05:49 +02:00
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
if (index >= 0) {
|
|
|
|
if (this.showConsoleMessages) console.log('Updating ' + this.state.words[index].name + ' to ' + wordObject.name);
|
|
|
|
|
|
|
|
let updatedWords = this.state.words;
|
|
|
|
updatedWords[index].name = wordObject.name;
|
|
|
|
updatedWords[index].pronunciation = wordObject.pronunciation;
|
|
|
|
updatedWords[index].partOfSpeech = wordObject.partOfSpeech;
|
|
|
|
updatedWords[index].simpleDefinition = wordObject.simpleDefinition;
|
|
|
|
updatedWords[index].longDefinition = wordObject.longDefinition;
|
2016-09-23 02:08:14 +02:00
|
|
|
|
2016-09-24 16:58:09 +02:00
|
|
|
updatedWords = this.sortWords(updatedWords);
|
|
|
|
|
|
|
|
this.setState({words: updatedWords}, () => {
|
|
|
|
if (this.showConsoleMessages) {
|
|
|
|
console.log('Updated successfully');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Could not update. No word with id of ' + wordId.toString());
|
|
|
|
}
|
2016-09-22 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 22:40:25 +02:00
|
|
|
render() {
|
|
|
|
return (
|
2016-09-22 02:18:54 +02:00
|
|
|
<div>
|
|
|
|
<Header />
|
2016-09-24 15:59:06 +02:00
|
|
|
<WordForm addWord={(wordObject) => this.addWord(wordObject)} submitLabel='Add Word' />
|
2016-09-22 16:25:49 +02:00
|
|
|
<Button
|
|
|
|
action={() => this.changeDictionaryName()}
|
|
|
|
label='change name' />
|
2016-09-22 22:05:49 +02:00
|
|
|
|
|
|
|
<div id="incompleteNotice">
|
|
|
|
Dictionary is complete: {this.state.settings.isComplete.toString()}
|
|
|
|
</div>
|
2016-09-23 02:08:14 +02:00
|
|
|
<Dictionary
|
|
|
|
details={this.state.details}
|
|
|
|
words={this.state.words}
|
|
|
|
settings={this.state.settings}
|
2016-09-24 16:58:09 +02:00
|
|
|
updateWord={(wordId, wordObject) => this.updateWord(wordId, wordObject)} />
|
2016-09-22 02:18:54 +02:00
|
|
|
</div>
|
2016-09-21 22:40:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:05:49 +02:00
|
|
|
ReactDOM.render(<Lexiconga showConsoleMessages={true} />, document.getElementById('site'));
|