2015-10-30 22:56:05 +01:00
/* global markdown */
/* global Defiant */
2015-11-02 18:28:43 +01:00
2015-10-28 22:55:51 +01:00
var currentVersion = 0.2 ;
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-10-26 05:41:25 +01:00
words : [ ] ,
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-10-26 22:50:09 +01:00
isComplete : false
} ,
2015-10-27 23:36:24 +01:00
dictionaryImportVersion : currentVersion // This needs to always be last.
2015-10-26 22:50:09 +01:00
}
var defaultDictionaryJSON = JSON . stringify ( currentDictionary ) ; //Saves a stringifyed default dictionary.
var savedScroll = {
x : 0 ,
y : 0
2015-10-26 05:41:25 +01:00
}
2015-11-02 18:28:43 +01:00
var aboutText , termsText , privacyText ;
2015-10-26 05:41:25 +01:00
window . onload = function ( ) {
LoadDictionary ( ) ;
2015-10-26 22:50:09 +01:00
ClearForm ( ) ;
2015-10-30 23:13:13 +01:00
2015-11-02 18:28:43 +01:00
GetTextFile ( "README.md" ) ;
GetTextFile ( "TERMS.md" ) ;
GetTextFile ( "PRIVACY.md" ) ;
}
function GetTextFile ( filename ) {
2015-10-30 23:13:13 +01:00
var readmeFileRequest = new XMLHttpRequest ( ) ;
2015-11-02 18:28:43 +01:00
readmeFileRequest . open ( 'GET' , filename ) ;
2015-10-30 23:13:13 +01:00
readmeFileRequest . onreadystatechange = function ( ) {
if ( readmeFileRequest . readyState == 4 && readmeFileRequest . status == 200 ) {
2015-11-02 18:28:43 +01:00
if ( filename == "TERMS.md" ) {
termsText = markdown . toHTML ( readmeFileRequest . responseText ) ;
} else if ( filename == "PRIVACY.md" ) {
privacyText = markdown . toHTML ( readmeFileRequest . responseText ) ;
} else {
aboutText = markdown . toHTML ( readmeFileRequest . responseText ) ;
}
2015-10-30 23:13:13 +01:00
}
}
readmeFileRequest . send ( ) ;
2015-10-26 05:41:25 +01:00
}
function AddWord ( ) {
2015-10-26 22:50:09 +01:00
var word = htmlEntities ( document . getElementById ( "word" ) . value ) ;
var simpleDefinition = htmlEntities ( document . getElementById ( "simpleDefinition" ) . value ) ;
var longDefinition = htmlEntities ( document . getElementById ( "longDefinition" ) . value ) ;
var partOfSpeech = htmlEntities ( document . getElementById ( "partOfSpeech" ) . 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-10-27 23:36:24 +01:00
if ( WordAtIndexWasChanged ( editIndex , word , simpleDefinition , longDefinition , partOfSpeech ) ) {
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>" ;
updateConflictArea . innerHTML += '<button type="button" id="updateConfirmButton" onclick="UpdateWord(' + editIndex + ', \'' +
htmlEntities ( word ) + '\', \'' +
htmlEntities ( simpleDefinition ) + '\', \'' +
htmlEntities ( longDefinition ) + '\', \'' +
htmlEntities ( partOfSpeech ) + '\'); return false;">Yes, Update it</button>' ;
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-10-27 23:36:24 +01:00
if ( currentDictionary . words [ wordIndex ] . simpleDefinition != simpleDefinition || currentDictionary . words [ wordIndex ] . longDefinition != longDefinition || currentDictionary . words [ wordIndex ] . partOfSpeech != partOfSpeech ) {
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>" ;
updateConflictText += '<button type="button" id="updateConfirmButton" onclick="UpdateWord(' + wordIndex + ', \'' +
htmlEntities ( word ) + '\', \'' +
htmlEntities ( simpleDefinition ) + '\', \'' +
htmlEntities ( longDefinition ) + '\', \'' +
htmlEntities ( partOfSpeech ) + '\'); return false;">Yes, Update it</button>' ;
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-10-30 22:56:05 +01:00
currentDictionary . words . push ( { name : word , simpleDefinition : simpleDefinition , longDefinition : longDefinition , partOfSpeech : partOfSpeech } ) ;
SaveAndUpdateDictionary ( false ) ;
2015-10-26 05:41:25 +01:00
}
2015-10-30 22:56:05 +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-10-27 23:36:24 +01:00
function WordAtIndexWasChanged ( indexString , word , simpleDefinition , longDefinition , partOfSpeech ) {
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 ) ] . simpleDefinition != simpleDefinition ||
currentDictionary . words [ parseInt ( indexString ) ] . longDefinition != longDefinition ||
currentDictionary . words [ parseInt ( indexString ) ] . partOfSpeech != partOfSpeech ;
}
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 ) ;
document . getElementById ( "simpleDefinition" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . simpleDefinition ) ;
document . getElementById ( "longDefinition" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . longDefinition ) ;
document . getElementById ( "partOfSpeech" ) . value = htmlEntitiesParse ( currentDictionary . words [ index ] . partOfSpeech ) ;
document . getElementById ( "newWordButtonArea" ) . style . display = "none" ;
document . getElementById ( "editWordButtonArea" ) . style . display = "block" ;
}
2015-10-30 22:56:05 +01:00
function SaveAndUpdateDictionary ( keepFormContents ) {
currentDictionary . words . sort ( dynamicSort ( "name" ) ) ;
SaveDictionary ( ) ;
ShowDictionary ( ) ;
if ( ! keepFormContents ) {
ClearForm ( ) ;
}
CloseUpdateConflictArea ( ) ;
}
2015-10-26 22:50:09 +01:00
function UpdateWord ( wordIndex , word , simpleDefinition , longDefinition , partOfSpeech ) {
currentDictionary . words [ wordIndex ] . name = word ;
2015-10-26 05:41:25 +01:00
currentDictionary . words [ wordIndex ] . simpleDefinition = simpleDefinition ;
currentDictionary . words [ wordIndex ] . longDefinition = longDefinition ;
currentDictionary . words [ wordIndex ] . partOfSpeech = partOfSpeech ;
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 22:50:09 +01:00
}
function CloseUpdateConflictArea ( ) {
document . getElementById ( "updateConflict" ) . style . display = "none" ;
}
function ClearForm ( ) {
document . getElementById ( "word" ) . value = "" ;
document . getElementById ( "simpleDefinition" ) . value = "" ;
document . getElementById ( "longDefinition" ) . value = "" ;
document . getElementById ( "partOfSpeech" ) . value = "" ;
document . getElementById ( "editIndex" ) . value = "" ;
document . getElementById ( "newWordButtonArea" ) . style . display = "block" ;
document . getElementById ( "editWordButtonArea" ) . style . display = "none" ;
document . getElementById ( "errorMessage" ) . innerHTML = "" ;
document . getElementById ( "updateConflict" ) . style . display = "none" ;
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 != "" ) {
if ( document . getElementById ( "searchOptionWord" ) . checked ) {
2015-10-30 22:56:05 +01:00
var wordNameSearch = JSON . search ( currentDictionary , '//words[contains(name, "' + search + '")]/name' ) ;
for ( var i = 0 ; i < wordNameSearch . length ; i ++ ) {
searchResults . push ( wordNameSearch [ i ] ) ;
}
2015-10-30 20:23:04 +01:00
}
if ( document . getElementById ( "searchOptionSimple" ) . checked ) {
2015-10-30 22:56:05 +01:00
var simpleDefinitionSearch = JSON . search ( currentDictionary , '//words[contains(simpleDefinition, "' + search + '")]/name' ) ;
2015-10-30 20:23:04 +01:00
for ( var i = 0 ; i < simpleDefinitionSearch . length ; i ++ ) {
if ( searchResults . indexOf ( simpleDefinitionSearch [ i ] ) < 0 ) {
searchResults . push ( simpleDefinitionSearch [ i ] ) ;
}
}
}
if ( document . getElementById ( "searchOptionLong" ) . checked ) {
2015-10-30 22:56:05 +01:00
var longDefinitionSearch = JSON . search ( currentDictionary , '//words[contains(longDefinition, "' + search + '")]/name' ) ;
2015-10-30 20:23:04 +01:00
for ( var i = 0 ; i < longDefinitionSearch . length ; i ++ ) {
if ( searchResults . indexOf ( longDefinitionSearch [ i ] ) < 0 ) {
searchResults . push ( longDefinitionSearch [ i ] ) ;
}
}
}
}
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" ) ;
dictionaryDescriptionArea . innerHTML = markdown . toHTML ( htmlEntitiesParse ( currentDictionary . description ) ) ;
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-10-30 20:23:04 +01:00
if ( search == "" || ( search != "" && searchResults . indexOf ( currentDictionary . words [ i ] . name ) >= 0 ) ) {
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-28 22:53:59 +01:00
function ToggleDescription ( ) {
var descriptionToggle = document . getElementById ( "descriptionToggle" ) ;
var descriptionArea = document . getElementById ( "dictionaryDescription" ) ;
if ( descriptionArea . style . display == "none" ) {
descriptionArea . style . display = "block" ;
descriptionToggle . innerHTML = "Hide Description" ;
} else {
descriptionArea . style . display = "none" ;
descriptionToggle . innerHTML = "Show Description" ;
}
}
2015-10-26 22:50:09 +01:00
function DictionaryEntry ( itemIndex ) {
var entryText = "<entry>" ;
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-10-26 05:41:25 +01:00
2015-10-26 22:50:09 +01:00
if ( currentDictionary . words [ itemIndex ] . partOfSpeech != "" ) {
entryText += " <partofspeech>" + currentDictionary . words [ itemIndex ] . partOfSpeech + "</partofspeech>" ;
}
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-10-30 23:56:29 +01:00
entryText += "<longdefinition>" + ( ( searchTerm != "" && document . getElementById ( "searchOptionLong" ) . checked ) ? markdown . toHTML ( htmlEntitiesParse ( currentDictionary . words [ itemIndex ] . longDefinition ) ) . replace ( searchRegEx , "<searchTerm>" + searchTerm + "</searchterm>" ) : markdown . toHTML ( 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 ;
}
2015-11-02 18:28:43 +01:00
function ShowInfo ( text ) {
if ( text == "terms" ) {
document . getElementById ( "infoText" ) . innerHTML = termsText ;
} else if ( text == "privacy" ) {
document . getElementById ( "infoText" ) . innerHTML = privacyText ;
} else {
document . getElementById ( "infoText" ) . innerHTML = aboutText ;
}
document . getElementById ( "infoScreen" ) . style . display = "block" ;
2015-10-28 22:53:59 +01:00
}
2015-11-02 18:28:43 +01:00
function HideInfo ( ) {
document . getElementById ( "infoScreen" ) . style . display = "none" ;
2015-10-28 22:53:59 +01:00
}
2015-10-30 18:38:41 +01:00
function ToggleCaseSensitiveOption ( ) {
if ( document . getElementById ( "dictionaryAllowDuplicates" ) . checked ) {
document . getElementById ( "dictionaryCaseSensitive" ) . disabled = true ;
} else {
document . getElementById ( "dictionaryCaseSensitive" ) . disabled = false ;
}
}
2015-10-26 22:50:09 +01:00
function ShowSettings ( ) {
document . getElementById ( "settingsScreen" ) . style . display = "block" ;
document . getElementById ( "dictionaryNameEdit" ) . value = htmlEntitiesParse ( currentDictionary . name ) ;
2015-10-28 06:41:01 +01:00
document . getElementById ( "dictionaryDescriptionEdit" ) . value = htmlEntitiesParse ( currentDictionary . description ) ;
2015-10-27 23:36:24 +01:00
document . getElementById ( "dictionaryPartsOfSpeechEdit" ) . value = htmlEntitiesParse ( currentDictionary . settings . partsOfSpeech ) ;
2015-10-30 18:38:41 +01:00
document . getElementById ( "dictionaryAllowDuplicates" ) . checked = currentDictionary . settings . allowDuplicates ;
document . getElementById ( "dictionaryCaseSensitive" ) . checked = currentDictionary . settings . caseSensitive ;
2015-10-26 22:50:09 +01:00
document . getElementById ( "dictionaryIsComplete" ) . checked = currentDictionary . settings . isComplete ;
2015-10-30 20:23:04 +01:00
document . getElementById ( "numberOfWordsInDictionary" ) . innerHTML = currentDictionary . words . length . toString ( ) ;
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 ) ;
}
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-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-10-26 22:50:09 +01:00
}
2015-10-30 18:38:41 +01:00
function HideSettingsWhenComplete ( ) {
if ( currentDictionary . settings . isComplete ) {
document . getElementById ( "hideIfComplete" ) . style . display = "none" ;
} else {
document . getElementById ( "hideIfComplete" ) . style . display = "block" ;
}
}
2015-10-27 23:36:24 +01:00
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 ( ) ;
}
}
}
function SetPartsOfSpeech ( ) {
var partsOfSpeechSelect = document . getElementById ( "partOfSpeech" ) ;
2015-10-28 05:54:27 +01:00
var wordFilterSelect = document . getElementById ( "wordFilter" ) ;
2015-10-27 23:36:24 +01:00
if ( partsOfSpeechSelect . options . length > 0 ) {
for ( var i = partsOfSpeechSelect . options . length - 1 ; i >= 0 ; i -- ) {
partsOfSpeechSelect . removeChild ( partsOfSpeechSelect . options [ i ] ) ;
2015-10-28 05:54:27 +01:00
wordFilterSelect . removeChild ( wordFilterSelect . options [ i + 1 ] ) ;
2015-10-27 23:36:24 +01:00
}
}
var newPartsOfSpeech = htmlEntitiesParse ( currentDictionary . settings . partsOfSpeech ) . trim ( ) . split ( "," ) ;
for ( var j = 0 ; j < newPartsOfSpeech . length ; j ++ ) {
var partOfSpeechOption = document . createElement ( 'option' ) ;
partOfSpeechOption . appendChild ( document . createTextNode ( newPartsOfSpeech [ j ] . trim ( ) ) ) ;
partOfSpeechOption . value = newPartsOfSpeech [ j ] . trim ( ) ;
partsOfSpeechSelect . appendChild ( partOfSpeechOption ) ;
2015-10-28 05:54:27 +01:00
var wordFilterOption = document . createElement ( 'option' ) ;
wordFilterOption . appendChild ( document . createTextNode ( newPartsOfSpeech [ j ] . trim ( ) ) ) ;
wordFilterOption . value = newPartsOfSpeech [ j ] . trim ( ) ;
wordFilterSelect . appendChild ( wordFilterOption ) ;
2015-10-27 23:36:24 +01:00
}
}
2015-10-26 22:50:09 +01:00
function HideSettings ( ) {
document . getElementById ( "settingsScreen" ) . style . display = "none" ;
document . getElementById ( "wordEntryForm" ) . style . display = ( currentDictionary . settings . isComplete ) ? "none" : "block" ;
2015-10-26 05:41:25 +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?" ) ) {
currentDictionary = JSON . parse ( defaultDictionaryJSON ) ;
2015-10-30 22:56:05 +01:00
SaveAndUpdateDictionary ( false ) ;
2015-10-29 01:44:36 +01:00
SetPartsOfSpeech ( ) ;
2015-10-26 22:50:09 +01:00
HideSettings ( ) ;
}
}
2015-10-26 05:41:25 +01:00
function SaveDictionary ( ) {
localStorage . setItem ( 'dictionary' , JSON . stringify ( currentDictionary ) ) ;
2015-10-30 22:56:05 +01:00
//location.reload();
2015-10-26 05:41:25 +01:00
}
function LoadDictionary ( ) {
if ( localStorage . getItem ( 'dictionary' ) ) {
2015-10-26 22:50:09 +01:00
var tmpDictionary = JSON . parse ( localStorage . getItem ( 'dictionary' ) ) ;
if ( tmpDictionary . words . length > 0 ) {
currentDictionary = JSON . parse ( localStorage . getItem ( 'dictionary' ) ) ;
}
tmpDictionary = null ;
2015-10-26 05:41:25 +01:00
}
2015-10-30 18:38:41 +01:00
HideSettingsWhenComplete ( ) ;
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
// Update search snapshot
2015-10-30 22:56:05 +01:00
//dictionarySearchSnapshot = Defiant.getSnapshot(currentDictionary);
2015-10-26 05:41:25 +01:00
}
2015-10-26 22:50:09 +01:00
function ExportDictionary ( ) {
2015-10-26 23:21:13 +01:00
var downloadName = currentDictionary . name . replace ( /\W/g , '' ) ;
if ( downloadName == "" ) {
downloadName = "export" ;
}
download ( downloadName + ".dict" , localStorage . getItem ( 'dictionary' ) ) ;
2015-10-26 22:50:09 +01:00
}
function ImportDictionary ( ) {
if ( ! window . FileReader ) {
alert ( 'Your browser is not supported' ) ;
return false ;
}
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 ) {
2015-10-27 23:36:24 +01:00
if ( reader . result . substr ( reader . result . length - 30 ) == '"dictionaryImportVersion":' + currentVersion + '}' ) {
2015-10-26 22:50:09 +01:00
localStorage . setItem ( 'dictionary' , reader . result ) ;
document . getElementById ( "importFile" ) . value = "" ;
LoadDictionary ( ) ;
HideSettings ( ) ;
} else {
alert ( "Uploaded file is not compatible." ) ;
}
} else {
alert ( "Upload Failed" ) ;
}
reader = null ;
}
} else {
alert ( "You must add a file to import." ) ;
}
}
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-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 ) {
var result = ( a [ property ] < b [ property ] ) ? - 1 : ( a [ property ] > b [ property ] ) ? 1 : 0 ;
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
}