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