forked from erichocean/blossom
/
text_field.js
213 lines (175 loc) · 6.15 KB
/
text_field.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// ==========================================================================
// Project: Blossom - Modern, Cross-Platform Application Framework
// Copyright: ©2012 Fohr Motion Picture Studios. All rights reserved.
// License: Licensed under the GPLv3 license (see BLOSSOM-LICENSE).
// ==========================================================================
/*globals sc_assert */
sc_require('layers/text');
var base03 = "#002b36";
var base02 = "#073642";
var base01 = "#586e75";
var base00 = "#657b83";
var base0 = "#839496";
var base1 = "#93a1a1";
var base2 = "#eee8d5";
var base3 = "#fdf6e3";
var yellow = "#b58900";
var orange = "#cb4b16";
var red = "#dc322f";
var magenta = "#d33682";
var violet = "#6c71c4";
var blue = "#268bd2";
var cyan = "#2aa198";
var green = "#859900";
var white = "white";
var black = "black";
// NOTE: Keep this in sync with SC.Responder's implementation.
SC.TextFieldWidget = SC.TextLayer.extend(SC.DelegateSupport, {
isWidget: true, // Walk like a duck.
behavior: function(key, val) {
sc_assert(val === undefined, "This property is read-only.");
return this;
}.property().cacheable(),
// ..........................................................
// IS ENABLED SUPPORT
//
/**
Set to true when the item is enabled. Note that changing this value
will also alter the isVisibleInWindow property for this view and any
child views.
Note that if you apply the SC.Control mixin, changing this property will
also automatically add or remove a 'disabled' CSS class name as well.
This property is observable and bindable.
@property {Boolean}
*/
isEnabled: true,
isEnabledBindingDefault: SC.Binding.oneWay().bool(),
/** @private
Observes the isEnabled property and resigns first responder if set to false.
This will avoid cases where, for example, a disabled text field retains
its focus rings.
@observes isEnabled
*/
_sc_isEnabledDidChange: function() {
if (!this.get('isEnabled') && this.get('isFirstResponder')) {
this.resignFirstResponder();
}
}.observes('isEnabled'),
// ..........................................................
// PRETEND WE'RE AN SC.RESPONDER SUBCLASS
//
isResponder: true,
/** @property
Set to true if your responder is willing to accept first responder status.
This is used when calculcating the key responder loop.
*/
acceptsFirstResponder: true,
/** @property
The surface this responder belongs to. This is used to determine where
you belong to in the responder chain. Normally you should leave this
property set to null.
@type SC.Surface
*/
surface: null,
/** @property
True when the responder is currently the first responder. This property
is always updated by a surface when its `firstResponder` property is set.
@type {Boolean}
*/
isFirstResponder: false,
/** @property
True when the responder is currently the input responder. This property
is always updated by a surface when its `firstResponder` property is set.
@type {Boolean}
*/
isInputResponder: false,
/** @property
True when the responder is currently the menu responder. This property
is always updated by a surface when its `firstResponder` property is set.
@type {Boolean}
*/
isMenuResponder: false,
/** @property
This is the nextResponder in the responder chain. If the receiver does
not implement a particular event handler, it will bubble up to the next
responder.
*/
nextResponder: function(key, val) {
sc_assert(val === undefined, "This property is read-only.");
var superlayer = this.get('superlayer');
while (superlayer && !superlayer.isResponder) {
superlayer = superlayer.get('superlayer');
}
return superlayer || null;
}.property('superlayer'),
/**
Call this method on your responder to make it become the first responder
in its surface. If the surface is also the app's keyboard surface, the
responder will have its `isInputResponder` property set to true.
*/
becomeFirstResponder: function() {
var surface = this.get('surface');
if (surface && this.get('acceptsFirstResponder')) {
if (surface.get('firstResponder') !== this) {
surface.set('firstResponder', this);
}
}
},
/**
Call this method on your responder to resign your first responder status.
Normally this is not necessary since you will lose first responder status
automatically when another responder becomes first responder.
*/
resignFirstResponder: function() {
var surface = this.get('surface');
if (surface && surface.get('firstResponder') === this) {
surface.set('firstResponder', null);
}
},
lineHeight: 22,
_sc_didBecomeInputResponder: function() {
// console.log('SC.TextFieldWidget#_sc_didBecomeInputResponder');
if (this.get('isInputResponder')) {
SC.BeginEditingTextLayer(this);
}
}.observes('isInputResponder'),
mouseDown: function(evt) {
SC.app.set('inputSurface', this.get('surface'));
if (!this.get('isFirstResponder')) this.becomeFirstResponder();
else if (this.get('isInputResponder')) {
SC.BeginEditingTextLayer(this);
}
return true;
},
color: function() {
return this.get('isEnabled')? black : 'rgba(0,43,54,0.5)';
}.property('isEnabled'),
backgroundColor: function() {
return this.get('isEnabled')? white : base3;
}.property('isEnabled'),
borderColor: function() {
return this.get('isEnabled')? black : 'rgba(0,43,54,0.5)';
}.property('isEnabled'),
borderWidth: 1,
render: function(ctx) {
var h = ctx.height,
w = ctx.width,
isEnabled = this.get('isEnabled');
ctx.fillStyle = this.get('backgroundColor');
ctx.fillRect(0, 0, w, h);
ctx.save();
ctx.translate(4, 2);
arguments.callee.base.apply(this, arguments);
ctx.restore();
// Draw the box.
ctx.strokeStyle = this.get('borderColor');
ctx.beginPath();
ctx.moveTo(0.5, 0.5);
ctx.lineTo(0.5, h-0.5);
ctx.lineTo(w-0.5, h-0.5);
ctx.lineTo(w-0.5, 0.5);
ctx.closePath();
ctx.lineWidth = this.get('borderWidth');
ctx.stroke();
}
});