      1 /*
      2  * JBoss, Home of Professional Open Source
      3  * Copyright 2013, Red Hat, Inc. and individual contributors
      4  * by the @authors tag. See the copyright.txt in the distribution for a
      5  * full listing of individual contributors.
      6  *
      7  * This is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU Lesser General Public License as
      9  * published by the Free Software Foundation; either version 2.1 of
     10  * the License, or (at your option) any later version.
     11  *
     12  * This software is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this software; if not, write to the Free
     19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
     20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
     21  */
     22
     23 window.RichFaces = window.RichFaces || {};
     24 RichFaces.jQuery = RichFaces.jQuery || window.jQuery;
     25
     26 (function (rf) {
     27
     28     rf.Selection = rf.Selection || {};
     29
     30     rf.Selection.set = function (field, start, end) {
     31         if (field.setSelectionRange) {
     32             field.focus();
     33             field.setSelectionRange(start, end);
     34         } else if (field.createTextRange) {
     35             var range = field.createTextRange();
     36             range.collapse(true);
     37             range.moveEnd('character', end);
     38             range.moveStart('character', start);
     39             range.select();
     40         }
     41     }
     42
     43     rf.Selection.getStart = function(field) {
     44         if (field.setSelectionRange) {
     45             return field.selectionStart;
     46         } else if (document.selection && document.selection.createRange) {
     47             var r = document.selection.createRange().duplicate();
     48             r.moveEnd('character', field.value.length);
     49             if (r.text == '') return field.value.length;
     50             return field.value.lastIndexOf(r.text);
     51         }
     52     }
     53
     54     rf.Selection.getEnd = function(field) {
     55         if (field.setSelectionRange) {
     56             return field.selectionEnd;
     57         } else if (document.selection && document.selection.createRange) {
     58             var r = document.selection.createRange().duplicate();
     59             r.moveStart('character', -field.value.length);
     60             return r.text.length;
     61         }
     62     }
     63
     64     rf.Selection.setCaretTo = function (field, pos) {
     65         if (!pos) pos = field.value.length;
     66         rf.Selection.set(field, pos, pos);
     67     }
     68 })(RichFaces);