From 0a82c7f5e79b2310f5579182a4878b946af9908a Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 28 Mar 2018 19:23:15 +0900 Subject: [PATCH] Add oo-molecules-project-users element --- .../oo-molecules-project-users/index.test.ts | 46 ++++++++++++ .../oo-molecules-project-users/index.ts | 71 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/elements/_molecules/oo-molecules-project-users/index.test.ts create mode 100644 src/elements/_molecules/oo-molecules-project-users/index.ts diff --git a/src/elements/_molecules/oo-molecules-project-users/index.test.ts b/src/elements/_molecules/oo-molecules-project-users/index.test.ts new file mode 100644 index 0000000..2a16163 --- /dev/null +++ b/src/elements/_molecules/oo-molecules-project-users/index.test.ts @@ -0,0 +1,46 @@ +import el from './index' +import define from '../../../lib/define' +import insertElement from '../../../lib/test/insert-element' +import getElement from '../../../lib/test/get-element' +import removeElement from '../../../lib/test/remove-element' + +const ELEMENT = 'oo-molecules-project-users' + +describe(`<${ELEMENT}>`, () => { + before(() => { + define(ELEMENT, el) + }) + + it('Mount on document', () => { + insertElement(ELEMENT) + expect(getElement(ELEMENT)[0]).to.be.ok() + }) + + describe('Display project author and assignee', () => { + it('Display author when exists "data-author" attribute', () => { + const element = insertElement(ELEMENT, new Map([['data-author', 'xxx']])) + const items = Array.from(element.shadowRoot.querySelectorAll('oo-atoms-user-name')) + expect(items).to.have.length(1) + expect(items[0].getAttribute('data-iam')).to.be('xxx') + }) + + it('Display assignee when exists "data-assignee" attribute', () => { + const element = insertElement(ELEMENT, new Map([['data-assignee', 'yyy']])) + const items = Array.from(element.shadowRoot.querySelectorAll('oo-atoms-user-name')) + expect(items).to.have.length(1) + expect(items[0].getAttribute('data-iam')).to.be('yyy') + }) + + it('Display author and assignee when exists "data-author" attribute and "data-assignee" attribute', () => { + const element = insertElement(ELEMENT, new Map([['data-author', 'xxx'], ['data-assignee', 'yyy']])) + const items = Array.from(element.shadowRoot.querySelectorAll('oo-atoms-user-name')) + expect(items).to.have.length(2) + expect(items[0].getAttribute('data-iam')).to.be('xxx') + expect(items[1].getAttribute('data-iam')).to.be('yyy') + }) + }) + + after(() => { + removeElement(ELEMENT) + }) +}) diff --git a/src/elements/_molecules/oo-molecules-project-users/index.ts b/src/elements/_molecules/oo-molecules-project-users/index.ts new file mode 100644 index 0000000..0ed6faf --- /dev/null +++ b/src/elements/_molecules/oo-molecules-project-users/index.ts @@ -0,0 +1,71 @@ +import {OOElement} from '../../oo-element' +import {html} from '../../../lib/html' +import weakMap from '../../../lib/weak-map' +import userName from '../../_atoms/oo-atoms-user-name' +import define from '../../../lib/define' + +define('oo-atoms-user-name', userName) + +const ATTR = { + DATA_AUTHOR: 'data-author', + DATA_ASSIGNEE: 'data-assignee' +} + +const stateAuthor = weakMap() +const stateAssignee = weakMap() + +const content = (uid?: string) => uid ? html`` : html`` + +export default class extends OOElement { + static get observedAttributes() { + return [ATTR.DATA_AUTHOR, ATTR.DATA_ASSIGNEE] + } + + attributeChangedCallback(attr, prev, next: string) { + if (prev === next && !next) { + return + } + switch (attr) { + case ATTR.DATA_AUTHOR: + stateAuthor.set(this, next) + break + case ATTR.DATA_ASSIGNEE: + stateAssignee.set(this, next) + break + default: + break + } + if (this.connected) { + this.update() + } + } + + render() { + const author = stateAuthor.get(this) + const assignee = stateAssignee.get(this) + + return html` + +
+ ${content(author)} + ${content(assignee)} +
+ ` + } +}