2015-11-28 07:17:52 +01:00
/* global markdown */
2015-10-30 22:56:05 +01:00
/* global Defiant */
2015-11-02 18:28:43 +01:00
2015-11-04 22:12:39 +01:00
var publicName = "Someone" ;
2015-10-26 22:50:09 +01:00
var currentDictionary = {
name : "New" ,
2015-10-28 06:41:01 +01:00
description : "A new dictionary." ,
2015-11-05 00:43:52 +01:00
createdBy : publicName ,
2015-10-26 05:41:25 +01:00
words : [ ] ,
2015-11-08 00:24:50 +01:00
nextWordId : 1 ,
2015-10-26 05:41:25 +01:00
settings : {
2015-10-30 18:38:41 +01:00
allowDuplicates : false ,
2015-10-26 05:41:25 +01:00
caseSensitive : false ,
2015-10-27 23:36:24 +01:00
partsOfSpeech : "Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction" ,
2015-11-07 18:40:28 +01:00
sortByEquivalent : false ,
2015-12-22 01:52:56 +01:00
isComplete : false ,
isPublic : false
2015-11-26 00:08:24 +01:00
} ,
externalID : 0
2015-10-26 22:50:09 +01:00
}
var defaultDictionaryJSON = JSON . stringify ( currentDictionary ) ; //Saves a stringifyed default dictionary.
2015-11-26 00:08:24 +01:00
var previousDictionary = { } ;
2015-10-26 22:50:09 +01:00
var savedScroll = {
x : 0 ,
y : 0
2015-10-26 05:41:25 +01:00
}
function AddWord ( ) {
2015-11-07 18:40:28 +01:00
var word = htmlEntities ( document . getElementById ( "word" ) . value ) . trim ( ) ;
var pronunciation = htmlEntities ( document . getElementById ( "pronunciation" ) . value ) . trim ( ) ;
var partOfSpeech = htmlEntities ( document . getElementById ( "partOfSpeech" ) . value ) . trim ( ) ;
var simpleDefinition = htmlEntities ( document . getElementById ( "simpleDefinition" ) . value ) . trim ( ) ;
2015-10-26 22:50:09 +01:00
var longDefinition = htmlEntities ( document . getElementById ( "longDefinition" ) . value ) ;
2016-06-07 19:44:36 +02:00
// var editIndex = htmlEntities(document.getElementById("editIndex").value);
2015-10-26 22:50:09 +01:00
var errorMessageArea = document . getElementById ( "errorMessage" ) ;
var errorMessage = "" ;
var updateConflictArea = document . getElementById ( "updateConflict" ) ;
2016-07-11 06:53:21 +02:00
2015-10-26 05:41:25 +01:00
if ( word != "" && ( simpleDefinition != "" || longDefinition != "" ) ) {
2015-10-30 20:23:04 +01:00
var wordIndex = ( ! currentDictionary . settings . allowDuplicates ) ? WordIndex ( word ) : - 1 ;
2015-10-26 22:50:09 +01:00
2016-06-07 19:44:36 +02:00
if ( wordIndex >= 0 ) {
2015-11-07 07:51:44 +01:00
if ( WordAtIndexWasChanged ( wordIndex , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) ) {
2015-12-17 02:13:45 +01:00
document . getElementById ( "newWordButtonArea" ) . style . display = "none" ;
2016-06-16 01:48:52 +02:00
DisableForm ( '' ) ;
2015-10-26 22:50:09 +01:00
updateConflictArea . style . display = "block" ;
2016-07-11 06:53:21 +02:00
2015-10-27 23:36:24 +01:00
var updateConflictText = "<span id='updateConflictMessage'>\"" + word + "\" is already in the dictionary" ;
if ( currentDictionary . words [ wordIndex ] . name != word ) {
updateConflictText += " as \"" + currentDictionary . words [ wordIndex ] . name + "\", and your dictionary is set to ignore case." ;
} else {
updateConflictText += "."
}
updateConflictText += "<br>Do you want to update it to what you have set above?</span>" ;
2015-11-07 07:51:44 +01:00
updateConflictText += ' < button type = "button" id = "updateConfirmButton" \
2015-11-10 22:51:36 +01:00
onclick = " UpdateWord ( ' + wordIndex + ' , \ '' + htmlEntities ( word ) + '\', \'' + htmlEntities ( pronunciation ) + '\', \'' + htmlEntities ( partOfSpeech ) + '\', \'' + htmlEntities ( simpleDefinition ) + '\', \'' + htmlEntities ( longDefinition ) + '\' ) ; \
2015-11-07 07:51:44 +01:00
return false ; " > Yes , Update it < / b u t t o n > ' ;
2016-06-15 07:21:05 +02:00
updateConflictText += ' <button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(\'\'); return false;">No, Leave it</button>' ;
2016-07-11 06:53:21 +02:00
2015-10-27 23:36:24 +01:00
updateConflictArea . innerHTML = updateConflictText ;
2015-10-26 22:50:09 +01:00
} else {
2015-10-27 23:36:24 +01:00
errorMessage = "\"" + word + "\" is already in the dictionary exactly as it is written above" ;
if ( currentDictionary . words [ wordIndex ] . name != word ) {
errorMessage += ". (Your dictionary is currently set to ignore case.)"
}
2015-10-26 05:41:25 +01:00
}
} else {
2016-06-10 20:35:57 +02:00
currentDictionary . words . push ( { name : word , pronunciation : pronunciation , partOfSpeech : ( ( partOfSpeech . length > 0 ) ? partOfSpeech : " " ) , simpleDefinition : simpleDefinition , longDefinition : longDefinition , wordId : currentDictionary . nextWordId ++ } ) ;
2016-06-16 01:48:52 +02:00
SaveAndUpdateWords ( "new" ) ;
2015-11-10 01:53:01 +01:00
FocusAfterAddingNewWord ( ) ;
NewWordNotification ( word ) ;
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
errorMessageArea . innerHTML = "" ;
2015-10-26 05:41:25 +01:00
} else {
if ( word == "" ) {
2015-10-26 22:50:09 +01:00
errorMessage += "Word cannot be blank" ;
2015-10-26 05:41:25 +01:00
if ( simpleDefinition == "" && longDefinition == "" ) {
2015-10-26 22:50:09 +01:00
errorMessage += " and you need at least one definition." ;
2015-10-26 05:41:25 +01:00
} else {
2015-10-26 22:50:09 +01:00
errorMessage += "." ;
2015-10-26 05:41:25 +01:00
}
} else if ( simpleDefinition == "" && longDefinition == "" ) {
2015-10-26 22:50:09 +01:00
errorMessage += "You need at least one definition."
2015-10-26 05:41:25 +01:00
}
}
2016-07-11 06:53:21 +02:00
2015-10-26 22:50:09 +01:00
errorMessageArea . innerHTML = errorMessage ;
2015-10-26 05:41:25 +01:00
}
2016-06-07 19:44:36 +02:00
function ShowWordEditForm ( index ) {
var indexString = index . toString ( ) ; // Variable for reduced processing
var word = currentDictionary . words [ index ] ; // Reference for easier reading
var editForm = '<form id="editForm' + indexString + ' " > \
2016-07-06 23:17:50 +02:00
< h2 > Editing ' + word.name + ' < / h 2 > \
2016-06-07 19:44:36 +02:00
< label > < span > Word < / s p a n > \
< input type = "text" id = "word' + indexString + '" value = "' + htmlEntitiesParse(word.name) + '" onkeydown = "SubmitWordOnCtrlEnter(this)" / > \
< / l a b e l > \
2016-06-29 18:40:10 +02:00
< label > < span > Pronunciation < a class = "clickable inline-button" href = "http://r12a.github.io/pickers/ipa/" target = "_blank" title = "IPA Character Picker located at http://r12a.github.io/pickers/ipa/" > IPA Characters < / a > < / s p a n > \
2016-06-07 19:44:36 +02:00
< input type = "text" id = "pronunciation' + indexString + '" value = "' + htmlEntitiesParse(word.pronunciation) + '" onkeydown = "SubmitWordOnCtrlEnter(this)" / > \
< / l a b e l > \
< label > < span > Part of Speech < / s p a n > \
2016-06-10 20:35:57 +02:00
< select id = "partOfSpeech' + indexString + '" onkeydown = "SubmitWordOnCtrlEnter(this)" > < / s e l e c t > \
2016-06-07 19:44:36 +02:00
< / l a b e l > \
2016-06-29 21:27:14 +02:00
< label > < span > Definition / Equivalent Word ( s ) < / s p a n > \
2016-06-07 19:44:36 +02:00
< input type = "text" id = "simpleDefinition' + indexString + '" value = "' + htmlEntitiesParse(word.simpleDefinition) + '" onkeydown = "SubmitWordOnCtrlEnter(this)" / > \
< / l a b e l > \
2016-06-22 16:26:54 +02:00
< label > < span > Explanation / Long Definition < span id = "showFullScreenTextbox" class = "clickable inline-button" onclick = "ShowFullScreenTextbox(\'longDefinition' + indexString + '\', \'Explanation/Long Definition\')" > Maximize < / s p a n > < / s p a n > \
< textarea id = "longDefinition' + indexString + '" class = "longDefinition" onkeydown = "SubmitWordOnCtrlEnter(this)" > ' + htmlEntitiesParse(word.longDefinition) + ' < / t e x t a r e a > \
2016-06-07 19:44:36 +02:00
< / l a b e l > \
< span id = "errorMessage' + indexString + '" > < / s p a n > \
< div id = "editWordButtonArea' + indexString + '" style = "display: block;" > \
< button type = "button" onclick = "EditWord(\'' + indexString + '\'); return false;" > Edit Word < / b u t t o n > < b u t t o n t y p e = " b u t t o n " o n c l i c k = " C a n c e l E d i t F o r m ( ' + i n d e x S t r i n g + ' ) ; r e t u r n f a l s e ; " > C a n c e l < / b u t t o n > \
< / d i v > \
< div id = "updateConflict' + indexString + '" style = "display: none;" > < / d i v > \
< / f o r m > ' ;
document . getElementById ( "entry" + indexString ) . innerHTML = editForm ;
SetPartsOfSpeech ( "partOfSpeech" + indexString ) ;
2016-06-10 20:35:57 +02:00
document . getElementById ( "partOfSpeech" + indexString ) . value = htmlEntitiesParse ( word . partOfSpeech ) ;
2016-07-11 23:39:31 +02:00
document . getElementById ( "word" + indexString ) . focus ( ) ;
2016-06-07 19:44:36 +02:00
}
2015-10-26 22:50:09 +01:00
2016-06-07 19:44:36 +02:00
function CancelEditForm ( index ) {
document . getElementById ( "entry" + index . toString ( ) ) . innerHTML = DictionaryEntry ( index ) . replace ( "<entry id='entry" + index . toString ( ) + "'>" , "" ) . replace ( "</entry>" , "" ) ;
}
2015-10-26 22:50:09 +01:00
2016-06-07 19:44:36 +02:00
function EditWord ( indexString ) {
var word = htmlEntities ( document . getElementById ( "word" + indexString ) . value ) . trim ( ) ;
var pronunciation = htmlEntities ( document . getElementById ( "pronunciation" + indexString ) . value ) . trim ( ) ;
var partOfSpeech = htmlEntities ( document . getElementById ( "partOfSpeech" + indexString ) . value ) . trim ( ) ;
var simpleDefinition = htmlEntities ( document . getElementById ( "simpleDefinition" + indexString ) . value ) . trim ( ) ;
var longDefinition = htmlEntities ( document . getElementById ( "longDefinition" + indexString ) . value ) ;
2016-06-15 07:21:05 +02:00
2016-06-07 19:44:36 +02:00
var errorMessageArea = document . getElementById ( "errorMessage" + indexString ) ;
var errorMessage = "" ;
var updateConflictArea = document . getElementById ( "updateConflict" + indexString ) ;
if ( WordAtIndexWasChanged ( indexString , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) ) {
document . getElementById ( "editWordButtonArea" + indexString ) . style . display = "none" ;
2016-06-15 07:21:05 +02:00
DisableForm ( indexString ) ;
2016-06-07 19:44:36 +02:00
updateConflictArea . style . display = "block" ;
2016-06-15 07:21:05 +02:00
updateConflictArea . innerHTML = "<span id='updateConflictMessage" + indexString + "'>Do you really want to change the word \"" + currentDictionary . words [ parseInt ( indexString ) ] . name + "\" to what you have set above?</span><br>" ;
2016-06-07 19:44:36 +02:00
updateConflictArea . innerHTML += '<button type="button" id="updateConfirmButton' + indexString + ' " \
onclick = " UpdateWord ( ' + indexString + ' , \ '' + htmlEntities ( word ) + '\', \'' + htmlEntities ( pronunciation ) + '\', \'' + htmlEntities ( partOfSpeech ) + '\', \'' + htmlEntities ( simpleDefinition ) + '\', \'' + htmlEntities ( longDefinition ) + '\' ) ; \
return false ; " > Yes , Update it < / b u t t o n > ' ;
2016-06-15 07:21:05 +02:00
updateConflictArea . innerHTML += '<button type="button" id="updateCancelButton' + indexString + '" onclick="CloseUpdateConflictArea(\'' + indexString + '\'); return false;">No, Leave it</button>' ;
2016-06-07 19:44:36 +02:00
} else {
errorMessage = "No change has been made to \"" + word + "\"" ;
if ( currentDictionary . words [ parseInt ( indexString ) ] . name != word ) {
errorMessage += ". (Your dictionary is currently set to ignore case.)" ;
}
}
2015-10-26 22:50:09 +01:00
2016-06-07 19:44:36 +02:00
errorMessageArea . innerHTML = errorMessage ;
2016-07-11 23:12:28 +02:00
if ( document . getElementById ( "updateConfirmButton" + indexString ) ) {
document . getElementById ( "updateConfirmButton" + indexString ) . focus ( ) ;
}
2015-10-26 22:50:09 +01:00
}
2015-11-07 07:51:44 +01:00
function UpdateWord ( wordIndex , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) {
2016-07-11 06:53:21 +02:00
SaveScroll ( ) ;
2015-10-26 22:50:09 +01:00
currentDictionary . words [ wordIndex ] . name = word ;
2015-11-07 07:51:44 +01:00
currentDictionary . words [ wordIndex ] . pronunciation = pronunciation ;
2016-06-10 20:35:57 +02:00
currentDictionary . words [ wordIndex ] . partOfSpeech = ( ( partOfSpeech . length > 0 ) ? partOfSpeech : " " ) ;
2015-10-26 05:41:25 +01:00
currentDictionary . words [ wordIndex ] . simpleDefinition = simpleDefinition ;
currentDictionary . words [ wordIndex ] . longDefinition = longDefinition ;
2016-03-11 16:14:13 +01:00
2016-06-15 02:16:41 +02:00
SaveAndUpdateWords ( "update" , wordIndex ) ;
2015-10-26 22:50:09 +01:00
window . scroll ( savedScroll . x , savedScroll . y ) ;
2016-03-11 16:14:13 +01:00
2016-03-18 17:12:06 +01:00
if ( ! wordFormIsLocked ( ) ) {
FocusAfterAddingNewWord ( ) ;
}
2015-10-26 22:50:09 +01:00
}
function DeleteWord ( index ) {
2016-06-16 01:48:52 +02:00
var deleteWord = new XMLHttpRequest ( ) ;
2016-06-20 06:58:27 +02:00
deleteWord . open ( 'POST' , "/php/ajax_dictionarymanagement.php?action=worddelete" ) ;
2016-06-16 01:48:52 +02:00
deleteWord . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
deleteWord . onreadystatechange = function ( ) {
if ( deleteWord . readyState == 4 && deleteWord . status == 200 ) {
2016-06-16 21:22:13 +02:00
if ( deleteWord . responseText == "deleted successfully" || deleteWord . responseText == "not signed in" ) {
2016-06-16 01:48:52 +02:00
currentDictionary . words . splice ( index , 1 ) ;
2016-07-11 06:53:21 +02:00
2016-06-16 21:22:13 +02:00
SaveWords ( false ) ;
2016-06-16 01:48:52 +02:00
}
console . log ( deleteWord . responseText ) ;
return true ;
} else {
return false ;
}
}
2016-06-21 21:40:20 +02:00
deleteWord . send ( "dict=" + currentDictionary . externalID . toString ( ) + "&word=" + currentDictionary . words [ index ] . wordId . toString ( ) ) ;
2015-10-26 05:41:25 +01:00
}
2015-10-30 22:56:05 +01:00
function ShowDictionary ( ) {
2016-05-27 01:37:37 +02:00
var filters = GetSelectedFilters ( ) ;
2016-07-11 06:53:21 +02:00
2015-10-30 22:56:05 +01:00
var searchResults = [ ] ;
2015-12-03 00:03:23 +01:00
var search = htmlEntitiesParseForSearchEntry ( document . getElementById ( "searchBox" ) . value ) ;
var searchByWord = document . getElementById ( "searchOptionWord" ) . checked ;
var searchBySimple = document . getElementById ( "searchOptionSimple" ) . checked ;
var searchByLong = document . getElementById ( "searchOptionLong" ) . checked ;
var searchIgnoreCase = ! document . getElementById ( "searchCaseSensitive" ) . checked ; //It's easier to negate case here instead of negating it every use since ignore case is default.
var searchIgnoreDiacritics = document . getElementById ( "searchIgnoreDiacritics" ) . checked ;
if ( search != "" && ( searchByWord || searchBySimple || searchByLong ) ) {
2015-11-07 07:51:44 +01:00
var xpath = [ ] ;
2015-12-03 00:03:23 +01:00
var searchDictionaryJSON = htmlEntitiesParseForSearch ( JSON . stringify ( currentDictionary ) ) ;
if ( searchIgnoreCase ) {
search = search . toLowerCase ( ) ;
2015-10-30 20:23:04 +01:00
}
2015-12-03 00:03:23 +01:00
if ( searchIgnoreDiacritics ) {
search = removeDiacritics ( search ) ;
searchDictionaryJSON = removeDiacritics ( searchDictionaryJSON ) ;
2015-10-30 20:23:04 +01:00
}
2015-12-03 00:03:23 +01:00
if ( searchByWord ) {
xpath . push ( 'contains(' + ( ( searchIgnoreCase ) ? 'name' : 'translate(name, "", "")' ) + ', "' + search + '")' ) ;
2015-10-30 20:23:04 +01:00
}
2015-12-03 00:03:23 +01:00
if ( searchBySimple ) {
xpath . push ( 'contains(' + ( ( searchIgnoreCase ) ? 'simpleDefinition' : 'translate(simpleDefinition, "", "")' ) + ', "' + search + '")' ) ;
}
if ( searchByLong ) {
xpath . push ( 'contains(' + ( ( searchIgnoreCase ) ? 'longDefinition' : 'translate(longDefinition, "", "")' ) + ', "' + search + '")' ) ;
}
var searchDictionary = JSON . parse ( searchDictionaryJSON ) ;
searchResults = JSON . search ( searchDictionary , '//words[' + xpath . join ( ' or ' ) + ']/wordId' ) ;
2015-10-30 20:23:04 +01:00
}
2016-07-11 06:53:21 +02:00
2015-10-26 22:50:09 +01:00
var dictionaryNameArea = document . getElementById ( "dictionaryName" ) ;
2016-07-06 23:17:50 +02:00
dictionaryNameArea . innerHTML = currentDictionary . name + " Dictionary" ;
2016-06-22 20:43:39 +02:00
if ( loggedIn && currentDictionary . settings . isPublic ) {
dictionaryNameArea . innerHTML += "<a href='/" + currentDictionary . externalID + "' target='_blank' id='dictionaryShareLink' class='clickable' title='Share Dictionary'>➦</a>" ;
}
2016-07-11 06:53:21 +02:00
2015-10-28 22:53:59 +01:00
var dictionaryDescriptionArea = document . getElementById ( "dictionaryDescription" ) ;
2016-07-07 01:10:29 +02:00
dictionaryDescriptionArea . innerHTML = marked ( htmlEntitiesParseForMarkdown ( currentDictionary . description ) ) ;
2016-07-11 06:53:21 +02:00
2015-10-26 05:41:25 +01:00
var dictionaryArea = document . getElementById ( "theDictionary" ) ;
var dictionaryText = "" ;
2016-01-18 20:14:36 +01:00
var numberOfWordsDisplayed = 0 ;
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words . length > 0 ) {
for ( var i = 0 ; i < currentDictionary . words . length ; i ++ ) {
2016-05-27 01:37:37 +02:00
if ( filters . length == 0 || ( filters . length > 0 && filters . indexOf ( currentDictionary . words [ i ] . partOfSpeech ) > - 1 ) ) {
2015-12-03 00:03:23 +01:00
if ( search == "" || ( search != "" && ( searchByWord || searchBySimple || searchByLong ) && searchResults . indexOf ( currentDictionary . words [ i ] . wordId ) >= 0 ) ) {
2015-11-08 00:24:50 +01:00
if ( ! currentDictionary . words [ i ] . hasOwnProperty ( "pronunciation" ) ) {
2015-11-07 07:51:44 +01:00
currentDictionary . words [ i ] . pronunciation = "" ; //Account for new property
}
2015-11-08 00:24:50 +01:00
if ( ! currentDictionary . words [ i ] . hasOwnProperty ( "wordId" ) ) {
currentDictionary . words [ i ] . wordId = i + 1 ; //Account for new property
}
2015-10-30 20:23:04 +01:00
dictionaryText += DictionaryEntry ( i ) ;
2016-01-18 20:14:36 +01:00
numberOfWordsDisplayed ++ ;
2015-10-30 20:23:04 +01:00
}
2015-10-28 05:54:27 +01:00
}
2015-10-26 22:50:09 +01:00
}
} else {
2016-07-07 01:10:29 +02:00
dictionaryText = "There are no entries in the dictionary." ;
2015-10-26 22:50:09 +01:00
}
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
dictionaryArea . innerHTML = dictionaryText ;
2016-01-18 20:14:36 +01:00
ShowFilterWordCount ( numberOfWordsDisplayed ) ;
2015-10-26 22:50:09 +01:00
}
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
function DictionaryEntry ( itemIndex ) {
2016-02-27 18:10:11 +01:00
var searchTerm = regexParseForSearch ( document . getElementById ( "searchBox" ) . value ) ;
2015-12-03 00:03:23 +01:00
var searchByWord = document . getElementById ( "searchOptionWord" ) . checked ;
var searchBySimple = document . getElementById ( "searchOptionSimple" ) . checked ;
var searchByLong = document . getElementById ( "searchOptionLong" ) . checked ;
var searchIgnoreCase = ! document . getElementById ( "searchCaseSensitive" ) . checked ; //It's easier to negate case here instead of negating it every use since ignore case is default.
var searchIgnoreDiacritics = document . getElementById ( "searchIgnoreDiacritics" ) . checked ;
2016-07-11 06:53:21 +02:00
2015-12-03 00:03:23 +01:00
var searchRegEx = new RegExp ( "(" + ( ( searchIgnoreDiacritics ) ? removeDiacritics ( searchTerm ) + "|" + searchTerm : searchTerm ) + ")" , "g" + ( ( searchIgnoreCase ) ? "i" : "" ) ) ;
2015-10-26 05:41:25 +01:00
2016-06-20 06:58:27 +02:00
var wordName = wordPronunciation = wordPartOfSpeech = wordSimpleDefinition = wordLongDefinition = "" ;
2015-12-03 00:03:23 +01:00
if ( searchTerm != "" && searchByWord ) {
2016-06-22 01:42:01 +02:00
// Parse HTML Entities while searching so the regex can search actual characters instead of HTML.
2016-07-07 01:10:29 +02:00
wordName += htmlEntities ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . name ) . replace ( searchRegEx , "<searchterm>$1</searchterm>" ) ) . replace ( /<(\/?)searchterm>/g , '<$1searchterm>' ) ;
2015-12-03 00:03:23 +01:00
} else {
2016-06-22 01:42:01 +02:00
// Don't need to parse if not searching because HTML displays correctly anyway!
2016-06-20 06:58:27 +02:00
wordName += currentDictionary . words [ itemIndex ] . name . toString ( ) ; // Use toString() to prevent using a reference instead of the value.
2015-12-03 00:03:23 +01:00
}
2016-07-11 06:53:21 +02:00
2015-11-07 07:51:44 +01:00
if ( currentDictionary . words [ itemIndex ] . pronunciation != "" ) {
2016-07-07 01:10:29 +02:00
wordPronunciation += marked ( htmlEntitiesParseForMarkdown ( currentDictionary . words [ itemIndex ] . pronunciation ) ) . replace ( /<\/?p>/g , "" ) ;
2015-11-07 07:51:44 +01:00
}
2016-07-11 06:53:21 +02:00
2016-06-22 01:42:01 +02:00
if ( currentDictionary . words [ itemIndex ] . partOfSpeech != " " && currentDictionary . words [ itemIndex ] . partOfSpeech != "" ) {
2016-06-20 06:58:27 +02:00
wordPartOfSpeech += currentDictionary . words [ itemIndex ] . partOfSpeech . toString ( ) ;
2015-10-26 22:50:09 +01:00
}
2016-07-11 06:53:21 +02:00
if ( currentDictionary . words [ itemIndex ] . simpleDefinition != "" ) {
2015-12-03 00:03:23 +01:00
if ( searchTerm != "" && searchBySimple ) {
2016-07-07 01:10:29 +02:00
wordSimpleDefinition += htmlEntities ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . simpleDefinition ) . replace ( searchRegEx , "<searchterm>$1</searchterm>" ) ) . replace ( /<(\/?)searchterm>/g , '<$1searchterm>' ) ;
2015-12-03 00:03:23 +01:00
} else {
2016-06-20 06:58:27 +02:00
wordSimpleDefinition += currentDictionary . words [ itemIndex ] . simpleDefinition . toString ( ) ;
2015-12-03 00:03:23 +01:00
}
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words [ itemIndex ] . longDefinition != "" ) {
2015-12-03 00:03:23 +01:00
if ( searchTerm != "" && searchByLong ) {
2016-07-07 01:10:29 +02:00
wordLongDefinition += marked ( htmlEntitiesParseForMarkdown ( htmlEntities ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . longDefinition ) . replace ( searchRegEx , "<searchterm>$1</searchterm>" ) ) ) ) . replace ( /<(\/?)searchterm>\;/g , '<$1searchterm>' ) ;
2015-12-03 00:03:23 +01:00
} else {
2016-07-07 01:10:29 +02:00
wordLongDefinition += marked ( htmlEntitiesParseForMarkdown ( currentDictionary . words [ itemIndex ] . longDefinition ) ) ;
2015-12-03 00:03:23 +01:00
}
2015-10-26 22:50:09 +01:00
}
2016-06-20 06:58:27 +02:00
return DictionaryEntryTemplate ( {
name : wordName ,
pronunciation : wordPronunciation ,
partOfSpeech : wordPartOfSpeech ,
simpleDefinition : wordSimpleDefinition ,
longDefinition : wordLongDefinition ,
wordId : currentDictionary . words [ itemIndex ] . wordId . toString ( )
} , ( ! currentDictionary . settings . isComplete ) ? itemIndex : false ) ;
2015-10-26 22:50:09 +01:00
}
function ManagementArea ( itemIndex ) {
var managementHTML = "<div class='management'>" ;
2016-06-07 19:44:36 +02:00
managementHTML += "<span class='clickable editButton' onclick='ShowWordEditForm(" + itemIndex + ")'>Edit</span>" ;
2015-10-28 22:53:59 +01:00
managementHTML += "<span class='clickable deleteButton' onclick='document.getElementById(\"delete" + itemIndex + "Confirm\").style.display = \"block\";'>Delete</span>" ;
2015-10-26 22:50:09 +01:00
2015-10-30 18:38:41 +01:00
managementHTML += "<div class='deleteConfirm' id='delete" + itemIndex + "Confirm' style='display:none;'>Are you sure you want to delete this entry?<br><br>" ;
2015-10-28 22:53:59 +01:00
managementHTML += "<span class='clickable deleteCancelButton' onclick='document.getElementById(\"delete" + itemIndex + "Confirm\").style.display = \"none\";'>No</span>" ;
managementHTML += "<span class='clickable deleteConfirmButton' onclick='DeleteWord(" + itemIndex + ")'>Yes</span>" ;
2015-10-26 22:50:09 +01:00
managementHTML += "</div>" ;
managementHTML += "</div>" ;
return managementHTML ;
}
2016-06-20 06:58:27 +02:00
function DictionaryEntryTemplate ( wordObject , managementIndex ) {
managementIndex = ( typeof managementIndex !== 'undefined' ) ? managementIndex : false ;
var entryText = "<entry id='entry" ;
if ( managementIndex !== false ) {
// If there's a managementIndex, append index number to the element id.
entryText += managementIndex . toString ( ) ;
}
2016-06-21 01:13:51 +02:00
entryText += "'><a name='" + wordObject . wordId + "'></a>" ;
2016-06-22 20:43:39 +02:00
if ( loggedIn && currentDictionary . settings . isPublic ) {
2016-06-21 20:30:00 +02:00
entryText += "<a href='/" + currentDictionary . externalID + "/" + wordObject . wordId + "' target='_blank' class='wordLink clickable' title='Share Word' style='margin-left:5px;'>➦</a>" ;
2016-06-21 01:13:51 +02:00
}
entryText += "<a href='#" + wordObject . wordId + "' class='wordLink clickable' title='Link Within Page'>🔗</a>" ;
2016-07-11 06:53:21 +02:00
2016-06-20 06:58:27 +02:00
entryText += "<word>" + wordObject . name + "</word>" ;
2016-07-11 06:53:21 +02:00
2016-06-20 06:58:27 +02:00
if ( wordObject . pronunciation != "" ) {
entryText += "<pronunciation>" + wordObject . pronunciation + "</pronunciation>" ;
}
2016-07-11 06:53:21 +02:00
2016-06-20 06:58:27 +02:00
if ( wordObject . partOfSpeech != "" ) {
entryText += "<partofspeech>" + wordObject . partOfSpeech + "</partofspeech>" ;
}
entryText += "<br>" ;
if ( wordObject . simpleDefinition != "" ) {
entryText += "<simpledefinition>" + wordObject . simpleDefinition + "</simpledefinition>" ;
}
if ( wordObject . longDefinition != "" ) {
entryText += "<longdefinition>" + wordObject . longDefinition + "</longdefinition>" ;
}
if ( managementIndex !== false ) {
entryText += ManagementArea ( managementIndex ) ;
}
entryText += "</entry>" ;
return entryText ;
}
2015-10-26 22:50:09 +01:00
function SaveSettings ( ) {
2015-10-26 23:21:13 +01:00
if ( htmlEntities ( document . getElementById ( "dictionaryNameEdit" ) . value ) != "" ) {
currentDictionary . name = htmlEntities ( document . getElementById ( "dictionaryNameEdit" ) . value ) ;
}
2016-07-11 06:53:21 +02:00
2015-10-28 06:41:01 +01:00
currentDictionary . description = htmlEntities ( document . getElementById ( "dictionaryDescriptionEdit" ) . value ) ;
2016-07-11 06:53:21 +02:00
2015-10-27 23:36:24 +01:00
CheckForPartsOfSpeechChange ( ) ;
2016-07-11 06:53:21 +02:00
2015-10-30 18:38:41 +01:00
currentDictionary . settings . allowDuplicates = document . getElementById ( "dictionaryAllowDuplicates" ) . checked ;
currentDictionary . settings . caseSensitive = document . getElementById ( "dictionaryCaseSensitive" ) . checked ;
2016-07-11 06:53:21 +02:00
2015-11-07 18:40:28 +01:00
currentDictionary . settings . sortByEquivalent = document . getElementById ( "dictionarySortByEquivalent" ) . checked ;
2016-07-11 06:53:21 +02:00
2015-10-26 22:50:09 +01:00
currentDictionary . settings . isComplete = document . getElementById ( "dictionaryIsComplete" ) . checked ;
2015-12-22 19:20:33 +01:00
if ( document . getElementById ( "dictionaryIsPublic" ) ) {
currentDictionary . settings . isPublic = document . getElementById ( "dictionaryIsPublic" ) . checked ;
}
2016-07-11 06:53:21 +02:00
2015-10-30 18:38:41 +01:00
HideSettingsWhenComplete ( ) ;
2016-07-11 06:53:21 +02:00
2015-10-30 22:56:05 +01:00
SaveAndUpdateDictionary ( true ) ;
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
2015-10-30 18:38:41 +01:00
}
2015-10-26 22:50:09 +01:00
function EmptyWholeDictionary ( ) {
if ( confirm ( "This will delete the entire current dictionary. If you do not have a backed up export, you will lose it forever!\n\nDo you still want to delete?" ) ) {
2015-12-01 00:08:43 +01:00
CreateNewDictionary ( ) ;
2015-10-26 22:50:09 +01:00
}
}
2015-11-28 07:17:52 +01:00
function CreateNewDictionary ( ) {
ResetDictionaryToDefault ( ) ;
SaveAndUpdateDictionary ( false ) ;
SetPartsOfSpeech ( ) ;
HideSettings ( ) ;
2016-06-16 01:48:52 +02:00
ShowSettings ( ) ;
document . getElementById ( "dictionaryNameEdit" ) . focus ( ) ;
2015-11-28 07:17:52 +01:00
}
2015-12-01 00:08:43 +01:00
function DeleteCurrentDictionary ( ) {
if ( confirm ( "This will delete the current dictionary from the database. If you do not have a backed up export, you will lose it forever!\n\nDo you still want to delete?" ) ) {
ResetDictionaryToDefault ( ) ;
2016-07-11 06:53:21 +02:00
2015-12-01 00:08:43 +01:00
var deleteDictionary = new XMLHttpRequest ( ) ;
2016-06-20 06:58:27 +02:00
deleteDictionary . open ( 'POST' , "/php/ajax_dictionarymanagement.php?action=delete" ) ;
2015-12-01 00:08:43 +01:00
deleteDictionary . setRequestHeader ( "Content-type" , "application/x-www-form-urlencoded" ) ;
deleteDictionary . onreadystatechange = function ( ) {
if ( deleteDictionary . readyState == 4 && deleteDictionary . status == 200 ) {
if ( deleteDictionary . responseText . length < 31 ) {
console . log ( deleteDictionary . responseText ) ;
CreateNewDictionary ( ) ;
} else {
HideSettings ( ) ;
2015-12-02 17:47:17 +01:00
ShowDictionaryDeleteMenu ( deleteDictionary . responseText ) ;
2015-12-01 00:08:43 +01:00
if ( document . getElementById ( "loadAfterDelete" ) . options . length == 0 ) {
document . getElementById ( 'loadAfterDeleteScreen' ) . style . display = 'none' ;
CreateNewDictionary ( ) ;
}
}
return true ;
} else {
return false ;
}
}
deleteDictionary . send ( ) ;
}
}
2015-11-28 07:17:52 +01:00
function ResetDictionaryToDefault ( ) {
currentDictionary = JSON . parse ( defaultDictionaryJSON ) ;
}
2016-06-15 02:16:41 +02:00
function SaveAndUpdateWords ( action , wordIndex ) {
var dataToSend = "" ;
if ( action == "all" ) {
// For dictionaries not already in the db. Send all the words to database.
dataToSend = JSON . stringify ( currentDictionary . words ) ;
} else if ( action == "update" ) {
// Only send the specified word to update.
dataToSend = JSON . stringify ( currentDictionary . words [ wordIndex ] ) ;
} else if ( action == "new" ) {
// Send the last word pushed to the words array before it's sorted.
dataToSend = JSON . stringify ( currentDictionary . words [ currentDictionary . words . length - 1 ] ) ;
}
var sendWords = new XMLHttpRequest ( ) ;
2016-06-21 21:40:20 +02:00
sendWords . open ( 'POST' , "/php/ajax_dictionarymanagement.php?action=word" + action + "&dict=" + currentDictionary . externalID . toString ( ) + "&nextwordid=" + currentDictionary . nextWordId . toString ( ) ) ;
2016-06-15 02:16:41 +02:00
sendWords . setRequestHeader ( "Content-Type" , "application/json; charset=UTF-8" ) ;
sendWords . onreadystatechange = function ( ) {
if ( sendWords . readyState == 4 && sendWords . status == 200 ) {
2016-06-16 21:22:13 +02:00
SaveWords ( ) ;
ClearForm ( ) ;
2016-06-16 01:48:52 +02:00
console . log ( sendWords . responseText ) ;
2016-06-15 02:16:41 +02:00
return true ;
} else {
return false ;
}
}
sendWords . send ( dataToSend ) ;
}
2016-06-16 21:22:13 +02:00
function SaveWords ( ) {
if ( ! currentDictionary . settings . sortByEquivalent ) {
currentDictionary . words . sort ( dynamicSort ( [ 'name' , 'partOfSpeech' ] ) ) ;
} else {
currentDictionary . words . sort ( dynamicSort ( [ 'simpleDefinition' , 'partOfSpeech' ] ) ) ;
}
SaveDictionary ( false ) ;
ProcessLoad ( ) ;
}
2016-02-27 01:18:29 +01:00
function SaveAndUpdateDictionary ( keepFormContents ) {
2016-06-16 01:48:52 +02:00
SaveDictionary ( true ) ;
2016-02-27 01:18:29 +01:00
ShowDictionary ( ) ;
if ( ! keepFormContents ) {
ClearForm ( ) ;
}
2016-06-15 07:21:05 +02:00
CloseUpdateConflictArea ( '' ) ;
2016-02-27 01:18:29 +01:00
}
2016-06-15 02:16:41 +02:00
function SaveDictionary ( sendToDatabase ) {
2015-11-04 22:12:39 +01:00
//Always save local copy of current dictionary, but if logged in also send to database.
2015-11-26 00:08:24 +01:00
if ( sendToDatabase ) {
2016-06-15 02:16:41 +02:00
SendDictionary ( ) ;
2015-11-04 22:12:39 +01:00
}
2016-07-11 06:53:21 +02:00
2016-06-16 01:48:52 +02:00
localStorage . setItem ( 'dictionary' , JSON . stringify ( currentDictionary ) ) ;
2016-07-11 06:53:21 +02:00
2015-11-04 22:12:39 +01:00
SavePreviousDictionary ( ) ;
}
2016-06-15 02:16:41 +02:00
function SendDictionary ( ) {
2015-11-04 22:12:39 +01:00
var action = "" ;
var postString = "" ;
if ( currentDictionary . externalID > 0 ) {
action = "update" ;
2016-06-15 02:16:41 +02:00
postString = DataToSend ( false ) ;
2015-11-04 22:12:39 +01:00
} else {
action = "new" ;
2016-06-15 02:16:41 +02:00
postString = DataToSend ( true ) ;
2015-11-04 22:12:39 +01:00
}
2015-12-01 00:08:43 +01:00
2015-11-04 22:12:39 +01:00
var sendDictionary = new XMLHttpRequest ( ) ;
2016-06-20 06:58:27 +02:00
sendDictionary . open ( 'POST' , "/php/ajax_dictionarymanagement.php?action=" + action ) ;
2015-11-05 00:43:52 +01:00
sendDictionary . setRequestHeader ( "Content-type" , "application/x-www-form-urlencoded" ) ;
2015-11-04 22:12:39 +01:00
sendDictionary . onreadystatechange = function ( ) {
if ( sendDictionary . readyState == 4 && sendDictionary . status == 200 ) {
if ( sendDictionary . responseText == "updated successfully" ) {
console . log ( sendDictionary . responseText ) ;
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
ProcessLoad ( ) ;
} else if ( isNaN ( parseInt ( sendDictionary . responseText ) ) ) {
console . log ( sendDictionary . responseText ) ;
} else { // It will only be a number if it is a new dictionary.
2015-11-04 22:12:39 +01:00
currentDictionary . externalID = parseInt ( sendDictionary . responseText ) ;
2016-06-16 01:48:52 +02:00
if ( currentDictionary . words . length > 0 ) {
SaveAndUpdateWords ( "all" ) ;
}
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
2015-11-05 01:47:54 +01:00
ProcessLoad ( ) ;
2016-06-16 01:48:52 +02:00
console . log ( "saved " + parseInt ( sendDictionary . responseText ) . toString ( ) + " successfully" ) ;
2015-11-04 22:12:39 +01:00
}
return true ;
} else {
return false ;
}
}
sendDictionary . send ( postString ) ;
}
2016-06-15 02:16:41 +02:00
function DataToSend ( sendAll ) {
2015-12-01 00:08:43 +01:00
sendAll = ( typeof sendAll !== 'undefined' && sendAll != null ) ? sendAll : false ;
2015-11-04 22:12:39 +01:00
var data = "" ;
if ( currentDictionary . externalID == 0 ) {
data = "name=" + encodeURIComponent ( currentDictionary . name ) + "&description=" + encodeURIComponent ( currentDictionary . description ) + "&words=" + encodeURIComponent ( JSON . stringify ( currentDictionary . words ) ) ;
2015-12-01 00:08:43 +01:00
data += "&nextwordid=" + currentDictionary . nextWordId + "&allowduplicates=" + ( ( currentDictionary . settings . allowDuplicates ) ? "1" : "0" ) + "&casesensitive=" + ( ( currentDictionary . settings . caseSensitive ) ? "1" : "0" ) ;
2015-12-22 01:52:56 +01:00
data += "&partsofspeech=" + encodeURIComponent ( currentDictionary . settings . partsOfSpeech ) + "&sortbyequivalent=" + ( ( currentDictionary . settings . sortByEquivalent ) ? "1" : "0" ) + "&iscomplete=" + ( ( currentDictionary . settings . isComplete ) ? "1" : "0" ) + "&ispublic=" + ( ( currentDictionary . settings . isPublic ) ? "1" : "0" ) + "" ;
2015-11-04 22:12:39 +01:00
} else {
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . name != previousDictionary . name ) {
2015-11-04 22:12:39 +01:00
data += "name=" + encodeURIComponent ( currentDictionary . name ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . description != previousDictionary . description ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "description=" + encodeURIComponent ( currentDictionary . description ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . nextWordId != previousDictionary . nextWordId ) {
data += ( ( data == "" ) ? "" : "&" ) + "nextwordid=" + currentDictionary . nextWordId ;
}
if ( sendAll || currentDictionary . settings . allowDuplicates != previousDictionary . allowDuplicates ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "allowduplicates=" + ( ( currentDictionary . settings . allowDuplicates ) ? "1" : "0" ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . settings . caseSensitive != previousDictionary . caseSensitive ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "casesensitive=" + ( ( currentDictionary . settings . caseSensitive ) ? "1" : "0" ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . settings . partsOfSpeech != previousDictionary . partsOfSpeech ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "partsofspeech=" + encodeURIComponent ( currentDictionary . settings . partsOfSpeech ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . settings . sortByEquivalent != previousDictionary . sortByEquivalent ) {
2015-11-26 00:08:24 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "sortbyequivalent=" + ( ( currentDictionary . settings . sortByEquivalent ) ? "1" : "0" ) ;
}
2015-12-01 00:08:43 +01:00
if ( sendAll || currentDictionary . settings . isComplete != previousDictionary . isComplete ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "iscomplete=" + ( ( currentDictionary . settings . isComplete ) ? "1" : "0" ) ;
}
2015-12-22 01:52:56 +01:00
if ( sendAll || currentDictionary . settings . isPublic != previousDictionary . isPublic ) {
data += ( ( data == "" ) ? "" : "&" ) + "ispublic=" + ( ( currentDictionary . settings . isPublic ) ? "1" : "0" ) ;
}
2015-11-04 22:12:39 +01:00
}
return data ;
2015-10-26 05:41:25 +01:00
}
function LoadDictionary ( ) {
2015-11-05 00:43:52 +01:00
LoadLocalDictionary ( ) ;
2015-11-26 00:08:24 +01:00
var loadDictionary = new XMLHttpRequest ( ) ;
2016-06-20 06:58:27 +02:00
loadDictionary . open ( 'GET' , "/php/ajax_dictionarymanagement.php?action=load" ) ;
2015-11-26 00:08:24 +01:00
loadDictionary . onreadystatechange = function ( ) {
if ( loadDictionary . readyState == 4 && loadDictionary . status == 200 ) {
if ( loadDictionary . responseText == "no dictionaries" ) {
2015-12-22 19:20:33 +01:00
// If there are no dictionaries in the database and there's one in memory, remove the id & public setting and send it as a new one.
2015-12-01 00:08:43 +01:00
currentDictionary . externalID = 0 ;
2015-12-22 19:20:33 +01:00
currentDictionary . settings . isPublic = false ;
2015-12-01 00:08:43 +01:00
SendDictionary ( true ) ;
} else if ( loadDictionary . responseText . length < 60 ) {
console . log ( loadDictionary . responseText ) ;
} else {
2015-11-26 00:08:24 +01:00
currentDictionary = JSON . parse ( loadDictionary . responseText ) ;
2016-06-16 01:48:52 +02:00
SaveDictionary ( false ) ;
2015-11-04 22:12:39 +01:00
}
}
2015-12-01 00:08:43 +01:00
ProcessLoad ( ) ;
2015-11-05 00:43:52 +01:00
}
2015-11-26 00:08:24 +01:00
loadDictionary . send ( ) ;
2015-11-05 00:43:52 +01:00
}
2015-12-01 00:08:43 +01:00
function ChangeDictionary ( userDictionariesSelect ) {
userDictionariesSelect = ( typeof userDictionariesSelect !== 'undefined' && userDictionariesSelect != null ) ? userDictionariesSelect : document . getElementById ( "userDictionaries" ) ;
if ( currentDictionary . externalID != userDictionariesSelect . value && userDictionariesSelect . options . length > 0 ) {
2016-06-21 20:30:00 +02:00
// Show the info page with loading screen and hide settings and stuff.
ShowInfoWithText ( "<h1>Loading " + userDictionariesSelect . options [ userDictionariesSelect . selectedIndex ] . text + "...</h1>" ) ;
HideSettings ( ) ;
2016-06-21 01:13:51 +02:00
ChangeDictionaryToId ( userDictionariesSelect . value , function ( response ) {
if ( response == "no dictionaries" ) {
console . log ( response ) ;
2015-11-28 07:17:52 +01:00
SendDictionary ( false ) ;
2016-06-21 01:13:51 +02:00
} else if ( response . length < 60 ) {
console . log ( response ) ;
2015-12-01 00:08:43 +01:00
} else {
2016-06-21 01:13:51 +02:00
currentDictionary = JSON . parse ( response ) ;
2016-06-16 01:48:52 +02:00
SaveDictionary ( false ) ;
2015-11-28 07:17:52 +01:00
ProcessLoad ( ) ;
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
2016-06-21 20:30:00 +02:00
HideInfo ( ) ; // Hide the loading screen.
2015-11-28 07:17:52 +01:00
}
2016-06-21 01:13:51 +02:00
} ) ;
}
}
function ChangeDictionaryToId ( dictionaryId , callbackFunction ) {
var changeDictionaryRequest = new XMLHttpRequest ( ) ;
changeDictionaryRequest . open ( 'POST' , "/php/ajax_dictionarymanagement.php?action=switch" ) ;
changeDictionaryRequest . setRequestHeader ( "Content-type" , "application/x-www-form-urlencoded" ) ;
var postString = "newdictionaryid=" + dictionaryId . toString ( ) ;
changeDictionaryRequest . onreadystatechange = function ( ) {
if ( changeDictionaryRequest . readyState == 4 && changeDictionaryRequest . status == 200 ) {
callbackFunction ( changeDictionaryRequest . responseText ) ;
2015-11-28 07:17:52 +01:00
}
}
changeDictionaryRequest . send ( postString ) ;
}
2015-11-05 00:43:52 +01:00
function LoadLocalDictionary ( ) {
if ( localStorage . getItem ( 'dictionary' ) ) {
var tmpDictionary = JSON . parse ( localStorage . getItem ( 'dictionary' ) ) ;
2015-12-01 00:08:43 +01:00
if ( tmpDictionary . words . length > 0 || tmpDictionary . description != "A new dictionary." || tmpDictionary . name != "New" ) {
2015-11-05 00:43:52 +01:00
currentDictionary = JSON . parse ( localStorage . getItem ( 'dictionary' ) ) ;
2015-10-26 22:50:09 +01:00
}
2015-11-05 00:43:52 +01:00
tmpDictionary = null ;
2015-10-26 05:41:25 +01:00
}
2015-11-05 00:43:52 +01:00
}
function ProcessLoad ( ) {
2015-11-08 00:24:50 +01:00
if ( ! currentDictionary . hasOwnProperty ( "nextWordId" ) ) {
currentDictionary . nextWordId = currentDictionary . words . length + 1 ;
}
2015-11-25 18:37:03 +01:00
2015-10-30 18:38:41 +01:00
HideSettingsWhenComplete ( ) ;
2016-07-11 06:53:21 +02:00
2015-11-05 01:47:54 +01:00
ShowDictionary ( ) ;
2015-12-17 02:13:45 +01:00
2015-10-27 23:36:24 +01:00
SetPartsOfSpeech ( ) ;
2016-07-11 06:53:21 +02:00
2015-10-27 23:36:24 +01:00
if ( currentDictionary . settings . isComplete ) {
document . getElementById ( "wordEntryForm" ) . style . display = "none" ;
}
2016-07-11 06:53:21 +02:00
2015-11-04 22:12:39 +01:00
SavePreviousDictionary ( ) ;
}
function SavePreviousDictionary ( ) {
// Save non-word data to check if anything has changed (words can identify themselves if changed).
// Used to minimize data pushed to database.
previousDictionary = {
name : currentDictionary . name ,
description : currentDictionary . description ,
2015-12-01 00:08:43 +01:00
nextWordId : currentDictionary . nextWordId ,
2015-11-04 22:12:39 +01:00
allowDuplicates : currentDictionary . settings . allowDuplicates ,
caseSensitive : currentDictionary . settings . caseSensitive ,
partsOfSpeech : currentDictionary . settings . partsOfSpeech ,
2015-11-26 00:08:24 +01:00
sortByEquivalent : currentDictionary . settings . sortByEquivalent ,
2015-12-22 01:52:56 +01:00
isComplete : currentDictionary . settings . isComplete ,
isPublic : currentDictionary . settings . isPublic
2015-11-04 22:12:39 +01:00
} ;
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
function ExportDictionary ( ) {
2016-06-21 20:30:00 +02:00
var downloadName = removeDiacritics ( stripHtmlEntities ( currentDictionary . name ) ) . replace ( /\W/g , '' ) ;
if ( downloadName == "" ) {
downloadName = "export" ;
}
download ( downloadName + ".dict" , localStorage . getItem ( 'dictionary' ) ) ;
}
function ExportWords ( ) {
2016-03-11 20:29:39 +01:00
if ( currentDictionary . words . length > 0 ) {
var downloadName = removeDiacritics ( stripHtmlEntities ( currentDictionary . name ) ) . replace ( /\W/g , '' ) ;
if ( downloadName == "" ) {
downloadName = "export" ;
}
2016-06-21 20:30:00 +02:00
downloadName += "_words" ;
2016-06-29 21:27:14 +02:00
var wordsCSV = "word,pronunciation,part of speech,definition,explanation\n" ;
2016-06-21 20:30:00 +02:00
for ( var i = 0 ; i < currentDictionary . words . length ; i ++ ) {
2016-06-22 01:42:01 +02:00
var word = "\"" + htmlEntitiesParse ( currentDictionary . words [ i ] . name ) . trim ( ) . replace ( /\"/g , "\"\"" ) + "\"" ;
var pronunciation = "\"" + htmlEntitiesParse ( currentDictionary . words [ i ] . pronunciation ) . trim ( ) . replace ( /\"/g , "\"\"" ) + "\"" ;
var partOfSpeech = "\"" + htmlEntitiesParse ( currentDictionary . words [ i ] . partOfSpeech ) . trim ( ) . replace ( /\"/g , "\"\"" ) + "\"" ;
var simpleDefinition = "\"" + htmlEntitiesParse ( currentDictionary . words [ i ] . simpleDefinition ) . trim ( ) . replace ( /\"/g , "\"\"" ) + "\"" ;
var longDefinition = "\"" + htmlEntitiesParse ( currentDictionary . words [ i ] . longDefinition ) . replace ( /\"/g , "\"\"" ) + "\"" ;
2016-06-21 20:30:00 +02:00
wordsCSV += word + "," + pronunciation + "," + partOfSpeech + "," + simpleDefinition + "," + longDefinition + "\n" ;
}
download ( downloadName + ".csv" , wordsCSV ) ;
2016-03-11 20:29:39 +01:00
} else {
2016-06-22 01:42:01 +02:00
alert ( "Dictionary must have at least 1 word to export." ) ;
2015-10-26 23:21:13 +01:00
}
2015-10-26 22:50:09 +01:00
}
function ImportDictionary ( ) {
2015-12-01 00:08:43 +01:00
if ( currentDictionary . externalID > 0 || confirm ( "Importing this dictionary will overwrite your current one, making it impossible to retrieve if you have not already exported it! Do you still want to import?" ) ) {
2015-11-25 18:37:03 +01:00
if ( ! window . FileReader ) {
alert ( 'Your browser is not supported' ) ;
return false ;
}
2015-10-26 22:50:09 +01:00
2015-11-25 18:37:03 +01:00
var reader = new FileReader ( ) ;
if ( document . getElementById ( "importFile" ) . files . length > 0 ) {
var file = document . getElementById ( "importFile" ) . files [ 0 ] ;
// Read the file
reader . readAsText ( file ) ;
// When it's loaded, process it
reader . onloadend = function ( ) {
if ( reader . result && reader . result . length ) {
var tmpDicitonary = JSON . parse ( reader . result ) ;
2016-07-11 06:53:21 +02:00
2015-11-25 18:37:03 +01:00
if ( tmpDicitonary . hasOwnProperty ( "name" ) && tmpDicitonary . hasOwnProperty ( "description" ) &&
tmpDicitonary . hasOwnProperty ( "words" ) && tmpDicitonary . hasOwnProperty ( "settings" ) )
{
2015-12-01 00:28:21 +01:00
currentDictionary = JSON . parse ( reader . result ) ;
currentDictionary . externalID = 0 ; // Reset external id for imported dictionary.
2015-12-22 19:20:33 +01:00
currentDictionary . settings . isPublic = false ; // Reset public setting for imported dictionary.
2016-06-16 01:48:52 +02:00
SaveDictionary ( true ) ;
2015-11-25 18:37:03 +01:00
ProcessLoad ( ) ;
2016-06-21 20:30:00 +02:00
HideInfo ( ) ;
2015-11-25 18:37:03 +01:00
HideSettings ( ) ;
2015-12-01 00:28:21 +01:00
document . getElementById ( "importFile" ) . value = "" ;
2016-06-21 20:30:00 +02:00
NewNotification ( "Successfully Imported the \"" + currentDictionary . name + "\" Dictionary." ) ;
2015-11-25 18:37:03 +01:00
} else {
var errorString = "File is missing:" ;
2016-07-11 06:53:21 +02:00
if ( ! tmpDicitonary . hasOwnProperty ( "name" ) )
2015-11-25 18:37:03 +01:00
errorString += " name" ;
if ( ! tmpDicitonary . hasOwnProperty ( "description" ) )
errorString += " description" ;
if ( ! tmpDicitonary . hasOwnProperty ( "words" ) )
errorString += " words" ;
if ( ! tmpDicitonary . hasOwnProperty ( "settings" ) )
errorString += " settings" ;
alert ( "Uploaded file is not compatible.\n\n" + errorString ) ;
}
2016-07-11 06:53:21 +02:00
2015-11-25 18:37:03 +01:00
tmpDicitonary = null ;
2015-10-26 22:50:09 +01:00
} else {
2015-11-25 18:37:03 +01:00
alert ( "Upload Failed" ) ;
2015-10-26 22:50:09 +01:00
}
2015-11-25 18:37:03 +01:00
reader = null ;
2015-10-26 22:50:09 +01:00
}
2015-11-25 18:37:03 +01:00
} else {
alert ( "You must add a file to import." ) ;
2015-10-26 22:50:09 +01:00
}
}
}
2016-06-08 21:43:58 +02:00
function ImportWords ( ) {
if ( currentDictionary . externalID > 0 || confirm ( "This will add words in a correctly formatted CSV file to your currently loaded dictionary. Do you still want to import?" ) ) {
if ( ! window . FileReader ) {
alert ( 'Your browser is not supported' ) ;
return false ;
}
if ( document . getElementById ( "importWordsCSV" ) . files . length > 0 ) {
var file = document . getElementById ( "importWordsCSV" ) . files [ 0 ] ;
var resultsArea = document . getElementById ( "importOptions" ) ;
2016-06-22 01:42:01 +02:00
resultsArea . innerHTML = "<h3>Importing Words...</h3>" ;
2016-06-08 21:43:58 +02:00
2016-06-10 07:27:41 +02:00
var currentRow = 0 ; // Because of the header, the first row of data is always on line 2.
var rowsImported = 0 ;
2016-06-08 21:43:58 +02:00
Papa . parse ( file , {
header : true ,
step : function ( row , parser ) {
2016-06-10 07:27:41 +02:00
currentRow ++ ;
2016-06-29 21:27:14 +02:00
// If there are no errors OR the word and either definition or explanation contain data, then import it.
if ( ( row . data [ 0 ] . word . trim ( ) . length > 0 && ( row . data [ 0 ] . definition . trim ( ) . length > 0 || row . data [ 0 ] . explanation . trim ( ) . length > 0 ) ) || row . errors . length == 0 ) {
2016-06-21 20:30:00 +02:00
var wordName = htmlEntities ( row . data [ 0 ] [ "word" ] ) . trim ( ) ,
wordPronunciation = htmlEntities ( row . data [ 0 ] [ "pronunciation" ] ) . trim ( ) ,
wordPartOfSpeech = ( ( htmlEntities ( row . data [ 0 ] [ "part of speech" ] ) . trim ( ) . length > 0 ) ? htmlEntities ( row . data [ 0 ] [ "part of speech" ] ) . trim ( ) : " " ) ,
2016-06-29 21:27:14 +02:00
wordSimpleDefinition = htmlEntities ( row . data [ 0 ] [ "definition" ] ) . trim ( ) ,
2016-06-21 20:30:00 +02:00
wordLongDefinition = htmlEntities ( row . data [ 0 ] [ "explanation" ] ) . trim ( ) ,
wordId = currentDictionary . nextWordId ++ ;
currentDictionary . words . push ( { name : wordName , pronunciation : wordPronunciation , partOfSpeech : wordPartOfSpeech , simpleDefinition : wordSimpleDefinition , longDefinition : wordLongDefinition , wordId : wordId } ) ;
var wordEntry = DictionaryEntryTemplate ( currentDictionary . words [ currentDictionary . words . length - 1 ] ) ;
resultsArea . innerHTML += wordEntry ;
2016-06-10 07:27:41 +02:00
rowsImported ++ ;
2016-06-08 21:43:58 +02:00
} else {
2016-06-10 20:35:57 +02:00
// If it's not just an empty line, give an error.
if ( row . data [ 0 ] . word . trim ( ) . length > 0 ) {
for ( var i = 0 ; i < row . errors . length ; i ++ ) {
resultsArea . innerHTML += "<p>Error on record #" + currentRow . toString ( ) + ": " + row . errors [ i ] . message + "</p>" ;
}
2016-06-10 07:27:41 +02:00
}
2016-06-08 21:43:58 +02:00
}
2016-06-10 07:27:41 +02:00
// Scroll to the bottom.
2016-06-22 01:42:01 +02:00
document . getElementById ( "infoPage" ) . scrollTop = document . getElementById ( "infoPage" ) . scrollHeight ;
2016-06-08 21:43:58 +02:00
} ,
complete : function ( results ) {
2016-06-16 21:22:13 +02:00
SaveAndUpdateWords ( "all" ) ;
2016-06-10 07:27:41 +02:00
resultsArea . innerHTML += "<p>The file has finished importing " + rowsImported . toString ( ) + " words.</p>" ;
2016-06-21 20:30:00 +02:00
NewNotification ( "Imported " + rowsImported . toString ( ) + " words." ) ;
2016-06-10 07:27:41 +02:00
// Scroll to the bottom.
document . getElementById ( "importOptions" ) . scrollTop = document . getElementById ( "importOptions" ) . scrollHeight ;
document . getElementById ( "numberOfWordsInDictionary" ) . innerHTML = currentDictionary . words . length . toString ( ) ;
2016-06-08 21:43:58 +02:00
}
} ) ;
} else {
alert ( "You must add a file to import." ) ;
}
}
}
2016-06-20 06:58:27 +02:00
function WordIndex ( word , byId ) {
// Use byId = true to enter word id number instead of string.
2015-10-30 20:23:04 +01:00
for ( var i = 0 ; i < currentDictionary . words . length ; i ++ )
{
2016-06-20 06:58:27 +02:00
if ( ( ! byId && ( ! currentDictionary . settings . caseSensitive && currentDictionary . words [ i ] . name . toLowerCase ( ) == word . toLowerCase ( ) ) ||
( currentDictionary . settings . caseSensitive && currentDictionary . words [ i ] . name == word ) ) ||
( byId && currentDictionary . words [ i ] . wordId == word ) ) {
2015-10-30 20:23:04 +01:00
return i ;
2015-10-26 05:41:25 +01:00
}
}
return - 1 ;
2015-10-26 22:50:09 +01:00
}
2016-02-27 01:18:29 +01:00
function WordAtIndexWasChanged ( indexString , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) {
return ( ! currentDictionary . settings . caseSensitive && currentDictionary . words [ parseInt ( indexString ) ] . name . toLowerCase ( ) != word . toLowerCase ( ) ) ||
( currentDictionary . settings . caseSensitive && currentDictionary . words [ parseInt ( indexString ) ] . name != word ) ||
currentDictionary . words [ parseInt ( indexString ) ] . pronunciation != pronunciation ||
2016-07-11 23:12:28 +02:00
currentDictionary . words [ parseInt ( indexString ) ] . partOfSpeech . trim ( ) != partOfSpeech ||
2016-02-27 01:18:29 +01:00
currentDictionary . words [ parseInt ( indexString ) ] . simpleDefinition != simpleDefinition ||
currentDictionary . words [ parseInt ( indexString ) ] . longDefinition != longDefinition ;
}
function CheckForPartsOfSpeechChange ( ) {
if ( htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) != currentDictionary . settings . partsOfSpeech ) {
if ( htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) != "" ) {
currentDictionary . settings . partsOfSpeech = htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) ;
SetPartsOfSpeech ( ) ;
}
}
}