Skip to content

Commit

Permalink
Update the background color to the new API.
Browse files Browse the repository at this point in the history
Now `BackgroundColor` provides a `newInstance()` method that
takes publicAPI and model objects, making it a good minimal example.
It also demonstrates how to import HTML fragments and CSS
style from external files.
See the `ToggleControl` and `Workbench` examples for usage.
  • Loading branch information
vibraphone committed Nov 1, 2016
1 parent bc4e7dd commit 8ec1ab0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 40 deletions.
4 changes: 4 additions & 0 deletions src/Component/Native/BackgroundColor/body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="bg-color-container">
<div class="bg-color-placeholder">
</div>
</div>
90 changes: 62 additions & 28 deletions src/Component/Native/BackgroundColor/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
/* eslint-disable class-methods-use-this */

export default class NativeBackgroundColorComponent {
constructor(color, el) {
this.color = color;
this.setContainer(el);
this.previousColor = '';
}

/*
* We must not mess with the position properties of the style on the container
* we are given, or we will break the workbench layout functionality! Setting the
* background color is fine, however, as long as we don't use the setAttribute()
* approach to this. Also, we could always create our own container
* within the element we are given, and we can do whatever we want with that.
*/

setContainer(el) {
if (this.el) {
this.el.style['background-color'] = this.previousColor;
import style from 'PVWStyle/ComponentNative/BackgroundColor.mcss';
import htmlContent from './body.html';
import CompositeClosureHelper from '../../../Common/Core/CompositeClosureHelper';

function backgroundColorComponent(publicAPI, model) {
publicAPI.resize = () => {
// When using this component as a template, respond
// to resize events by updating the DOM to match.
};

publicAPI.setContainer = (el) => {
if (model.container) {
while (model.container.firstChild) {
model.container.removeChild(model.container.firstChild);
}
}

this.el = el;
model.container = el;

if (el) {
this.previousColor = this.el.style['background-color'];
this.el.style['background-color'] = this.color;
if (model.container) {
// Create placeholder
model.container.innerHTML = htmlContent;

// Apply style
const colorEle = model.container.querySelector('.bg-color-container');
colorEle.classList.add(style.bgColorContainer);
colorEle.style.backgroundColor = model.color;
}
}
};

publicAPI.setColor = (colorSpec) => {
let color = colorSpec;
if (typeof colorSpec !== 'string' || colorSpec === '') {
color = 'inherit';
}
model.color = color;
if (model.container) {
model.container.querySelector('.bg-color-container').style.backgroundColor = model.color;
}
};
}

render() {}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

resize() {}
const DEFAULT_VALUES = {
container: null,
color: 'inherit',
};

destroy() {}
// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

CompositeClosureHelper.destroy(publicAPI, model);
CompositeClosureHelper.isA(publicAPI, model, 'VizComponent');
CompositeClosureHelper.get(publicAPI, model, ['color', 'container']);

backgroundColorComponent(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = CompositeClosureHelper.newInstance(extend);

// ----------------------------------------------------------------------------

export default { newInstance, extend };
10 changes: 5 additions & 5 deletions src/Component/Native/Composite/example/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CompositeComponent from '..';
import BGColorComponent from '../../BackgroundColor';
import BackgroundColor from '../../BackgroundColor';

// Load CSS
require('normalize.css');
Expand All @@ -10,10 +10,10 @@ container.style.width = '100%';
container.style.height = '600px';

const composite = new CompositeComponent();
const green = new BGColorComponent('green');
const red = new BGColorComponent('red');
const blue = new BGColorComponent('blue');
const pink = new BGColorComponent('pink');
const green = BackgroundColor.newInstance({ color:'green' });
const red = BackgroundColor.newInstance({ color:'red' });
const blue = BackgroundColor.newInstance({ color:'blue' });
const pink = BackgroundColor.newInstance({ color:'pink' });

composite.addViewport(green);
composite.addViewport(red);
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Native/ToggleControl/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'normalize.css';
const container = document.querySelector('.content');
container.style.height = '100vh';

const green = new BGColorComponent('green');
const red = new BGColorComponent('red');
const green = BGColorComponent.newInstance({ color:'green' });
const red = BGColorComponent.newInstance({ color:'red' });
const toggleView = new ToggleControlComponent(green, red);

toggleView.setContainer(container);
Expand Down
10 changes: 5 additions & 5 deletions src/Component/Native/Workbench/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const container = document.querySelector('.content');
container.style.height = '100vh';
container.style.width = '100vw';

const green = new BGColor('green');
const red = new BGColor('red');
const blue = new BGColor('blue');
const pink = new BGColor('pink');
const gray = new BGColor('gray');
const green = BGColor.newInstance({ color:'green' });
const red = BGColor.newInstance({ color:'red' });
const blue = BGColor.newInstance({ color:'blue' });
const pink = BGColor.newInstance({ color:'pink' });
const gray = BGColor.newInstance({ color:'gray' });

// const toggleView = new ToggleControl(green, red);

Expand Down
11 changes: 11 additions & 0 deletions style/ComponentNative/BackgroundColor.mcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.bgColorContainer {
border-radius: 15px;
font-family: "Optima", "Linux Biolinum", "URW Classico", sans;
position: absolute;
top: 5px;
right: 5px;
bottom: 5px;
left: 5px;
pointer-events: none;
overflow: hidden;
}

0 comments on commit 8ec1ab0

Please sign in to comment.