aboutsummaryrefslogtreecommitdiffstats
path: root/browser_object.js
blob: 2cdbec499d44803cd63a41ada91cd247c635aa35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Vimperator plugin: 'Map behave like text-object'
// Version: 0.1
// Last Change: 04-Apr-2008. Jan 2008
// License: Creative Commons
// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
//
// Map behave like text-object for vimperator0.6.*
//
// Mappings:
//  {motion}{scope}{target}
//      Motions:
//          'd' : Delete
//          'y' : Yank
//      Scopes:
//          'l' : Left
//          'r' : Right
//          'a' : All
//          'o' : Other
//      Target:
//          't' : Tabs
(function(){
    function Tab(){
        return {
            close: function(ary){
                for each(var i in ary)
                    i.close();
            },
            yank: function(ary){
                var copyStrings = [];
                for each(var i in ary)
                    copyStrings.push(i.document.title);
                liberator.copyToClipboard(copyStrings.join(", "));
            },

            //default function
            active: function(){
                return Application.activeWindow.activeTab.index;
            },
            collection: function(){
                return Application.activeWindow.tabs;
            },
        }
    }

    function Motions(){
        var motions = {};
        function iterator(){
            for(let i in motions)
                yield motions[i];
            throw StopIteration;
        }
        return {
            __iterator__:function(){
                return iterator();
            },
            add: function(id,handlerName){
                motions[id] = {
                    id: id,
                    handler: handlerName,
                };
            },
            get: function(i){
                return motions[id];
            },
            getAll: function(){
                return motions;
            }
        }
    }
    function Scopes(){
        var scopes = {};
        function iterator(){
            for(let i in scopes)
                yield scopes[i];
            throw StopIteration;
        }
        return {
            __iterator__: function(){
                return iterator();
            },
            add: function(id,handler){
                scopes[id] = {
                    id: id,
                    handler: handler,
                };
            },
            get: function(id){
                return scopes[id];
            },
            getAll: function(){
                return scopes;
            }
        }
    }
    function Targets(){
        var targets = {};
        function iterator(){
            for(var i in motions)
                yield targets[i];
            throw StopIteration;
        }
        return {
            __iterator__: function(){
                return iterator();
            },
            add: function(id,handler){
                targets[id] = {
                    id: id,
                    handler: handler,
                };
            },
            get: function(id){
                return targets[id];
            },
        }
    }

    var browserObject = {};
    browserObject.motions = new Motions();
    browserObject.scopes = new Scopes();
    browserObject.targets = new Targets();

    browserObject.motions.add('d','close');
    browserObject.motions.add('y','yank');

    browserObject.scopes.add('l',function(ary){
        var active = this.active();
        return [ary[i] for (i in ary) if (i < active)];
    });
    browserObject.scopes.add('r',function(ary){
        var active = this.active();
        return [ary[i] for (i in ary) if (i > active)];
    });
    browserObject.scopes.add('o',function(ary){
        var active = this.active();
        liberator.log([i for (i in ary) if (i != active)]);
        return [ary[i] for (i in ary) if (i != active)];
    });
    browserObject.scopes.add('i',function(ary){
        return [ary[this.active()]];
    });
    browserObject.scopes.add('a',function(ary){
        return ary;
    });

    browserObject.targets.add('t',new Tab());
    for (let m in browserObject.motions){
        for (let s in browserObject.scopes){
            let motion = m;
            let scope = s;
            liberator.log(motion.id + scope.id);

            liberator.mappings.addUserMap([liberator.modes.NORMAL], [motion.id + scope.id],
                "Browser Object Mapping",
                function (arg) {
                    var target = browserObject.targets.get(arg);
                    var targetCollection = target.handler.collection();

                    targetCollection = scope.handler.call(target.handler,targetCollection);
                    target.handler[motion.handler].call(target.handler,targetCollection);
                },
                { flags: liberator.Mappings.flags.ARGUMENT});
        }
    }
    liberator.mappings.addUserMap([liberator.modes.NORMAL], ["dd"],
        "Delete current buffer",
        function (count) { liberator.tabs.remove(getBrowser().mCurrentTab, count, false, 0); },
        { flags: liberator.Mappings.flags.COUNT });
})();