-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathractive-events-typing.js
124 lines (99 loc) · 3.76 KB
/
ractive-events-typing.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
;
(function (global, factory) {
'use strict';
// Common JS (i.e. browserify) environment
if (typeof module !== 'undefined' && module.exports && typeof require === 'function') {
factory(require('ractive'));
}
// AMD?
else if (typeof define === 'function' && define.amd) {
define(['ractive'], factory);
}
// browser global
else if (global.Ractive) {
factory(global.Ractive);
} else {
throw new Error('Could not find Ractive! It must be loaded before the ractive-events-typing plugin');
}
}(typeof window !== 'undefined' ? window : this, function (Ractive) {
'use strict';
var typing = function (node, fire) {
var eligibleForTyping = ({
"inputtext": 0,
"input": 0,
"textareatextarea": 0
})[[(node.tagName || "").toLowerCase(), (node.type || "").toLowerCase()].join('')] === 0 || typeof(node.contentEditable) != 'undefined';
var delay;
// Options
try {
document.createEvent("TouchEvent");
delay = 1000;
} catch (e) {
delay = 500;
}
if (eligibleForTyping) {
var typing = false,
timer = null,
stopped;
var startedTyping = function (event) {
var that = this;
clearTimeout(timer);
(stopped === undefined || stopped === true) && fire({
node: that,
original: event,
typingState: 'typing',
sourceKey: event.type === "paste" ? "paste" : "typed"
});
stopped = false;
timer = setTimeout(function () {
// stopped is used to stop continuous fire of state `typing`
stopped = true;
fire({
node: that,
original: event,
typingState: 'paused'
});
}, delay);
};
var typedKeys = function (event) {
if ([8, 46].indexOf(event.keyCode) > -1) {
startedTyping.call(this, event);
}
};
var stoppedTyping = function (event) {
timer && clearTimeout(timer);
fire({
node: this,
original: event,
typingState: 'stopped'
});
stopped = undefined;
};
var beforeTyping = function (event) {
fire({
node: this,
original: event,
typingState: 'beforetyping' // may be, need a good name for this.
});
};
node.addEventListener('focus', beforeTyping);
node.addEventListener('keypress', startedTyping);
node.addEventListener('keydown', typedKeys);
node.addEventListener('paste', startedTyping);
node.addEventListener('blur', stoppedTyping);
// Todo : stoppedTyping when window lose focus, for now getting error from ractive.js (?)
// window.addEventListener('blur', stoppedTyping);
}
return {
teardown: function () {
node.removeEventListener('focus', beforeTyping);
node.removeEventListener('keypress', startedTyping);
node.removeEventListener('keydown', typedKeys);
node.removeEventListener('paste', startedTyping);
node.removeEventListener('blur', stoppedTyping);
// window.removeEventListener('blur', stoppedTyping);
}
};
};
Ractive.events.typing = typing;
}));