eads@16
|
1 /* |
eads@16
|
2 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16 |
eads@16
|
3 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd |
eads@16
|
4 */ |
eads@16
|
5 |
eads@16
|
6 (function() { |
eads@16
|
7 |
eads@16
|
8 var fieldSelection = { |
eads@16
|
9 |
eads@16
|
10 getSelection: function() { |
eads@16
|
11 |
eads@16
|
12 var e = this.jquery ? this[0] : this; |
eads@16
|
13 |
eads@16
|
14 return ( |
eads@16
|
15 |
eads@16
|
16 /* mozilla / dom 3.0 */ |
eads@16
|
17 ('selectionStart' in e && function() { |
eads@16
|
18 var l = e.selectionEnd - e.selectionStart; |
eads@16
|
19 return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) }; |
eads@16
|
20 }) || |
eads@16
|
21 |
eads@16
|
22 /* exploder */ |
eads@16
|
23 (document.selection && function() { |
eads@16
|
24 |
eads@16
|
25 e.focus(); |
eads@16
|
26 |
eads@16
|
27 var r = document.selection.createRange(); |
eads@16
|
28 if (r == null) { |
eads@16
|
29 return { start: 0, end: e.value.length, length: 0 } |
eads@16
|
30 } |
eads@16
|
31 |
eads@16
|
32 var re = e.createTextRange(); |
eads@16
|
33 var rc = re.duplicate(); |
eads@16
|
34 re.moveToBookmark(r.getBookmark()); |
eads@16
|
35 rc.setEndPoint('EndToStart', re); |
eads@16
|
36 |
eads@16
|
37 return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text }; |
eads@16
|
38 }) || |
eads@16
|
39 |
eads@16
|
40 /* browser not supported */ |
eads@16
|
41 function() { |
eads@16
|
42 return { start: 0, end: e.value.length, length: 0 }; |
eads@16
|
43 } |
eads@16
|
44 |
eads@16
|
45 )(); |
eads@16
|
46 |
eads@16
|
47 }, |
eads@16
|
48 |
eads@16
|
49 replaceSelection: function() { |
eads@16
|
50 |
eads@16
|
51 var e = this.jquery ? this[0] : this; |
eads@16
|
52 var text = arguments[0] || ''; |
eads@16
|
53 |
eads@16
|
54 return ( |
eads@16
|
55 |
eads@16
|
56 /* mozilla / dom 3.0 */ |
eads@16
|
57 ('selectionStart' in e && function() { |
eads@16
|
58 e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length); |
eads@16
|
59 return this; |
eads@16
|
60 }) || |
eads@16
|
61 |
eads@16
|
62 /* exploder */ |
eads@16
|
63 (document.selection && function() { |
eads@16
|
64 e.focus(); |
eads@16
|
65 document.selection.createRange().text = text; |
eads@16
|
66 return this; |
eads@16
|
67 }) || |
eads@16
|
68 |
eads@16
|
69 /* browser not supported */ |
eads@16
|
70 function() { |
eads@16
|
71 e.value += text; |
eads@16
|
72 return this; |
eads@16
|
73 } |
eads@16
|
74 |
eads@16
|
75 )(); |
eads@16
|
76 |
eads@16
|
77 } |
eads@16
|
78 |
eads@16
|
79 }; |
eads@16
|
80 |
eads@16
|
81 jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; }); |
eads@16
|
82 |
eads@16
|
83 })(); |