Simplify config flattening. Added a list of allowed keys for keybinds. Refactor keybinds settings.

This commit is contained in:
Nicolas Stepien 2012-02-04 22:02:59 +01:00
parent 0fe6de6236
commit b936b6bfcb
2 changed files with 82 additions and 120 deletions

View File

@ -146,28 +146,28 @@
backlink: '>>%id', backlink: '>>%id',
favicon: 'ferongr', favicon: 'ferongr',
hotkeys: { hotkeys: {
openOptions: 'ctrl+o', openOptions: ['ctrl+o', 'Open Options'],
close: 'Esc', close: ['Esc', 'Close Options or QR'],
spoiler: 'ctrl+s', spoiler: ['ctrl+s', 'Quick spoiler'],
openQR: 'i', openQR: ['i', 'Open QR with post number inserted'],
openEmptyQR: 'I', openEmptyQR: ['I', 'Open QR without post number inserted'],
submit: 'alt+s', submit: ['alt+s', 'Submit post'],
nextReply: 'J', nextReply: ['J', 'Select next reply'],
previousReply: 'K', previousReply: ['K', 'Select previous reply'],
nextThread: 'n', nextThread: ['n', 'See next thread'],
previousThread: 'p', previousThread: ['p', 'See previous thread'],
nextPage: 'L', nextPage: ['L', 'Jump to the next page'],
previousPage: 'H', previousPage: ['H', 'Jump to the previous page'],
zero: '0', zero: ['0', 'Jump to page 0'],
openThreadTab: 'o', openThreadTab: ['o', 'Open thread in current tab'],
openThread: 'O', openThread: ['O', 'Open thread in new tab'],
expandThread: 'e', expandThread: ['e', 'Expand thread'],
watch: 'w', watch: ['w', 'Watch thread'],
hide: 'x', hide: ['x', 'Hide thread'],
expandImages: 'm', expandImages: ['m', 'Expand selected image'],
expandAllImages: 'M', expandAllImages: ['M', 'Expand all images'],
update: 'u', update: ['u', 'Update now'],
unreadCountTo0: 'z' unreadCountTo0: ['z', 'Reset unread count to 0']
}, },
updater: { updater: {
checkbox: { checkbox: {
@ -186,19 +186,17 @@
(flatten = function(parent, obj) { (flatten = function(parent, obj) {
var key, val, _results; var key, val, _results;
if (obj.length) { if (typeof obj === 'object') {
if (typeof obj[0] === 'boolean') { if (obj.length) {
return conf[parent] = obj[0]; return conf[parent] = obj[0];
} else { } else {
return conf[parent] = obj; _results = [];
for (key in obj) {
val = obj[key];
_results.push(flatten(key, val));
}
return _results;
} }
} else if (typeof obj === 'object') {
_results = [];
for (key in obj) {
val = obj[key];
_results.push(flatten(key, val));
}
return _results;
} else { } else {
return conf[parent] = obj; return conf[parent] = obj;
} }
@ -1969,7 +1967,7 @@
} }
}, },
dialog: function() { dialog: function() {
var arr, back, checked, description, dialog, favicon, hiddenNum, hiddenThreads, indicator, indicators, input, key, li, obj, overlay, ta, time, ul, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4; var arr, back, checked, description, dialog, favicon, hiddenNum, hiddenThreads, indicator, indicators, input, key, li, obj, overlay, ta, time, tr, ul, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4;
dialog = $.el('div', { dialog = $.el('div', {
id: 'options', id: 'options',
className: 'reply dialog', className: 'reply dialog',
@ -2041,30 +2039,9 @@
<input type=radio name=tab hidden id=keybinds_tab>\ <input type=radio name=tab hidden id=keybinds_tab>\
<div>\ <div>\
<div class=warning><code>Keybinds</code> are disabled.</div>\ <div class=warning><code>Keybinds</code> are disabled.</div>\
<div>Allowed keys: Ctrl, Alt, a-z, A-Z, 0-1, Up, Down, Right, Left.</div>\
<table><tbody>\ <table><tbody>\
<tr><th>Actions</th><th>Keybinds</th></tr>\ <tr><th>Actions</th><th>Keybinds</th></tr>\
<tr><td>Open Options</td><td><input name=openOptions></td></tr>\
<tr><td>Close Options or QR</td><td><input name=close></td></tr>\
<tr><td>Quick spoiler</td><td><input name=spoiler></td></tr>\
<tr><td>Open QR with post number inserted</td><td><input name=openQR></td></tr>\
<tr><td>Open QR without post number inserted</td><td><input name=openEmptyQR></td></tr>\
<tr><td>Submit post</td><td><input name=submit></td></tr>\
<tr><td>Select next reply</td><td><input name=nextReply ></td></tr>\
<tr><td>Select previous reply</td><td><input name=previousReply></td></tr>\
<tr><td>See next thread</td><td><input name=nextThread></td></tr>\
<tr><td>See previous thread</td><td><input name=previousThread></td></tr>\
<tr><td>Jump to the next page</td><td><input name=nextPage></td></tr>\
<tr><td>Jump to the previous page</td><td><input name=previousPage></td></tr>\
<tr><td>Jump to page 0</td><td><input name=zero></td></tr>\
<tr><td>Open thread in current tab</td><td><input name=openThread></td></tr>\
<tr><td>Open thread in new tab</td><td><input name=openThreadTab></td></tr>\
<tr><td>Expand thread</td><td><input name=expandThread></td></tr>\
<tr><td>Watch thread</td><td><input name=watch></td></tr>\
<tr><td>Hide thread</td><td><input name=hide></td></tr>\
<tr><td>Expand selected image</td><td><input name=expandImages></td></tr>\
<tr><td>Expand all images</td><td><input name=expandAllImages></td></tr>\
<tr><td>Update now</td><td><input name=update></td></tr>\
<tr><td>Reset the unread count to 0</td><td><input name=unreadCountTo0></td></tr>\
</tbody></table>\ </tbody></table>\
</div>\ </div>\
</div>' </div>'
@ -2110,17 +2087,21 @@
favicon.value = conf['favicon']; favicon.value = conf['favicon'];
$.on(favicon, 'change', $.cb.value); $.on(favicon, 'change', $.cb.value);
$.on(favicon, 'change', options.favicon); $.on(favicon, 'change', options.favicon);
_ref3 = $$('#keybinds_tab + div input', dialog); _ref3 = config.hotkeys;
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { for (key in _ref3) {
input = _ref3[_j]; arr = _ref3[key];
input.type = 'text'; tr = $.el('tr', {
input.value = conf[input.name]; innerHTML: "<td>" + arr[1] + "</td><td><input name=" + key + "></td>"
});
input = $('input', tr);
input.value = conf[key];
$.on(input, 'keydown', options.keybind); $.on(input, 'keydown', options.keybind);
$.add($('#keybinds_tab + div tbody', dialog), tr);
} }
indicators = {}; indicators = {};
_ref4 = $$('.warning', dialog); _ref4 = $$('.warning', dialog);
for (_k = 0, _len3 = _ref4.length; _k < _len3; _k++) { for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
indicator = _ref4[_k]; indicator = _ref4[_j];
key = indicator.firstChild.textContent; key = indicator.firstChild.textContent;
indicator.hidden = conf[key]; indicator.hidden = conf[key];
indicators[key] = indicator; indicators[key] = indicator;

View File

@ -71,34 +71,34 @@ config =
backlink: '>>%id' backlink: '>>%id'
favicon: 'ferongr' favicon: 'ferongr'
hotkeys: hotkeys:
openOptions: 'ctrl+o' openOptions: ['ctrl+o', 'Open Options']
close: 'Esc' close: ['Esc', 'Close Options or QR']
spoiler: 'ctrl+s' spoiler: ['ctrl+s', 'Quick spoiler']
openQR: 'i' openQR: ['i', 'Open QR with post number inserted']
openEmptyQR: 'I' openEmptyQR: ['I', 'Open QR without post number inserted']
submit: 'alt+s' submit: ['alt+s', 'Submit post']
nextReply: 'J' nextReply: ['J', 'Select next reply']
previousReply: 'K' previousReply: ['K', 'Select previous reply']
nextThread: 'n' nextThread: ['n', 'See next thread']
previousThread: 'p' previousThread: ['p', 'See previous thread']
nextPage: 'L' nextPage: ['L', 'Jump to the next page']
previousPage: 'H' previousPage: ['H', 'Jump to the previous page']
zero: '0' zero: ['0', 'Jump to page 0']
openThreadTab: 'o' openThreadTab: ['o', 'Open thread in current tab']
openThread: 'O' openThread: ['O', 'Open thread in new tab']
expandThread: 'e' expandThread: ['e', 'Expand thread']
watch: 'w' watch: ['w', 'Watch thread']
hide: 'x' hide: ['x', 'Hide thread']
expandImages: 'm' expandImages: ['m', 'Expand selected image']
expandAllImages: 'M' expandAllImages: ['M', 'Expand all images']
update: 'u' update: ['u', 'Update now']
unreadCountTo0: 'z' unreadCountTo0: ['z', 'Reset unread count to 0']
updater: updater:
checkbox: checkbox:
'Scrolling': [false, 'Scroll updated posts into view. Only enabled at bottom of page.'] 'Scrolling': [false, 'Scroll updated posts into view. Only enabled at bottom of page.']
'Scroll BG': [false, 'Scroll background tabs'] 'Scroll BG': [false, 'Scroll background tabs']
'Verbose': [true, 'Show countdown timer, new post count'] 'Verbose': [true, 'Show countdown timer, new post count']
'Auto Update': [true, 'Automatically fetch new posts'] 'Auto Update': [true, 'Automatically fetch new posts']
'Interval': 30 'Interval': 30
# XXX Chrome can't into {log} = console # XXX Chrome can't into {log} = console
@ -108,15 +108,14 @@ log = console.log.bind? console
# flatten the config # flatten the config
conf = {} conf = {}
(flatten = (parent, obj) -> (flatten = (parent, obj) ->
if obj.length #array if typeof obj is 'object'
if typeof obj[0] is 'boolean' # array
if obj.length
conf[parent] = obj[0] conf[parent] = obj[0]
else # object
conf[parent] = obj else for key, val of obj
else if typeof obj is 'object'
for key, val of obj
flatten key, val flatten key, val
else #constant else # string or number
conf[parent] = obj conf[parent] = obj
) null, config ) null, config
@ -1577,30 +1576,9 @@ options =
<input type=radio name=tab hidden id=keybinds_tab> <input type=radio name=tab hidden id=keybinds_tab>
<div> <div>
<div class=warning><code>Keybinds</code> are disabled.</div> <div class=warning><code>Keybinds</code> are disabled.</div>
<div>Allowed keys: Ctrl, Alt, a-z, A-Z, 0-1, Up, Down, Right, Left.</div>
<table><tbody> <table><tbody>
<tr><th>Actions</th><th>Keybinds</th></tr> <tr><th>Actions</th><th>Keybinds</th></tr>
<tr><td>Open Options</td><td><input name=openOptions></td></tr>
<tr><td>Close Options or QR</td><td><input name=close></td></tr>
<tr><td>Quick spoiler</td><td><input name=spoiler></td></tr>
<tr><td>Open QR with post number inserted</td><td><input name=openQR></td></tr>
<tr><td>Open QR without post number inserted</td><td><input name=openEmptyQR></td></tr>
<tr><td>Submit post</td><td><input name=submit></td></tr>
<tr><td>Select next reply</td><td><input name=nextReply ></td></tr>
<tr><td>Select previous reply</td><td><input name=previousReply></td></tr>
<tr><td>See next thread</td><td><input name=nextThread></td></tr>
<tr><td>See previous thread</td><td><input name=previousThread></td></tr>
<tr><td>Jump to the next page</td><td><input name=nextPage></td></tr>
<tr><td>Jump to the previous page</td><td><input name=previousPage></td></tr>
<tr><td>Jump to page 0</td><td><input name=zero></td></tr>
<tr><td>Open thread in current tab</td><td><input name=openThread></td></tr>
<tr><td>Open thread in new tab</td><td><input name=openThreadTab></td></tr>
<tr><td>Expand thread</td><td><input name=expandThread></td></tr>
<tr><td>Watch thread</td><td><input name=watch></td></tr>
<tr><td>Hide thread</td><td><input name=hide></td></tr>
<tr><td>Expand selected image</td><td><input name=expandImages></td></tr>
<tr><td>Expand all images</td><td><input name=expandAllImages></td></tr>
<tr><td>Update now</td><td><input name=update></td></tr>
<tr><td>Reset the unread count to 0</td><td><input name=unreadCountTo0></td></tr>
</tbody></table> </tbody></table>
</div> </div>
</div>' </div>'
@ -1643,10 +1621,13 @@ options =
$.on favicon, 'change', options.favicon $.on favicon, 'change', options.favicon
#keybinds #keybinds
for input in $$ '#keybinds_tab + div input', dialog for key, arr of config.hotkeys
input.type = 'text' tr = $.el 'tr',
input.value = conf[input.name] innerHTML: "<td>#{arr[1]}</td><td><input name=#{key}></td>"
input = $ 'input', tr
input.value = conf[key]
$.on input, 'keydown', options.keybind $.on input, 'keydown', options.keybind
$.add $('#keybinds_tab + div tbody', dialog), tr
#indicate if the settings require a feature to be enabled #indicate if the settings require a feature to be enabled
indicators = {} indicators = {}