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