This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
subject_composer.js
178 lines (141 loc) · 4.64 KB
/
subject_composer.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
/* global EventDispatcher */
/* exported SubjectComposer */
(function(exports) {
'use strict';
const MAX_LENGTH = 40;
const privateMembers = new WeakMap();
function onKeyDown(e) {
/* jshint validthis: true */
var priv = privateMembers.get(this);
if (e.keyCode === e.DOM_VK_BACK_SPACE) {
if (!priv.isHoldingBackspace) {
priv.isEmptyOnBackspace = !this.getValue();
priv.isHoldingBackspace = true;
}
} else {
priv.isHoldingBackspace = false;
// Input char will be ignored when:
// - Reaching the maximum subject length. Any char input is not allowed
// - Return key(new line) input. Since new line won't work in subject
if (this.getValue().length >= MAX_LENGTH ||
e.keyCode === e.DOM_VK_RETURN) {
e.preventDefault();
e.stopPropagation();
}
}
}
function onKeyUp(e) {
/* jshint validthis: true */
var priv = privateMembers.get(this);
// User removes subject field by either:
// - Selecting options menu in top right hand corner, or
// - Selecting the delete button in the keyboard. if all
// the text is removed from the subject field and the user
// selects delete on the keyboard the subject field is removed
if (e.keyCode === e.DOM_VK_BACK_SPACE && priv.isHoldingBackspace &&
priv.isEmptyOnBackspace) {
this.hide();
}
priv.isEmptyOnBackspace = false;
priv.isHoldingBackspace = false;
}
function onInput(e) {
/* jshint validthis: true */
privateMembers.get(this).updateValue(e.target.textContent);
}
function updatePlaceholder() {
/* jshint validthis: true */
var priv = privateMembers.get(this);
if (priv.value) {
priv.placeholder.remove();
} else {
priv.node.appendChild(priv.placeholder);
}
}
function updateValue(newValue) {
/* jshint validthis: true */
var priv = privateMembers.get(this);
// Bug 877141 - contenteditable will insert non-break spaces when
// multiple consecutive spaces are entered, we don't want them.
if (newValue) {
newValue = newValue.replace(/\n\s*|\u00A0/g, ' ');
}
if (priv.value !== newValue) {
priv.value = newValue;
priv.updatePlaceholder();
this.emit('change');
}
return newValue;
}
var SubjectComposer = function(node) {
EventDispatcher.mixin(this, [
'focus',
'change',
'visibility-change'
]);
if (!node) {
throw new Error('Subject node is required');
}
var priv = {
isHoldingBackspace: false,
isEmptyOnBackspace: false,
isVisible: false,
value: '',
// nodes
node: node,
input: null,
placeholder: null,
// methods
updateValue: updateValue.bind(this),
updatePlaceholder: updatePlaceholder.bind(this)
};
privateMembers.set(this, priv);
priv.input = priv.node.querySelector('.subject-composer-input');
priv.placeholder = priv.node.querySelector(
'.subject-composer-placeholder'
);
priv.input.addEventListener('keydown', onKeyDown.bind(this));
priv.input.addEventListener('keyup', onKeyUp.bind(this));
priv.input.addEventListener('input', onInput.bind(this));
priv.input.addEventListener('focus', this.emit.bind(this, 'focus'));
};
SubjectComposer.prototype.show = function ms_show() {
this.toggle(true);
};
SubjectComposer.prototype.hide = function ms_hide() {
this.toggle(false);
};
SubjectComposer.prototype.toggle = function ms_toggle(toggle) {
var priv = privateMembers.get(this);
priv.node.classList.toggle('hide', !toggle);
priv.isVisible = toggle;
this.emit('visibility-change');
};
SubjectComposer.prototype.isVisible = function ms_is_visible() {
return privateMembers.get(this).isVisible;
};
SubjectComposer.prototype.focus = function ms_focus() {
privateMembers.get(this).input.focus();
};
SubjectComposer.prototype.getMaxLength = function ms_get_maxLength() {
return MAX_LENGTH;
};
SubjectComposer.prototype.getValue = function ms_get_value() {
return privateMembers.get(this).value;
};
SubjectComposer.prototype.setValue = function ms_set_value(value) {
var priv = privateMembers.get(this);
if (typeof value !== 'string') {
throw new Error('Value should be a valid string!');
}
priv.input.textContent = priv.updateValue(value);
};
SubjectComposer.prototype.reset = function ms_reset() {
var priv = privateMembers.get(this);
priv.value = priv.input.textContent = '';
priv.node.classList.add('hide');
priv.isVisible = false;
priv.updatePlaceholder();
};
exports.SubjectComposer = SubjectComposer;
})(window);