User:Subh83/JavaScriptTools/textareaRightclickMenu.js

/****************************************************
* Created by Subhrajit Bhattacharya [[User:Subh83]] *
* Licensed under GNU-GPL v3.0                       *
*****************************************************/
 
if (typeof(RightClickMenuKey)=='undefined') RightClickMenuKey = ''; // 'shift', 'alt', 'ctrl' or ''
if (typeof(FloatingMovableMenuSource)=='undefined') var FloatingMovableMenuSource = "User:Subh83/JavaScriptTools/SymbolsAndMarkups";
 
if (typeof(UserSubh83_utils1)=='undefined') importScript("User:Subh83/JavaScriptTools/utils1.js");
 
function getTextareaMenuHTML(selText)
{
    selText = selText.toString();
    encodedSelText = XEncodeURIComponent(selText);
 
    var pageTitle = getPageTitle();
 
    // Generate the HTML
    var ret = "<div class='rightclickmenu'> " +
                  "<span class='heading'>\"" + HTMLSpecialCharactersEncode(((selText.length>40)?(selText.substr(0,17)+' ... '+selText.substr(selText.length-17,17)):(selText))) + 
                  "\"</span><hr/><div class='items'>" + 
                  "<div class='menuitem'><a href=\"javascript:void(0);\" onClick=\"WikiBlameFromWikitext('"+
                                                     XEncodeURIComponent(pageTitle)+"','"+encodedSelText+ "'); " + 
                                                     "document.getElementById('textarearightclickmenu').style.display='none'; " + 
                                                     "\">Trace history on WikiBlame</a></div>" + 
                  "<div class='menuitem'><a href=\"javascript:void(0);\" onClick='showInsertPanel();'>Insert symbols &amp; markups</a></div>" +
                  "<hr/>" + 
                  "<div class='menuitem'><a href=\"http://www.google.com/search?q="+encodedSelText+"\" target=\"_blank\">Search Google for this text</a></div>" +
              "</div></div>";
 
    return ret;
}

var floatinginsertpanelCreated = false;
function showInsertPanel() {
    floatinginsertpanel = document.getElementById("floatinginsertpanel");
    if (!floatinginsertpanel) {  // Create the panel if it does not exist
        if (floatinginsertpanelCreated) {
            setTimeout(showInsertPanel, 200);
        }
        else {
            FetchWikiTextAndPerformAction( FloatingMovableMenuSource, -1, CreateFloatingMovableMenuFromList, 
                                            { divID: 'floatinginsertpanel', title: 'Insert', itemClickHandler: 'MenuItemClickHandler' } );
            floatinginsertpanelCreated = true;
            showInsertPanel();
        }
        return;
    }
    retElems = FetchWikiTextAndPerformAction_lastReturn;
    for (i=0; i<retElems.length; i++)
        retElems[i].onmouseup = FloatingPanelClickHandler;
    floatinginsertpanel.style.display = "block";
    var textarearightclickmenu = document.getElementById("textarearightclickmenu");
    if (textarearightclickmenu) textarearightclickmenu.style.display = "none";
    selectRangeInTextarea(document.getElementById('wpTextbox1'), LastTextareaSelection.start, LastTextareaSelection.end, false);
}

function MenuItemClickHandler(itemval) {
    markupparts = itemval.split("...");
    if (markupparts.length == 1) newsellength = 0;
    else newsellength = LastTextareaSelection.text.length;

    newtext = itemval.replace(/\.\.\./g, LastTextareaSelection.text);
    newsel = updateSelectionInTextarea(document.getElementById('wpTextbox1'), newtext, LastTextareaSelection, 
                                          markupparts[0].length, newsellength);
    LastTextareaSelection = newsel;
    selectRangeInTextarea(document.getElementById('wpTextbox1'), LastTextareaSelection.start, LastTextareaSelection.end);
}

function FloatingPanelClickHandler() {
    selectRangeInTextarea(document.getElementById('wpTextbox1'), LastTextareaSelection.start, LastTextareaSelection.end, false);
}

// ------------------

var LastTextareaSelection;
function UpdateTextareaSelection() {
    if (document.getElementById('wpTextbox1')) {
        LastTextareaSelection = getSelectionInTextarea(document.getElementById('wpTextbox1'));
    }
}

function showTextareaMenu(e)
{
    if (!e) var e = window.event;
    var rightclick;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
 
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // Safari bug
        targ = targ.parentNode;
 
    var keyPressed = true;
    if (RightClickMenuKey=='' && (e.shiftKey || e.ctrlKey || e.altKey)) keyPressed = false;
    else if (RightClickMenuKey=='shift' && !e.shiftKey) keyPressed = false;
    else if (RightClickMenuKey=='ctrl' && !e.ctrlKey) keyPressed = false;
    else if (RightClickMenuKey=='alt' && !e.altKey) keyPressed = false;
 
    var textarearightclickmenu = document.getElementById("textarearightclickmenu");
    if (!textarearightclickmenu) {
        alert("Unable to locate rightclick-menu!");
        return false;
    }
    if (rightclick && keyPressed) {
        textarearightclickmenu.innerHTML = getTextareaMenuHTML(LastTextareaSelection.text);
        setMenuPosition (e, textarearightclickmenu);
        textarearightclickmenu.style.zIndex = "10000";
        textarearightclickmenu.style.display = "block";
        return true;
    }
    else {
        textarearightclickmenu.style.display = "none";
        delCookie("lastRightClickSelText");
        return false;
    }
}
 
function registerTextAreaRightClickHandler () {
    var MainTextArea = document.getElementById('wpTextbox1');
    if (MainTextArea && !MainTextArea.RightClickMenuRegistered) {
        AppendHandlerToEvent( "document.getElementById('wpTextbox1').onmouseup", showTextareaMenu, 'pre', false, "return (!retNew);" );
        AppendHandlerToEvent( "document.getElementById('wpTextbox1').oncontextmenu", showTextareaMenu, 'pre', false, "return (!retNew);" );
        MainTextArea.RightClickMenuRegistered = true;
    }
}

var onTextareaMenu = false;
$( function () {
 
    // The right-click menu 
    registerTextAreaRightClickHandler();
    AppendHandlerToEvent( "document.onmousedown", registerTextAreaRightClickHandler ); // Support for 'secedit' (User:Supadawg/secedit.js)

    var textarearightclickmenu = document.createElement("div");
    textarearightclickmenu.setAttribute("id", "textarearightclickmenu");
    textarearightclickmenu.setAttribute("display", "none");
    var theBody = document.getElementsByTagName("body")[0];
    theBody.insertBefore(textarearightclickmenu, theBody.childNodes[0]);

    AppendHandlerToEvent( "document.onmousedown", 
                          function() { if(!onTextareaMenu) { document.getElementById('textarearightclickmenu').style.display='none'; } } );
    AppendHandlerToEvent( "document.onmouseup", UpdateTextareaSelection );
    AppendHandlerToEvent( "document.onkeyup", UpdateTextareaSelection );

    document.getElementById('textarearightclickmenu').onmouseover = function() { onTextareaMenu = true; };
    document.getElementById('textarearightclickmenu').onmouseout = function() { onTextareaMenu = false; };
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.