serenabenny / nk-gesture

Automatically exported from code.google.com/p/nk-gesture
0 stars 0 forks source link

恳请开发分支版本 #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
我并不是不喜欢鼠标轨迹,但是在看过chromegestures这个插件之
后,我觉得这
个鼠标手势的图片主题很符合chrome的风格。不知道伟大的作��
�能不能开发一个
类似的分支版本。

Original issue reported on code.google.com by cet...@gmail.com on 2 Sep 2009 at 7:29

Attachments:

GoogleCodeExporter commented 9 years ago
项目主页
http://code.google.com/p/chromegestures/

Original comment by cet...@gmail.com on 2 Sep 2009 at 7:30

GoogleCodeExporter commented 9 years ago
完全没问题 我尽快改出来 我也是觉得这样画线太废内存了 

Original comment by niklen...@gmail.com on 2 Sep 2009 at 11:45

GoogleCodeExporter commented 9 years ago
期待!

Original comment by cet...@gmail.com on 3 Sep 2009 at 12:57

GoogleCodeExporter commented 9 years ago
var nkGestures =
{
    Direction : { up:'U' , right:'R', down:'D', left:'L', forward:'F', back:'B' 
},
    directions : '',
    lastDirection : null,
    x : 0,
    y : 0,
    isRightButtonDown : false,
    isRightClickDisable : false,
    connection : null,
    isOpenDrag : false,
    isOpenHint : false,
    isEnglish : false,

    actionsConfig :
    new Array(
        //newtab:
        "U",
        "DR",
        "L",
        "R",
        "D",
        "UD",
        "RD",
        "UL",
        "UR",
        "LU",
        "RU",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "URDLD"
    ),

    actionNames :
        new Array(
            "鏂版爣绛?,
            "鍏虫爣绛?,
            "鍚庨€€",
            "鍓嶈繘",
            "鍋滄",
            "閲嶈浇",
            "鍒锋柊",
            "宸︽爣绛?,
            "鍙虫爣绛?,
            "鍓嶇鏍囩",
            "鍚庣鏍囩",
            "涓婄炕椤?,
            "涓嬬炕椤?,
            "鍒版渶椤剁",
            "鍒版渶搴曠",
            "鍏虫父瑙堝櫒",
            "鍏冲叏閮ㄦ爣绛鹃櫎褰撳墠鏍囩",
            "鍏冲乏鏍囩",
            "鍏冲彸鏍囩",
            "褰撳墠鏍囩寮€涓婚〉",
            "鎭㈠鍒氬叧闂爣绛?,
            "鎵撳紑閰嶇疆鏂囦欢"
        ),
    actionNames_en :
        new Array(
            "New Tab",
            "Close Tab",
            "Back",
            "Forward",
            "Stop",
            "Reload",
            "Refresh",
            "Left Tab",
            "Right Tab",
            "First Tab",
            "Last Tab",
            "Page Up",
            "Page Down",
            "Goto Top",
            "Goto Bottom",
            "Exit Browser",
            "Close Others",
            "Close All Left Tab",
            "Close All Right Tab",
            "Open Home Page",
            "Restore Tab",
            "Configure"
        ),
    actions :
        new Array(
        //          newtab:
        function(connection) { connection.postMessage("do:newtab");},
        //          close:
        function(connection) { connection.postMessage("do:close");},
        //          goback:
        function(connection) { history.back();},
        //          forward:
        function(connection) { history.forward();},
        //          stop:
        function(connection) { stop();},
        //          reload:
        function(connection) { window.location.reload(true);},
        //          refresh:
        function(connection) { window.location.reload();},
        //          lefttab:
        function(connection) { connection.postMessage("do:lefttab");},
        //          righttab:
        function(connection) { connection.postMessage("do:righttab");},
        //          firsttab:
        function(connection) { connection.postMessage("do:firsttab");},
        //          lasttab:
        function(connection) { connection.postMessage("do:lasttab");},
        //          pageup
        function(connection) { window.scrollBy(0,45-window.innerHeight);},
        //          pagedown
        function(connection) { window.scrollBy(0,window.innerHeight-45);},
        //          gotop
        function(connection) { top.window.scrollBy(0,-
top.document.body.scrollHeight);window.scrollBy(0,-document.body.scrollHeight);}
,
        //          gobotton
        function(connection) { 
top.window.scrollBy(0,top.document.body.scrollHeight);window.scrollBy(0,document
.body
.scrollHeight);},
        //          closeall
        function(connection) { connection.postMessage("do:closeall");},
        //          closeonly
        function(connection) { connection.postMessage("do:closeonly");},
        //          closeleft
        function(connection) { connection.postMessage("do:closeleft");},
        //          closeright
        function(connection) { connection.postMessage("do:closeright");},
        //          openindex
        function(connection) { connection.postMessage("do:openindex");},
        //          restore
        function(connection) { connection.postMessage("do:restore");},
        //          openconfig
        function(connection) { connection.postMessage("do:config");}
    ),
    init:   function()
    {
        this.connection = chrome.extension.connect({name : "nkGestures"});
        window.addEventListener('mousedown', this, true);
//      window.addEventListener('mousemove', this, true);
//      window.addEventListener('mouseup', this, true);
//      window.addEventListener('mousewheel', this, true);
        window.addEventListener('contextmenu', this, true);
        window.addEventListener ('drop', this, false);
        window.addEventListener ('drag', this, false);
        window.addEventListener ('dragstart', this, false);
        window.addEventListener ('dragover', this, false);
        window.addEventListener ('dragenter', this, false);
    },

    uninit: function()
    {
        window.removeEventListener('mousedown', this, true);
        window.removeEventListener('mousemove', this, true);
        window.removeEventListener('mouseup', this, true);
        window.removeEventListener('mousewheel', this, true);
        window.removeEventListener('contextmenu', this, true);
        window.removeEventListener('drop', this, false);
        window.removeEventListener('drag', this, false);
        window.removeEventListener('dragstart', this, false);
        window.removeEventListener('dragover', this, false);
        window.removeEventListener('dragenter', this, false);
    },

    handleEvent: function(event)
    {
        switch (event.type) {
        case "mousedown":
            if (event.button == 2)
            {
                this.isRightButtonDown = true;
                this.x = event.clientX;
                this.y = event.clientY;
                window.addEventListener('mousemove', this, true);
                window.addEventListener('mouseup', this, true);
                window.addEventListener('mousewheel', this, true);
                window.removeEventListener('mousedown', this, true);
            } else
            {
                this.stopGesture();
            }
            break;

        case "mousemove":
        case "drag":
            if (this.isRightButtonDown)
            {
                var tx      = event.clientX;
                var ty      = event.clientY;
                var offsetX = tx - this.x;
                var offsetY = ty - this.y;
                var direction;
                var actname = null;
                if (Math.pow(offsetX,2) + Math.pow(offsetY,2) > 30)
                {
                    this.isRightClickDisable = true;
                    var tan = offsetY / offsetX;
                    if(Math.abs(offsetY) > Math.abs(offsetX))
                    {
                        if(offsetY < 0)
                        {
                            direction = 
this.Direction.up;
                        } else
                        {
                            direction = 
this.Direction.down;
                        }
                    } else
                    {
                        if(offsetX < 0)
                        {
                            direction = 
this.Direction.left;
                        } else
                        {
                            direction = 
this.Direction.right;
                        }
                    }
                    if(this.lastDirection != direction)
                    {
                        this.directions += direction;
                        this.lastDirection = direction;
                        if(this.isOpenHint)
                        {
                            for(i = 
0;i<this.actionsConfig.length;i++)
                            {

if(this.actionsConfig[i] == this.directions)
                                {
                                    actname = 
this.isEnglish?this.actionNames_en[i]:this.actionNames[i];
                                    break;
                                }
                            }
                            if(event.type != "drag")
                                this.createHint( 
this.directions + ' : ' + actname );
                        }
                    }
                    if(event.type != "drag")
                        this.drawLine(this.x, this.y, tx, 
ty);
                    this.x = tx;
                    this.y = ty;
                }
            }
            break;

        case "mouseup":
            if (event.button == 2 && this.isRightButtonDown)
            {
                window.addEventListener('mousedown', this, true);
                window.removeEventListener('mousemove', this, true);
                window.removeEventListener('mouseup', this, true);
                window.removeEventListener('mousewheel', this, true);
                this.isRightButtonDown = false;
                this.clearLines();
                if(this.directions.length)
                {
                    for(i = 0;i<this.actionsConfig.length;i++)
                    {
                        if(this.actionsConfig[i] == 
this.directions)
                        {

this.actions[i](this.connection);
                            break;
                        }
                    }
                }
                this.stopGesture();
            }
            break;

        case "contextmenu":
            if(this.isRightClickDisable)
            {
                this.isRightClickDisable = false;
                event.stopPropagation();
                event.preventDefault();
            }
            break;

        case "drop":
            if (!this.isOpenDrag) {
                break;
            }
            this.isRightButtonDown = false;
            var direction = this.directions;
            this.stopGesture();

            if (event.preventDefault) event.preventDefault ();
            var link = window.getSelection().toString();
            if (link != '') {
                this.connection.postMessage(new Array("do:dragtxt", 
link, direction));
                break;
            }
            link = event.dataTransfer.getData("URL");
            if (link != '') {
                this.connection.postMessage(new Array("do:dragurl", 
link, direction));
                break;
            }
            link = event.dataTransfer.getData("Text");
            if (link != '') {
                this.connection.postMessage(new Array("do:dragtxt", 
link, direction));
                break;
            }
            break;

        case "dragover":
        case "dragenter":
            if (!this.isOpenDrag) {
                break;
            }
            if (event.preventDefault)
            {
                event.preventDefault();
            }
            break;

        case "dragstart":
            if (!this.isOpenDrag) {
                break;
            }
            this.x = event.clientX;
            this.y = event.clientY;
            this.isRightButtonDown = true;
            break;

        case "mousewheel":
            if (this.isRightButtonDown)
            {
                var direction;
                var actname = null;
                this.isRightClickDisable = true;

                if (event.wheelDelta > 0) {
                    direction = this.Direction.forward;
                }
                else
                    direction = this.Direction.back;
                if (this.lastDirection != direction) {
                    this.lastDirection = direction;
                    this.directions += direction;
                    if(this.isOpenHint)
                    {
                        for(i = 
0;i<this.actionsConfig.length;i++)
                        {
                            if(this.actionsConfig[i] == 
this.directions)
                            {
                                actname = 
this.isEnglish?this.actionNames_en[i]:this.actionNames[i];
                                break;
                            }
                        }
                        this.createHint( this.directions + ' 
: ' + actname );
                    }
                }
                if (event.preventDefault)
                {
                    event.preventDefault();
                }
            }
            break;
        }
    },

    stopGesture: function()
    {
        this.directions = '';
        this.lastDirection = null;
        this.clearLines();
        if(this.isOpenHint)
            this.deleteHint();
    },

    clearLines: function()
    {
        var canvas = document.getElementById('_nk_drag_svgline');
        if(canvas)
        {
            while( canvas.lastChild )
            {
                canvas.removeChild( canvas.lastChild ).innerHTML = 
'';
            }
            document.body.removeChild(canvas).innerHTML = '';
        }
    },

    drawLine: function(x, y, tx, ty)
    {
        x   += pageXOffset;
        tx  += pageXOffset;
        y   += pageYOffset;
        ty  += pageYOffset;
        var canvas = document.getElementById('_nk_drag_svgline');
        if(!canvas)
        {
            canvas = 
document.createElementNS('http://www.w3.org/2000/svg', 'svg');
            canvas.setAttribute("id", "_nk_drag_svgline");
            canvas.setAttribute("width",document.width);
            canvas.setAttribute("height",document.height);
            canvas.setAttribute("xmlns","http://www.w3.org/2000/svg");

canvas.setAttribute("style","position:absolute;top:0px;left:0px");
            document.body.appendChild(canvas);
        }
        var 
line=document.createElementNS('http://www.w3.org/2000/svg','line');
        line.setAttribute("x1", x);
        line.setAttribute("y1", y);
        line.setAttribute("x2", tx);
        line.setAttribute("y2", ty);
        line.setAttribute("style","stroke:blue;stroke-width:4");
        canvas.appendChild( line );
    },

    createHint : function (msg) {
    var hint = document.getElementById('_nk_drag_hint');
        if(!hint)
        {
            hint = document.createElement('div');
            hint.id = '_nk_drag_hint';
            hint.innerHTML = '<div 
style="height:20px;position:fixed;display:block;bottom:0px;left:1px;zIndex:1000;
backg
round:#D2E1F6;font-size:10px;color:#808080;">'
            +'<span id="_nk_drag_dirs"></span></div>';
            document.body.appendChild(hint);
        }
        hint.firstChild.innerHTML = msg;
    },

    deleteHint : function () {
        var hint = document.getElementById('_nk_drag_hint');
        if(hint)
        {
            var child = hint.childNodes;
            var i = child.length;
            while(i>0)
            {
                hint.removeChild(child[i-1]);
                i--;
            }
            document.body.removeChild(hint);
        }
    } 
};
//鐩戝惉绔彛鎺ユ墜鐢ㄦ埛鑷畾涔夋墜鍔?function 
string2array(string,array) {
    var i,j;
    var temp = '';
    i = 0;
    j = 0;
    while(string[j])
    {
        temp = '';
        while (j<string.length && string[j] != ',') {
            temp += string[j];
            j++;
        }
        array[i] = temp;
        j++;
        i++;
    }
}

chrome.extension.onConnect.addListener(function (port) {
    if(port.name != "nkGesturesTab")
        return;
    port.onMessage.addListener(function (message) {
        if ( message == 'OpenDrag' ) {
            nkGestures.isOpenDrag = true;
        }
        else if( message == 'OpenHint' )
        {
            nkGestures.isOpenHint = true;
        }
        else if( message == 'isEnglish' )
        {
            nkGestures.isEnglish = true;
        }
        else{
            string2array(message,nkGestures.actionsConfig );
        }
    });
});
//initialize nkGestures Object
nkGestures.init();
window.addEventListener('unload', function(){ nkGestures.uninit(); }, false);

Original comment by andyfur...@gmail.com on 15 Oct 2009 at 8:15