2015-12-22 01:52:56 +01:00
function IsValidPublicDicitonary ( ) {
return typeof publicDictionary !== 'string' ;
}
2016-06-21 01:13:51 +02:00
function ShowPublicDictionary ( ignoreFilters ) {
ignoreFilters = ( typeof ignoreFilters !== 'undefined' ) ? ignoreFilters : false ;
2015-12-22 01:52:56 +01:00
if ( IsValidPublicDicitonary ( ) ) {
2016-06-21 01:13:51 +02:00
var filters = ( ignoreFilters ) ? [ ] : GetSelectedFilters ( ) ;
2015-12-22 01:52:56 +01:00
var searchResults = [ ] ;
2016-06-21 01:13:51 +02:00
var search = ( ignoreFilters ) ? "" : htmlEntitiesParseForSearchEntry ( document . getElementById ( "searchBox" ) . value ) ;
var searchByWord = ( ignoreFilters ) ? null : document . getElementById ( "searchOptionWord" ) . checked ;
var searchBySimple = ( ignoreFilters ) ? null : document . getElementById ( "searchOptionSimple" ) . checked ;
var searchByLong = ( ignoreFilters ) ? null : document . getElementById ( "searchOptionLong" ) . checked ;
var searchIgnoreCase = ( ignoreFilters ) ? null : ! document . getElementById ( "searchCaseSensitive" ) . checked ; //It's easier to negate case here instead of negating it every use since ignore case is default.
var searchIgnoreDiacritics = ( ignoreFilters ) ? null : document . getElementById ( "searchIgnoreDiacritics" ) . checked ;
if ( ! ignoreFilters && search != "" && ( searchByWord || searchBySimple || searchByLong ) ) {
2015-12-22 01:52:56 +01:00
var xpath = [ ] ;
var searchDictionaryJSON = htmlEntitiesParseForSearch ( JSON . stringify ( publicDictionary ) ) ;
if ( searchIgnoreCase ) {
search = search . toLowerCase ( ) ;
//searchDictionaryJSON = searchDictionaryJSON.toLowerCase();
}
if ( searchIgnoreDiacritics ) {
search = removeDiacritics ( search ) ;
searchDictionaryJSON = removeDiacritics ( searchDictionaryJSON ) ;
}
if ( searchByWord ) {
xpath . push ( 'contains(' + ( ( searchIgnoreCase ) ? 'name' : 'translate(name, "", "")' ) + ', "' + search + '")' ) ;
}
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' ) ;
}
var dictionaryNameArea = document . getElementById ( "dictionaryName" ) ;
dictionaryNameArea . innerHTML = htmlEntitiesParse ( publicDictionary . name ) + " Dictionary" ;
var dictionaryByArea = document . getElementById ( "dictionaryBy" ) ;
dictionaryByArea . innerHTML = "created by " + htmlEntitiesParse ( publicDictionary . createdBy ) ;
2015-12-22 23:16:36 +01:00
var dictionaryIncompleteArea = document . getElementById ( "incompleteNotice" ) ;
if ( ! publicDictionary . settings . isComplete ) {
dictionaryIncompleteArea . innerHTML = "<em>Note: This dictionary is not yet complete and is likely to change.</em>" ;
}
2015-12-22 01:52:56 +01:00
var dictionaryDescriptionArea = document . getElementById ( "dictionaryDescription" ) ;
dictionaryDescriptionArea . innerHTML = marked ( htmlEntitiesParse ( publicDictionary . description ) ) ;
var dictionaryArea = document . getElementById ( "theDictionary" ) ;
var dictionaryText = "" ;
2016-01-18 20:14:36 +01:00
var numberOfWordsDisplayed = 0 ;
2015-12-22 01:52:56 +01:00
if ( publicDictionary . words . length > 0 ) {
for ( var i = 0 ; i < publicDictionary . words . length ; i ++ ) {
2016-05-27 02:10:17 +02:00
if ( filters . length == 0 || ( filters . length > 0 && filters . indexOf ( publicDictionary . words [ i ] . partOfSpeech ) > - 1 ) ) {
2015-12-22 01:52:56 +01:00
if ( search == "" || ( search != "" && ( searchByWord || searchBySimple || searchByLong ) && searchResults . indexOf ( publicDictionary . words [ i ] . wordId ) >= 0 ) ) {
if ( ! publicDictionary . words [ i ] . hasOwnProperty ( "pronunciation" ) ) {
publicDictionary . words [ i ] . pronunciation = "" ; //Account for new property
}
if ( ! publicDictionary . words [ i ] . hasOwnProperty ( "wordId" ) ) {
publicDictionary . words [ i ] . wordId = i + 1 ; //Account for new property
}
2016-06-21 01:13:51 +02:00
dictionaryText += PublicDictionaryEntry ( i , ignoreFilters ) ;
2016-01-18 20:14:36 +01:00
numberOfWordsDisplayed ++ ;
2015-12-22 01:52:56 +01:00
}
}
}
} else {
dictionaryText = "There are no entries in the dictionary."
}
dictionaryArea . innerHTML = dictionaryText ;
2016-06-21 01:13:51 +02:00
if ( ! ignoreFilters ) {
ShowFilterWordCount ( numberOfWordsDisplayed ) ;
}
2015-12-22 01:52:56 +01:00
} else {
2016-07-06 15:36:24 +02:00
document . getElementById ( "dictionaryContent" ) . innerHTML = publicDictionary ;
2015-12-22 01:52:56 +01:00
}
}
2016-06-21 01:13:51 +02:00
function PublicDictionaryEntry ( itemIndex , ignoreFilters ) {
var searchTerm = ( ignoreFilters ) ? "" : regexParseForSearch ( document . getElementById ( "searchBox" ) . value ) ;
var searchByWord = ( ignoreFilters ) ? false : document . getElementById ( "searchOptionWord" ) . checked ;
var searchBySimple = ( ignoreFilters ) ? false : document . getElementById ( "searchOptionSimple" ) . checked ;
var searchByLong = ( ignoreFilters ) ? false : document . getElementById ( "searchOptionLong" ) . checked ;
var searchIgnoreCase = ( ignoreFilters ) ? false : ! document . getElementById ( "searchCaseSensitive" ) . checked ; //It's easier to negate case here instead of negating it every use since ignore case is default.
var searchIgnoreDiacritics = ( ignoreFilters ) ? false : document . getElementById ( "searchIgnoreDiacritics" ) . checked ;
2015-12-22 01:52:56 +01:00
var searchRegEx = new RegExp ( "(" + ( ( searchIgnoreDiacritics ) ? removeDiacritics ( searchTerm ) + "|" + searchTerm : searchTerm ) + ")" , "g" + ( ( searchIgnoreCase ) ? "i" : "" ) ) ;
2016-06-21 01:13:51 +02:00
var wordName = wordPronunciation = wordPartOfSpeech = wordSimpleDefinition = wordLongDefinition = "" ;
2015-12-22 01:52:56 +01:00
if ( searchTerm != "" && searchByWord ) {
2016-06-21 01:13:51 +02:00
wordName += htmlEntitiesParse ( publicDictionary . words [ itemIndex ] . name ) . replace ( searchRegEx , "<searchTerm>$1</searchterm>" ) ;
2015-12-22 01:52:56 +01:00
} else {
2016-06-21 01:13:51 +02:00
wordName += publicDictionary . words [ itemIndex ] . name . toString ( ) ; // Use toString() to prevent using a reference instead of the value.
2015-12-22 01:52:56 +01:00
}
if ( publicDictionary . words [ itemIndex ] . pronunciation != "" ) {
2016-06-21 01:13:51 +02:00
wordPronunciation += marked ( htmlEntitiesParse ( publicDictionary . words [ itemIndex ] . pronunciation ) ) . replace ( "<p>" , "" ) . replace ( "</p>" , "" ) ;
2015-12-22 01:52:56 +01:00
}
if ( publicDictionary . words [ itemIndex ] . partOfSpeech != "" ) {
2016-06-21 01:13:51 +02:00
wordPartOfSpeech += publicDictionary . words [ itemIndex ] . partOfSpeech . toString ( ) ;
2015-12-22 01:52:56 +01:00
}
2016-06-21 01:13:51 +02:00
if ( publicDictionary . words [ itemIndex ] . simpleDefinition != "" ) {
2015-12-22 01:52:56 +01:00
if ( searchTerm != "" && searchBySimple ) {
2016-06-21 01:13:51 +02:00
wordSimpleDefinition += htmlEntitiesParse ( publicDictionary . words [ itemIndex ] . simpleDefinition ) . replace ( searchRegEx , "<searchTerm>$1</searchterm>" ) ;
2015-12-22 01:52:56 +01:00
} else {
2016-06-21 01:13:51 +02:00
wordSimpleDefinition += publicDictionary . words [ itemIndex ] . simpleDefinition . toString ( ) ;
2015-12-22 01:52:56 +01:00
}
}
if ( publicDictionary . words [ itemIndex ] . longDefinition != "" ) {
if ( searchTerm != "" && searchByLong ) {
2016-06-21 01:13:51 +02:00
wordLongDefinition += marked ( htmlEntitiesParse ( publicDictionary . words [ itemIndex ] . longDefinition ) . replace ( searchRegEx , "<searchTerm>$1</searchterm>" ) ) ;
2015-12-22 01:52:56 +01:00
} else {
2016-06-21 01:13:51 +02:00
wordLongDefinition += marked ( htmlEntitiesParse ( publicDictionary . words [ itemIndex ] . longDefinition ) ) ;
2015-12-22 01:52:56 +01:00
}
2016-06-21 01:13:51 +02:00
}
return PublicDictionaryEntryTemplate ( {
name : wordName ,
pronunciation : wordPronunciation ,
partOfSpeech : wordPartOfSpeech ,
simpleDefinition : wordSimpleDefinition ,
longDefinition : wordLongDefinition ,
wordId : publicDictionary . words [ itemIndex ] . wordId . toString ( )
} , false ) ;
}
function PublicDictionaryEntryTemplate ( 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 ( ) ;
}
entryText += "'><a href='/" + publicDictionary . id + "/" + wordObject . wordId + "' class='wordLink clickable' title='Share Word'>➦</a>" ;
entryText += "<word>" + wordObject . name + "</word>" ;
if ( wordObject . pronunciation != "" ) {
entryText += "<pronunciation>" + wordObject . pronunciation + "</pronunciation>" ;
}
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>" ;
}
2015-12-22 01:52:56 +01:00
2016-06-21 01:13:51 +02:00
if ( managementIndex !== false ) {
entryText += ManagementArea ( managementIndex ) ;
2015-12-22 01:52:56 +01:00
}
entryText += "</entry>" ;
return entryText ;
2015-12-22 19:20:33 +01:00
}
function SetPublicPartsOfSpeech ( ) {
2016-05-27 02:10:17 +02:00
var wordFilterOptions = document . getElementById ( "filterOptions" ) ;
2015-12-22 19:20:33 +01:00
var newPartsOfSpeech = htmlEntitiesParse ( publicDictionary . settings . partsOfSpeech ) . trim ( ) . split ( "," ) ;
for ( var j = 0 ; j < newPartsOfSpeech . length ; j ++ ) {
2016-05-27 02:10:17 +02:00
var thePartOfSpeech = newPartsOfSpeech [ j ] . trim ( ) ;
var wordFilterLabel = document . createElement ( 'label' ) ;
wordFilterLabel . appendChild ( document . createTextNode ( thePartOfSpeech + " " ) ) ;
wordFilterLabel [ 'part-of-speech' ] = thePartOfSpeech ;
wordFilterLabel . className = 'filterOption' ;
var wordFilterCheckbox = document . createElement ( 'input' ) ;
wordFilterCheckbox . type = 'checkbox' ;
wordFilterCheckbox . onchange = function ( ) { ShowPublicDictionary ( ) } ;
wordFilterLabel . appendChild ( wordFilterCheckbox ) ;
wordFilterOptions . appendChild ( wordFilterLabel ) ;
2015-12-22 19:20:33 +01:00
}
2015-12-22 01:52:56 +01:00
}