-
Notifications
You must be signed in to change notification settings - Fork 2
/
crowdspell.min.js
4 lines (4 loc) · 5.89 KB
/
crowdspell.min.js
1
2
3
4
var CrowdSpell;(function(){var t=function(){function n(){this.Url="";this.Correction="";this.Was="";this.Context="";this.ApiKey=""}return n}(),i=function(){function n(){var n=this;this.apiEndpoint="http://crowdspell.se/api/Corrections";this.el=document.getElementById("crowdspell");this.APIkey=this.el.getAttribute("data-apikey");this.editor=new u(function(t){return n.ClosedEditor(t)});this.button=new r(function(t,i){return n.ShowEditor(t,i),!1})}return n.prototype.ClosedEditor=function(n){n!==null?(this.button.ShowMessage("Correction sent!",2600),this.SendCorrection(n)):this.button.Enable()},n.prototype.ShowEditor=function(n,i){this.activeCorrection=new t;this.button.Disable();this.button.Hide();this.editor.Show(i,n.pageY)},n.prototype.SendCorrection=function(n){if(n.ApiKey=this.APIkey,"withCredentials"in new XMLHttpRequest){var t=new XMLHttpRequest;t.open("post",this.apiEndpoint,!0);t.setRequestHeader("Content-Type","application/json");t.send(JSON.stringify(n))}},n}(),r=function(){function t(t){this.enabled=!0;this.el=n.ActivateBubble();this.clickHandler=t;this.innerLink=document.getElementById("crowdspell-button");this.SetupEvents()}return t.prototype.SetupHTML=function(){var r=document.createElement("div"),t,i;r.innerHTML=n.BubbleHtml;t=document.getElementById("crowdspell");i=r.firstElementChild;t.parentNode.insertBefore(i,t);this.el=i},t.prototype.Disable=function(){this.enabled=!1},t.prototype.Enable=function(){this.enabled=!0},t.prototype.Hide=function(){var n=this;this.el.className="text-options fade";setTimeout(function(){n.el.className=="text-options fade"&&(n.el.className="text-options",n.el.style.top="-999px",n.el.style.left="-999px")},260)},t.prototype.ShowMessage=function(n,t){var i=this,r=this.GetText();this.SetText(n);this.Show();setTimeout(function(){i.Hide();i.Selection.removeAllRanges();i.Enable()},t);setTimeout(function(){i.SetText(r)},t+250)},t.prototype.SetText=function(n){this.innerLink.textContent=n},t.prototype.GetText=function(){return this.innerLink.textContent},t.prototype.Show=function(){this.UpdateBubblePosition();this.el.className="text-options active"},t.prototype.SetupEvents=function(){var n=this;this.el.onclick=function(t){return n.clickHandler(t,n.Selection)};document.onmousedown=function(t){n.CheckTextHighlighting(t)};document.onmouseup=function(t){setTimeout(function(){return n.CheckTextHighlighting(t)},1)};window.addEventListener("resize",function(){return n.UpdateBubblePosition()})},t.prototype.CheckTextHighlighting=function(){if(this.enabled){var n=window.getSelection();this.Selection=n;n.isCollapsed===!0&&this.Hide();n.isCollapsed===!1&&this.Show()}},t.prototype.UpdateBubblePosition=function(){var n=this.lastBoundary,t,i;this.Selection!=null&&this.Selection.toString()!==""&&(t=this.Selection,i=t.getRangeAt(0),n=i.getBoundingClientRect(),this.lastBoundary=n);this.el.style.top=n.top-5+window.pageYOffset+"px";this.el.style.left=(n.left+n.right)/2+"px"},t}(),u=function(){function i(t){this.editor=document.getElementById("crowdspell-editor");this.editor=n.ActivateEditor();this.closeCallback=t;this.closeButton=document.getElementById("crowdspell-button-close");this.correctionInput=document.getElementById("crowdspell-correction");this.SetupButtonEvent()}return i.prototype.Show=function(n,t){this.Selection=n;this.anchorNode=n.anchorNode;this.selectedText=n.toString();this.SetDialogData();this.ShowDialog(t);this.correctionInput.focus()},i.prototype.SetupButtonEvent=function(){var n=this,i;this.editor.addEventListener("keydown",function(t){t.keyCode===27&&n.Close(null)});this.closeButton.onclick=function(){n.Close(null)};i=document.getElementById("crowdspell-button-save");i.onclick=function(){var r=n.correctionInput.value,i=new t;i.Was=n.selectedText;i.Correction=r;i.Url=location.toString();n.anchorNode!=null&&(i.Context=n.anchorNode.textContent.substr(0,140));n.Close(i)}},i.prototype.Close=function(n){this.Hide();this.closeCallback(n)},i.prototype.Hide=function(){this.editor.getAttribute("data-open")!==null&&this.editor.removeAttribute("data-open")},i.prototype.ShowDialog=function(n){this.editor.style.top=n+"px";this.editor.getAttribute("data-open")===null&&this.editor.setAttribute("data-open","open")},i.prototype.SetDialogData=function(){var n=document.getElementById("crowdspell-misstake"),t=this.correctionInput;n.innerHTML=this.selectedText;t.value=this.selectedText},i}(),f=function(){function n(){this.ConstructEdiotrHtml();this.ConstructButtonHtml()}return n.prototype.GetElement=function(n){var t=document.createElement("div");return t.innerHTML=n,t.firstElementChild},n.prototype.ActivateElement=function(n){var t=document.getElementById("crowdspell");t.parentNode.insertBefore(n,t)},n.prototype.ActivateHtml=function(n){var t=this.GetElement(n);return this.ActivateElement(t),t},n.prototype.ActivateEditor=function(){return this.ActivateHtml(this.EditorHtml)},n.prototype.ActivateBubble=function(){return this.ActivateHtml(this.BubbleHtml)},n.prototype.ConstructEdiotrHtml=function(){var n="";n+="<div id='crowdspell-editor'>";n+=" <h3>Hello! Help by correcting the spelling error.<\/h3>";n+=' <div class="crowdspell-label">Was:<\/div><div id="crowdspell-misstake"><\/div>';n+="";n+=' <div class="crowdspell-label">Correction<\/div>';n+=" <div>";n+=' <textarea id="crowdspell-correction"><\/textarea>';n+=" <\/div>";n+=' <div class="crowdspell-actions">';n+=' <button id="crowdspell-button-save">Send correction<\/button>';n+=' <span id="crowdspell-button-close">Close<\/span>';n+=" <\/div>";n+="<\/div>";n+="";this.EditorHtml=n},n.prototype.ConstructButtonHtml=function(){var n="";n+=' <div id="crowdspell-button-wrapper" class="text-options" style="top: -5px; left: 0px;">';n+=' <div class="options">';n+=' <a href="#" id="crowdspell-button" class="crowdspell-button">Correct typo<\/a>';n+=" <\/div>";n+=" <\/div>";this.BubbleHtml=n},n}(),n=new f,e=new i})(CrowdSpell||(CrowdSpell={}));
/*
//# sourceMappingURL=crowdspell.min.js.map
*/