~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to timeline/exporting-server/java/highcharts-export/highcharts-export-web/src/main/webapp/resources/lib/codemirror/util/formatting.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
// ============== Formatting extensions ============================
 
2
(function() {
 
3
  // Define extensions for a few modes
 
4
  CodeMirror.extendMode("css", {
 
5
    commentStart: "/*",
 
6
    commentEnd: "*/",
 
7
    wordWrapChars: [";", "\\{", "\\}"],
 
8
    autoFormatLineBreaks: function (text) {
 
9
      return text.replace(new RegExp("(;|\\{|\\})([^\r\n])", "g"), "$1\n$2");
 
10
    }
 
11
  });
 
12
 
 
13
  function jsNonBreakableBlocks(text) {
 
14
    var nonBreakableRegexes = [/for\s*?\((.*?)\)/,
 
15
                               /\"(.*?)(\"|$)/,
 
16
                               /\'(.*?)(\'|$)/,
 
17
                               /\/\*(.*?)(\*\/|$)/,
 
18
                               /\/\/.*/];
 
19
    var nonBreakableBlocks = [];
 
20
    for (var i = 0; i < nonBreakableRegexes.length; i++) {
 
21
      var curPos = 0;
 
22
      while (curPos < text.length) {
 
23
        var m = text.substr(curPos).match(nonBreakableRegexes[i]);
 
24
        if (m != null) {
 
25
          nonBreakableBlocks.push({
 
26
            start: curPos + m.index,
 
27
            end: curPos + m.index + m[0].length
 
28
          });
 
29
          curPos += m.index + Math.max(1, m[0].length);
 
30
        }
 
31
        else { // No more matches
 
32
          break;
 
33
        }
 
34
      }
 
35
    }
 
36
    nonBreakableBlocks.sort(function (a, b) {
 
37
      return a.start - b.start;
 
38
    });
 
39
 
 
40
    return nonBreakableBlocks;
 
41
  }
 
42
 
 
43
  CodeMirror.extendMode("javascript", {
 
44
    commentStart: "/*",
 
45
    commentEnd: "*/",
 
46
    wordWrapChars: [";", "\\{", "\\}"],
 
47
 
 
48
    autoFormatLineBreaks: function (text) {
 
49
      var curPos = 0;
 
50
      var reLinesSplitter = /(;|\{|\})([^\r\n;])/g;
 
51
      var nonBreakableBlocks = jsNonBreakableBlocks(text);
 
52
      if (nonBreakableBlocks != null) {
 
53
        var res = "";
 
54
        for (var i = 0; i < nonBreakableBlocks.length; i++) {
 
55
          if (nonBreakableBlocks[i].start > curPos) { // Break lines till the block
 
56
            res += text.substring(curPos, nonBreakableBlocks[i].start).replace(reLinesSplitter, "$1\n$2");
 
57
            curPos = nonBreakableBlocks[i].start;
 
58
          }
 
59
          if (nonBreakableBlocks[i].start <= curPos
 
60
              && nonBreakableBlocks[i].end >= curPos) { // Skip non-breakable block
 
61
            res += text.substring(curPos, nonBreakableBlocks[i].end);
 
62
            curPos = nonBreakableBlocks[i].end;
 
63
          }
 
64
        }
 
65
        if (curPos < text.length)
 
66
          res += text.substr(curPos).replace(reLinesSplitter, "$1\n$2");
 
67
        return res;
 
68
      } else {
 
69
        return text.replace(reLinesSplitter, "$1\n$2");
 
70
      }
 
71
    }
 
72
  });
 
73
 
 
74
  CodeMirror.extendMode("xml", {
 
75
    commentStart: "<!--",
 
76
    commentEnd: "-->",
 
77
    wordWrapChars: [">"],
 
78
 
 
79
    autoFormatLineBreaks: function (text) {
 
80
      var lines = text.split("\n");
 
81
      var reProcessedPortion = new RegExp("(^\\s*?<|^[^<]*?)(.+)(>\\s*?$|[^>]*?$)");
 
82
      var reOpenBrackets = new RegExp("<", "g");
 
83
      var reCloseBrackets = new RegExp("(>)([^\r\n])", "g");
 
84
      for (var i = 0; i < lines.length; i++) {
 
85
        var mToProcess = lines[i].match(reProcessedPortion);
 
86
        if (mToProcess != null && mToProcess.length > 3) { // The line starts with whitespaces and ends with whitespaces
 
87
          lines[i] = mToProcess[1]
 
88
            + mToProcess[2].replace(reOpenBrackets, "\n$&").replace(reCloseBrackets, "$1\n$2")
 
89
            + mToProcess[3];
 
90
          continue;
 
91
        }
 
92
      }
 
93
      return lines.join("\n");
 
94
    }
 
95
  });
 
96
 
 
97
  function localModeAt(cm, pos) {
 
98
    return CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(pos).state).mode;
 
99
  }
 
100
 
 
101
  function enumerateModesBetween(cm, line, start, end) {
 
102
    var outer = cm.getMode(), text = cm.getLine(line);
 
103
    if (end == null) end = text.length;
 
104
    if (!outer.innerMode) return [{from: start, to: end, mode: outer}];
 
105
    var state = cm.getTokenAt({line: line, ch: start}).state;
 
106
    var mode = CodeMirror.innerMode(outer, state).mode;
 
107
    var found = [], stream = new CodeMirror.StringStream(text);
 
108
    stream.pos = stream.start = start;
 
109
    for (;;) {
 
110
      outer.token(stream, state);
 
111
      var curMode = CodeMirror.innerMode(outer, state).mode;
 
112
      if (curMode != mode) {
 
113
        var cut = stream.start;
 
114
        // Crappy heuristic to deal with the fact that a change in
 
115
        // mode can occur both at the end and the start of a token,
 
116
        // and we don't know which it was.
 
117
        if (mode.name == "xml" && text.charAt(stream.pos - 1) == ">") cut = stream.pos;
 
118
        found.push({from: start, to: cut, mode: mode});
 
119
        start = cut;
 
120
        mode = curMode;
 
121
      }
 
122
      if (stream.pos >= end) break;
 
123
      stream.start = stream.pos;
 
124
    }
 
125
    if (start < end) found.push({from: start, to: end, mode: mode});
 
126
    return found;
 
127
  }
 
128
 
 
129
  // Comment/uncomment the specified range
 
130
  CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
 
131
    var curMode = localModeAt(this, from), cm = this;
 
132
    this.operation(function() {
 
133
      if (isComment) { // Comment range
 
134
        cm.replaceRange(curMode.commentEnd, to);
 
135
        cm.replaceRange(curMode.commentStart, from);
 
136
        if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside
 
137
          cm.setCursor(from.line, from.ch + curMode.commentStart.length);
 
138
      } else { // Uncomment range
 
139
        var selText = cm.getRange(from, to);
 
140
        var startIndex = selText.indexOf(curMode.commentStart);
 
141
        var endIndex = selText.lastIndexOf(curMode.commentEnd);
 
142
        if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
 
143
          // Take string till comment start
 
144
          selText = selText.substr(0, startIndex)
 
145
          // From comment start till comment end
 
146
            + selText.substring(startIndex + curMode.commentStart.length, endIndex)
 
147
          // From comment end till string end
 
148
            + selText.substr(endIndex + curMode.commentEnd.length);
 
149
        }
 
150
        cm.replaceRange(selText, from, to);
 
151
      }
 
152
    });
 
