5a920362 |
1 | /* |
2 | * |
3 | * @licstart The following is the entire license notice for the |
4 | * JavaScript code in this page. |
5 | * |
6 | * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16 |
7 | * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd |
8 | * |
9 | * fieldSelection - a jQuery plugin |
10 | * |
11 | * Copyright (c) 2006, Alex Brem |
12 | * All rights reserved. |
13 | * |
14 | * Redistribution and use in source and binary forms, with or without |
15 | * modification, are permitted provided that the following conditions |
16 | * are met: |
17 | * |
18 | * Redistributions of source code must retain the above copyright |
19 | * notice, this list of conditions and the following disclaimer. |
20 | * |
21 | * Redistributions in binary form must reproduce the above copyright |
22 | * notice, this list of conditions and the following disclaimer in the |
23 | * documentation and/or other materials provided with the |
24 | * distribution. |
25 | * |
26 | * Neither the name of Alex Brem nor the names of its contributors may |
27 | * be used to endorse or promote products derived from this software |
28 | * without specific prior written permission. |
29 | * |
30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED |
31 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
32 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
33 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
36 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
37 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | * |
39 | * @licend The above is the entire license notice |
40 | * for the JavaScript code in this page. |
41 | * |
42 | */ |
43 | |
44 | (function() { |
45 | |
46 | var fieldSelection = { |
47 | |
48 | getSelection: function() { |
49 | |
50 | var e = this.jquery ? this[0] : this; |
51 | |
52 | return ( |
53 | |
54 | /* mozilla / dom 3.0 */ |
55 | ('selectionStart' in e && function() { |
56 | var l = e.selectionEnd - e.selectionStart; |
57 | return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) }; |
58 | }) || |
59 | |
60 | /* exploder */ |
61 | (document.selection && function() { |
62 | |
63 | e.focus(); |
64 | |
65 | var r = document.selection.createRange(); |
66 | if (r == null) { |
67 | return { start: 0, end: e.value.length, length: 0 } |
68 | } |
69 | |
70 | var re = e.createTextRange(); |
71 | var rc = re.duplicate(); |
72 | re.moveToBookmark(r.getBookmark()); |
73 | rc.setEndPoint('EndToStart', re); |
74 | |
75 | return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text }; |
76 | }) || |
77 | |
78 | /* browser not supported */ |
79 | function() { |
80 | return { start: 0, end: e.value.length, length: 0 }; |
81 | } |
82 | |
83 | )(); |
84 | |
85 | }, |
86 | |
87 | replaceSelection: function() { |
88 | |
89 | var e = this.jquery ? this[0] : this; |
90 | var text = arguments[0] || ''; |
91 | |
92 | return ( |
93 | |
94 | /* mozilla / dom 3.0 */ |
95 | ('selectionStart' in e && function() { |
96 | var cursorlength = e.selectionStart + text.length; |
97 | e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length); |
98 | e.selectionStart = e.selectionEnd = cursorlength; |
99 | return this; |
100 | }) || |
101 | |
102 | /* exploder */ |
103 | (document.selection && function() { |
104 | // get the current cursor position |
105 | // currently IE 8 does not support methods to get cursor start position |
106 | // replace below code with the equivalent once method is available |
107 | var startPosition = e.value.length; |
108 | var endPosition = startPosition + text.length; |
109 | |
110 | // set the value |
111 | e.value = e.value.substr(0, startPosition) + text + e.value.substr( endPosition, e.value.length); |
112 | |
113 | // move the focus to correct position, end of inserted token |
114 | e.focus(); |
115 | var range = e.createTextRange(); |
116 | range.move( "character", endPosition ); |
117 | range.select(); |
118 | return this; |
119 | }) || |
120 | |
121 | /* browser not supported */ |
122 | function() { |
123 | e.value += text; |
124 | return this; |
125 | } |
126 | |
127 | )(); |
128 | |
129 | } |
130 | }; |
131 | |
132 | jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; }); |
133 | |
134 | })(); |