-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (106 loc) · 2.61 KB
/
index.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
// Imports
var { ToggleButton } = require('sdk/ui/button/toggle');
var { Panel } = require('sdk/panel');
var { Hotkey } = require('sdk/hotkeys');
var { prefs } = require('sdk/simple-prefs');
var self = require('sdk/self')
var selection = require('sdk/selection');
var carddb = require('lib/card-db');
// Initialize card DB and begin listening for text selections
carddb.initialize(beginListening);
// Toolbar button
var button = ToggleButton({
id: 'so-button',
label: 'Special Order (v' + self.version + ')',
icon: {
'16': './click-16.png',
'32': './click-32.png',
'64': './click-64.png'
},
disabled: true,
onChange: showPanel
});
// Panel for displaying cards
var panel = Panel({
width: 300,
height: 418,
contentURL: self.data.url('card-renderer.html'),
contentScriptFile: self.data.url('card-renderer.js'),
onHide: handleHide
});
/**
* If the link in the panel gets clicked, hide the panel as the full
* NetrunnerDB page gets opened.
*/
panel.port.on('card-clicked', function(){
panel.hide();
});
// Keyboard shortcut
var showHotKey = Hotkey({
combo: 'control-alt-s',
onPress: showPanel
});
// After init, start listening for selections.
function beginListening() {
selection.on('select', function() {
if (selection.text) {
setContentUrl(selection.text)
}
});
}
/**
* Displays the panel, contents already set
*/
function showPanel() {
panel.show({
position: button
});
}
/**
* Hides the panel after escape / clicking elsewhere
*/
function handleHide() {
button.state('window', {checked: false});
reset();
}
/**
* Resets the icons and button state
*/
function reset() {
button.icon = {
'16': './click-16.png',
'32': './click-32.png',
'64': './click-64.png'
};
button.disabled = true;
panel.port.emit('load-card', './loading.png', '#');
}
/**
* Checks if a card matches the current selection. If so, set the
* card displayed in the panel and color of the icon (blue for Corp,
* red for Runner) accordingly.
*/
function setContentUrl(selectedText) {
var card = carddb.findCard(selectedText);
if (card && card.imagesrc) {
panel.port.emit('load-card', card.imagesrc, card.url);
if (prefs['autoDisplay']) {
showPanel();
}
if (card.side === 'runner') {
button.icon = {
'16': './click-red-16.png',
'32': './click-red-32.png',
'64': './click-red-64.png'
};
}
else {
button.icon = {
'16': './click-blue-16.png',
'32': './click-blue-32.png',
'64': './click-blue-64.png'
};
}
button.disabled = false;
}
}