Skip to content

Commit

Permalink
add defaultCropType param
Browse files Browse the repository at this point in the history
  • Loading branch information
dblatcher committed Oct 28, 2024
1 parent 50a5220 commit a6d84f1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion kahuna/public/js/crop/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ crop.controller('ImageCropCtrl', [
const allCropOptions = cropSettings.getCropOptions();

const storageCropType = cropSettings.getCropType();
const storageDefaultCropType = cropSettings.getDefaultCropType();

const cropOptionDisplayValue = cropOption => cropOption.ratioString
? `${cropOption.key} (${cropOption.ratioString})`
Expand All @@ -63,7 +64,7 @@ crop.controller('ImageCropCtrl', [
disabled: storageCropType && storageCropType !== option.key
}));

ctrl.cropType = storageCropType || defaultCrop.key;
ctrl.cropType = storageCropType || storageDefaultCropType ||defaultCrop.key;

Check failure on line 67 in kahuna/public/js/crop/controller.js

View workflow job for this annotation

GitHub Actions / CI

Operator '||' must be spaced

ctrl.image = image;
ctrl.optimisedImageUri = optimisedImageUri;
Expand Down
2 changes: 1 addition & 1 deletion kahuna/public/js/crop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crop.config(['$stateProvider',
function($stateProvider) {

$stateProvider.state('crop', {
url: '/images/:imageId/crop?cropType&customRatio&shouldShowCropGuttersIfApplicable',
url: '/images/:imageId/crop?cropType&customRatio&shouldShowCropGuttersIfApplicable&defaultCropType',
template: cropTemplate,
controller: 'ImageCropCtrl',
controllerAs: 'ctrl',
Expand Down
2 changes: 1 addition & 1 deletion kahuna/public/js/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ image.config(['$stateProvider',
function($stateProvider) {

$stateProvider.state('image', {
url: '/images/:imageId?crop?cropType&customRatio&shouldShowCropGuttersIfApplicable',
url: '/images/:imageId?crop?cropType&customRatio&shouldShowCropGuttersIfApplicable&defaultCropType',
template: imageTemplate,
controller: 'ImageCtrl',
controllerAs: 'ctrl',
Expand Down
2 changes: 1 addition & 1 deletion kahuna/public/js/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ search.config(['$stateProvider', '$urlMatcherFactoryProvider',
$stateProvider.state('search', {
// FIXME [1]: This state should be abstract, but then we can't navigate to
// it, which we need to do to access it's deeper / remembered chile state
url: '/?cropType&customRatio&shouldShowCropGuttersIfApplicable',
url: '/?cropType&customRatio&shouldShowCropGuttersIfApplicable&defaultCropType',
template: searchTemplate,
deepStateRedirect: {
// Inject a transient $stateParams for the results state
Expand Down
25 changes: 23 additions & 2 deletions kahuna/public/js/util/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { landscape, portrait, video, square, freeform, cropOptions } from './con
const CROP_TYPE_STORAGE_KEY = 'cropType';
const CUSTOM_CROP_STORAGE_KEY = 'customCrop';
const SHOULD_SHOW_CROP_GUTTERS_IF_APPLICABLE_STORAGE_KEY = 'shouldShowCropGuttersIfApplicable';
const CROP_DEFAULT_TYPE_STORAGE_KEY = 'defaultCropType';

const customCrop = (label, xRatio, yRatio) => {
return { key:label, ratio: xRatio / yRatio, ratioString: `${xRatio}:${yRatio}`};
Expand Down Expand Up @@ -53,6 +54,14 @@ cropUtil.factory('cropSettings', ['storage', function(storage) {
}
};

const setDefaultCropType = (defaultCropType) => {
if (isValidCropType(defaultCropType)) {
storage.setJs(CROP_DEFAULT_TYPE_STORAGE_KEY, defaultCropType, true);
} else {
storage.clearJs(CROP_DEFAULT_TYPE_STORAGE_KEY);
}
};

const setCustomCrop = customRatio => {
const parsedRatio = parseRatio(customRatio);
if (parsedRatio) {
Expand All @@ -70,7 +79,7 @@ cropUtil.factory('cropSettings', ['storage', function(storage) {
);
};

function set({cropType, customRatio, shouldShowCropGuttersIfApplicable}) {
function set({cropType, customRatio, shouldShowCropGuttersIfApplicable, defaultCropType}) {
// set customRatio first in case cropType relies on a custom crop
if (customRatio) {
setCustomCrop(customRatio);
Expand All @@ -80,6 +89,10 @@ cropUtil.factory('cropSettings', ['storage', function(storage) {
setCropType(cropType);
}

if (defaultCropType) {
setDefaultCropType(defaultCropType)

Check failure on line 93 in kahuna/public/js/util/crop.js

View workflow job for this annotation

GitHub Actions / CI

Missing semicolon
}

if (shouldShowCropGuttersIfApplicable) {
setShouldShowCropGuttersIfApplicable(shouldShowCropGuttersIfApplicable);
}
Expand All @@ -94,11 +107,19 @@ cropUtil.factory('cropSettings', ['storage', function(storage) {
}
}

function getDefaultCropType() {
const defaultCropType = storage.getJs(CROP_DEFAULT_TYPE_STORAGE_KEY, true);

if (isValidCropType(defaultCropType)) {
return defaultCropType;
}
}

function shouldShowCropGuttersIfApplicable() {
return storage.getJs(SHOULD_SHOW_CROP_GUTTERS_IF_APPLICABLE_STORAGE_KEY, true);
}

return { set, getCropType, getCropOptions, shouldShowCropGuttersIfApplicable };
return { set, getCropType, getCropOptions, shouldShowCropGuttersIfApplicable, getDefaultCropType };
}]);

cropUtil.filter('asCropType', function() {
Expand Down

0 comments on commit a6d84f1

Please sign in to comment.