Added option to upscale small posts

This commit is contained in:
rr- 2015-07-19 18:30:20 +02:00
parent b75df289e9
commit 6380043a9a
5 changed files with 61 additions and 38 deletions

View file

@ -34,6 +34,7 @@ App.BrowsingSettings = function(
}, },
keyboardShortcuts: true, keyboardShortcuts: true,
fitMode: 'fit-width', fitMode: 'fit-width',
upscale: false,
}; };
} }

View file

@ -13,6 +13,7 @@ App.Presenters.PostContentPresenter = function(
var post; var post;
var templates = {}; var templates = {};
var $target; var $target;
var $wrapper;
function init(params, loaded) { function init(params, loaded) {
$target = params.$target; $target = params.$target;
@ -29,57 +30,67 @@ App.Presenters.PostContentPresenter = function(
}); });
} }
var fitters = { function getFitters() {
'fit-height': function($wrapper) {
var originalWidth = $wrapper.attr('data-width'); var originalWidth = $wrapper.attr('data-width');
var originalHeight = $wrapper.attr('data-height'); var originalHeight = $wrapper.attr('data-height');
var ratio = originalWidth / originalHeight; var ratio = originalWidth / originalHeight;
var height = jQuery(window).height() - $wrapper.offset().top; var containerHeight = jQuery(window).height() - $wrapper.offset().top - 10;
var width = (height - 10) * ratio; var containerWidth = $wrapper.parent().outerWidth() - 10;
if (width > originalWidth) {
return {
'fit-height': function(allowUpscale) {
var width = containerHeight * ratio;
if (width > originalWidth && !allowUpscale) {
width = originalWidth; width = originalWidth;
} }
$wrapper.css({maxWidth: width + 'px', width: ''}); $wrapper.css({maxWidth: width + 'px', width: ''});
}, },
'fit-width': function($wrapper) { 'fit-width': function(allowUpscale) {
var originalWidth = $wrapper.attr('data-width'); if (allowUpscale) {
$wrapper.css({maxWidth: containerWidth + 'px', width: ''});
} else {
$wrapper.css({maxWidth: originalWidth + 'px', width: ''}); $wrapper.css({maxWidth: originalWidth + 'px', width: ''});
}
}, },
'original': function($wrapper) { 'original': function(allowUpscale) {
var originalWidth = $wrapper.attr('data-width');
$wrapper.css({maxWidth: '', width: originalWidth + 'px'}); $wrapper.css({maxWidth: '', width: originalWidth + 'px'});
} }
}; };
var fitterNames = Object.keys(fitters);
function getFitMode() {
var $wrapper = $target.find('.object-wrapper');
return $wrapper.data('current-fit');
} }
function changeFitMode(mode) { function getFitMode() {
var $wrapper = $target.find('.object-wrapper'); return $wrapper.data('fit-mode');
$wrapper.data('current-fit', mode); }
fitters[$wrapper.data('current-fit')]($wrapper);
function changeFitMode(fitMode) {
$wrapper.data('fit-mode', fitMode);
getFitters()[fitMode.style](fitMode.upscale);
updatePostNotesSize(); updatePostNotesSize();
} }
function cycleFitMode() { function cycleFitMode() {
var $wrapper = $target.find('.object-wrapper');
var oldMode = getFitMode(); var oldMode = getFitMode();
var newMode = fitterNames[(fitterNames.indexOf(oldMode) + 1) % fitterNames.length]; var fitterNames = Object.keys(getFitters());
var newMode = {
style: fitterNames[(fitterNames.indexOf(oldMode.style) + 1) % fitterNames.length],
upscale: oldMode.upscale,
};
changeFitMode(newMode); changeFitMode(newMode);
} }
function render() { function render() {
$target.html(templates.postContent({post: post})); $target.html(templates.postContent({post: post}));
$wrapper = $target.find('.object-wrapper');
if (post.contentType === 'image') { if (post.contentType === 'image') {
loadPostNotes(); loadPostNotes();
updatePostNotesSize(); updatePostNotesSize();
} }
changeFitMode(browsingSettings.getSettings().fitMode); changeFitMode({
style: browsingSettings.getSettings().fitMode,
upscale: browsingSettings.getSettings().upscale,
});
keyboard.keyup('f', cycleFitMode); keyboard.keyup('f', cycleFitMode);
jQuery(window).resize(updatePostNotesSize); jQuery(window).resize(updatePostNotesSize);
@ -111,9 +122,6 @@ App.Presenters.PostContentPresenter = function(
render: render, render: render,
addNewPostNote: addNewPostNote, addNewPostNote: addNewPostNote,
updatePostNotesSize: updatePostNotesSize, updatePostNotesSize: updatePostNotesSize,
fitWidth: function() { changeFitMode('fit-width'); },
fitHeight: function() { changeFitMode('fit-height'); },
fitOriginal: function() { changeFitMode('original'); },
getFitMode: getFitMode, getFitMode: getFitMode,
changeFitMode: changeFitMode, changeFitMode: changeFitMode,
cycleFitMode: cycleFitMode, cycleFitMode: cycleFitMode,

View file

@ -212,10 +212,10 @@ App.Presenters.PostPresenter = function(
} }
function syncFitModeButtons() { function syncFitModeButtons() {
var fitMode = postContentPresenter.getFitMode(); var fitStyle = postContentPresenter.getFitMode().style;
$el.find('#sidebar .fit-mode a').each(function(i, item) { $el.find('#sidebar .fit-mode a').each(function(i, item) {
var $item = jQuery(item); var $item = jQuery(item);
$item.toggleClass('active', $item.attr('data-fit-mode') === fitMode); $item.toggleClass('active', $item.attr('data-fit-mode') === fitStyle);
}); });
} }
@ -229,7 +229,12 @@ App.Presenters.PostPresenter = function(
function fitModeButtonsClicked(e) { function fitModeButtonsClicked(e) {
e.preventDefault(); e.preventDefault();
postContentPresenter.changeFitMode(jQuery(e.target).attr('data-fit-mode')); var oldMode = postContentPresenter.getFitMode();
var newMode = {
style: jQuery(e.target).attr('data-fit-mode'),
upscale: oldMode.upscale,
};
postContentPresenter.changeFitMode(newMode);
syncFitModeButtons(); syncFitModeButtons();
} }

View file

@ -53,6 +53,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
}, },
keyboardShortcuts: $el.find('[name=keyboardShortcuts]').is(':checked'), keyboardShortcuts: $el.find('[name=keyboardShortcuts]').is(':checked'),
fitMode: $el.find('[name=fitMode]:checked').val(), fitMode: $el.find('[name=fitMode]:checked').val(),
upscale: $el.find('[name=upscale]').is(':checked'),
}; };
promise.wait(browsingSettings.setSettings(newSettings)) promise.wait(browsingSettings.setSettings(newSettings))

