1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var EventPropagators = require('./EventPropagators');
|
14 | var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
|
15 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
16 | var ReactInputSelection = require('./ReactInputSelection');
|
17 | var SyntheticEvent = require('./SyntheticEvent');
|
18 |
|
19 | var getActiveElement = require('fbjs/lib/getActiveElement');
|
20 | var isTextInputElement = require('./isTextInputElement');
|
21 | var shallowEqual = require('fbjs/lib/shallowEqual');
|
22 |
|
23 | var skipSelectionChangeEvent = ExecutionEnvironment.canUseDOM && 'documentMode' in document && document.documentMode <= 11;
|
24 |
|
25 | var eventTypes = {
|
26 | select: {
|
27 | phasedRegistrationNames: {
|
28 | bubbled: 'onSelect',
|
29 | captured: 'onSelectCapture'
|
30 | },
|
31 | dependencies: ['topBlur', 'topContextMenu', 'topFocus', 'topKeyDown', 'topKeyUp', 'topMouseDown', 'topMouseUp', 'topSelectionChange']
|
32 | }
|
33 | };
|
34 |
|
35 | var activeElement = null;
|
36 | var activeElementInst = null;
|
37 | var lastSelection = null;
|
38 | var mouseDown = false;
|
39 |
|
40 |
|
41 |
|
42 | var hasListener = false;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function getSelection(node) {
|
54 | if ('selectionStart' in node && ReactInputSelection.hasSelectionCapabilities(node)) {
|
55 | return {
|
56 | start: node.selectionStart,
|
57 | end: node.selectionEnd
|
58 | };
|
59 | } else if (window.getSelection) {
|
60 | var selection = window.getSelection();
|
61 | return {
|
62 | anchorNode: selection.anchorNode,
|
63 | anchorOffset: selection.anchorOffset,
|
64 | focusNode: selection.focusNode,
|
65 | focusOffset: selection.focusOffset
|
66 | };
|
67 | } else if (document.selection) {
|
68 | var range = document.selection.createRange();
|
69 | return {
|
70 | parentElement: range.parentElement(),
|
71 | text: range.text,
|
72 | top: range.boundingTop,
|
73 | left: range.boundingLeft
|
74 | };
|
75 | }
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function constructSelectEvent(nativeEvent, nativeEventTarget) {
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | if (mouseDown || activeElement == null || activeElement !== getActiveElement()) {
|
90 | return null;
|
91 | }
|
92 |
|
93 |
|
94 | var currentSelection = getSelection(activeElement);
|
95 | if (!lastSelection || !shallowEqual(lastSelection, currentSelection)) {
|
96 | lastSelection = currentSelection;
|
97 |
|
98 | var syntheticEvent = SyntheticEvent.getPooled(eventTypes.select, activeElementInst, nativeEvent, nativeEventTarget);
|
99 |
|
100 | syntheticEvent.type = 'select';
|
101 | syntheticEvent.target = activeElement;
|
102 |
|
103 | EventPropagators.accumulateTwoPhaseDispatches(syntheticEvent);
|
104 |
|
105 | return syntheticEvent;
|
106 | }
|
107 |
|
108 | return null;
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | var SelectEventPlugin = {
|
126 | eventTypes: eventTypes,
|
127 |
|
128 | extractEvents: function (topLevelType, targetInst, nativeEvent, nativeEventTarget) {
|
129 | if (!hasListener) {
|
130 | return null;
|
131 | }
|
132 |
|
133 | var targetNode = targetInst ? ReactDOMComponentTree.getNodeFromInstance(targetInst) : window;
|
134 |
|
135 | switch (topLevelType) {
|
136 |
|
137 | case 'topFocus':
|
138 | if (isTextInputElement(targetNode) || targetNode.contentEditable === 'true') {
|
139 | activeElement = targetNode;
|
140 | activeElementInst = targetInst;
|
141 | lastSelection = null;
|
142 | }
|
143 | break;
|
144 | case 'topBlur':
|
145 | activeElement = null;
|
146 | activeElementInst = null;
|
147 | lastSelection = null;
|
148 | break;
|
149 |
|
150 |
|
151 | case 'topMouseDown':
|
152 | mouseDown = true;
|
153 | break;
|
154 | case 'topContextMenu':
|
155 | case 'topMouseUp':
|
156 | mouseDown = false;
|
157 | return constructSelectEvent(nativeEvent, nativeEventTarget);
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | case 'topSelectionChange':
|
168 | if (skipSelectionChangeEvent) {
|
169 | break;
|
170 | }
|
171 |
|
172 | case 'topKeyDown':
|
173 | case 'topKeyUp':
|
174 | return constructSelectEvent(nativeEvent, nativeEventTarget);
|
175 | }
|
176 |
|
177 | return null;
|
178 | },
|
179 |
|
180 | didPutListener: function (inst, registrationName, listener) {
|
181 | if (registrationName === 'onSelect') {
|
182 | hasListener = true;
|
183 | }
|
184 | }
|
185 | };
|
186 |
|
187 | module.exports = SelectEventPlugin; |
\ | No newline at end of file |