forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.js
More file actions
172 lines (146 loc) · 3.51 KB
/
Copy pathComponent.js
File metadata and controls
172 lines (146 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name Component
* @class
*/
var Component = this.Component = Base.extend(Callback, /** @lends Component# */{
_events: [ 'onChange', 'onClick' ],
_types: {
'boolean': {
type: 'checkbox',
value: 'checked'
},
string: {
type: 'text'
},
number: {
type: 'number',
number: true
},
button: {
type: 'button'
},
text: {
tag: 'div',
value: 'text'
},
slider: {
type: 'range',
number: true
},
list: {
tag: 'select',
options: function() {
DomElement.removeChildren(this._inputItem);
DomElement.create(Base.each(this._options, function(option) {
this.push('option', { value: option, text: option });
}, []), this._inputItem);
}
}
},
initialize: function(obj) {
this._type = obj.type in this._types
? obj.type
: 'options' in obj
? 'list'
: 'onClick' in obj
? 'button'
: typeof obj.value;
this._info = this._types[this._type] || { type: this._type };
var that = this,
fireChange = false;
this._inputItem = DomElement.create(this._info.tag || 'input', {
type: this._info.type,
events: {
change: function() {
that.setValue(
DomElement.get(this, that._info.value || 'value'));
if (fireChange) {
that._palette.fire('change', that, that.name, that._value);
that.fire('change', that._value);
}
},
click: function() {
that.fire('click');
}
}
});
this._element = DomElement.create('tr', [
this._labelItem = DomElement.create('td'),
'td', [this._inputItem]
]);
Base.each(obj, function(value, key) {
this[key] = value;
}, this);
this._defaultValue = this._value;
// Only fire change events after we have initalized
fireChange = true;
},
getType: function() {
return this._type;
},
getLabel: function() {
return this._label;
},
setLabel: function(label) {
this._label = label;
DomElement.set(this._labelItem, 'text', label + ':');
},
getOptions: function() {
return this._options;
},
setOptions: function(options) {
this._options = options;
if (this._info.options)
this._info.options.call(this);
},
getValue: function() {
return this._value;
},
setValue: function(value) {
var key = this._info.value || 'value';
DomElement.set(this._inputItem, key, value);
// Read back and convert from input again, to make sure we're in sync
value = DomElement.get(this._inputItem, key);
this._value = this._info.number ? parseFloat(value, 10) : value;
},
getRange: function() {
return [parseFloat(DomElement.get(this._inputItem, 'min')),
parseFloat(DomElement.get(this._inputItem, 'max'))];
},
setRange: function(min, max) {
var range = Array.isArray(min) ? min : [min, max];
DomElement.set(this._inputItem, { min: range[0], max: range[1] });
},
getMin: function() {
return this.getRange()[0];
},
setMin: function(min) {
this.setRange(min, this.getMax());
},
getMax: function() {
return this.getRange()[1];
},
setMax: function(max) {
this.setRange(this.getMin(), max);
},
getStep: function() {
return parseFloat(DomElement.get(this._inputItem, 'step'));
},
setStep: function(step) {
DomElement.set(this._inputItem, 'step', step);
},
reset: function() {
this.setValue(this._defaultValue);
}
});