Lexiconga/js/min/dictionaryBuilder.js

2 lines
19 KiB
JavaScript

function AddWord(){var e=htmlEntities(document.getElementById("word").value).trim(),t=htmlEntities(document.getElementById("pronunciation").value).trim(),n=htmlEntities(document.getElementById("partOfSpeech").value).trim(),i=htmlEntities(document.getElementById("simpleDefinition").value).trim(),r=htmlEntities(document.getElementById("longDefinition").value),o=htmlEntities(document.getElementById("editIndex").value),a=document.getElementById("errorMessage"),c="",s=document.getElementById("updateConflict");if(""==e||""==i&&""==r)""==e?(c+="Word cannot be blank",c+=""==i&&""==r?" and you need at least one definition.":"."):""==i&&""==r&&(c+="You need at least one definition.");else{var d=currentDictionary.settings.allowDuplicates?-1:WordIndex(e);if(""!=o)WordAtIndexWasChanged(o,e,t,n,i,r)?(document.getElementById("editWordButtonArea").style.display="none",DisableForm(),s.style.display="block",s.innerHTML="<span id='updateConflictMessage'>Do you really want to change the word \""+currentDictionary.words[parseInt(o)].name+'" to what you have set above?</span>',s.innerHTML+='<button type="button" id="updateConfirmButton" onclick="UpdateWord('+o+", '"+htmlEntities(e)+"', '"+htmlEntities(t)+"', '"+htmlEntities(n)+"', '"+htmlEntities(i)+"', '"+htmlEntities(r)+"'); return false;\">Yes, Update it</button>",s.innerHTML+='<button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(\'editWordButtonArea\'); return false;">No, Leave it</button>'):(c='No change has been made to "'+e+'"',currentDictionary.words[parseInt(o)].name!=e&&(c+=". (Your dictionary is currently set to ignore case.)"));else if(d>=0)if(WordAtIndexWasChanged(d,e,t,n,i,r)){document.getElementById("newWordButtonArea").style.display="none",DisableForm(),s.style.display="block";var l="<span id='updateConflictMessage'>\""+e+'" is already in the dictionary';l+=currentDictionary.words[d].name!=e?' as "'+currentDictionary.words[d].name+'", and your dictionary is set to ignore case.':".",l+="<br>Do you want to update it to what you have set above?</span>",l+='<button type="button" id="updateConfirmButton" onclick="UpdateWord('+d+", '"+htmlEntities(e)+"', '"+htmlEntities(t)+"', '"+htmlEntities(n)+"', '"+htmlEntities(i)+"', '"+htmlEntities(r)+"'); return false;\">Yes, Update it</button>",l+=' <button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(\'newWordButtonArea\'); return false;">No, Leave it</button>',s.innerHTML=l}else c='"'+e+'" is already in the dictionary exactly as it is written above',currentDictionary.words[d].name!=e&&(c+=". (Your dictionary is currently set to ignore case.)");else currentDictionary.words.push({name:e,pronunciation:t,partOfSpeech:n,simpleDefinition:i,longDefinition:r,wordId:currentDictionary.nextWordId++}),FocusAfterAddingNewWord(),NewWordNotification(e),SaveAndUpdateDictionary(!1);a.innerHTML=""}a.innerHTML=c}function EditWord(e){SaveScroll(),wordFormIsLocked()&&window.scroll(0,0),ClearForm(),document.getElementById("editIndex").value=e.toString(),document.getElementById("word").value=htmlEntitiesParse(currentDictionary.words[e].name),document.getElementById("pronunciation").value=htmlEntitiesParse(currentDictionary.words[e].pronunciation),document.getElementById("partOfSpeech").value=htmlEntitiesParse(currentDictionary.words[e].partOfSpeech),document.getElementById("simpleDefinition").value=htmlEntitiesParse(currentDictionary.words[e].simpleDefinition),document.getElementById("longDefinition").value=htmlEntitiesParse(currentDictionary.words[e].longDefinition),document.getElementById("newWordButtonArea").style.display="none",document.getElementById("editWordButtonArea").style.display="block"}function UpdateWord(e,t,n,i,r,o){currentDictionary.words[e].name=t,currentDictionary.words[e].pronunciation=n,currentDictionary.words[e].partOfSpeech=i,currentDictionary.words[e].simpleDefinition=r,currentDictionary.words[e].longDefinition=o,SaveAndUpdateDictionary(),window.scroll(savedScroll.x,savedScroll.y),FocusAfterAddingNewWord()}function DeleteWord(e){""!=document.getElementById("editIndex").value&&ClearForm(),currentDictionary.words.splice(e,1),SaveAndUpdateDictionary(!0)}function ShowDictionary(){var e=document.getElementById("wordFilter").value,t=[],n=htmlEntitiesParseForSearchEntry(document.getElementById("searchBox").value),i=document.getElementById("searchOptionWord").checked,r=document.getElementById("searchOptionSimple").checked,o=document.getElementById("searchOptionLong").checked,a=!document.getElementById("searchCaseSensitive").checked,c=document.getElementById("searchIgnoreDiacritics").checked;if(""!=n&&(i||r||o)){var s=[],d=htmlEntitiesParseForSearch(JSON.stringify(currentDictionary));a&&(n=n.toLowerCase()),c&&(n=removeDiacritics(n),d=removeDiacritics(d)),i&&s.push("contains("+(a?"name":'translate(name, "", "")')+', "'+n+'")'),r&&s.push("contains("+(a?"simpleDefinition":'translate(simpleDefinition, "", "")')+', "'+n+'")'),o&&s.push("contains("+(a?"longDefinition":'translate(longDefinition, "", "")')+', "'+n+'")');var l=JSON.parse(d);t=JSON.search(l,"//words["+s.join(" or ")+"]/wordId")}var u=document.getElementById("dictionaryName");u.innerHTML=htmlEntitiesParse(currentDictionary.name)+" Dictionary";var y=document.getElementById("dictionaryDescription");y.innerHTML=marked(htmlEntitiesParse(currentDictionary.description));var p=document.getElementById("theDictionary"),m="",D=0;if(currentDictionary.words.length>0)for(var g=0;g<currentDictionary.words.length;g++)(""==e||""!=e&&currentDictionary.words[g].partOfSpeech==e)&&(""==n||""!=n&&(i||r||o)&&t.indexOf(currentDictionary.words[g].wordId)>=0)&&(currentDictionary.words[g].hasOwnProperty("pronunciation")||(currentDictionary.words[g].pronunciation=""),currentDictionary.words[g].hasOwnProperty("wordId")||(currentDictionary.words[g].wordId=g+1),m+=DictionaryEntry(g),D++);else m="There are no entries in the dictionary.";p.innerHTML=m,ShowFilterWordCount(D)}function DictionaryEntry(e){displayPublic="undefined"!=typeof displayPublic&&null!=displayPublic?displayPublic:!1;var t="<entry><a name='"+currentDictionary.words[e].wordId+"'></a><a href='#"+currentDictionary.words[e].wordId+"' class='wordLink clickable'>&#x1f517;</a>",n=regexParseForSearch(document.getElementById("searchBox").value),i=document.getElementById("searchOptionWord").checked,r=document.getElementById("searchOptionSimple").checked,o=document.getElementById("searchOptionLong").checked,a=!document.getElementById("searchCaseSensitive").checked,c=document.getElementById("searchIgnoreDiacritics").checked,s=new RegExp("("+(c?removeDiacritics(n)+"|"+n:n)+")","g"+(a?"i":""));return t+="<word>",t+=""!=n&&i?htmlEntitiesParse(currentDictionary.words[e].name).replace(s,"<searchTerm>$1</searchterm>"):currentDictionary.words[e].name,t+="</word>",""!=currentDictionary.words[e].pronunciation&&(t+="<pronunciation>",t+=marked(htmlEntitiesParse(currentDictionary.words[e].pronunciation)).replace("<p>","").replace("</p>",""),t+="</pronunciation>"),""!=currentDictionary.words[e].partOfSpeech&&(t+="<partofspeech>",t+=currentDictionary.words[e].partOfSpeech,t+="</partofspeech>"),t+="<br>",""!=currentDictionary.words[e].simpleDefinition&&(t+="<simpledefinition>",t+=""!=n&&r?htmlEntitiesParse(currentDictionary.words[e].simpleDefinition).replace(s,"<searchTerm>$1</searchterm>"):currentDictionary.words[e].simpleDefinition,t+="</simpledefinition>"),""!=currentDictionary.words[e].longDefinition&&(t+="<longdefinition>",t+=""!=n&&o?marked(htmlEntitiesParse(currentDictionary.words[e].longDefinition).replace(s,"<searchTerm>$1</searchterm>")):marked(htmlEntitiesParse(currentDictionary.words[e].longDefinition)),t+="</longdefinition>"),currentDictionary.settings.isComplete||(t+=ManagementArea(e)),t+="</entry>"}function ManagementArea(e){var t="<div class='management'>";return t+="<span class='clickable editButton' onclick='EditWord("+e+")'>Edit</span>",t+="<span class='clickable deleteButton' onclick='document.getElementById(\"delete"+e+'Confirm").style.display = "block";\'>Delete</span>',t+="<div class='deleteConfirm' id='delete"+e+"Confirm' style='display:none;'>Are you sure you want to delete this entry?<br><br>",t+="<span class='clickable deleteCancelButton' onclick='document.getElementById(\"delete"+e+'Confirm").style.display = "none";\'>No</span>',t+="<span class='clickable deleteConfirmButton' onclick='DeleteWord("+e+")'>Yes</span>",t+="</div>",t+="</div>"}function SaveSettings(){""!=htmlEntities(document.getElementById("dictionaryNameEdit").value)&&(currentDictionary.name=htmlEntities(document.getElementById("dictionaryNameEdit").value)),currentDictionary.description=htmlEntities(document.getElementById("dictionaryDescriptionEdit").value),CheckForPartsOfSpeechChange(),currentDictionary.settings.allowDuplicates=document.getElementById("dictionaryAllowDuplicates").checked,currentDictionary.settings.caseSensitive=document.getElementById("dictionaryCaseSensitive").checked,currentDictionary.settings.sortByEquivalent=document.getElementById("dictionarySortByEquivalent").checked,currentDictionary.settings.isComplete=document.getElementById("dictionaryIsComplete").checked,document.getElementById("dictionaryIsPublic")&&(currentDictionary.settings.isPublic=document.getElementById("dictionaryIsPublic").checked),HideSettingsWhenComplete(),SaveAndUpdateDictionary(!0),LoadUserDictionaries()}function EmptyWholeDictionary(){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?")&&CreateNewDictionary()}function CreateNewDictionary(){ResetDictionaryToDefault(),SaveAndUpdateDictionary(!1),SetPartsOfSpeech(),HideSettings()}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 e=new XMLHttpRequest;e.open("POST","php/ajax_dictionarymanagement.php?action=delete"),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.onreadystatechange=function(){return 4==e.readyState&&200==e.status?(e.responseText.length<31?(console.log(e.responseText),CreateNewDictionary()):(HideSettings(),ShowDictionaryDeleteMenu(e.responseText),0==document.getElementById("loadAfterDelete").options.length&&(document.getElementById("loadAfterDeleteScreen").style.display="none",CreateNewDictionary())),!0):!1},e.send()}}function ResetDictionaryToDefault(){currentDictionary=JSON.parse(defaultDictionaryJSON)}function SaveAndUpdateDictionary(e){currentDictionary.settings.sortByEquivalent?currentDictionary.words.sort(dynamicSort(["simpleDefinition","partOfSpeech"])):currentDictionary.words.sort(dynamicSort(["name","partOfSpeech"])),SaveDictionary(!0,!0),ShowDictionary(),e||ClearForm(),CloseUpdateConflictArea("newWordButtonArea")}function SaveDictionary(e,t){localStorage.setItem("dictionary",JSON.stringify(currentDictionary)),e&&(t="undefined"!=typeof t?t:!1,SendDictionary(t)),SavePreviousDictionary()}function SendDictionary(e){e="undefined"!=typeof e?e:!1;var t="",n="";currentDictionary.externalID>0?(t="update",n=DataToSend(e)):(t="new",n=DataToSend(!0,!0));var i=new XMLHttpRequest;i.open("POST","php/ajax_dictionarymanagement.php?action="+t),i.setRequestHeader("Content-type","application/x-www-form-urlencoded"),i.onreadystatechange=function(){return 4==i.readyState&&200==i.status?("updated successfully"==i.responseText?(console.log(i.responseText),LoadUserDictionaries(),ProcessLoad()):isNaN(parseInt(i.responseText))?console.log(i.responseText):(currentDictionary.externalID=parseInt(i.responseText),LoadUserDictionaries(),ProcessLoad(),console.log("saved successfully")),!0):!1},i.send(n)}function DataToSend(e,t){t="undefined"!=typeof t&&null!=t?t:!1;var n="";return 0==currentDictionary.externalID?(n="name="+encodeURIComponent(currentDictionary.name)+"&description="+encodeURIComponent(currentDictionary.description)+"&words="+encodeURIComponent(JSON.stringify(currentDictionary.words)),n+="&nextwordid="+currentDictionary.nextWordId+"&allowduplicates="+(currentDictionary.settings.allowDuplicates?"1":"0")+"&casesensitive="+(currentDictionary.settings.caseSensitive?"1":"0"),n+="&partsofspeech="+encodeURIComponent(currentDictionary.settings.partsOfSpeech)+"&sortbyequivalent="+(currentDictionary.settings.sortByEquivalent?"1":"0")+"&iscomplete="+(currentDictionary.settings.isComplete?"1":"0")+"&ispublic="+(currentDictionary.settings.isPublic?"1":"0")):((t||currentDictionary.name!=previousDictionary.name)&&(n+="name="+encodeURIComponent(currentDictionary.name)),(t||currentDictionary.description!=previousDictionary.description)&&(n+=(""==n?"":"&")+"description="+encodeURIComponent(currentDictionary.description)),(t||e)&&(n+=(""==n?"":"&")+"words="+encodeURIComponent(JSON.stringify(currentDictionary.words))),(t||currentDictionary.nextWordId!=previousDictionary.nextWordId)&&(n+=(""==n?"":"&")+"nextwordid="+currentDictionary.nextWordId),(t||currentDictionary.settings.allowDuplicates!=previousDictionary.allowDuplicates)&&(n+=(""==n?"":"&")+"allowduplicates="+(currentDictionary.settings.allowDuplicates?"1":"0")),(t||currentDictionary.settings.caseSensitive!=previousDictionary.caseSensitive)&&(n+=(""==n?"":"&")+"casesensitive="+(currentDictionary.settings.caseSensitive?"1":"0")),(t||currentDictionary.settings.partsOfSpeech!=previousDictionary.partsOfSpeech)&&(n+=(""==n?"":"&")+"partsofspeech="+encodeURIComponent(currentDictionary.settings.partsOfSpeech)),(t||currentDictionary.settings.sortByEquivalent!=previousDictionary.sortByEquivalent)&&(n+=(""==n?"":"&")+"sortbyequivalent="+(currentDictionary.settings.sortByEquivalent?"1":"0")),(t||currentDictionary.settings.isComplete!=previousDictionary.isComplete)&&(n+=(""==n?"":"&")+"iscomplete="+(currentDictionary.settings.isComplete?"1":"0")),(t||currentDictionary.settings.isPublic!=previousDictionary.isPublic)&&(n+=(""==n?"":"&")+"ispublic="+(currentDictionary.settings.isPublic?"1":"0"))),n}function LoadDictionary(){LoadLocalDictionary();var e=new XMLHttpRequest;e.open("GET","php/ajax_dictionarymanagement.php?action=load"),e.onreadystatechange=function(){4==e.readyState&&200==e.status&&("no dictionaries"==e.responseText?(currentDictionary.externalID=0,currentDictionary.settings.isPublic=!1,SendDictionary(!0)):e.responseText.length<60?console.log(e.responseText):(currentDictionary=JSON.parse(e.responseText),SaveDictionary(!1,!1))),ProcessLoad()},e.send()}function ChangeDictionary(e){if(e="undefined"!=typeof e&&null!=e?e:document.getElementById("userDictionaries"),currentDictionary.externalID!=e.value&&e.options.length>0){var t=new XMLHttpRequest;t.open("POST","php/ajax_dictionarymanagement.php?action=switch"),t.setRequestHeader("Content-type","application/x-www-form-urlencoded");var n="newdictionaryid="+e.value.toString();t.onreadystatechange=function(){4==t.readyState&&200==t.status&&("no dictionaries"==t.responseText?(console.log(t.responseText),SendDictionary(!1)):t.responseText.length<60?console.log(t.responseText):(currentDictionary=JSON.parse(t.responseText),SaveDictionary(!1,!1),ProcessLoad(),LoadUserDictionaries(),HideSettings()))},t.send(n)}}function LoadLocalDictionary(){if(localStorage.getItem("dictionary")){var e=JSON.parse(localStorage.getItem("dictionary"));(e.words.length>0||"A new dictionary."!=e.description||"New"!=e.name)&&(currentDictionary=JSON.parse(localStorage.getItem("dictionary"))),e=null}}function ProcessLoad(){currentDictionary.hasOwnProperty("nextWordId")||(currentDictionary.nextWordId=currentDictionary.words.length+1),HideSettingsWhenComplete(),ShowDictionary(),SetPartsOfSpeech(),currentDictionary.settings.isComplete&&(document.getElementById("wordEntryForm").style.display="none"),SavePreviousDictionary()}function SavePreviousDictionary(){previousDictionary={name:currentDictionary.name,description:currentDictionary.description,nextWordId:currentDictionary.nextWordId,allowDuplicates:currentDictionary.settings.allowDuplicates,caseSensitive:currentDictionary.settings.caseSensitive,partsOfSpeech:currentDictionary.settings.partsOfSpeech,sortByEquivalent:currentDictionary.settings.sortByEquivalent,isComplete:currentDictionary.settings.isComplete,isPublic:currentDictionary.settings.isPublic}}function ExportDictionary(){if(currentDictionary.words.length>0){var e=removeDiacritics(stripHtmlEntities(currentDictionary.name)).replace(/\W/g,"");""==e&&(e="export"),download(e+".dict",localStorage.getItem("dictionary"))}else alert("Dictionary must have at least 1 word to export.")}function ImportDictionary(){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?")){if(!window.FileReader)return alert("Your browser is not supported"),!1;var e=new FileReader;if(document.getElementById("importFile").files.length>0){var t=document.getElementById("importFile").files[0];e.readAsText(t),e.onloadend=function(){if(e.result&&e.result.length){var t=JSON.parse(e.result);if(t.hasOwnProperty("name")&&t.hasOwnProperty("description")&&t.hasOwnProperty("words")&&t.hasOwnProperty("settings"))currentDictionary=JSON.parse(e.result),currentDictionary.externalID=0,currentDictionary.settings.isPublic=!1,SaveDictionary(!0,!0),ProcessLoad(),HideSettings(),document.getElementById("importFile").value="";else{var n="File is missing:";t.hasOwnProperty("name")||(n+=" name"),t.hasOwnProperty("description")||(n+=" description"),t.hasOwnProperty("words")||(n+=" words"),t.hasOwnProperty("settings")||(n+=" settings"),alert("Uploaded file is not compatible.\n\n"+n)}t=null}else alert("Upload Failed");e=null}}else alert("You must add a file to import.")}}function WordIndex(e){for(var t=0;t<currentDictionary.words.length;t++)if(!currentDictionary.settings.caseSensitive&&currentDictionary.words[t].name.toLowerCase()==e.toLowerCase()||currentDictionary.settings.caseSensitive&&currentDictionary.words[t].name==e)return t;return-1}function WordAtIndexWasChanged(e,t,n,i,r,o){return!currentDictionary.settings.caseSensitive&&currentDictionary.words[parseInt(e)].name.toLowerCase()!=t.toLowerCase()||currentDictionary.settings.caseSensitive&&currentDictionary.words[parseInt(e)].name!=t||currentDictionary.words[parseInt(e)].pronunciation!=n||currentDictionary.words[parseInt(e)].partOfSpeech!=i||currentDictionary.words[parseInt(e)].simpleDefinition!=r||currentDictionary.words[parseInt(e)].longDefinition!=o}function CheckForPartsOfSpeechChange(){htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value)!=currentDictionary.settings.partsOfSpeech&&""!=htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value)&&(currentDictionary.settings.partsOfSpeech=htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value),SetPartsOfSpeech())}var publicName="Someone",currentDictionary={name:"New",description:"A new dictionary.",createdBy:publicName,words:[],nextWordId:1,settings:{allowDuplicates:!1,caseSensitive:!1,partsOfSpeech:"Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction",sortByEquivalent:!1,isComplete:!1,isPublic:!1},externalID:0},defaultDictionaryJSON=JSON.stringify(currentDictionary),previousDictionary={},savedScroll={x:0,y:0};