153
  });
 
154
 
 
155
  // Applies automatic mode-aware indentation to the specified range
 
156
  CodeMirror.defineExtension("autoIndentRange", function (from, to) {
 
157
    var cmInstance = this;
 
158
    this.operation(function () {
 
159
      for (var i = from.line; i <= to.line; i++) {
 
160
        cmInstance.indentLine(i, "smart");
 
161
      }
 
162
    });
 
163
  });
 
164
 
 
165
  // Applies automatic formatting to the specified range
 
166
  CodeMirror.defineExtension("autoFormatRange", function (from, to) {
 
167
    var cm = this;
 
168
    cm.operation(function () {
 
169
      for (var cur = from.line, end = to.line; cur <= end; ++cur) {
 
170
        var f = {line: cur, ch: cur == from.line ? from.ch : 0};
 
171
        var t = {line: cur, ch: cur == end ? to.ch : null};
 
172
        var modes = enumerateModesBetween(cm, cur, f.ch, t.ch), mangled = "";
 
173
        var text = cm.getRange(f, t);
 
174
        for (var i = 0; i < modes.length; ++i) {
 
175
          var part = modes.length > 1 ? text.slice(modes[i].from, modes[i].to) : text;
 
176
          if (mangled) mangled += "\n";
 
177
          if (modes[i].mode.autoFormatLineBreaks) {
 
178
            mangled += modes[i].mode.autoFormatLineBreaks(part);
 
179
          } else mangled += text;
 
180
        }
 
181
        if (mangled != text) {
 
182
          for (var count = 0, pos = mangled.indexOf("\n"); pos != -1; pos = mangled.indexOf("\n", pos + 1), ++count) {}
 
183
          cm.replaceRange(mangled, f, t);
 
184
          cur += count;
 
185
          end += count;
 
186
        }
 
187
      }
 
188
      for (var cur = from.line + 1; cur <= end; ++cur)
 
189
        cm.indentLine(cur, "smart");
 
190
      cm.setSelection(from, cm.getCursor(false));
 
191
    });
 
192
  });
 
193
})();