forked from clvrobj/Emacsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
director.js
180 lines (174 loc) · 5.33 KB
/
director.js
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
170
171
172
173
174
175
176
177
178
/*
* Director:
* bind keys event functions;
* pass all key events to actor;
* check if it is repeated key stroke
*/
var BaseDirector = function (producer, actor) {
this.repeatedKeyStack = [];
this.keyStack = [];
this.repeatDelayChecker = false;
this.repeatInvalidTime = 0;
this.lineHeight = 0;
this.leaveLines = 0;
this.leaveHeight = 0;
this.actor = false;
this.bindKeys();
};
BaseDirector.prototype.getKeysMap = function () {
if (!this.keysMap) {
this.keysMap = this.parseKeys({});
}
return this.keysMap;
};
BaseDirector.prototype.parseKeys = function (keysMap) {
// currently mainly parse the meta key to the user customized key
var meta = 'meta', keys = _.keys(keysMap), keysWithMeta = [],
metaKey = this.producer.metaKey;
for (var i=0; i<keys.length; i++) {
var k = keys[i];
if (k.indexOf(meta) != -1) {
keysWithMeta.push(k);
}
}
for (var j=0; j<keysWithMeta.length; j++) {
var k = keysWithMeta[j], v = keysMap[k];
keysMap[k.replace(meta, metaKey)] = v;
delete keysMap[k];
}
return keysMap;
};
BaseDirector.prototype.sayAction = function (key) {
this._globalKeyHandler(key);
this.actor.perform(this.getKeysMap()[key]);
};
BaseDirector.prototype._globalKeyHandler = function (key) {
if (this.keyStack) {
if (this.keyStack[0] == key) { // repeated
this.repeatedKeyStack.unshift(key);
// delay for repeat
clearTimeout(this.repeatDelayChecker);
var director = this;
this.repeatDelayChecker = setTimeout(
function () {
clearTimeout(director.repeatDelayChecker);
director.repeatedKeyStack = [];
}, this.repeatInvalidTime);
} else {
this.repeatedKeyStack = [key];
}
this.keyStack.unshift(key);
}
};
BaseDirector.prototype.checkIsRepeating = function () {
return this.repeatedKeyStack.length > 7;
};
BaseDirector.prototype.bindKeys = function () {
var keys = _.keys(this.getKeysMap());
this.producer.registerKeys(keys);
};
// basic key actions director
var DefaultDirector = function (producer, actor) {
this.producer = producer;
this.actor = actor;
this.repeatedKeyStack = [];
this.keyStack = [];
this.repeatDelayChecker = false;
this.repeatInvalidTime = 800;
this.lineHeight = 16;
this.leaveLines = 3;
this.leaveHeight = this.leaveLines * this.lineHeight;
this.bindKeys();
};
extend(DefaultDirector, BaseDirector);
DefaultDirector.prototype.getKeysMap = function () {
if (!this.keysMap) {
this.keysMap = this.parseKeys(DEFAULT_KEYSMAP);
}
return this.keysMap;
};
DefaultDirector.prototype.sayAction = function (key) {
this.constructor.uber.sayAction.call(this, key);
};
var TabSelectDirector = function (producer, actor) {
this.producer = producer;
this.actor = actor;
this.bindKeys();
};
extend(TabSelectDirector, BaseDirector);
TabSelectDirector.prototype.getKeysMap = function () {
if (!this.keysMap) {
this.keysMap = this.parseKeys(
{'ctrl+s':'highlight-next-tab',
'ctrl+r':'highlight-previous-tab',
'ctrl+g':'cancel',
'enter':'active-tab',
'esc':'cancel'
});
}
return this.keysMap;
};
var LinkOpenDirector = function (producer, actor) {
this.producer = producer;
this.actor = actor;
this.repeatedKeyStack = [];
this.keyStack = [];
this.repeatDelayChecker = false;
this.repeatInvalidTime = 1000;
this.lineHeight = 16;
this.leaveLines = 3;
this.leaveHeight = this.leaveLines * this.lineHeight;
this.bindKeys();
};
extend(LinkOpenDirector, BaseDirector);
LinkOpenDirector.prototype.getKeysMap = function () {
if (!this.keysMap) {
var keysMap = DEFAULT_KEYSMAP;
delete keysMap['ctrl+x b'];
this.keysMap = this.parseKeys(keysMap);
}
return this.keysMap;
};
LinkOpenDirector.prototype.bindKeys = function () {
this.producer.bindNumKeys();
this.constructor.uber.bindKeys.call(this);
};
LinkOpenDirector.prototype.sayAction = function (key) {
if (_.isNumber(key)) {
this.actor.perform('open-link-newtab', {index:key});
return;
}
this.constructor.uber.sayAction.call(this, key);
};
var LinkOpenAlternateDirector = function (producer, actor) {
this.producer = producer;
this.actor = actor;
this.repeatedKeyStack = [];
this.keyStack = [];
this.repeatDelayChecker = false;
this.repeatInvalidTime = 1000;
this.lineHeight = 16;
this.leaveLines = 3;
this.leaveHeight = this.leaveLines * this.lineHeight;
this.bindKeys();
};
extend(LinkOpenAlternateDirector, BaseDirector);
LinkOpenAlternateDirector.prototype.getKeysMap = function () {
if (!this.keysMap) {
var keysMap = DEFAULT_KEYSMAP;
delete keysMap['ctrl+x b'];
this.keysMap = this.parseKeys(keysMap);
}
return this.keysMap;
};
LinkOpenAlternateDirector.prototype.bindKeys = function () {
this.producer.bindNumKeys();
this.constructor.uber.bindKeys.call(this);
};
LinkOpenAlternateDirector.prototype.sayAction = function (key) {
if (_.isNumber(key)) {
this.actor.perform('open-link', {index:key});
return;
}
this.constructor.uber.sayAction.call(this, key);
};