-
Notifications
You must be signed in to change notification settings - Fork 400
Add theme preview #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add theme preview #344
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,66 @@ | ||
import React, { PropTypes } from 'react'; | ||
import classNames from 'classnames'; | ||
import themeAction from 'disco/themePreview'; | ||
import { gettext as _ } from 'core/utils'; | ||
|
||
import InstallButton from './InstallButton'; | ||
import { | ||
validAddonTypes, | ||
EXTENSION_TYPE, | ||
THEME_TYPE, | ||
THEME_PREVIEW, | ||
THEME_RESET_PREVIEW, | ||
} from 'disco/constants'; | ||
|
||
import 'disco/css/Addon.scss'; | ||
|
||
|
||
export default class Addon extends React.Component { | ||
static propTypes = { | ||
id: PropTypes.number.isRequired, | ||
type: PropTypes.string.isRequired, | ||
accentcolor: PropTypes.string, | ||
editorialDescription: PropTypes.string.isRequired, | ||
footerURL: PropTypes.string, | ||
headerURL: PropTypes.string, | ||
heading: PropTypes.string.isRequired, | ||
id: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired, | ||
subHeading: PropTypes.string, | ||
editorialDescription: PropTypes.string.isRequired, | ||
textcolor: PropTypes.string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also looks like it should be camelCased. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
type: PropTypes.oneOf(validAddonTypes).isRequired, | ||
themeAction: PropTypes.func, | ||
} | ||
|
||
static defaultProps = { | ||
// Defaults themeAction to the imported func. | ||
themeAction, | ||
} | ||
|
||
getBrowserThemeData() { | ||
const { id, name, headerURL, footerURL, textcolor, accentcolor } = this.props; | ||
return JSON.stringify({id, name, headerURL, footerURL, textcolor, accentcolor}); | ||
} | ||
|
||
getLogo() { | ||
const { id } = this.props; | ||
const imageURL = `https://addons-dev-cdn.allizom.org/user-media/addon_icons/0/${id}-64.png?modified=1388632826`; | ||
if (this.props.type === 'Extension') { | ||
if (this.props.type === EXTENSION_TYPE) { | ||
return <div className="logo"><img src={imageURL} alt="" /></div>; | ||
} | ||
return null; | ||
} | ||
|
||
getThemeImage() { | ||
const { id } = this.props; | ||
const { id, name } = this.props; | ||
const themeURL = `https://addons-dev-cdn.allizom.org/user-media/addons/${id}/preview_large.jpg?1239806327`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expectation is we should be handed URLs so yes it would make sense to move to that rather than building the URLS here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've filed #348 to deal with that separately. |
||
if (this.props.type === 'Theme') { | ||
return <img className="theme-image" src={themeURL} alt="" />; | ||
if (this.props.type === THEME_TYPE) { | ||
return (<a href="#" className="theme-image" | ||
data-browsertheme={this.getBrowserThemeData()} | ||
onBlur={this.resetPreviewTheme} | ||
onClick={this.handleClick} | ||
onFocus={this.previewTheme} | ||
onMouseOut={this.resetPreviewTheme} | ||
onMouseOver={this.previewTheme}> | ||
<img src={themeURL} alt={_(`Preview ${name}`)} /></a>); | ||
} | ||
return null; | ||
} | ||
|
@@ -37,11 +69,29 @@ export default class Addon extends React.Component { | |
return { __html: this.props.editorialDescription }; | ||
} | ||
|
||
handleClick = (e) => { | ||
e.preventDefault(); | ||
} | ||
|
||
previewTheme = (e) => { | ||
this.props.themeAction(e.currentTarget, THEME_PREVIEW); | ||
} | ||
|
||
resetPreviewTheme = (e) => { | ||
this.props.themeAction(e.currentTarget, THEME_RESET_PREVIEW); | ||
} | ||
|
||
render() { | ||
const { heading, subHeading, type } = this.props; | ||
|
||
if (validAddonTypes.indexOf(type) === -1) { | ||
throw new Error('Invalid addon type'); | ||
} | ||
|
||
const addonClasses = classNames('addon', { | ||
theme: type === 'Theme', | ||
theme: type === THEME_TYPE, | ||
}); | ||
|
||
return ( | ||
<div className={addonClasses}> | ||
{this.getThemeImage()} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
// Addon States. | ||
export const DOWNLOADING = 'downloading'; | ||
export const INSTALLED = 'installed'; | ||
export const INSTALLING = 'installing'; | ||
export const UNINSTALLED = 'uninstalled'; | ||
export const UNINSTALLING = 'uninstalling'; | ||
export const UNKNOWN = 'unknown'; | ||
|
||
// Add-on types. | ||
export const THEME_TYPE = 'Theme'; | ||
export const EXTENSION_TYPE = 'Extension'; | ||
export const validAddonTypes = [ | ||
THEME_TYPE, | ||
EXTENSION_TYPE, | ||
]; | ||
|
||
// Theme preview actions. | ||
export const THEME_INSTALL = 'InstallBrowserTheme'; | ||
export const THEME_PREVIEW = 'PreviewBrowserTheme'; | ||
export const THEME_RESET_PREVIEW = 'ResetBrowserThemePreview'; | ||
export const validThemeActions = [ | ||
THEME_INSTALL, | ||
THEME_PREVIEW, | ||
THEME_RESET_PREVIEW, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$focus-outline-color: #0096dc; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { validThemeActions } from 'disco/constants'; | ||
|
||
export default function themeAction(node, action, _doc = document) { | ||
if (validThemeActions.indexOf(action) === -1) { | ||
throw new Error('Invalid theme action requested'); | ||
} | ||
const event = _doc.createEvent('Events'); | ||
event.initEvent(action, true, false); | ||
node.dispatchEvent(event); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import themeAction from 'disco/themePreview'; | ||
import { THEME_PREVIEW } from 'disco/constants'; | ||
|
||
|
||
describe('Theme Preview Lib', () => { | ||
it('throws for invalid action', () => { | ||
assert.throws(() => { | ||
themeAction(null, 'whatever'); | ||
}, Error, 'Invalid theme action requested'); | ||
}); | ||
|
||
it('sets-up the event for previews', () => { | ||
const fakeNode = { | ||
dispatchEvent: sinon.stub(), | ||
}; | ||
const fakeEvent = { | ||
initEvent: sinon.stub(), | ||
}; | ||
const fakeDoc = { | ||
createEvent: sinon.stub(), | ||
}; | ||
fakeDoc.createEvent.returns(fakeEvent); | ||
themeAction(fakeNode, THEME_PREVIEW, fakeDoc); | ||
assert.ok(fakeDoc.createEvent.calledWith('Events'), 'Should call createEvent'); | ||
assert.ok(fakeEvent.initEvent.calledWith(THEME_PREVIEW, true, false), 'Should call initEvent'); | ||
assert.ok(fakeNode.dispatchEvent.calledWith(fakeEvent), 'should call dispatchEvent'); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it should be camelCased.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key expected by the theme preview (not sure why) - so I kept it simple rather than making the component have a different key in this case.
I'll do a test and see if it works with camel-case instead if it does we can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep it needs to be the lower-case key.