Skip to content

Commit

Permalink
Merge pull request #2878 from andytuba/multireddit-menu
Browse files Browse the repository at this point in the history
Multireddit quicklinks menu
  • Loading branch information
jewel-andraia committed Apr 13, 2016
2 parents 2904043 + ecd6d69 commit 20180f7
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 6 deletions.
1 change: 1 addition & 0 deletions chrome/manifest.json
Expand Up @@ -49,6 +49,7 @@
"modules/keyboardNav.js",
"modules/about.js",
"modules/commandLine.js",
"modules/multiredditNavbar.js",
"modules/xPostLinks.js",
"modules/messageMenu.js",
"modules/easterEgg.js",
Expand Down
1 change: 1 addition & 0 deletions firefox/index.js
Expand Up @@ -502,6 +502,7 @@ PageMod({
self.data.url('modules/userTagger.js'),
self.data.url('modules/keyboardNav.js'),
self.data.url('modules/commandLine.js'),
self.data.url('modules/multiredditNavbar.js'),
self.data.url('modules/xPostLinks.js'),
self.data.url('modules/messageMenu.js'),
self.data.url('modules/easterEgg.js'),
Expand Down
1 change: 1 addition & 0 deletions lib/core/utils.js
Expand Up @@ -424,6 +424,7 @@ RESUtils.getUserInfo = (username = RESUtils.loggedInUser(), live = false) => {
};
RESUtils.regexes = {
all: /^https?:\/\/(?:[\-\w\.]+\.)?reddit\.com\//i,
frontpage: /^https?:\/\/(?:[\-\w\.]+\.)?reddit\.com\/(new|rising|controversial|top)?\/?(?:\?.*)?$/i,
comments: /^https?:\/\/(?:[\-\w\.]+\.)?reddit\.com\/[\-\w\.\/]*\/comments/i,
nosubComments: /https?:\/\/(?:[\-\w\.]+\.)?reddit\.com\/comments\/[\-\w\.\/]*/i,
friendsComments: /^https?:\/\/(?:[\-\w\.]+\.)?reddit\.com\/r\/friends\/comments/i,
Expand Down
43 changes: 39 additions & 4 deletions lib/modules/hover.js
Expand Up @@ -4,6 +4,11 @@ addModule('hover', (module, moduleID) => {
module.description = 'Customize the behavior of the large informational pop-ups which appear when you hover your mouse over certain elements.';
module.alwaysEnabled = true;

module.pin = {
bottom: 'bottom',
right: 'right'
};

module.options = {
instances: {
description: 'Manage particular pop-ups',
Expand Down Expand Up @@ -349,17 +354,47 @@ addModule('hover', (module, moduleID) => {

class HoverDropdownList extends Hover {
templateName = 'RESHoverDropdownList';
_options = { ...Hover._defaultOptions,
pin: module.pin.bottom,
offsetWidth: 0,
offsetHeight: 2,
bottomPadding: 10
};

_updatePosition($container) {
const trigger = $(this._target)[0];
const menu = $container[0];
const { x, y } = RESUtils.getXYpos(trigger);
const leftAlign = x + $(menu).outerWidth() < document.body.scrollWidth;

menu.style.right = leftAlign ? 'auto' : `${document.body.scrollWidth - x - trigger.offsetWidth}px`;
menu.style.left = leftAlign ? `${x}px` : 'auto';
menu.style.zIndex = 2147483646;
menu.style.top = `${y + trigger.offsetHeight + 2}px`;

switch (this._options.pin) {
case module.pin.right:
const viewportBottom = document.documentElement.clientHeight + window.scrollY;
const top = y + this._options.offsetHeight;
const height = $(menu).height();
const bottomAlign = top + height + this._options.bottomPadding > viewportBottom;
menu.style.left = `${x + trigger.offsetWidth + this._options.offsetWidth}px`;
menu.style.right = 'auto';
if (bottomAlign) {
menu.style.position = 'fixed';
menu.style.top = 'auto';
menu.style.bottom = `${this._options.bottomPadding}px`;
} else {
menu.style.position = 'absolute';
menu.style.top = `${top}px`;
menu.style.bottom = 'auto';
}
break;
case module.pin.bottom:
// falls through
default:
const leftAlign = x + $(menu).outerWidth() < document.body.scrollWidth;
menu.style.right = leftAlign ? 'auto' : `${document.body.scrollWidth - x - trigger.offsetWidth + this._options.offsetWidth}px`;
menu.style.left = leftAlign ? `${x}px` : 'auto';
menu.style.top = `${y + trigger.offsetHeight + this._options.offsetHeight}px`;
break;
}
}
}
});
7 changes: 5 additions & 2 deletions lib/modules/messageMenu.js
Expand Up @@ -6,7 +6,7 @@ addModule('messageMenu', (module, moduleID) => {
module.options = {
links: {
type: 'table',
addRowNext: '+add shortcut',
addRowText: '+add shortcut',
fields: [{
name: 'label',
type: 'text'
Expand Down Expand Up @@ -72,14 +72,17 @@ addModule('messageMenu', (module, moduleID) => {
const populate = RESUtils.once(() => [
module.options.links.value
.map(populateItem)
.reduce((prev, curr) => prev.add(curr), $())
.reduce((prev, curr) => (curr ? prev.add(curr) : prev), $())
.add(populateItem([
`<i>${module.moduleName}</i>`,
modules['settingsNavigation'].makeUrlHash(moduleID)
]))
]);

function populateItem(link) {
if (!(link && link.length >= 2)) {
return false;
}
const label = link[0] || '';
const url = link[1] || '';
const compose = url.indexOf('/message/compose') !== -1;
Expand Down
117 changes: 117 additions & 0 deletions lib/modules/multiredditNavbar.js
@@ -0,0 +1,117 @@
addModule('multiredditNavbar', (module, moduleID) => {
module.moduleName = 'Multireddit Navigation';
module.description = 'Enhance the navigation bar shown on the left side of the frontpage.';
module.category = 'Subreddits';

module.includes = [
RESUtils.regexes.frontpage
];

module.options = {
sectionMenu: {
type: 'boolean',
value: true,
description: 'Show a menu linking to various sections of the multireddit when hovering your mouse over the link.'
},
sectionLinks: {
dependsOn: 'sectionMenu',
type: 'table',
addRowText: '+add multireddit section shortcut',
fields: [{
name: 'label',
type: 'text'
}, {
name: 'url',
type: 'text'
}],
value: [
['new', './new'],
['rising', './rising'],
['controversial', './controversial'],
['top', './top'],
['top this month', './top?t=month'],
['gilded', './gilded'],
['promoted', './ads']
]
},
hoverDelay: {
dependsOn: 'sectionMenu',
type: 'text',
value: 1000,
description: 'Delay, in milliseconds, before hover tooltip loads. Default is 1000.',
advanced: true
},
fadeDelay: {
dependsOn: 'sectionMenu',
type: 'text',
value: 200,
description: 'Delay, in milliseconds, before hover tooltip fades away. Default is 200.',
advanced: true
},
fadeSpeed: {
dependsOn: 'sectionMenu',
type: 'text',
value: 0.7,
description: 'Fade animation\'s speed (in seconds). Default is 0.7.',
advanced: true
}
};

module.go = function() {
const $multis = $('.listing-chooser .multis');

if (module.options.sectionMenu.value) {
$multis.on('mouseover', 'li', onMouseoverMultiLink);
}
};

function onMouseoverMultiLink(e) {
const link = e.currentTarget.querySelector('a[href^="/me/m"]');
if (!link) {
return;
}
modules['hover'].dropdownList(moduleID)
.target(e.currentTarget)
.options({
openDelay: module.options.hoverDelay.value,
fadeDelay: module.options.fadeDelay.value,
fadeSpeed: module.options.fadeSpeed.value,
pin: modules['hover'].pin.right,
offsetWidth: -10,
offsetHeight: 1,
bottomPadding: 0
})
.populateWith(() => populateSectionMenu(link.href))
.begin();
}

const populateSectionMenu = baseUrl => [
module.options.sectionLinks.value
.map(link => populateSectionItem(baseUrl, link))
.reduce((prev, curr) => (curr ? prev.add(curr) : prev), $())
.add(populateSectionItem('.', [
`<i>${module.moduleName}</i>`,
modules['settingsNavigation'].makeUrlHash(moduleID, 'sectionMenu')
]))
];

function populateSectionItem(baseUrl, link) {
if (!(link && link.length >= 2)) {
return false;
}

const label = link[0] || '';
const url = link[1] || '';
const $link = $('<a />')
.safeHtml(label)
.attr('href', `${baseUrl}/${url}`);

if (url.indexOf('#!settings') !== -1) {
$link.append('<span class="RESMenuItemButton gearIcon" />');
}

$link.on('click', () => modules['hover'].dropdownList(moduleID).close());

return $('<li />').append($link);
}
});
1 change: 1 addition & 0 deletions node/files.json
Expand Up @@ -20,6 +20,7 @@
"modules/keyboardNav.js",
"modules/about.js",
"modules/commandLine.js",
"modules/multiredditNavbar.js",
"modules/xPostLinks.js",
"modules/messageMenu.js",
"modules/easterEgg.js",
Expand Down
1 change: 1 addition & 0 deletions safari/Info.plist
Expand Up @@ -55,6 +55,7 @@
<string>modules/userTagger.js</string>
<string>modules/keyboardNav.js</string>
<string>modules/commandLine.js</string>
<string>modules/multiredditNavbar.js</string>
<string>modules/xPostLinks.js</string>
<string>modules/messageMenu.js</string>
<string>modules/easterEgg.js</string>
Expand Down

0 comments on commit 20180f7

Please sign in to comment.