mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Switch to object literal constructor for Palette and support props.parent, to set a palette's parent in the DOM.
This commit is contained in:
parent
689e150bb7
commit
90d088e708
1 changed files with 14 additions and 20 deletions
|
@ -24,23 +24,19 @@
|
||||||
// DOCS: Palette#values
|
// DOCS: Palette#values
|
||||||
// DOCS: Palette#remove()
|
// DOCS: Palette#remove()
|
||||||
|
|
||||||
initialize: function Palette(title, components, values) {
|
initialize: function Palette(props) {
|
||||||
// Support object literal constructor
|
// Support legacy constructor(title, components, values)
|
||||||
var props = Base.isPlainObject(title) && title,
|
if (!Base.isPlainObject(props)) {
|
||||||
name;
|
var args = arguments;
|
||||||
if (props) {
|
props = { title: args[0], components: args[1], values: args[2] };
|
||||||
title = props.title;
|
|
||||||
components = props.components;
|
|
||||||
values = props.values;
|
|
||||||
name = props.name;
|
|
||||||
}
|
}
|
||||||
|
var components = this._components = props.components,
|
||||||
|
title = props.title;
|
||||||
this._id = Palette._id = (Palette._id || 0) + 1;
|
this._id = Palette._id = (Palette._id || 0) + 1;
|
||||||
this._title = title;
|
this._name = props.name || title
|
||||||
this._name = name || title
|
|
||||||
? Base.hyphenate(title).replace(/\W/g, '_')
|
? Base.hyphenate(title).replace(/\W/g, '_')
|
||||||
: 'palette-' + this._id;
|
: 'palette-' + this._id;
|
||||||
this._values = values || {};
|
this._values = props.values || {};
|
||||||
this._components = components;
|
|
||||||
// Create one root component that handles the layout and contains all
|
// Create one root component that handles the layout and contains all
|
||||||
// the components.
|
// the components.
|
||||||
var root = this._root = new Component(this, null, 'root', components,
|
var root = this._root = new Component(this, null, 'root', components,
|
||||||
|
@ -48,17 +44,15 @@
|
||||||
// Write the created components back into the passed components object,
|
// Write the created components back into the passed components object,
|
||||||
// so they are exposed and can easily be accessed from the outside.
|
// so they are exposed and can easily be accessed from the outside.
|
||||||
Base.set(components, root._components);
|
Base.set(components, root._components);
|
||||||
var parent = DomElement.find('.palettejs-root')
|
var parent = props.parent
|
||||||
|| DomElement.find('body').appendChild(DomElement.create('div', {
|
|| DomElement.find('.palettejs-root')
|
||||||
class: 'palettejs-root'
|
|| DomElement.find('body').appendChild(
|
||||||
}));
|
DomElement.create('div', { class: 'palettejs-root' }));
|
||||||
this._element = parent.appendChild(DomElement.create('div', {
|
this._element = parent.appendChild(DomElement.create('div', {
|
||||||
class: 'palettejs-palette palettejs-' + root._className,
|
class: 'palettejs-palette palettejs-' + root._className,
|
||||||
id: 'palettejs-palette-' + this._name
|
id: 'palettejs-palette-' + this._name
|
||||||
}, [root._table]));
|
}, [root._table]));
|
||||||
this.setTitle(title);
|
this._set(props, { components: true, values: true });
|
||||||
if (props)
|
|
||||||
this._set(props, { title: true, components: true, values: true });
|
|
||||||
// Link to the current scope's palettes list.
|
// Link to the current scope's palettes list.
|
||||||
// TODO: This is the only paper dependency in Palette.js
|
// TODO: This is the only paper dependency in Palette.js
|
||||||
// Find a way to make it independent.
|
// Find a way to make it independent.
|
||||||
|
|
Loading…
Reference in a new issue