Skip to content

Commit

Permalink
make searchWithModifiers reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-nowak committed Apr 12, 2024
1 parent e618211 commit 768ad9e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 131 deletions.
110 changes: 45 additions & 65 deletions kahuna/public/js/components/gr-image-metadata/gr-image-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { editOptions, overwrite } from '../../util/constants/editOptions';
import '../../services/image-accessor';
import '../../services/image-list';
import '../../services/label';
import { List } from 'immutable';
import '../../search/query-filter';

import { fieldFilter} from '../../search/query-filter';
import { List } from 'immutable';

export const module = angular.module('gr.imageMetadata', [
'gr.image.service',
Expand All @@ -26,8 +26,6 @@ export const module = angular.module('gr.imageMetadata', [
module.controller('grImageMetadataCtrl', [
'$rootScope',
'$scope',
'$state',
'$stateParams',
'$window',
'editsService',
'mediaApi',
Expand All @@ -38,12 +36,9 @@ module.controller('grImageMetadataCtrl', [
'inject$',
'labelService',
'storage',


'searchWithModifiers',
function ($rootScope,
$scope,
$state,
$stateParams,
$window,
editsService,
mediaApi,
Expand All @@ -53,7 +48,8 @@ module.controller('grImageMetadataCtrl', [
imageAccessor,
inject$,
labelService,
storage) {
storage,
searchWithModifiers) {

let ctrl = this;

Expand All @@ -64,14 +60,14 @@ module.controller('grImageMetadataCtrl', [
ctrl.metadataUpdatedByTemplate = [];

ctrl.$onInit = () => {
$scope.$watchCollection('ctrl.selectedImages', function() {
$scope.$watchCollection('ctrl.selectedImages', function () {
ctrl.singleImage = singleImage();
ctrl.selectedLabels = selectedLabels();
ctrl.usageRights = selectedUsageRights();
inject$($scope, Rx.Observable.fromPromise(selectedUsageCategory(ctrl.usageRights)), ctrl, 'usageCategory');
ctrl.rawMetadata = rawMetadata();
ctrl.metadata = displayMetadata();
ctrl.metadata.dateTaken = ctrl.displayDateTakenMetadata();
ctrl.metadata.dateTaken = ctrl.displayDateTakenMetadata();
ctrl.newPeopleInImage = "";
ctrl.newKeywords = "";
ctrl.extraInfo = extraInfo();
Expand All @@ -82,26 +78,28 @@ module.controller('grImageMetadataCtrl', [
});

const freeUpdateListener = $rootScope.$on('images-updated',
(e, updatedImages) => updateHandler(updatedImages));
(e, updatedImages) => updateHandler(updatedImages));

const updateHandler = (updatedImages) => {
ctrl.selectedImages = new List(updatedImages);
};

ctrl.hasMultipleValues = (val) => Array.isArray(val) && val.length > 1;

ctrl.displayDateTakenMetadata = function() {
ctrl.displayDateTakenMetadata = function () {
let dateTaken = ctrl.metadata.dateTaken ? new Date(ctrl.metadata.dateTaken) : undefined;
if (dateTaken) { dateTaken.setSeconds(0, 0); }
if (dateTaken) {
dateTaken.setSeconds(0, 0);
}
return dateTaken;
};

ctrl.credits = function(searchText) {
ctrl.credits = function (searchText) {
return ctrl.metadataSearch('credit', searchText);
};

ctrl.metadataSearch = (field, q) => {
return mediaApi.metadataSearch(field, { q }).then(resource => {
return mediaApi.metadataSearch(field, {q}).then(resource => {
return resource.data.map(d => d.key);
});
};
Expand All @@ -110,11 +108,11 @@ module.controller('grImageMetadataCtrl', [

ctrl.descriptionOptions = editOptions;

ctrl.updateDescriptionField = function() {
ctrl.updateDescriptionField = function () {
ctrl.updateMetadataField('description', ctrl.metadata.description);
};

ctrl.updateLocationField = function(data, value) {
ctrl.updateLocationField = function (data, value) {
Object.keys(value).forEach(key => {
if (value[key] === undefined) {
delete value[key];
Expand All @@ -126,7 +124,7 @@ module.controller('grImageMetadataCtrl', [
ctrl.updateMetadataField = function (field, value) {
var imageArray = Array.from(ctrl.selectedImages);
if (field === 'dateTaken') {
value = value.toISOString();
value = value.toISOString();
}
if (field === 'peopleInImage') {
ctrl.addPersonToImages(imageArray, value);
Expand All @@ -144,7 +142,7 @@ module.controller('grImageMetadataCtrl', [
);
};

ctrl.updateDomainMetadataField = function(name, field, value) {
ctrl.updateDomainMetadataField = function (name, field, value) {
return editsService.updateDomainMetadataField(ctrl.singleImage, name, field, value)
.then((updatedImage) => {
if (updatedImage) {
Expand Down Expand Up @@ -246,7 +244,7 @@ module.controller('grImageMetadataCtrl', [
.map(([key, value]) => {
let fieldAlias = ctrl.fieldAliases.find(_ => _.alias === key);
if (fieldAlias && fieldAlias.displayInAdditionalMetadata === true) {
return [fieldAlias.label, { value, alias: fieldAlias.alias}];
return [fieldAlias.label, {value, alias: fieldAlias.alias}];
}
})
.filter(_ => _ !== undefined));
Expand All @@ -256,7 +254,7 @@ module.controller('grImageMetadataCtrl', [
ctrl.domainMetadata = ctrl.domainMetadataSpecs
.filter(domainMetadataSpec => domainMetadataSpec.fields.length > 0)
.reduce((acc, domainMetadataSpec) => {
let domainMetadata = { ...domainMetadataSpec };
let domainMetadata = {...domainMetadataSpec};

if (ctrl.singleImage.data.metadata) {
const imageDomainMetadata = ctrl.singleImage.data.metadata.domainMetadata ? ctrl.singleImage.data.metadata.domainMetadata : {};
Expand Down Expand Up @@ -290,7 +288,7 @@ module.controller('grImageMetadataCtrl', [
field.selectOptions = field.options
.filter(option => option)
.map(option => {
return { value: option, text: option };
return {value: option, text: option};
});
}

Expand Down Expand Up @@ -338,7 +336,7 @@ module.controller('grImageMetadataCtrl', [
ctrl.hasLocationInformation = hasLocationInformation;

function singleImage() {
if (ctrl.selectedImages.size === 1){
if (ctrl.selectedImages.size === 1) {
return ctrl.selectedImages.first();
}
}
Expand Down Expand Up @@ -377,18 +375,23 @@ module.controller('grImageMetadataCtrl', [
function rawMetadata() {
return selectedMetadata().map((values) => {
switch (values.size) {
case 0: return undefined;
case 1: return values.first();
default: return Array.from(values);
case 0:
return undefined;
case 1:
return values.first();
default:
return Array.from(values);
}
}).toObject();
}

function displayMetadata() {
return selectedMetadata().map((values) => {
switch (values.size) {
case 1: return values.first();
default: return undefined;
case 1:
return values.first();
default:
return undefined;
}
}).toObject();
}
Expand All @@ -398,9 +401,12 @@ module.controller('grImageMetadataCtrl', [
const properties = imageList.getSetOfProperties(info);
return properties.map((values) => {
switch (values.size) {
case 0: return undefined;
case 1: return values.first();
default: return Array.from(values);
case 0:
return undefined;
case 1:
return values.first();
default:
return Array.from(values);
}
}).toObject();
}
Expand All @@ -427,11 +433,11 @@ module.controller('grImageMetadataCtrl', [
ctrl.removeImageFromCollection = (collection) => {
ctrl.removingCollection = collection;
collections.removeImageFromCollection(collection, ctrl.singleImage)
.then(() => ctrl.removingCollection = false);
.then(() => ctrl.removingCollection = false);
};

$scope.$on('$destroy', function() {
freeUpdateListener();
$scope.$on('$destroy', function () {
freeUpdateListener();
});

ctrl.onMetadataTemplateSelected = (metadata, usageRights, collection, leasesWithConfig) => {
Expand Down Expand Up @@ -504,44 +510,18 @@ module.controller('grImageMetadataCtrl', [
};

ctrl.isDomainMetadataEmpty = (key) => {
return ctrl.domainMetadata.find(obj => obj.name === key ).fields.every(field => field.value === undefined );
return ctrl.domainMetadata.find(obj => obj.name === key).fields.every(field => field.value === undefined);
};

ctrl.isAdditionalMetadataEmpty = () => {
const totalAdditionalMetadataCount = Object.keys(ctrl.metadata).filter(key => ctrl.isUsefulMetadata(key)).length +
Object.keys(ctrl.additionalMetadata).length +
Object.keys(ctrl.identifiers).length;
Object.keys(ctrl.additionalMetadata).length +
Object.keys(ctrl.identifiers).length;

return totalAdditionalMetadataCount == 0;
};

function updateQueryWithModifiers(field, fieldValue, alt, shift, prevQuery) {
if (alt && prevQuery) {
return `${prevQuery} -${fieldFilter(field, fieldValue)}`;
}
if (alt) {
return `-${fieldFilter(field, fieldValue)}`;
}
if (shift && prevQuery) {
return `${prevQuery} ${fieldFilter(field, fieldValue)}`;
}
return fieldFilter(field, fieldValue);
}


ctrl.searchWithModifiers = ($event, fieldName, fieldValue) => {
const alt = event.getModifierState('Alt');
const shift = event.getModifierState('Shift');
if (alt || shift) {
$event.preventDefault();
const nonFree = ctrl.srefNonfree();

return $state.go('search.results', {
query: updateQueryWithModifiers(fieldName, fieldValue, alt, shift, $stateParams.query),
nonFree: nonFree
});
}
};
ctrl.searchWithModifiers = searchWithModifiers;
};
}
]);
Expand Down
37 changes: 4 additions & 33 deletions kahuna/public/js/edits/list-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import '../services/image-list';
import '../util/storage';

import '../search/query-filter';
import { fieldFilter} from '../search/query-filter';

export var listEditor = angular.module('kahuna.edits.listEditor', [
'kahuna.search.filters.query',
Expand All @@ -18,22 +17,20 @@ export var listEditor = angular.module('kahuna.edits.listEditor', [
listEditor.controller('ListEditorCtrl', [
'$rootScope',
'$scope',
'$state',
'$stateParams',
'$window',
'$timeout',
'imageLogic',
'imageList',
'storage',
'searchWithModifiers',
function($rootScope,
$scope,
$state,
$stateParams,
$window,
$timeout,
imageLogic,
imageList,
storage) {
storage,
searchWithModifiers) {
var ctrl = this;

ctrl.$onInit = () => {
Expand Down Expand Up @@ -152,34 +149,8 @@ listEditor.controller('ListEditorCtrl', [
$scope.$on('$destroy', function() {
updateListener();
});
function updateQueryWithModifiers(field, fieldValue, alt, shift, prevQuery) {
if (alt && prevQuery) {
return `${prevQuery} -${fieldFilter(field, fieldValue)}`;
}
if (alt) {
return `-${fieldFilter(field, fieldValue)}`;
}
if (shift && prevQuery) {
return `${prevQuery} ${fieldFilter(field, fieldValue)}`;
}
return fieldFilter(field, fieldValue);
}


ctrl.searchWithModifiers = ($event, fieldName, fieldValue) => {
const alt = event.getModifierState('Alt');
const shift = event.getModifierState('Shift');
if (alt || shift) {
$event.preventDefault();
const nonFree = ctrl.srefNonfree();

return $state.go('search.results', {
query: updateQueryWithModifiers(fieldName, fieldValue, alt, shift, $stateParams.query),
nonFree: nonFree
});
}
};

ctrl.searchWithModifiers = searchWithModifiers;
};
}]);

Expand Down
Loading

0 comments on commit 768ad9e

Please sign in to comment.