~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/search.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
// Define search commands. Depends on dialog.js or another
 
2
// implementation of the openDialog method.
 
3
 
 
4
// Replace works a little oddly -- it will do the replace on the next
 
5
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
 
6
// replace by making sure the match is no longer selected when hitting
 
7
// Ctrl-G.
 
8
 
 
9
(function() {
 
10
  function SearchState() {
 
11
    this.posFrom = this.posTo = this.query = null;
 
12
    this.marked = [];
 
13
  }
 
14
  function getSearchState(cm) {
 
15
    return cm._searchState || (cm._searchState = new SearchState());
 
16
  }
 
17
  function getSearchCursor(cm, query, pos) {
 
18
    // Heuristic: if the query string is all lowercase, do a case insensitive search.
 
19
    return cm.getSearchCursor(query, pos, typeof query == "string" && query == query.toLowerCase());
 
20
  }
 
21
  function dialog(cm, text, shortText, f) {
 
22
    if (cm.openDialog) cm.openDialog(text, f);
 
23
    else f(prompt(shortText, ""));
 
24
  }
 
25
  function confirmDialog(cm, text, shortText, fs) {
 
26
    if (cm.openConfirm) cm.openConfirm(text, fs);
 
27
    else if (confirm(shortText)) fs[0]();
 
28
  }
 
29
  function parseQuery(query) {
 
30
    var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
 
31
    return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i") : query;
 
32
  }
 
33
  var queryDialog =
 
34
    'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
 
35
  function doSearch(cm, rev) {
 
36
    var state = getSearchState(cm);
 
37
    if (state.query) return findNext(cm, rev);
 
38
    dialog(cm, queryDialog, "Search for:", function(query) {
 
39
      cm.operation(function() {
 
40
        if (!query || state.query) return;
 
41
        state.query = parseQuery(query);
 
42
        if (cm.lineCount() < 2000) { // This is too expensive on big documents.
 
43
          for (var cursor = getSearchCursor(cm, state.query); cursor.findNext();)
 
44
            state.marked.push(cm.markText(cursor.from(), cursor.to(), "CodeMirror-searching"));
 
45
        }
 
46
        state.posFrom = state.posTo = cm.getCursor();
 
47
        findNext(cm, rev);
 
48
      });
 
49
    });
 
50
  }
 
51
  function findNext(cm, rev) {cm.operation(function() {
 
52
    var state = getSearchState(cm);
 
53
    var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
 
54
    if (!cursor.find(rev)) {
 
55
      cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
 
56
      if (!cursor.find(rev)) return;
 
57
    }
 
58
    cm.setSelection(cursor.from(), cursor.to());
 
59
    state.posFrom = cursor.from(); state.posTo = cursor.to();
 
60
  });}
 
61
  function clearSearch(cm) {cm.operation(function() {
 
62
    var state = getSearchState(cm);
 
63
    if (!state.query) return;
 
64
    state.query = null;
 
65
    for (var i = 0; i < state.marked.length; ++i) state.marked[i].clear();
 
66
    state.marked.length = 0;
 
67
  });}
 
68
 
 
69
  var replaceQueryDialog =
 
70
    'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
 
71
  var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
 
72
  var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
 
73
  function replace(cm, all) {
 
74
    dialog(cm, replaceQueryDialog, "Replace:", function(query) {
 
75
      if (!query) return;
 
76
      query = parseQuery(query);
 
77
      dialog(cm, replacementQueryDialog, "Replace with:", function(text) {
 
78
        if (all) {
 
79
          cm.compoundChange(function() { cm.operation(function() {
 
80
            for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
 
81
              if (typeof query != "string") {
 
82
                var match = cm.getRange(cursor.from(), cursor.to()).match(query);
 
83
                cursor.replace(text.replace(/\$(\d)/, function(w, i) {return match[i];}));
 
84
              } else cursor.replace(text);
 
85
            }
 
86
          });});
 
87
        } else {
 
88
          clearSearch(cm);
 
89
          var cursor = getSearchCursor(cm, query, cm.getCursor());
 
90
          function advance() {
 
91
            var start = cursor.from(), match;
 
92
            if (!(match = cursor.findNext())) {
 
93
              cursor = getSearchCursor(cm, query);
 
94
              if (!(match = cursor.findNext()) ||
 
95
                  (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
 
96
            }
 
97
            cm.setSelection(cursor.from(), cursor.to());
 
98
            confirmDialog(cm, doReplaceConfirm, "Replace?",
 
99
                          [function() {doReplace(match);}, advance]);
 
100
          }
 
101
          function doReplace(match) {
 
102
            cursor.replace(typeof query == "string" ? text :
 
103
                           text.replace(/\$(\d)/, function(w, i) {return match[i];}));
 
104
            advance();
 
105
          }
 
106
          advance();
 
107
        }
 
108
      });
 
109
    });
 
110
  }
 
111
 
 
112
  CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
 
113
  CodeMirror.commands.findNext = doSearch;
 
114
  CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
 
115
  CodeMirror.commands.clearSearch = clearSearch;
 
116
  CodeMirror.commands.replace = replace;
 
117
  CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
 
118
})();