View file

@ -58,16 +58,24 @@
<label for="browsing-settings-fit-width"> <label for="browsing-settings-fit-width">
Fit width Fit width
</label> </label>
<br/>
<input <% print(settings.fitMode === 'fit-height' ? 'checked="checked"' : '') %> type="radio" id="browsing-settings-fit-height" name="fitMode" value="fit-height"/> <input <% print(settings.fitMode === 'fit-height' ? 'checked="checked"' : '') %> type="radio" id="browsing-settings-fit-height" name="fitMode" value="fit-height"/>
<label for="browsing-settings-fit-height"> <label for="browsing-settings-fit-height">
Fit height Fit height
</label> </label>
<br/>
<input <% print(settings.fitMode === 'original' ? 'checked="checked"' : '') %> type="radio" id="browsing-settings-fit-original" name="fitMode" value="original"/> <input <% print(settings.fitMode === 'original' ? 'checked="checked"' : '') %> type="radio" id="browsing-settings-fit-original" name="fitMode" value="original"/>
<label for="browsing-settings-fit-original"> <label for="browsing-settings-fit-original">
Original Original
</label> </label>
<br/>
<input <% print(settings.upscale ? 'checked="checked"' : '') %> type="checkbox" id="browsing-settings-upscale" name="upscale" value="upscale"/>
<label for="browsing-settings-upscale">
Upscale small posts
</label>
</div> </div>
</div> </div>