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 currentUser = 0 ;
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-10-26 22:50:09 +01:00
isComplete : 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 ) ;
var editIndex = htmlEntities ( document . getElementById ( "editIndex" ) . value ) ;
var errorMessageArea = document . getElementById ( "errorMessage" ) ;
var errorMessage = "" ;
var updateConflictArea = document . getElementById ( "updateConflict" ) ;
2015-10-27 23:36:24 +01: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
if ( editIndex != "" ) {
2015-11-07 07:51:44 +01:00
if ( WordAtIndexWasChanged ( editIndex , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) ) {
2015-10-26 22:50:09 +01:00
updateConflictArea . style . display = "block" ;
updateConflictArea . innerHTML = "<span id='updateConflictMessage'>Do you really want to change the word \"" + currentDictionary . words [ parseInt ( editIndex ) ] . name + "\" to what you have set above?</span>" ;
2015-11-07 07:51:44 +01:00
updateConflictArea . innerHTML += ' < button type = "button" id = "updateConfirmButton" \
2015-11-10 22:51:36 +01:00
onclick = " UpdateWord ( ' + editIndex + ' , \ '' + 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 > ' ;
2015-10-26 22:50:09 +01:00
updateConflictArea . innerHTML += '<button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(); return false;">No, Leave it</button>' ;
} else {
errorMessage = "No change has been made to \"" + word + "\"" ;
2015-10-27 23:36:24 +01:00
if ( currentDictionary . words [ parseInt ( editIndex ) ] . name != word ) {
errorMessage += ". (Your dictionary is currently set to ignore case.)"
}
2015-10-26 22:50:09 +01:00
}
} else if ( wordIndex >= 0 ) {
2015-11-07 07:51:44 +01:00
if ( WordAtIndexWasChanged ( wordIndex , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) ) {
2015-10-26 22:50:09 +01:00
updateConflictArea . style . display = "block" ;
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 > ' ;
2015-10-27 23:36:24 +01:00
updateConflictText += ' <button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(); return false;">No, Leave it</button>' ;
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 {
2015-11-08 00:24:50 +01:00
currentDictionary . words . push ( { name : word , pronunciation : pronunciation , partOfSpeech : partOfSpeech , simpleDefinition : simpleDefinition , longDefinition : longDefinition , wordId : currentDictionary . nextWordId ++ } ) ;
2015-11-10 01:53:01 +01:00
FocusAfterAddingNewWord ( ) ;
NewWordNotification ( word ) ;
2015-10-30 22:56:05 +01:00
SaveAndUpdateDictionary ( false ) ;
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
}
}
2015-10-27 23:36:24 +01:00
2015-10-26 22:50:09 +01:00
errorMessageArea . innerHTML = errorMessage ;
2015-10-26 05:41:25 +01:00
}
2015-11-07 07:51:44 +01:00
function WordAtIndexWasChanged ( indexString , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) {
2015-10-27 23:36:24 +01:00
return ( ! currentDictionary . settings . caseSensitive && currentDictionary . words [ parseInt ( indexString ) ] . name . toLowerCase ( ) != word . toLowerCase ( ) ) ||
( currentDictionary . settings . caseSensitive && currentDictionary . words [ parseInt ( indexString ) ] . name != word ) ||
2015-11-07 07:51:44 +01:00
currentDictionary . words [ parseInt ( indexString ) ] . pronunciation != pronunciation ||
currentDictionary . words [ parseInt ( indexString ) ] . partOfSpeech != partOfSpeech ||
2015-10-27 23:36:24 +01:00
currentDictionary . words [ parseInt ( indexString ) ] . simpleDefinition != simpleDefinition ||
2015-11-07 07:51:44 +01:00
currentDictionary . words [ parseInt ( indexString ) ] . longDefinition != longDefinition ;
2015-10-27 23:36:24 +01:00
}
2015-10-26 22:50:09 +01:00
function SaveScroll ( ) {
var doc = document . documentElement ;
var left = ( window . pageXOffset || doc . scrollLeft ) - ( doc . clientLeft || 0 ) ;
var top = ( window . pageYOffset || doc . scrollTop ) - ( doc . clientTop || 0 ) ;
savedScroll . x = left ;
savedScroll . y = top ;
}
function EditWord ( index ) {
SaveScroll ( ) ;
window . scroll ( 0 , 0 ) ;
ClearForm ( ) ;
document . getElementById ( "editIndex" ) . value = index . toString ( ) ;
document . getElementById ( "word" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . name ) ;
2015-11-07 07:51:44 +01:00
document . getElementById ( "pronunciation" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . pronunciation ) ;
document . getElementById ( "partOfSpeech" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . partOfSpeech ) ;
2015-10-26 22:50:09 +01:00
document . getElementById ( "simpleDefinition" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . simpleDefinition ) ;
document . getElementById ( "longDefinition" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . longDefinition ) ;
document . getElementById ( "newWordButtonArea" ) . style . display = "none" ;
document . getElementById ( "editWordButtonArea" ) . style . display = "block" ;
}
2015-10-30 22:56:05 +01:00
function SaveAndUpdateDictionary ( keepFormContents ) {
2015-11-07 18:40:28 +01:00
if ( ! currentDictionary . settings . sortByEquivalent ) {
currentDictionary . words . sort ( dynamicSort ( "name" ) ) ;
} else {
currentDictionary . words . sort ( dynamicSort ( "simpleDefinition" ) ) ;
}
2015-11-26 00:08:24 +01:00
SaveDictionary ( true , true ) ;
2015-10-30 22:56:05 +01:00
ShowDictionary ( ) ;
if ( ! keepFormContents ) {
ClearForm ( ) ;
}
CloseUpdateConflictArea ( ) ;
}
2015-11-07 07:51:44 +01:00
function UpdateWord ( wordIndex , word , pronunciation , partOfSpeech , simpleDefinition , longDefinition ) {
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 ;
currentDictionary . words [ wordIndex ] . partOfSpeech = partOfSpeech ;
2015-10-26 05:41:25 +01:00
currentDictionary . words [ wordIndex ] . simpleDefinition = simpleDefinition ;
currentDictionary . words [ wordIndex ] . longDefinition = longDefinition ;
2015-10-30 22:56:05 +01:00
SaveAndUpdateDictionary ( ) ;
2015-10-26 22:50:09 +01:00
window . scroll ( savedScroll . x , savedScroll . y ) ;
}
function DeleteWord ( index ) {
if ( document . getElementById ( "editIndex" ) . value != "" )
ClearForm ( ) ;
currentDictionary . words . splice ( index , 1 ) ;
2015-10-30 22:56:05 +01:00
SaveAndUpdateDictionary ( true ) ;
2015-10-26 05:41:25 +01:00
}
2015-10-28 05:54:27 +01:00
function UpdateFilter ( ) {
2015-10-30 22:56:05 +01:00
ShowDictionary ( ) ;
2015-10-28 05:54:27 +01:00
}
2015-10-30 22:56:05 +01:00
function ShowDictionary ( ) {
var filter = document . getElementById ( "wordFilter" ) . value ;
2015-10-28 05:54:27 +01:00
2015-10-30 22:56:05 +01:00
var searchResults = [ ] ;
var search = htmlEntities ( document . getElementById ( "searchBox" ) . value ) ;
2015-10-30 20:23:04 +01:00
if ( search != "" ) {
2015-11-07 07:51:44 +01:00
var xpath = [ ] ;
2015-10-30 20:23:04 +01:00
if ( document . getElementById ( "searchOptionWord" ) . checked ) {
2015-11-07 07:51:44 +01:00
xpath . push ( 'contains(name, "' + search + '")' ) ;
2015-10-30 20:23:04 +01:00
}
if ( document . getElementById ( "searchOptionSimple" ) . checked ) {
2015-11-07 07:51:44 +01:00
xpath . push ( 'contains(simpleDefinition, "' + search + '")' ) ;
2015-10-30 20:23:04 +01:00
}
if ( document . getElementById ( "searchOptionLong" ) . checked ) {
2015-11-07 07:51:44 +01:00
xpath . push ( 'contains(longDefinition, "' + search + '")' ) ;
2015-10-30 20:23:04 +01:00
}
2015-11-07 07:51:44 +01:00
searchResults = JSON . search ( currentDictionary , '//words[' + xpath . join ( ' or ' ) + ']/name' ) ;
2015-10-30 20:23:04 +01:00
}
2015-10-26 22:50:09 +01:00
var dictionaryNameArea = document . getElementById ( "dictionaryName" ) ;
dictionaryNameArea . innerHTML = htmlEntitiesParse ( currentDictionary . name ) + " Dictionary" ;
2015-10-28 22:53:59 +01:00
var dictionaryDescriptionArea = document . getElementById ( "dictionaryDescription" ) ;
2015-12-01 02:31:40 +01:00
dictionaryDescriptionArea . innerHTML = micromarkdown . parse ( htmlEntitiesParse ( currentDictionary . description ) , true ) ;
2015-10-28 22:53:59 +01:00
2015-10-26 05:41:25 +01:00
var dictionaryArea = document . getElementById ( "theDictionary" ) ;
var dictionaryText = "" ;
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words . length > 0 ) {
for ( var i = 0 ; i < currentDictionary . words . length ; i ++ ) {
2015-10-28 05:54:27 +01:00
if ( filter == "" || ( filter != "" && currentDictionary . words [ i ] . partOfSpeech == filter ) ) {
2015-11-10 22:51:36 +01:00
if ( search == "" || ( search != "" && searchResults . indexOf ( htmlEntities ( currentDictionary . words [ i ] . name ) ) >= 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 ) ;
}
2015-10-28 05:54:27 +01:00
}
2015-10-26 22:50:09 +01:00
}
} else {
dictionaryText = "There are no entries in the dictionary."
}
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
dictionaryArea . innerHTML = dictionaryText ;
}
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
function DictionaryEntry ( itemIndex ) {
2015-11-08 00:24:50 +01:00
var entryText = "<entry><a name='" + currentDictionary . words [ itemIndex ] . wordId + "'></a><a href='#" + currentDictionary . words [ itemIndex ] . wordId + "' class='wordLink clickable'>🔗</a>" ;
2015-10-30 23:45:29 +01:00
var searchTerm = htmlEntities ( document . getElementById ( "searchBox" ) . value ) ;
2015-10-30 23:56:29 +01:00
var searchRegEx = new RegExp ( searchTerm , "gi" ) ;
2015-10-26 05:41:25 +01:00
2015-10-30 23:56:29 +01:00
entryText += "<word>" + ( ( searchTerm != "" && document . getElementById ( "searchOptionWord" ) . checked ) ? currentDictionary . words [ itemIndex ] . name . replace ( searchRegEx , "<searchTerm>" + searchTerm + "</searchterm>" ) : currentDictionary . words [ itemIndex ] . name ) + "</word>" ;
2015-11-07 07:51:44 +01:00
if ( currentDictionary . words [ itemIndex ] . pronunciation != "" ) {
2015-12-01 02:31:40 +01:00
entryText += "<pronunciation>" + micromarkdown . parse ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . pronunciation ) ) . replace ( "<p>" , "" ) . replace ( "</p>" , "" ) + "</pronunciation>" ;
2015-11-07 07:51:44 +01:00
}
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words [ itemIndex ] . partOfSpeech != "" ) {
2015-11-07 07:51:44 +01:00
entryText += "<partofspeech>" + currentDictionary . words [ itemIndex ] . partOfSpeech + "</partofspeech>" ;
2015-10-26 22:50:09 +01:00
}
entryText += "<br>" ;
2015-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words [ itemIndex ] . simpleDefinition != "" ) {
2015-10-30 23:56:29 +01:00
entryText += "<simpledefinition>" + ( ( searchTerm != "" && document . getElementById ( "searchOptionSimple" ) . checked ) ? currentDictionary . words [ itemIndex ] . simpleDefinition . replace ( searchRegEx , "<searchTerm>" + searchTerm + "</searchterm>" ) : currentDictionary . words [ itemIndex ] . simpleDefinition ) + "</simpledefinition>" ;
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words [ itemIndex ] . longDefinition != "" ) {
2015-12-01 02:31:40 +01:00
entryText += "<longdefinition>" + ( ( searchTerm != "" && document . getElementById ( "searchOptionLong" ) . checked ) ? micromarkdown . parse ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . longDefinition ) ) . replace ( searchRegEx , "<searchTerm>" + searchTerm + "</searchterm>" ) : micromarkdown . parse ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . longDefinition ) ) ) + "</longdefinition>" ;
2015-10-26 22:50:09 +01:00
}
2015-10-30 18:38:41 +01:00
if ( ! currentDictionary . settings . isComplete ) {
entryText += ManagementArea ( itemIndex ) ;
}
2015-10-26 22:50:09 +01:00
entryText += "</entry>" ;
return entryText ;
}
function ManagementArea ( itemIndex ) {
var managementHTML = "<div class='management'>" ;
2015-10-28 22:53:59 +01:00
managementHTML += "<span class='clickable editButton' onclick='EditWord(" + itemIndex + ")'>Edit</span>" ;
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 ;
}
function SaveSettings ( ) {
2015-10-26 23:21:13 +01:00
if ( htmlEntities ( document . getElementById ( "dictionaryNameEdit" ) . value ) != "" ) {
currentDictionary . name = htmlEntities ( document . getElementById ( "dictionaryNameEdit" ) . value ) ;
}
2015-10-27 23:36:24 +01:00
2015-10-28 06:41:01 +01:00
currentDictionary . description = htmlEntities ( document . getElementById ( "dictionaryDescriptionEdit" ) . value ) ;
2015-10-27 23:36:24 +01:00
CheckForPartsOfSpeechChange ( ) ;
2015-10-30 18:38:41 +01:00
currentDictionary . settings . allowDuplicates = document . getElementById ( "dictionaryAllowDuplicates" ) . checked ;
currentDictionary . settings . caseSensitive = document . getElementById ( "dictionaryCaseSensitive" ) . checked ;
2015-11-07 18:40:28 +01:00
currentDictionary . settings . sortByEquivalent = document . getElementById ( "dictionarySortByEquivalent" ) . checked ;
2015-10-26 22:50:09 +01:00
currentDictionary . settings . isComplete = document . getElementById ( "dictionaryIsComplete" ) . checked ;
2015-10-27 23:36:24 +01:00
2015-10-30 18:38:41 +01:00
HideSettingsWhenComplete ( ) ;
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-11-28 07:17:52 +01:00
function CheckForPartsOfSpeechChange ( ) {
2015-10-27 23:36:24 +01:00
if ( htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) != currentDictionary . settings . partsOfSpeech ) {
if ( htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) != "" ) {
currentDictionary . settings . partsOfSpeech = htmlEntities ( document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value ) ;
SetPartsOfSpeech ( ) ;
}
}
}
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 ( ) ;
}
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 ( ) ;
var deleteDictionary = new XMLHttpRequest ( ) ;
deleteDictionary . open ( 'POST' , "php/ajax_dictionarymanagement.php?action=delete" ) ;
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 ( ) ;
document . getElementById ( 'loadAfterDeleteScreen' ) . style . display = 'block' ;
//Parse response into the list that forces you to load one and reload select in settings.
ParseUserDictionariesIntoSelect ( document . getElementById ( "loadAfterDelete" ) , deleteDictionary . responseText ) ;
ParseUserDictionariesIntoSelect ( document . getElementById ( "userDictionaries" ) , deleteDictionary . responseText ) ;
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 ) ;
}
2015-11-26 00:08:24 +01:00
function SaveDictionary ( sendToDatabase , sendWords ) {
2015-10-26 05:41:25 +01:00
localStorage . setItem ( 'dictionary' , JSON . stringify ( currentDictionary ) ) ;
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 ) {
2015-11-04 22:12:39 +01:00
sendWords = ( typeof sendWords !== 'undefined' ) ? sendWords : false ;
SendDictionary ( sendWords ) ;
}
SavePreviousDictionary ( ) ;
}
function SendDictionary ( sendWords ) {
sendWords = ( typeof sendWords !== 'undefined' ) ? sendWords : false ;
var action = "" ;
var postString = "" ;
if ( currentDictionary . externalID > 0 ) {
action = "update" ;
postString = DataToSend ( sendWords ) ;
} else {
action = "new" ;
2015-12-01 00:08:43 +01:00
postString = DataToSend ( true , 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 ( ) ;
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 ) ;
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
2015-11-05 01:47:54 +01:00
ProcessLoad ( ) ;
2015-11-04 22:12:39 +01:00
console . log ( "saved successfully" ) ;
}
return true ;
} else {
return false ;
}
}
sendDictionary . send ( postString ) ;
}
2015-12-01 00:08:43 +01:00
function DataToSend ( doSendWords , sendAll ) {
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-11-28 07:17:52 +01:00
data += "&partsofspeech=" + encodeURIComponent ( currentDictionary . settings . partsOfSpeech ) + "&sortbyequivalent=" + ( ( currentDictionary . settings . sortByEquivalent ) ? "1" : "0" ) + "&iscomplete=" + ( ( currentDictionary . settings . isComplete ) ? "1" : "0" ) + "&ispublic=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 || doSendWords ) {
2015-11-04 22:12:39 +01:00
data += ( ( data == "" ) ? "" : "&" ) + "words=" + encodeURIComponent ( JSON . stringify ( currentDictionary . words ) ) ;
}
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" ) ;
}
data += ( ( data == "" ) ? "" : "&" ) + "ispublic=0" ;
}
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 ( ) ;
loadDictionary . open ( 'GET' , "php/ajax_dictionarymanagement.php?action=load" ) ;
loadDictionary . onreadystatechange = function ( ) {
if ( loadDictionary . readyState == 4 && loadDictionary . status == 200 ) {
if ( loadDictionary . responseText == "no dictionaries" ) {
2015-12-01 00:08:43 +01:00
// If there are no dictionaries in the database and there's one in memory, remove the id and send it as a new one.
currentDictionary . externalID = 0 ;
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 ) ;
SaveDictionary ( false , 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 ) {
2015-11-28 07:17:52 +01:00
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=" + userDictionariesSelect . value . toString ( ) ;
changeDictionaryRequest . onreadystatechange = function ( ) {
if ( changeDictionaryRequest . readyState == 4 && changeDictionaryRequest . status == 200 ) {
if ( changeDictionaryRequest . responseText == "no dictionaries" ) {
2015-12-01 00:08:43 +01:00
console . log ( changeDictionaryRequest . responseText ) ;
2015-11-28 07:17:52 +01:00
SendDictionary ( false ) ;
2015-12-01 00:08:43 +01:00
} else if ( changeDictionaryRequest . responseText . length < 60 ) {
2015-11-28 07:17:52 +01:00
console . log ( changeDictionaryRequest . responseText ) ;
2015-12-01 00:08:43 +01:00
} else {
2015-11-28 07:17:52 +01:00
currentDictionary = JSON . parse ( changeDictionaryRequest . responseText ) ;
SaveDictionary ( false , false ) ;
ProcessLoad ( ) ;
2015-12-01 00:08:43 +01:00
LoadUserDictionaries ( ) ;
2015-11-28 07:17:52 +01:00
HideSettings ( ) ;
}
}
}
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 ( ) ;
2015-11-05 01:47:54 +01:00
ShowDictionary ( ) ;
2015-10-27 23:36:24 +01:00
SetPartsOfSpeech ( ) ;
if ( currentDictionary . settings . isComplete ) {
document . getElementById ( "wordEntryForm" ) . style . display = "none" ;
}
2015-10-30 20:23:04 +01: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-11-04 22:12:39 +01:00
isComplete : currentDictionary . settings . isComplete
} ;
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
function ExportDictionary ( ) {
2015-12-01 00:08:43 +01:00
var downloadName = stripHtmlEntities ( currentDictionary . name ) . replace ( /\W/g , '' ) ;
2015-10-26 23:21:13 +01:00
if ( downloadName == "" ) {
downloadName = "export" ;
}
download ( downloadName + ".dict" , localStorage . getItem ( 'dictionary' ) ) ;
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 ) ;
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.
SaveDictionary ( true , true ) ;
2015-11-25 18:37:03 +01:00
ProcessLoad ( ) ;
HideSettings ( ) ;
2015-12-01 00:28:21 +01:00
document . getElementById ( "importFile" ) . value = "" ;
2015-11-25 18:37:03 +01:00
} else {
var errorString = "File is missing:" ;
if ( ! tmpDicitonary . hasOwnProperty ( "name" ) )
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 ) ;
}
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
}
}
}
2015-10-26 05:41:25 +01:00
function WordIndex ( word ) {
2015-10-30 20:23:04 +01:00
for ( var i = 0 ; i < currentDictionary . words . length ; i ++ )
{
if ( ( ! currentDictionary . settings . caseSensitive && currentDictionary . words [ i ] . name . toLowerCase ( ) == word . toLowerCase ( ) ) ||
( currentDictionary . settings . caseSensitive && currentDictionary . words [ i ] . name == word ) ) {
return i ;
2015-10-26 05:41:25 +01:00
}
}
return - 1 ;
2015-10-26 22:50:09 +01:00
}
function htmlEntities ( string ) {
return String ( string ) . replace ( /&/g , '&' ) . replace ( /</g , '<' ) . replace ( />/g , '>' ) . replace ( /"/g , '"' ) . replace ( /'/g , ''' ) . replace ( /\n/g , '<br>' ) ;
}
function htmlEntitiesParse ( string ) {
return String ( string ) . replace ( /&/g , '&' ) . replace ( /</g , '<' ) . replace ( />/g , '>' ) . replace ( /"/g , '"' ) . replace ( /'/g , "'" ) . replace ( /<br>/g , '\n' ) ;
}
2015-12-01 00:08:43 +01:00
function stripHtmlEntities ( string ) {
// This is for the export name.
return String ( string ) . replace ( /&/g , '' ) . replace ( /</g , '' ) . replace ( />/g , '' ) . replace ( /"/g , '' ) . replace ( /'/g , "" ) . replace ( /<br>/g , '' ) ;
}
2015-10-26 23:21:13 +01:00
function dynamicSort ( property ) {
/* Retrieved from http:/ / stackoverflow . com / a / 4760279
Usage : theArray . sort ( dynamicSort ( "objectProperty" ) ) ; * /
var sortOrder = 1 ;
if ( property [ 0 ] === "-" ) {
sortOrder = - 1 ;
property = property . substr ( 1 ) ;
}
return function ( a , b ) {
2015-11-11 00:11:10 +01:00
var result = ( a [ property ] . toLowerCase ( ) < b [ property ] . toLowerCase ( ) ) ? - 1 : ( a [ property ] . toLowerCase ( ) > b [ property ] . toLowerCase ( ) ) ? 1 : 0 ;
2015-10-26 23:21:13 +01:00
return result * sortOrder ;
}
}
2015-10-26 22:50:09 +01:00
function download ( filename , text ) {
/* Retrieved from http:/ / stackoverflow . com / a / 18197341 / 3508346
Usage : download ( 'test.txt' , 'Hello world!' ) ; * /
var element = document . createElement ( 'a' ) ;
element . setAttribute ( 'href' , 'data:text/plain;charset=utf-8,' + encodeURIComponent ( text ) ) ;
element . setAttribute ( 'download' , filename ) ;
element . style . display = 'none' ;
document . body . appendChild ( element ) ;
element . click ( ) ;
document . body . removeChild ( element ) ;
2015-10-26 05:41:25 +01:00
}