diff --git a/docs/README.md b/docs/README.md index 2a54c85de0..1b2b1dd328 100644 --- a/docs/README.md +++ b/docs/README.md @@ -323,6 +323,7 @@ for more information about installing and using the source code. - [Node permission directive](node-permission.directive.md) - [Node restore directive](node-restore.directive.md) - [Upload directive](upload.directive.md) +- [Node Name Tooltip directive](node-name-tooltip.pipe.md) - [*Card view content proxy directive](../ng2-components/ng2-alfresco-core/src/components/view/card-view-content-proxy.directive.ts) - [*Highlight directive](../ng2-components/ng2-alfresco-core/src/directives/highlight.directive.ts) diff --git a/docs/node-name-tooltip.pipe.md b/docs/node-name-tooltip.pipe.md new file mode 100644 index 0000000000..346a73dac8 --- /dev/null +++ b/docs/node-name-tooltip.pipe.md @@ -0,0 +1,37 @@ +# Node Name Tooltip directive + +Formats the tooltip of the underlying Node based on the following rules: + +* if the *title* and *description* are missing, then the tooltip shows the *name*; +* if the *title* is missing, then the tooltip shows the *name* and *description*; +* if the *description* is missing, then the tooltip shows the *name* and *title*; +* if *name* and *title*, *name* and *description*, or *title* and *description* are the same, then only a single line is displayed. + + + + + +- [Basic Usage](#basic-usage) + + + + + +## Basic Usage + +```html + + + {{ value }} + + +``` + + + + +## See also + + \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.spec.ts b/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.spec.ts new file mode 100644 index 0000000000..45be5ea3f3 --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.spec.ts @@ -0,0 +1,145 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { MinimalNodeEntity } from 'alfresco-js-api'; +import { NodeNameTooltipPipe } from './node-name-tooltip.pipe'; + +describe('NodeNameTooltipPipe', () => { + + const nodeName = 'node-name'; + const nodeTitle = 'node-title'; + const nodeDescription = 'node-description'; + + let pipe: NodeNameTooltipPipe; + + beforeEach(() => { + pipe = new NodeNameTooltipPipe(); + }); + + it('should not transform when missing node', () => { + expect(pipe.transform(null)).toBe(null); + }); + + it('should not transform when missing node entry', () => { + expect(pipe.transform( {})).toBe(null); + }); + + it('should use title and description when all fields present', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': nodeTitle, + 'cm:description': nodeDescription + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(`${nodeTitle}\n${nodeDescription}`); + }); + + it('should use name when other properties are missing', () => { + const node = { + entry: { + name: nodeName + } + }; + let tooltip = pipe.transform( node); + expect(tooltip).toBe(nodeName); + }); + + it('should display name when title and description are missing', () => { + const node: any = { + entry: { + name: nodeName, + properties: {} + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(nodeName); + }); + + it('should use name and description when title is missing', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': null, + 'cm:description': nodeDescription + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(`${nodeName}\n${nodeDescription}`); + }); + + it('should use name and title when description is missing', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': nodeTitle, + 'cm:description': null + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(`${nodeName}\n${nodeTitle}`); + }); + + it('should use name if name and description are the same', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': null, + 'cm:description': nodeName + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(nodeName); + }); + + it('should use name if name and title are the same', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': nodeName, + 'cm:description': null + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(nodeName); + }); + + it('should use name if all values are the same', () => { + const node: any = { + entry: { + name: nodeName, + properties: { + 'cm:title': nodeName, + 'cm:description': nodeName + } + } + }; + let tooltip = pipe.transform(node); + expect(tooltip).toBe(nodeName); + }); +}); diff --git a/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.ts b/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.ts new file mode 100644 index 0000000000..a3abee264f --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/pipes/node-name-tooltip.pipe.ts @@ -0,0 +1,78 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Pipe, PipeTransform } from '@angular/core'; +import { MinimalNodeEntity } from 'alfresco-js-api'; + +@Pipe({ + name: 'adfNodeNameTooltip' +}) +export class NodeNameTooltipPipe implements PipeTransform { + + transform(node: MinimalNodeEntity): string { + if (node) { + return this.getNodeTooltip(node); + } + return null; + } + + private containsLine(lines: string[], line: string): boolean { + return lines.some((item: string) => { + return item.toLowerCase() === line.toLowerCase(); + }); + } + + private removeDuplicateLines(lines: string[]): string[] { + const reducer = (acc: string[], line: string): string[] => { + if (!this.containsLine(acc, line)) { acc.push(line); } + return acc; + }; + + return lines.reduce(reducer, []); + } + + private getNodeTooltip(node: MinimalNodeEntity): string { + if (!node || !node.entry) { + return null; + } + + const { entry: { properties, name } } = node; + const lines = [ name ]; + + if (properties) { + const { + 'cm:title': title, + 'cm:description': description + } = properties; + + if (title && description) { + lines[0] = title; + lines[1] = description; + } + + if (title) { + lines[1] = title; + } + + if (description) { + lines[1] = description; + } + } + + return this.removeDuplicateLines(lines).join(`\n`); + } +}