Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { themePalette, themeTokens } from 'kolibri-design-system/lib/styles/theme';
import { getContentKindColor } from '../colors';

describe('getContentKindColor', () => {
const tokens = themeTokens();
const palette = themePalette();

it.each([
['video', tokens.watch],
['audio', tokens.listen],
['document', tokens.read],
['slideshow', tokens.read],
['exercise', tokens.practice],
['html5', tokens.explore],
['h5p', tokens.explore],
['zim', tokens.explore],
['topic', palette.grey.v_400],
])('returns the KDS color for %s', (kind, expected) => {
expect(getContentKindColor(kind)).toBe(expected);
});

it.each(['unsupported', undefined, null, ''])('falls back to grey for %s', kind => {
expect(getContentKindColor(kind)).toBe(palette.grey.v_400);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getContentKindIcon } from '../icons';

describe('getContentKindIcon', () => {
it.each([
['topic', 'topic'],
['video', 'video'],
['audio', 'audio'],
['slideshow', 'slideshow'],
['exercise', 'exercise'],
['document', 'document'],
['html5', 'html5'],
['h5p', 'html5'],
['zim', 'html5'],
['unsupported', 'error'],
])('maps %s to %s', (kind, icon) => {
expect(getContentKindIcon(kind)).toBe(icon);
});

it('uses the empty-topic icon only when a topic is empty', () => {
expect(getContentKindIcon('topic', true)).toBe('emptyTopic');
expect(getContentKindIcon('h5p', true)).toBe('html5');
});

it('throws when a kind has no mapping', () => {
expect(() => getContentKindIcon('missing')).toThrow('Icon not found for content kind: missing');
});
});
19 changes: 19 additions & 0 deletions contentcuration/contentcuration/frontend/shared/utils/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { themePalette, themeTokens } from 'kolibri-design-system/lib/styles/theme';
import { ContentKindsNames } from 'shared/leUtils/ContentKinds';

export function getContentKindColor(kind) {
const palette = themePalette();
const tokens = themeTokens();
const colors = {
[ContentKindsNames.TOPIC]: palette.grey.v_400,
[ContentKindsNames.VIDEO]: tokens.watch,
[ContentKindsNames.AUDIO]: tokens.listen,
[ContentKindsNames.DOCUMENT]: tokens.read,
[ContentKindsNames.SLIDESHOW]: tokens.read,
[ContentKindsNames.EXERCISE]: tokens.practice,
[ContentKindsNames.H5P]: tokens.explore,
[ContentKindsNames.HTML5]: tokens.explore,
[ContentKindsNames.ZIM]: tokens.explore,
};
return colors[kind] || palette.grey.v_400;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ const CONTENT_KIND_ICONS = {
[ContentKindsNames.SLIDESHOW]: 'slideshow',
[ContentKindsNames.EXERCISE]: 'exercise',
[ContentKindsNames.DOCUMENT]: 'document',
[ContentKindsNames.H5P]: 'html5',
[ContentKindsNames.HTML5]: 'html5',
[ContentKindsNames.ZIM]: 'html5',
// Not a content kind. Base Vuetify uses error_outline; KDS has no such token.
// Unknown kinds still throw below.
unsupported: 'error',
};

export function getContentKindIcon(kind, isEmpty = false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<template>

<span
v-if="icon"
class="studio-content-node-icon"
:style="{ backgroundColor: backgroundColor }"
capture-as-image
>
<KIcon
Comment thread
rtibblesbot marked this conversation as resolved.
Resolved
:icon="icon"
:color="$themeTokens.textInverted"
v-bind="$attrs"
/>
</span>

</template>


<script>

import ContentKinds from 'shared/leUtils/ContentKinds';
import { getContentKindIcon } from 'shared/utils/icons';
import { getContentKindColor } from 'shared/utils/colors';

export default {
name: 'StudioContentNodeIcon',
inheritAttrs: false,
props: {
kind: {
type: String,
validator: value => {
return value === 'unsupported' || ContentKinds.has(value);
},
default: 'unsupported',
},
isEmpty: {
type: Boolean,
default: false,
},
},
computed: {
icon() {
return getContentKindIcon(this.kind, this.isEmpty);
},
backgroundColor() {
return getContentKindColor(this.kind);
},
},
};

</script>


<style lang="scss" scoped>

// Icon-only ContentNodeIcon: .v-chip__content is 22px with 0 5px padding,
// and the Vuetify icon is small (16px). KIcon's svg defaults to 1.125em.
.studio-content-node-icon {
box-sizing: border-box;
display: inline-flex;
align-items: center;
justify-content: center;
height: 22px;
padding: 0 5px;
line-height: 1;
vertical-align: middle;
border-radius: 4px;

svg {
top: 0;
width: 16px;
height: 16px;
}
}

</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { render } from '@testing-library/vue';
import Vue from 'vue';
import VueRouter from 'vue-router';
import KIcon from 'kolibri-design-system/lib/KIcon';
import StudioContentNodeIcon from '../StudioContentNodeIcon.vue';
import { getContentKindColor } from 'shared/utils/colors';
import { getContentKindIcon } from 'shared/utils/icons';

const renderComponent = (props = {}) =>
render(StudioContentNodeIcon, { props, routes: new VueRouter() });

const renderIcon = icon => render(KIcon, { props: { icon }, routes: new VueRouter() });

function iconShape(container) {
const paths = [...container.querySelectorAll('svg path')].map(path => path.getAttribute('d'));
expect(paths.length).toBeGreaterThan(0);
return paths.join('|');
}

describe('StudioContentNodeIcon', () => {
it.each(['topic', 'video', 'audio', 'exercise', 'document', 'slideshow', 'html5', 'h5p', 'zim'])(
'renders the %s icon on its content-kind background',
kind => {
const { container } = renderComponent({ kind });
const chip = container.querySelector('.studio-content-node-icon');

expect(chip).toHaveStyle({ backgroundColor: getContentKindColor(kind) });
expect(iconShape(container)).toBe(iconShape(renderIcon(getContentKindIcon(kind)).container));
},
);

it('uses the html5 glyph for h5p', () => {
const h5p = iconShape(renderComponent({ kind: 'h5p' }).container);
const html5 = iconShape(renderComponent({ kind: 'html5' }).container);
const video = iconShape(renderComponent({ kind: 'video' }).container);

expect(h5p).toBe(html5);
expect(h5p).not.toBe(video);
});

it('renders the error icon for an unsupported kind', () => {
const { container } = renderComponent({ kind: 'unsupported' });
const chip = container.querySelector('.studio-content-node-icon');

expect(chip).toHaveStyle({ backgroundColor: getContentKindColor('unsupported') });
expect(iconShape(container)).toBe(iconShape(renderIcon('error').container));
});

it('does not swallow a missing icon mapping', () => {
const errorHandler = jest.fn();
const previous = Vue.config.errorHandler;
Vue.config.errorHandler = errorHandler;

try {
renderComponent({ kind: 'missing' });

expect(errorHandler).toHaveBeenCalled();
expect(errorHandler.mock.calls[0][0]).toEqual(
expect.objectContaining({ message: 'Icon not found for content kind: missing' }),
);
} finally {
Vue.config.errorHandler = previous;
}
});

it('is marked to be captured as an image in PDF exports', () => {
const { container } = renderComponent({ kind: 'video' });
expect(container.querySelector('[capture-as-image]')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/vue';
import { render, screen, within } from '@testing-library/vue';
import VueRouter from 'vue-router';
import ContentLevels from 'kolibri-constants/labels/Levels';
import Categories from 'kolibri-constants/labels/Subjects';
Expand Down Expand Up @@ -115,4 +115,30 @@ describe('StudioDetailsPanel', () => {
expect(placeholders.length).toBeGreaterThan(0);
});
});
describe('total resources', () => {
it('lists each content kind with its translated label and count', () => {
renderComponent({
details: {
...fullChannel,
kind_count: [
{ kind_id: 'video', count: 30 },
{ kind_id: 'exercise', count: 12 },
{ kind_id: 'h5p', count: 7 },
],
},
loading: false,
});

[
['Video', '30'],
['Exercise', '12'],
['H5P App', '7'],
].forEach(([label, count]) => {
const row = screen.getByText(label).closest('.resource-item');
expect(row).not.toBeNull();
expect(within(row).getByText(count)).toBeInTheDocument();
expect(row.querySelector('.studio-content-node-icon svg')).toBeInTheDocument();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
>
<KGridItem :layout="{ span: 3 }">
<dt>
<ContentNodeIcon
<StudioContentNodeIcon
class="resource-icon"
:kind="item.kind_id"
/>
Expand Down Expand Up @@ -445,7 +445,7 @@
metadataTranslationMixin,
} from '../../mixins';
import { CategoriesLookup, LevelsLookup } from '../../constants';
import ContentNodeIcon from '../ContentNodeIcon';
import StudioContentNodeIcon from '../StudioContentNodeIcon';
import ExpandableList from '../ExpandableList';
import StudioChip from '../StudioChip';
import StudioLargeLoader from '../StudioLargeLoader';
Expand Down Expand Up @@ -495,7 +495,7 @@
name: 'StudioDetailsPanel',
components: {
StudioLargeLoader,
ContentNodeIcon,
StudioContentNodeIcon,
ExpandableList,
StudioCopyToken,
StudioDetailsRow,
Expand Down
Loading