summaryrefslogtreecommitdiff
path: root/plugins/legacy_browser/js/iehacks.js
blob: 8f88e6f57912da86b4f890387cd66476c410f059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

// Make getElementById() case-sensitive on IE7
document._getElementById = document.getElementById;
document.getElementById = function(id)
{
  var i = 0, obj = document._getElementById(id);

  if (obj && obj.id != id)
    while ((obj = document.all[i]) && obj.id != id)
      i++;

  return obj;
}

// fix missing :last-child selectors
$(document).ready(function() {
  if (rcmail && rcmail.env.skin != 'classic')
    $('ul.treelist ul').each(function(i, ul) {
      $('li:last-child', ul).css('border-bottom', 0);
  });
});

// gets cursor position (IE<9)
rcube_webmail.prototype.get_caret_pos = function(obj)
{
  if (document.selection && document.selection.createRange) {
    var range = document.selection.createRange();
    if (range.parentElement() != obj)
      return 0;

    var gm = range.duplicate();
    if (obj.tagName == 'TEXTAREA')
      gm.moveToElementText(obj);
    else
      gm.expand('textedit');

    gm.setEndPoint('EndToStart', range);
    var p = gm.text.length;

    return p <= obj.value.length ? p : -1;
  }

  return obj.value.length;
};

// moves cursor to specified position (IE<9)
rcube_webmail.prototype.set_caret_pos = function(obj, pos)
{
  if (obj.createTextRange) {
    var range = obj.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
};

// get selected text from an input field (IE<9)
// http://stackoverflow.com/questions/7186586/how-to-get-the-selected-text-in-textarea-using-jquery-in-internet-explorer-7
rcube_webmail.prototype.get_input_selection = function(obj)
{
  var start = 0, end = 0, len,
    normalizedValue, textInputRange, endRange,
    range = document.selection.createRange();

  if (range && range.parentElement() == obj) {
    len = obj.value.length;
    normalizedValue = obj.value; //.replace(/\r\n/g, "\n");

    // create a working TextRange that lives only in the input
    textInputRange = obj.createTextRange();
    textInputRange.moveToBookmark(range.getBookmark());

    // Check if the start and end of the selection are at the very end
    // of the input, since moveStart/moveEnd doesn't return what we want
    // in those cases
    endRange = obj.createTextRange();
    endRange.collapse(false);

    if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
      start = end = len;
    }
    else {
      start = -textInputRange.moveStart("character", -len);
      start += normalizedValue.slice(0, start).split("\n").length - 1;

      if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
        end = len;
      }
      else {
        end = -textInputRange.moveEnd("character", -len);
        end += normalizedValue.slice(0, end).split("\n").length - 1;
      }
    }
  }

  return {start: start, end: end, text: normalizedValue.substr(start, end-start)};
};

// For IE<9 we have to do it this way
// otherwise the form will be posted to a new window
rcube_webmail.prototype.async_upload_form_frame = function(name)
{
  document.body.insertAdjacentHTML('BeforeEnd', '<iframe name="' + name + '"'
    + ' src="program/resources/blank.gif" style="width:0; height:0; visibility:hidden"></iframe>');

  return $('iframe[name="' + name + '"]');
};