mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Allow finding of palettes by their ids.
This commit is contained in:
parent
369fcfc7cf
commit
d4c509b4d4
1 changed files with 25 additions and 7 deletions
|
@ -14,7 +14,7 @@
|
||||||
* @name Palette
|
* @name Palette
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
/* var Palette = */ Base.extend(Callback, /** @lends Palette# */{
|
var Palette = Base.extend(Callback, /** @lends Palette# */{
|
||||||
_class: 'Palette',
|
_class: 'Palette',
|
||||||
_events: [ 'onChange' ],
|
_events: [ 'onChange' ],
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@
|
||||||
props = { title: args[0], components: args[1], values: args[2] };
|
props = { title: args[0], components: args[1], values: args[2] };
|
||||||
}
|
}
|
||||||
var components = this._components = props.components,
|
var components = this._components = props.components,
|
||||||
title = props.title;
|
id = this._id = Palette._id = (Palette._id || 0) + 1,
|
||||||
this._id = Palette._id = (Palette._id || 0) + 1;
|
title = props.title,
|
||||||
this._name = props.name || title
|
name = this._name = props.name || (title
|
||||||
? Base.hyphenate(title).replace(/\W/g, '_')
|
? Base.hyphenate(title).replace(/\W/g, '_')
|
||||||
: 'palette-' + this._id;
|
: 'palette-' + this._id);
|
||||||
this._values = props.values || {};
|
this._values = props.values || {};
|
||||||
// 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.
|
||||||
|
@ -50,13 +50,15 @@
|
||||||
DomElement.create('div', { class: 'palettejs-root' }));
|
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-' + name,
|
||||||
|
'data-id': id
|
||||||
}, [root._table]));
|
}, [root._table]));
|
||||||
this._set(props, { components: true, values: true, parent: true });
|
this._set(props, { components: true, values: true, parent: 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.
|
||||||
(this._palettes = paper.palettes).push(this);
|
(this._palettes = paper.palettes).push(this);
|
||||||
|
Palette._palettes[id] = this;
|
||||||
},
|
},
|
||||||
|
|
||||||
getName: function() {
|
getName: function() {
|
||||||
|
@ -91,6 +93,22 @@
|
||||||
if (remove)
|
if (remove)
|
||||||
palettes.splice(index, 1);
|
palettes.splice(index, 1);
|
||||||
return remove;
|
return remove;
|
||||||
|
},
|
||||||
|
|
||||||
|
statics: {
|
||||||
|
_palettes: {},
|
||||||
|
|
||||||
|
get: function(idOrElement) {
|
||||||
|
if (typeof idOrElement === 'object') {
|
||||||
|
// Support child elements by walking up the parents of the
|
||||||
|
// element until the palette element is found.
|
||||||
|
while (idOrElement && !DomElement.hasClass(idOrElement,
|
||||||
|
'palettejs-palette'))
|
||||||
|
idOrElement = idOrElement.parentNode;
|
||||||
|
idOrElement = DomElement.get(idOrElement, 'data-id');
|
||||||
|
}
|
||||||
|
return Palette._palettes[idOrElement];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, Base.each(['getTitle', 'setTitle', 'isEnabled', 'setEnabled', 'reset'],
|
}, Base.each(['getTitle', 'setTitle', 'isEnabled', 'setEnabled', 'reset'],
|
||||||
function(name) {
|
function(name) {
|
||||||
|
|
Loading…
Reference in a new issue