commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / jquery / plugins / 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
7 (function() {
8
9 var fieldSelection = {
10
11 getSelection: function() {
12
13 var e = this.jquery ? this[0] : this;
14
15 return (
16
17 /* mozilla / dom 3.0 */
18 ('selectionStart' in e && function() {
19 var l = e.selectionEnd - e.selectionStart;
20 return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
21 }) ||
22
23 /* exploder */
24 (document.selection && function() {
25
26 e.focus();
27
28 var r = document.selection.createRange();
29 if (r == null) {
30 return { start: 0, end: e.value.length, length: 0 }
31 }
32
33 var re = e.createTextRange();
34 var rc = re.duplicate();
35 re.moveToBookmark(r.getBookmark());
36 rc.setEndPoint('EndToStart', re);
37
38 return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
39 }) ||
40
41 /* browser not supported */
42 function() {
43 return { start: 0, end: e.value.length, length: 0 };
44 }
45
46 )();
47
48 },
49
50 replaceSelection: function() {
51
52 var e = this.jquery ? this[0] : this;
53 var text = arguments[0] || '';
54
55 return (
56
57 /* mozilla / dom 3.0 */
58 ('selectionStart' in e && function() {
59 var cursorlength = e.selectionStart + text.length;
60 e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
61 e.selectionStart = e.selectionEnd = cursorlength;
62 return this;
63 }) ||
64
65 /* exploder */
66 (document.selection && function() {
67 // get the current cursor position
68 // currently IE 8 does not support methods to get cursor start position
69 // replace below code with the equivalent once method is available
70 var startPosition = e.value.length;
71 var endPosition = startPosition + text.length;
72
73 // set the value
74 e.value = e.value.substr(0, startPosition) + text + e.value.substr( endPosition, e.value.length);
75
76 // move the focus to correct position, end of inserted token
77 e.focus();
78 var range = e.createTextRange();
79 range.move( "character", endPosition );
80 range.select();
81 return this;
82 }) ||
83
84 /* browser not supported */
85 function() {
86 e.value += text;
87 return this;
88 }
89
90 )();
91
92 }
93 };
94
95 cj.each(fieldSelection, function(i) { cj.fn[i] = this; });
96
97 })();