Lexiconga/js/min/ui.js

2 lines
16 KiB
JavaScript

function Initialize(){LoadDictionary(),ClearForm(),LoadUserDictionaries(),GetTextFile("README.md","aboutText",!0),GetTextFile("TERMS.md","termsText",!0),GetTextFile("PRIVACY.md","privacyText",!0),GetTextFile("LOGIN.form","loginForm",!1),GetTextFile("FORGOT.form","forgotForm",!1),SetKeyboardShortcuts()}function SetKeyboardShortcuts(){document.addEventListener("keydown",function(e){var t=e.which?e.which:e.keyCode;if(t==keyCodeFor("escape"))"block"==document.getElementById("infoScreen").style.display?HideInfo():"block"==document.getElementById("fullScreenTextboxScreen").style.display?HideFullScreenTextbox():"block"==document.getElementById("settingsScreen").style.display?HideSettings():document.getElementById("accountSettingsScreen")&&"block"==document.getElementById("accountSettingsScreen").style.display&&HideAccountSettings();else if(e.ctrlKey)if("none"==document.getElementById("fullScreenTextboxScreen").style.display)if(t==keyCodeFor("m"))"longDefinition"==document.activeElement.id?(e.preventDefault(),ShowFullScreenTextbox("longDefinition","Explanation/Long Definition")):"dictionaryDescriptionEdit"==document.activeElement.id?(e.preventDefault(),ShowFullScreenTextbox("dictionaryDescriptionEdit","Dictionary Details")):"fullScreenTextbox"==document.activeElement.id&&(e.preventDefault(),HideFullScreenTextbox());else if(t==keyCodeFor("u"))e.preventDefault(),ToggleWordFormLock();else if(t==keyCodeFor("d"))e.preventDefault(),ToggleDescription();else if(e.shiftKey&&t==keyCodeFor("s")||t==keyCodeFor("e"))e.preventDefault(),ExportDictionary();else if(t==keyCodeFor("s")){e.preventDefault();var n=document.getElementById("searchFilterToggle"),o=document.getElementById("searchFilterArea");"none"==o.style.display&&(o.style.display="block",n.innerHTML="Hide Search/Filter Options"),document.getElementById("searchBox").focus()}else t==keyCodeFor("h")&&(e.preventDefault(),ShowInfo("aboutText"));else t==keyCodeFor("m")?(e.preventDefault(),HideFullScreenTextbox()):t==keyCodeFor("u")?e.preventDefault():t==keyCodeFor("d")?e.preventDefault():e.shiftKey&&t==keyCodeFor("s")||t==keyCodeFor("e")?e.preventDefault():t==keyCodeFor("s")?e.preventDefault():t==keyCodeFor("h")&&e.preventDefault();else e.altKey&&"none"==document.getElementById("fullScreenTextboxScreen").style.display&&(t==keyCodeFor("s")?(e.preventDefault(),ToggleSettingsScreen(!0)):t==keyCodeFor("a")&&(e.preventDefault(),ToggleAccountSettings()))},!1)}function SubmitWordOnCtrlEnter(e){var t=event.which?event.which:event.keyCode;(t===keyCodeFor("ctrlEnter")||t==keyCodeFor("enter")&&event.ctrlKey)&&(event.preventDefault(),AddWord(),"none"==document.getElementById("newWordButtonArea").style.display&&"none"==document.getElementById("editWordButtonArea").style.display&&document.getElementById("updateConfirmButton").focus())}function LoadUserDictionaries(){var e=new XMLHttpRequest,t=document.getElementById("userDictionaries");null!=t&&(e.open("GET","php/ajax_dictionarymanagement.php?action=getall"),e.onreadystatechange=function(){4==e.readyState&&200==e.status&&ParseUserDictionariesIntoSelect(t,e.responseText)},e.send())}function ParseUserDictionariesIntoSelect(e,t){if(e.options.length>0)for(var n=e.options.length-1;n>=0;n--)e.removeChild(e.options[n]);for(var o=t.split("_DICTIONARYSEPARATOR_"),i=0;i<o.length-1;i++){var r=document.createElement("option"),d=o[i].split("_IDNAMESEPARATOR_");r.appendChild(document.createTextNode(htmlEntitiesParse(d[1]))),r.value=d[0],e.appendChild(r)}e.value=currentDictionary.externalID>0?currentDictionary.externalID:""}function GetTextFile(e,t,n){n="undefined"!=typeof n?n:!1;var o=new XMLHttpRequest;o.open("GET",e),o.onreadystatechange=function(){4==o.readyState&&200==o.status&&(window[t]=n?marked(o.responseText):o.responseText)},o.send()}function ValidateLogin(){var e=document.getElementById("loginError"),t=document.getElementById("loginEmailField").value,n=document.getElementById("loginPasswordField").value;return""==t?(e.innerHTML="Email cannot be blank!",!1):/[^\s@]+@[^\s@]+\.[^\s@]+/.test(t)?""==n?(e.innerHTML="Password cannot be blank!",!1):void document.getElementById("loginForm").submit():(e.innerHTML="Your email address looks fake. Email addresses look like this: name@email.com.",!1)}function ValidateCreateAccount(){var e=document.getElementById("createAccountError"),t=document.getElementById("createAccountEmailField").value,n=document.getElementById("createAccountPasswordField").value,o=document.getElementById("createAccountPasswordConfirmField").value,i=document.getElementById("createAccountPublicNameField").value;if(""==t)return e.innerHTML="Email cannot be blank!",!1;if(!/[^\s@]+@[^\s@]+\.[^\s@]+/.test(t))return e.innerHTML="Your email address looks fake. Email addresses look like this: name@email.com.",!1;if(""==n)return e.innerHTML="Password cannot be blank!",!1;if(n!=o)return e.innerHTML="Passwords do not match!",!1;if(""==i)return e.innerHTML="Public Name cannot be blank!",!1;var r=new XMLHttpRequest;r.open("GET","php/ajax_createaccountemailcheck.php?email="+t),r.onreadystatechange=function(){if(4==r.readyState&&200==r.status){if("ok"!=r.responseText)return e.innerHTML="The email address entered is already being used. Try logging in or using a different email address instead.",!1;document.getElementById("createAccountForm").submit()}},r.send()}function ValidateAccountSettings(){var e=document.getElementById("accountSettingsError"),t=document.getElementById("accountSettingsEmailField").value,n=document.getElementById("accountSettingsPublicNameField").value;return""==t?(e.innerHTML="Email cannot be blank!",!1):/[^\s@]+@[^\s@]+\.[^\s@]+/.test(t)?""==n?(e.innerHTML="Public Name cannot be blank!",!1):void document.getElementById("createAccountForm").submit():(e.innerHTML="Your email address looks fake. Email addresses look like this: name@email.com.",!1)}function ValidateForgotPassword(){var e=document.getElementById("forgotError"),t=document.getElementById("forgotEmailField").value;if(""==t)return e.innerHTML="Email cannot be blank!",!1;if(!/[^\s@]+@[^\s@]+\.[^\s@]+/.test(t))return e.innerHTML="Your email address looks fake. Email addresses look like this: name@email.com.",!1;var n=new XMLHttpRequest;n.open("GET","php/ajax_passwordresetemailcheck.php?email="+t),n.onreadystatechange=function(){if(4==n.readyState&&200==n.status){if("email exists"!=n.responseText)return e.innerHTML="The email address entered is not in use and therefore can't have its password reset. Try <span class='clickable' onclick='ShowInfo(\"loginForm\")'>creating an account</span> instead!",!1;document.getElementById("forgotForm").submit()}},n.send()}function ValidateResetPassword(){var e=document.getElementById("resetPasswordError"),t=document.getElementById("newPasswordField").value,n=document.getElementById("newPasswordConfirmField").value;return""==t?(e.innerHTML="Password cannot be blank!",!1):t!=n?(e.innerHTML="Passwords do not match!",!1):void document.getElementById("resetPasswordForm").submit()}function WarnEmailChange(){var e=document.getElementById("accountSettingsEmailChangeWarning"),t=document.getElementById("accountSettingsEmailField").value,n=document.getElementById("accountSettingsPreviousEmailField").value;t!=n?e.style.display="block":e.style.display="none"}function LoggedInResetPassword(){var e=new XMLHttpRequest;e.open("GET","php/ajax_setnewpassword.php"),e.onreadystatechange=function(){if(4==e.readyState&&200==e.status){if("done"!=e.responseText)return console.log(e.responseText),alert("Error resetting password.\n\nTry again later."),!1;window.location="./"}},e.send()}function ExplainPublicName(){alert("This is the name we greet you with. It's also the name displayed if you ever decide to share any of your dictionaries.\n\nNote: this is not a username, and as such is not guaranteed to be unique. Use something people will recognize you as to differentiate from other people who might use the same name!")}function ExplainAllowEmails(){alert("We'll make sure that you're the first to hear about any new features that get added or if any of our policies change for any reason. We'll never spam you or sell your information, but you may need to mark emails from lexicon.ga as not spam to receive them.\nNOTE: Password reset emails will be sent regardless of your choice.")}function wordFormIsLocked(){return"🔒"==document.getElementById("formLockButton").innerHTML}function ToggleWordFormLock(){wordFormIsLocked()?UnlockWordForm():LockWordForm()}function UnlockWordForm(){var e=document.getElementById("formLockButton"),t=document.getElementById("leftColumn"),n=document.getElementById("wordEntryForm"),o=n.offsetWidth-20,i=t.offsetWidth,r=t.offsetHeight;e.innerHTML="&#128275;",n.style.position="fixed",n.style.top=document.getElementsByTagName("header")[0].offsetHeight.toString()+"px",n.style.width=o.toString()+"px",t.style.width=i.toString()+"px",t.style.height=r.toString()+"px"}function LockWordForm(){var e=document.getElementById("formLockButton"),t=document.getElementById("leftColumn"),n=document.getElementById("wordEntryForm");e.innerHTML="&#128274;",t.removeAttribute("style"),n.removeAttribute("style")}function CloseUpdateConflictArea(e){e="undefined"!=typeof e&&null!=e?e:!1,0!=e&&(document.getElementById(e).style.display="block"),document.getElementById("updateConflict").style.display="none",EnableForm()}function DisableForm(){document.getElementById("word").disabled=!0,document.getElementById("pronunciation").disabled=!0,document.getElementById("partOfSpeech").disabled=!0,document.getElementById("simpleDefinition").disabled=!0,document.getElementById("longDefinition").disabled=!0,document.getElementById("editIndex").disabled=!0}function EnableForm(){document.getElementById("word").disabled=!1,document.getElementById("pronunciation").disabled=!1,document.getElementById("partOfSpeech").disabled=!1,document.getElementById("simpleDefinition").disabled=!1,document.getElementById("longDefinition").disabled=!1,document.getElementById("editIndex").disabled=!1}function ClearForm(){document.getElementById("wordEntryForm")&&(document.getElementById("word").value="",document.getElementById("pronunciation").value="",document.getElementById("partOfSpeech").value="",document.getElementById("simpleDefinition").value="",document.getElementById("longDefinition").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",EnableForm())}function ToggleDescription(){var e=document.getElementById("descriptionToggle"),t=document.getElementById("dictionaryDescription");"none"==t.style.display?(t.style.display="block",e.innerHTML="Hide Description"):(t.style.display="none",e.innerHTML="Show Description")}function ToggleSearchFilter(){var e=document.getElementById("searchFilterToggle"),t=document.getElementById("searchFilterArea");"none"==t.style.display?(t.style.display="block",e.innerHTML="Hide Search/Filter Options"):(t.style.display="none",e.innerHTML="Search/Filter Options")}function ShowInfo(e){document.getElementById("infoText").innerHTML=window[e],"loginForm"==e&&(currentDictionary.words.length>0||"New"!=currentDictionary.name||"A new dictionary."!=currentDictionary.description)&&(document.getElementById("dictionaryWarn").innerHTML="If your current dictionary is not already saved to your account, be sure to <span class='exportWarnText' onclick='ExportDictionary()'>export it before logging in</span> so you don't lose anything!"),HideAccountSettings(),document.getElementById("infoPage").scrollTop=0,document.getElementById("infoScreen").style.display="block"}function HideInfo(){document.getElementById("infoScreen").style.display="none"}function ToggleAccountSettings(){if(document.getElementById("accountSettingsScreen")){var e=document.getElementById("accountSettingsScreen");"block"==e.style.display?HideAccountSettings():ShowAccountSettings()}}function ShowAccountSettings(e){document.getElementById("accountSettingsScreen")&&(document.getElementById("accountSettingsScreen").style.display="block"),HideInfo()}function HideAccountSettings(){document.getElementById("accountSettingsScreen")&&(document.getElementById("accountSettingsScreen").style.display="none")}function ToggleSettingsScreen(e){var t=document.getElementById("settingsScreen");"block"==t.style.display?(e&&SaveSettings(),HideSettings()):ShowSettings()}function ShowSettings(){document.getElementById("settingsScreen").style.display="block",document.getElementById("dictionaryNameEdit").value=htmlEntitiesParse(currentDictionary.name),document.getElementById("dictionaryDescriptionEdit").value=htmlEntitiesParse(currentDictionary.description),document.getElementById("dictionaryPartsOfSpeechEdit").value=htmlEntitiesParse(currentDictionary.settings.partsOfSpeech),document.getElementById("dictionaryAllowDuplicates").checked=currentDictionary.settings.allowDuplicates,document.getElementById("dictionaryCaseSensitive").checked=currentDictionary.settings.caseSensitive,document.getElementById("dictionarySortByEquivalent").checked=currentDictionary.settings.sortByEquivalent,document.getElementById("dictionaryIsComplete").checked=currentDictionary.settings.isComplete,document.getElementById("dictionaryIsPublic")&&(document.getElementById("dictionaryIsPublic").checked=currentDictionary.settings.isPublic,TogglePublicLink()),document.getElementById("numberOfWordsInDictionary").innerHTML=currentDictionary.words.length.toString()}function HideSettings(){document.getElementById("settingsScreen").style.display="none",currentDictionary.settings.isComplete?(LockWordForm(),document.getElementById("wordEntryForm").style.display="none"):document.getElementById("wordEntryForm").style.display="block"}function HideSettingsWhenComplete(){document.getElementById("settingsScreen")&&(currentDictionary.settings.isComplete?document.getElementById("hideIfComplete").style.display="none":document.getElementById("hideIfComplete").style.display="block")}function ShowFullScreenTextbox(e,t){var n=document.getElementById(e),o=document.getElementById("fullScreenTextbox");document.getElementById("fullScreenTextboxLabel").innerHTML=t;var i=getInputSelection(n);document.getElementById("expandedTextboxId").innerHTML=e,o.value=n.value,document.getElementById("fullScreenTextboxScreen").style.display="block",setSelectionRange(o,i.start,i.end)}function HideFullScreenTextbox(){var e=document.getElementById("expandedTextboxId").innerHTML,t=document.getElementById("fullScreenTextbox"),n=document.getElementById(e),o=getInputSelection(t);n.value=t.value,document.getElementById("fullScreenTextboxScreen").style.display="none",setSelectionRange(n,o.start,o.end)}function ShowDictionaryDeleteMenu(e){document.getElementById("loadAfterDeleteScreen").style.display="block",ParseUserDictionariesIntoSelect(document.getElementById("loadAfterDelete"),e),ParseUserDictionariesIntoSelect(document.getElementById("userDictionaries"),e)}function ToggleCaseSensitiveOption(){document.getElementById("dictionaryAllowDuplicates").checked?document.getElementById("dictionaryCaseSensitive").disabled=!0:document.getElementById("dictionaryCaseSensitive").disabled=!1}function TogglePublicLink(){if(document.getElementById("dictionaryIsPublic").checked){var e="http://lexicon.ga/"+currentDictionary.externalID;document.getElementById("publicLink").innerHTML="<strong>Public Link:</strong><br>"+e}else document.getElementById("publicLink").innerHTML=""}function SetPartsOfSpeech(){var e=document.getElementById("partOfSpeech"),t=document.getElementById("wordFilter"),n=t.value,o=!1;if(e.options.length>0)for(var i=e.options.length-1;i>=0;i--)e.removeChild(e.options[i]),t.removeChild(t.options[i+1]);for(var r=htmlEntitiesParse(currentDictionary.settings.partsOfSpeech).trim().split(","),d=0;d<r.length;d++){var l=document.createElement("option");l.appendChild(document.createTextNode(r[d].trim())),l.value=r[d].trim(),e.appendChild(l);var a=document.createElement("option");a.appendChild(document.createTextNode(r[d].trim())),a.value=r[d].trim(),t.appendChild(a),o||r[d].trim()!=n||(o=!0)}o&&(t.value=n)}function ShowFilterWordCount(e){var t=document.getElementById("wordFilter").value,n=htmlEntitiesParseForSearchEntry(document.getElementById("searchBox").value),o=document.getElementById("filterWordCount");""!=t||""!=n?o.innerHTML="Showing "+e.toString()+" result"+(1!=e?"s":""):o.innerHTML=""}function NewWordNotification(e){var t=document.getElementById("notificationArea"),n=document.getElementById("notificationMessage"),o=currentDictionary.nextWordId-1;t.style.display="block",n.innerHTML="New Word Added: <a href='#"+o.toString()+"'>"+e+"</a>"}function FocusAfterAddingNewWord(){document.getElementById("word").focus()}