3 android.selection = {};
5 android.selection.selectionStartRange = null;
6 android.selection.selectionEndRange = null;
8 /** The last point touched by the user. { 'x': xPoint, 'y': yPoint } */
9 android.selection.lastTouchPoint = null;
13 * Starts the touch and saves the given x and y coordinates as last touch point
15 android.selection.startTouch = function(x, y) {
16 android.selection.lastTouchPoint = {'x': x, 'y': y};
20 * Checks to see if there is a selection.
24 android.selection.hasSelection = function() {
25 return window.getSelection().toString().length > 0;
29 * Clears the current selection.
31 android.selection.clearSelection = function() {
33 // if current selection clear it.
34 var sel = window.getSelection();
35 sel.removeAllRanges();
38 window.TextSelection.jsError(e);
43 * Handles the long touch action by selecting the last touched element.
45 android.selection.longTouch = function() {
47 android.selection.clearSelection();
48 var sel = window.getSelection();
49 var range = document.caretRangeFromPoint(android.selection.lastTouchPoint.x, android.selection.lastTouchPoint.y);
51 var text = range.toString();
52 if (text.length == 1) {
53 var baseKind = jpntext.kind(text);
54 if (baseKind != jpntext.KIND['ascii']) {
57 range.setEnd(range.endContainer, range.endOffset + 1);
58 text = range.toString();
59 var kind = jpntext.kind(text);
60 } while (baseKind == kind);
61 range.setEnd(range.endContainer, range.endOffset - 1);
67 range.setStart(range.startContainer, range.startOffset - 1);
68 text = range.toString();
69 var kind = jpntext.kind(text);
70 } while (baseKind == kind);
71 range.setStart(range.startContainer, range.startOffset + 1);
77 if (text.length > 0) {
79 android.selection.saveSelectionStart();
80 android.selection.saveSelectionEnd();
81 android.selection.selectionChanged(true);
85 window.TextSelection.jsError(err);
90 * Tells the app to show the context menu.
92 android.selection.selectionChanged = function(isReallyChanged) {
94 var sel = window.getSelection();
98 var range = sel.getRangeAt(0);
100 // Add spans to the selection to get page offsets
101 var selectionStart = $("<span id=\"selectionStart\"></span>");
102 var selectionEnd = $("<span id=\"selectionEnd\"></span>");
104 var startRange = document.createRange();
105 startRange.setStart(range.startContainer, range.startOffset);
106 startRange.insertNode(selectionStart[0]);
108 var endRange = document.createRange();
109 endRange.setStart(range.endContainer, range.endOffset);
110 endRange.insertNode(selectionEnd[0]);
112 var handleBounds = "{'left': " + (selectionStart.offset().left) + ", ";
113 handleBounds += "'top': " + (selectionStart.offset().top + selectionStart.height()) + ", ";
114 handleBounds += "'right': " + (selectionEnd.offset().left) + ", ";
115 handleBounds += "'bottom': " + (selectionEnd.offset().top + selectionEnd.height()) + "}";
118 selectionStart.remove();
119 selectionEnd.remove();
122 sel.removeAllRanges();
126 var rangyRange = android.selection.getRange();
128 // Text to send to the selection
129 var text = window.getSelection().toString();
131 // Set the content width
132 window.TextSelection.setContentWidth(document.body.clientWidth);
134 // Tell the interface that the selection changed
135 window.TextSelection.selectionChanged(rangyRange, text, handleBounds, isReallyChanged);
138 window.TextSelection.jsError(e);
142 android.selection.getRange = function() {
143 var serializedRangeSelected = rangy.serializeSelection();
144 var serializerModule = rangy.modules.Serializer;
145 if (serializedRangeSelected != '') {
146 if (rangy.supported && serializerModule && serializerModule.supported) {
147 var beginingCurly = serializedRangeSelected.indexOf("{");
148 serializedRangeSelected = serializedRangeSelected.substring(0, beginingCurly);
149 return serializedRangeSelected;
155 * Returns the last touch point as a readable string.
157 android.selection.lastTouchPointString = function(){
158 if (android.selection.lastTouchPoint == null)
160 return "{" + android.selection.lastTouchPoint.x + "," + android.selection.lastTouchPoint.y + "}";
163 android.selection.saveSelectionStart = function(){
165 // Save the starting point of the selection
166 var sel = window.getSelection();
167 var range = sel.getRangeAt(0);
168 var saveRange = document.createRange();
169 saveRange.setStart(range.startContainer, range.startOffset);
170 android.selection.selectionStartRange = saveRange;
173 window.TextSelection.jsError(e);
177 android.selection.saveSelectionEnd = function(){
179 // Save the end point of the selection
180 var sel = window.getSelection();
181 var range = sel.getRangeAt(0);
182 var saveRange = document.createRange();
183 saveRange.setStart(range.endContainer, range.endOffset);
184 android.selection.selectionEndRange = saveRange;
187 window.TextSelection.jsError(e);
192 * Sets the last caret position for the start handle.
194 android.selection.setStartPos = function(x, y){
196 android.selection.selectBetweenHandles(document.caretRangeFromPoint(x, y), android.selection.selectionEndRange);
199 window.TextSelection.jsError(e);
204 * Sets the last caret position for the end handle.
206 android.selection.setEndPos = function(x, y){
208 android.selection.selectBetweenHandles(android.selection.selectionStartRange, document.caretRangeFromPoint(x, y));
211 window.TextSelection.jsError(e);
215 android.selection.restoreStartEndPos = function() {
217 android.selection.selectBetweenHandles(android.selection.selectionEndRange, android.selection.selectionStartRange);
220 window.TextSelection.jsError(e);
225 * Selects all content between the two handles
227 android.selection.selectBetweenHandles = function(startCaret, endCaret) {
229 if (startCaret && endCaret) {
230 var rightOrder = startCaret.compareBoundaryPoints(Range.START_TO_END, endCaret) <= 0;
232 android.selection.selectionStartRange = startCaret;
233 android.selection.selectionEndRange = endCaret;
236 startCaret = android.selection.selectionStartRange;
237 endCaret = android.selection.selectionEndRange;
239 var range = document.createRange();
240 range.setStart(startCaret.startContainer, startCaret.startOffset);
241 range.setEnd(endCaret.startContainer, endCaret.startOffset);
242 android.selection.clearSelection();
243 var selection = window.getSelection();
244 selection.addRange(range);
245 android.selection.selectionChanged(rightOrder);
248 android.selection.selectionStartRange = startCaret;
249 android.selection.selectionEndRange = endCaret;
253 window.TextSelection.jsError(e);