comparison src/DML/VendorAssetsBundle/Resources/assets/jquery.textrange/1.3.0/jquery.textrange.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:493bcb69166c
1 /**
2 * jquery-textrange
3 * A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.
4 * See the [README](https://github.com/dwieeb/jquery-textrange/blob/1.x/README.md) for usage and examples.
5 *
6 * (c) 2012-2014 Daniel Imhoff <dwieeb@gmail.com> - danielimhoff.com
7 */
8
9 (function(factory) {
10
11 if (typeof define === 'function' && define.amd) {
12 define(['jquery'], factory);
13 } else if (typeof exports === 'object') {
14 factory(require('jquery'));
15 } else {
16 factory(jQuery);
17 }
18
19 })(function($) {
20
21 var browserType,
22
23 textrange = {
24
25 /**
26 * $().textrange() or $().textrange('get')
27 *
28 * Retrieves an object containing the start and end location of the text range, the length of the range and the
29 * substring of the range.
30 *
31 * @param (optional) property
32 * @return An object of properties including position, start, end, length, and text or a specific property.
33 */
34 get: function(property) {
35 return _textrange[browserType].get.apply(this, [property]);
36 },
37
38 /**
39 * $().textrange('set')
40 *
41 * Sets the selected text of an object by specifying the start and length of the selection.
42 *
43 * The start and length parameters are identical to PHP's substr() function with the following changes:
44 * - excluding start will select all the text in the field.
45 * - passing 0 for length will set the cursor at start. See $().textrange('setcursor')
46 *
47 * @param (optional) start
48 * @param (optional) length
49 *
50 * @see http://php.net/manual/en/function.substr.php
51 */
52 set: function(start, length) {
53 var s = parseInt(start),
54 l = parseInt(length),
55 e;
56
57 if (typeof start === 'undefined') {
58 s = 0;
59 } else if (start < 0) {
60 s = this[0].value.length + s;
61 }
62
63 if (typeof length !== 'undefined') {
64 if (length >= 0) {
65 e = s + l;
66 } else {
67 e = this[0].value.length + l;
68 }
69 }
70
71 _textrange[browserType].set.apply(this, [s, e]);
72
73 return this;
74 },
75
76 /**
77 * $().textrange('setcursor')
78 *
79 * Sets the cursor at a position of the text field.
80 *
81 * @param position
82 */
83 setcursor: function(position) {
84 return this.textrange('set', position, 0);
85 },
86
87 /**
88 * $().textrange('replace')
89 * Replaces the selected text in the input field or textarea with text.
90 *
91 * @param text The text to replace the selection with.
92 */
93 replace: function(text) {
94 _textrange[browserType].replace.apply(this, [String(text)]);
95
96 return this;
97 },
98
99 /**
100 * Alias for $().textrange('replace')
101 */
102 insert: function(text) {
103 return this.textrange('replace', text);
104 }
105 },
106
107 _textrange = {
108 xul: {
109 get: function(property) {
110 var props = {
111 position: this[0].selectionStart,
112 start: this[0].selectionStart,
113 end: this[0].selectionEnd,
114 length: this[0].selectionEnd - this[0].selectionStart,
115 text: this.val().substring(this[0].selectionStart, this[0].selectionEnd)
116 };
117
118 return typeof property === 'undefined' ? props : props[property];
119 },
120
121 set: function(start, end) {
122 if (typeof end === 'undefined') {
123 end = this[0].value.length;
124 }
125
126 this[0].selectionStart = start;
127 this[0].selectionEnd = end;
128 },
129
130 replace: function(text) {
131 var start = this[0].selectionStart;
132 var end = this[0].selectionEnd;
133 var val = this.val();
134 this.val(val.substring(0, start) + text + val.substring(end, val.length));
135 this[0].selectionStart = start;
136 this[0].selectionEnd = start + text.length;
137 }
138 },
139
140 msie: {
141 get: function(property) {
142 var range = document.selection.createRange();
143
144 if (typeof range === 'undefined') {
145 var props = {
146 position: 0,
147 start: 0,
148 end: this.val().length,
149 length: this.val().length,
150 text: this.val()
151 };
152
153 return typeof property === 'undefined' ? props : props[property];
154 }
155
156 var start = 0;
157 var end = 0;
158 var length = this[0].value.length;
159 var lfValue = this[0].value.replace(/\r\n/g, '\n');
160 var rangeText = this[0].createTextRange();
161 var rangeTextEnd = this[0].createTextRange();
162 rangeText.moveToBookmark(range.getBookmark());
163 rangeTextEnd.collapse(false);
164
165 if (rangeText.compareEndPoints('StartToEnd', rangeTextEnd) === -1) {
166 start = -rangeText.moveStart('character', -length);
167 start += lfValue.slice(0, start).split('\n').length - 1;
168
169 if (rangeText.compareEndPoints('EndToEnd', rangeTextEnd) === -1) {
170 end = -rangeText.moveEnd('character', -length);
171 end += lfValue.slice(0, end).split('\n').length - 1;
172 } else {
173 end = length;
174 }
175 } else {
176 start = length;
177 end = length;
178 }
179
180 var props = {
181 position: start,
182 start: start,
183 end: end,
184 length: length,
185 text: range.text
186 };
187
188 return typeof property === 'undefined' ? props : props[property];
189 },
190
191 set: function(start, end) {
192 var range = this[0].createTextRange();
193
194 if (typeof range === 'undefined') {
195 return;
196 }
197
198 if (typeof end === 'undefined') {
199 end = this[0].value.length;
200 }
201
202 var ieStart = start - (this[0].value.slice(0, start).split("\r\n").length - 1);
203 var ieEnd = end - (this[0].value.slice(0, end).split("\r\n").length - 1);
204
205 range.collapse(true);
206
207 range.moveEnd('character', ieEnd);
208 range.moveStart('character', ieStart);
209
210 range.select();
211 },
212
213 replace: function(text) {
214 document.selection.createRange().text = text;
215 }
216 }
217 };
218
219 $.fn.textrange = function(method) {
220 if (typeof this[0] === 'undefined') {
221 return this;
222 }
223
224 if (typeof browserType === 'undefined') {
225 browserType = 'selectionStart' in this[0] ? 'xul' : document.selection ? 'msie' : 'unknown';
226 }
227
228 // I don't know how to support this browser. :c
229 if (browserType === 'unknown') {
230 return this;
231 }
232
233 // Focus on the element before operating upon it.
234 if (document.activeElement !== this[0]) {
235 this[0].focus();
236 }
237
238 if (typeof method === 'undefined' || typeof method !== 'string') {
239 return textrange.get.apply(this);
240 } else if (typeof textrange[method] === 'function') {
241 return textrange[method].apply(this, Array.prototype.slice.call(arguments, 1));
242 } else {
243 $.error("Method " + method + " does not exist in jQuery.textrange");
244 }
245 };
246 });