Skip to content

Commit

Permalink
Include extended file rights infos in file meta data
Browse files Browse the repository at this point in the history
REDMINE-20093
  • Loading branch information
tf committed Dec 11, 2023
1 parent a03b3b7 commit da5c9ec
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
21 changes: 20 additions & 1 deletion package/spec/editor/api/FileTypes-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'underscore';

import {TextInputView, TextTableCellView} from 'pageflow/ui';

import {ImageFile, TextTrackFile, VideoFile} from 'pageflow/editor';
import {ImageFile, TextTrackFile, VideoFile, editor} from 'pageflow/editor';
import {FileTypes} from 'pageflow/editor/api/FileTypes';

describe('FileTypes', () => {
Expand Down Expand Up @@ -64,6 +64,25 @@ describe('FileTypes', () => {

expect(_.pluck(fileTypes.first().settingsDialogTabs, 'name')).toEqual(['general', 'extra']);
});

it('includes common metadata attribute', () => {
var fileTypes = new FileTypes();
fileTypes.commonMetaDataAttributes = [
{name: 'rights'},
{name: 'source_url'}
];

fileTypes.register('image_files', {
model: ImageFile,
matchUpload: /^image/,
metaDataAttributes: [{
name: 'alt'
}]
});
fileTypes.setup([{collectionName: 'image_files'}]);

expect(fileTypes.first().metaDataAttributes.map(attribute => attribute.name)).toEqual(['rights', 'source_url', 'alt']);
});
});

describe('#modify', () => {
Expand Down
73 changes: 73 additions & 0 deletions package/spec/editor/views/TextFileMetaDataItemValueView-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {TextFileMetaDataItemValueView} from 'pageflow/editor';

import I18n from 'i18n-js';
import * as support from '$support';

describe('TextFileMetaDataItemValueView', () => {
describe('#getText', () => {
support.useFakeTranslations({
'pageflow.file_licenses.cc_by_sa_4.name': 'CC-BY-SA 4.0',
'pageflow.editor.views.file_meta_data_item_value_view.blank': '(Blank)'
});

it('reads value from attribute by default', () => {
var view = new TextFileMetaDataItemValueView({
model: support.factories.imageFile({rights: 'Some author'}),
name: 'rights'
});

view.render();

expect(view.$el.text()).toContain('Some author');
});

it('supports reading value from configuration', () => {
var view = new TextFileMetaDataItemValueView({
model: support.factories.imageFile({configuration: {alt: 'A tree'}}),
fromConfiguration: true,
name: 'alt'
});

view.render();

expect(view.$el.text()).toContain('A tree');
});

it('displays placeholder if blank', () => {
var view = new TextFileMetaDataItemValueView({
model: support.factories.imageFile({rights: ''}),
name: 'rights'
});

view.render();

expect(view.$el.text()).toContain('(Blank)');
});

it('supports formatting value', () => {
var view = new TextFileMetaDataItemValueView({
model: support.factories.imageFile({configuration: {license: 'cc_by_sa_4'}}),
fromConfiguration: true,
formatValue: value => I18n.t(`pageflow.file_licenses.${value}.name`),
name: 'license'
});

view.render();

expect(view.$el.text()).toContain('CC-BY-SA 4.0');
});

it('only formats non-blank value', () => {
var view = new TextFileMetaDataItemValueView({
model: support.factories.imageFile({configuration: {license: ''}}),
fromConfiguration: true,
formatValue: value => I18n.t(`pageflow.file_licenses.${value}.name`),
name: 'license'
});

view.render();

expect(view.$el.text()).toContain('(Blank)');
});
});
});
12 changes: 1 addition & 11 deletions package/src/editor/api/FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ export const FileType = Object.extend({
this.skipUploadConfirmation = options.skipUploadConfirmation || false;
this.filters = options.filters || [];
this.noExtendedFileRights = options.noExtendedFileRights;

this.metaDataAttributes = [
{
name: 'rights',
valueView: TextFileMetaDataItemValueView,
valueViewOptions: {
settingsDialogTabLink: 'general'
}
},
].concat(options.metaDataAttributes || []);

this.metaDataAttributes = options.metaDataAttributes || [];

if (typeof options.matchUpload === 'function') {
this.matchUpload = options.matchUpload;
Expand Down
5 changes: 5 additions & 0 deletions package/src/editor/api/FileTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const FileTypes = Object.extend({
this.clientSideConfigModifications = {};

this.commonSettingsDialogTabs = [];
this.commonMetaDataAttributes = [];
},

register: function(name, config) {
Expand Down Expand Up @@ -73,6 +74,10 @@ export const FileTypes = Object.extend({
clientSideConfig.settingsDialogTabs = this.commonSettingsDialogTabs.concat(
clientSideConfig.settingsDialogTabs || []
);

clientSideConfig.metaDataAttributes = this.commonMetaDataAttributes.concat(
clientSideConfig.metaDataAttributes || []
);
},

applyModifications(serverSideConfig, clientSideConfig) {
Expand Down
27 changes: 27 additions & 0 deletions package/src/editor/initializers/setupFileTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ import {TextTracksView} from '../views/TextTracksView';
import {state} from '$state';

app.addInitializer(function(options) {
editor.fileTypes.commonMetaDataAttributes = [
{
name: 'rights',
valueView: TextFileMetaDataItemValueView,
valueViewOptions: {
fromConfiguration: true,
settingsDialogTabLink: 'general'
}
},
{
name: 'source_url',
valueView: TextFileMetaDataItemValueView,
valueViewOptions: {
fromConfiguration: true,
settingsDialogTabLink: 'general'
}
},
{
name: 'license',
valueView: TextFileMetaDataItemValueView,
valueViewOptions: {
fromConfiguration: true,
formatValue: value => I18n.t(`pageflow.file_licenses.${value}.name`),
settingsDialogTabLink: 'general'
}
}
];
editor.fileTypes.commonSettingsDialogTabs = [
{
name: 'general',
Expand Down
8 changes: 7 additions & 1 deletion package/src/editor/views/TextFileMetaDataItemValueView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export const TextFileMetaDataItemValueView = FileMetaDataItemValueView.extend({
model = this.model;
}

return model.get(this.options.name);
const value = model.get(this.options.name);

if (value && this.options.formatValue) {
return this.options.formatValue(value);
}

return value;
}
});

0 comments on commit da5c9ec

Please sign in to comment.