-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
text_support.js
181 lines (142 loc) · 4.41 KB
/
text_support.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
require("ember-handlebars/ext");
require("ember-views/views/view");
/**
@module ember
@submodule ember-handlebars
*/
var get = Ember.get, set = Ember.set;
/**
Shared mixin used by `Ember.TextField` and `Ember.TextArea`.
@class TextSupport
@namespace Ember
@private
*/
Ember.TextSupport = Ember.Mixin.create({
value: "",
attributeBindings: ['placeholder', 'disabled', 'maxlength', 'tabindex'],
placeholder: null,
disabled: false,
maxlength: null,
init: function() {
this._super();
this.on("focusOut", this, this._elementValueDidChange);
this.on("change", this, this._elementValueDidChange);
this.on("paste", this, this._elementValueDidChange);
this.on("cut", this, this._elementValueDidChange);
this.on("input", this, this._elementValueDidChange);
this.on("keyUp", this, this.interpretKeyEvents);
},
/**
The action to be sent when the user presses the return key.
This is similar to the `{{action}}` helper, but is fired when
the user presses the return key when editing a text field, and sends
the value of the field as the context.
@property action
@type String
@default null
*/
action: null,
/**
The event that should send the action.
Options are:
* `enter`: the user pressed enter
* `keyPress`: the user pressed a key
@property onEvent
@type String
@default enter
*/
onEvent: 'enter',
/**
Whether they `keyUp` event that triggers an `action` to be sent continues
propagating to other views.
By default, when the user presses the return key on their keyboard and
the text field has an `action` set, the action will be sent to the view's
controller and the key event will stop propagating.
If you would like parent views to receive the `keyUp` event even after an
action has been dispatched, set `bubbles` to true.
@property bubbles
@type Boolean
@default false
*/
bubbles: false,
interpretKeyEvents: function(event) {
var map = Ember.TextSupport.KEY_EVENTS;
var method = map[event.keyCode];
this._elementValueDidChange();
if (method) { return this[method](event); }
},
_elementValueDidChange: function() {
set(this, 'value', this.$().val());
},
/**
The action to be sent when the user inserts a new line.
Called by the `Ember.TextSupport` mixin on keyUp if keycode matches 13.
Uses sendAction to send the `enter` action to the controller.
@method insertNewline
@param {Event} event
*/
insertNewline: function(event) {
sendAction('enter', this, event);
sendAction('insert-newline', this, event);
},
/**
Called when the user hits escape.
Called by the `Ember.TextSupport` mixin on keyUp if keycode matches 27.
Uses sendAction to send the `escape-press` action to the controller.
@method cancel
@param {Event} event
*/
cancel: function(event) {
sendAction('escape-press', this, event);
},
/**
Called when the text area is focused.
@method focusIn
@param {Event} event
*/
focusIn: function(event) {
sendAction('focus-in', this, event);
},
/**
Called when the text area is blurred.
@method focusOut
@param {Event} event
*/
focusOut: function(event) {
sendAction('focus-out', this, event);
},
/**
The action to be sent when the user presses a key. Enabled by setting
the `onEvent` property to `keyPress`.
Uses sendAction to send the `keyPress` action to the controller.
@method keyPress
@param {Event} event
*/
keyPress: function(event) {
sendAction('key-press', this, event);
}
});
Ember.TextSupport.KEY_EVENTS = {
13: 'insertNewline',
27: 'cancel'
};
// In principle, this shouldn't be necessary, but the legacy
// sectionAction semantics for TextField are different from
// the component semantics so this method normalizes them.
function sendAction(eventName, view, event) {
var action = get(view, eventName),
on = get(view, 'onEvent'),
value = get(view, 'value');
// back-compat support for keyPress as an event name even though
// it's also a method name that consumes the event (and therefore
// incompatible with sendAction semantics).
if (on === eventName || (on === 'keyPress' && eventName === 'key-press')) {
view.sendAction('action', value);
}
view.sendAction(eventName, value);
if (action || on === eventName) {
if(!get(view, 'bubbles')) {
event.stopPropagation();
}
}
}