~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to timeline/Highstock-1.3.10/exporting-server/java/highcharts-export/highcharts-export-web/src/main/webapp/resources/lib/codemirror/util/foldcode.js

  • Committer: Jeff Stys
  • Date: 2014-04-21 12:46:26 UTC
  • Revision ID: jstys@sesda3.com-20140421124626-2332pb2dyjc33jxi
Proof-of-concept version of Data Coverage Timeline using Highchart/Highstock javascript library.  Changes to getDataCoverage API in order to feed the necessary data to the Timeline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// the tagRangeFinder function is
 
2
//   Copyright (C) 2011 by Daniel Glazman <daniel@glazman.org>
 
3
// released under the MIT license (../../LICENSE) like the rest of CodeMirror
 
4
CodeMirror.tagRangeFinder = function(cm, line, hideEnd) {
 
5
  var nameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
 
6
  var nameChar = nameStartChar + "\-\:\.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
 
7
  var xmlNAMERegExp = new RegExp("^[" + nameStartChar + "][" + nameChar + "]*");
 
8
 
 
9
  var lineText = cm.getLine(line);
 
10
  var found = false;
 
11
  var tag = null;
 
12
  var pos = 0;
 
13
  while (!found) {
 
14
    pos = lineText.indexOf("<", pos);
 
15
    if (-1 == pos) // no tag on line
 
16
      return;
 
17
    if (pos + 1 < lineText.length && lineText[pos + 1] == "/") { // closing tag
 
18
      pos++;
 
19
      continue;
 
20
    }
 
21
    // ok we weem to have a start tag
 
22
    if (!lineText.substr(pos + 1).match(xmlNAMERegExp)) { // not a tag name...
 
23
      pos++;
 
24
      continue;
 
25
    }
 
26
    var gtPos = lineText.indexOf(">", pos + 1);
 
27
    if (-1 == gtPos) { // end of start tag not in line
 
28
      var l = line + 1;
 
29
      var foundGt = false;
 
30
      var lastLine = cm.lineCount();
 
31
      while (l < lastLine && !foundGt) {
 
32
        var lt = cm.getLine(l);
 
33
        var gt = lt.indexOf(">");
 
34
        if (-1 != gt) { // found a >
 
35
          foundGt = true;
 
36
          var slash = lt.lastIndexOf("/", gt);
 
37
          if (-1 != slash && slash < gt) {
 
38
            var str = lineText.substr(slash, gt - slash + 1);
 
39
            if (!str.match( /\/\s*\>/ )) { // yep, that's the end of empty tag
 
40
              if (hideEnd === true) l++;
 
41
              return l;
 
42
            }
 
43
          }
 
44
        }
 
45
        l++;
 
46
      }
 
47
      found = true;
 
48
    }
 
49
    else {
 
50
      var slashPos = lineText.lastIndexOf("/", gtPos);
 
51
      if (-1 == slashPos) { // cannot be empty tag
 
52
        found = true;
 
53
        // don't continue
 
54
      }
 
55
      else { // empty tag?
 
56
        // check if really empty tag
 
57
        var str = lineText.substr(slashPos, gtPos - slashPos + 1);
 
58
        if (!str.match( /\/\s*\>/ )) { // finally not empty
 
59
          found = true;
 
60
          // don't continue
 
61
        }
 
62
      }
 
63
    }
 
64
    if (found) {
 
65
      var subLine = lineText.substr(pos + 1);
 
66
      tag = subLine.match(xmlNAMERegExp);
 
67
      if (tag) {
 
68
        // we have an element name, wooohooo !
 
69
        tag = tag[0];
 
70
        // do we have the close tag on same line ???
 
71
        if (-1 != lineText.indexOf("</" + tag + ">", pos)) // yep
 
72
        {
 
73
          found = false;
 
74
        }
 
75
        // we don't, so we have a candidate...
 
76
      }
 
77
      else
 
78
        found = false;
 
79
    }
 
80
    if (!found)
 
81
      pos++;
 
82
  }
 
83
 
 
84
  if (found) {
 
85
    var startTag = "(\\<\\/" + tag + "\\>)|(\\<" + tag + "\\>)|(\\<" + tag + "\\s)|(\\<" + tag + "$)";
 
86
    var startTagRegExp = new RegExp(startTag, "g");
 
87
    var endTag = "</" + tag + ">";
 
88
    var depth = 1;
 
89
    var l = line + 1;
 
90
    var lastLine = cm.lineCount();
 
91
    while (l < lastLine) {
 
92
      lineText = cm.getLine(l);
 
93
      var match = lineText.match(startTagRegExp);
 
94
      if (match) {
 
95
        for (var i = 0; i < match.length; i++) {
 
96
          if (match[i] == endTag)
 
97
            depth--;
 
98
          else
 
99
            depth++;
 
100
          if (!depth) {
 
101
            if (hideEnd === true) l++;
 
102
            return l;
 
103
          }
 
104
        }
 
105
      }
 
106
      l++;
 
107
    }
 
108
    return;
 
109
  }
 
110
};
 
111
 
 
112
CodeMirror.braceRangeFinder = function(cm, line, hideEnd) {
 
113
  var lineText = cm.getLine(line), at = lineText.length, startChar, tokenType;
 
114
  for (;;) {
 
115
    var found = lineText.lastIndexOf("{", at);
 
116
    if (found < 0) break;
 
117
    tokenType = cm.getTokenAt({line: line, ch: found}).className;
 
118
    if (!/^(comment|string)/.test(tokenType)) { startChar = found; break; }
 
119
    at = found - 1;
 
120
  }
 
121
  if (startChar == null || lineText.lastIndexOf("}") > startChar) return;
 
122
  var count = 1, lastLine = cm.lineCount(), end;
 
123
  outer: for (var i = line + 1; i < lastLine; ++i) {
 
124
    var text = cm.getLine(i), pos = 0;
 
125
    for (;;) {
 
126
      var nextOpen = text.indexOf("{", pos), nextClose = text.indexOf("}", pos);
 
127
      if (nextOpen < 0) nextOpen = text.length;
 
128
      if (nextClose < 0) nextClose = text.length;
 
129
      pos = Math.min(nextOpen, nextClose);
 
130
      if (pos == text.length) break;
 
131
      if (cm.getTokenAt({line: i, ch: pos + 1}).className == tokenType) {
 
132
        if (pos == nextOpen) ++count;
 
133
        else if (!--count) { end = i; break outer; }
 
134
      }
 
135
      ++pos;
 
136
    }
 
137
  }
 
138
  if (end == null || end == line + 1) return;
 
139
  if (hideEnd === true) end++;
 
140
  return end;
 
141
};
 
142
 
 
143
CodeMirror.indentRangeFinder = function(cm, line) {
 
144
  var tabSize = cm.getOption("tabSize");
 
145
  var myIndent = cm.getLineHandle(line).indentation(tabSize), last;
 
146
  for (var i = line + 1, end = cm.lineCount(); i < end; ++i) {
 
147
    var handle = cm.getLineHandle(i);
 
148
    if (!/^\s*$/.test(handle.text)) {
 
149
      if (handle.indentation(tabSize) <= myIndent) break;
 
150
      last = i;
 
151
    }
 
152
  }
 
153
  if (!last) return null;
 
154
  return last + 1;
 
155
};
 
156
 
 
157
CodeMirror.newFoldFunction = function(rangeFinder, markText, hideEnd) {
 
158
  var folded = [];
 
159
  if (markText == null) markText = '<div style="position: absolute; left: 2px; color:#600">&#x25bc;</div>%N%';
 
160
 
 
161
  function isFolded(cm, n) {
 
162
    for (var i = 0; i < folded.length; ++i) {
 
163
      var start = cm.lineInfo(folded[i].start);
 
164
      if (!start) folded.splice(i--, 1);
 
165
      else if (start.line == n) return {pos: i, region: folded[i]};
 
166
    }
 
167
  }
 
168
 
 
169
  function expand(cm, region) {
 
170
    cm.clearMarker(region.start);
 
171
    for (var i = 0; i < region.hidden.length; ++i)
 
172
      cm.showLine(region.hidden[i]);
 
173
  }
 
174
 
 
175
  return function(cm, line) {
 
176
    cm.operation(function() {
 
177
      var known = isFolded(cm, line);
 
178
      if (known) {
 
179
        folded.splice(known.pos, 1);
 
180
        expand(cm, known.region);
 
181
      } else {
 
182
        var end = rangeFinder(cm, line, hideEnd);
 
183
        if (end == null) return;
 
184
        var hidden = [];
 
185
        for (var i = line + 1; i < end; ++i) {
 
186
          var handle = cm.hideLine(i);
 
187
          if (handle) hidden.push(handle);
 
188
        }
 
189
        var first = cm.setMarker(line, markText);
 
190
        var region = {start: first, hidden: hidden};
 
191
        cm.onDeleteLine(first, function() { expand(cm, region); });
 
192
        folded.push(region);
 
193
      }
 
194
    });
 
195
  };
 
196
};