1function centerSimileAjax(date) {
2    tl.getBand(0).setCenterVisibleDate(SimileAjax.DateTime.parseGregorianDateTime(date));
3}
4
5function setupFilterHighlightControls(div, timeline, bandIndices, theme) {
6    var table = document.createElement("table");
7    var tr = table.insertRow(0);
8
9    var td = tr.insertCell(0);
10    td.innerHTML = "Filter:";
11
12    td = tr.insertCell(1);
13    td.innerHTML = "Highlight:";
14
15    var handler = function(elmt, evt, target) {
16        onKeyPress(timeline, bandIndices, table);
17    };
18
19    tr = table.insertRow(1);
20    tr.style.verticalAlign = "top";
21
22    td = tr.insertCell(0);
23
24    var input = document.createElement("input");
25    input.type = "text";
26    SimileAjax.DOM.registerEvent(input, "keypress", handler);
27    td.appendChild(input);
28
29    for (var i = 0; i < theme.event.highlightColors.length; i++) {
30        td = tr.insertCell(i + 1);
31
32        input = document.createElement("input");
33        input.type = "text";
34        input.size = "15";
35        SimileAjax.DOM.registerEvent(input, "keypress", handler);
36        td.appendChild(input);
37
38        var divColor = document.createElement("div");
39        divColor.style.height = "0.5em";
40        divColor.style.background = theme.event.highlightColors[i];
41        td.appendChild(divColor);
42    }
43
44    td = tr.insertCell(tr.cells.length);
45    var button = document.createElement("button");
46    button.innerHTML = "Clear All";
47    SimileAjax.DOM.registerEvent(button, "click", function() {
48        clearAll(timeline, bandIndices, table);
49    });
50    td.appendChild(button);
51
52    div.appendChild(table);
53}
54
55var timerID = null;
56function onKeyPress(timeline, bandIndices, table) {
57    if (timerID != null) {
58        window.clearTimeout(timerID);
59    }
60    timerID = window.setTimeout(function() {
61        performFiltering(timeline, bandIndices, table);
62    }, 300);
63}
64function cleanString(s) {
65    return s.replace(/^\s+/, '').replace(/\s+$/, '');
66}
67function performFiltering(timeline, bandIndices, table) {
68    timerID = null;
69
70    var tr = table.rows[1];
71    var text = cleanString(tr.cells[0].firstChild.value);
72
73    var filterMatcher = null;
74    if (text.length > 0) {
75        var regex = new RegExp(text, "i");
76        filterMatcher = function(evt) {
77            return regex.test(evt.getText()) || regex.test(evt.getDescription());
78        };
79    }
80
81    var regexes = [];
82    var hasHighlights = false;
83    for (var x = 1; x < tr.cells.length - 1; x++) {
84        var input = tr.cells[x].firstChild;
85        var text2 = cleanString(input.value);
86        if (text2.length > 0) {
87            hasHighlights = true;
88            regexes.push(new RegExp(text2, "i"));
89        } else {
90            regexes.push(null);
91        }
92    }
93    var highlightMatcher = hasHighlights ? function(evt) {
94        var text = evt.getText();
95        var description = evt.getDescription();
96        for (var x = 0; x < regexes.length; x++) {
97            var regex = regexes[x];
98            if (regex != null && (regex.test(text) || regex.test(description))) {
99                return x;
100            }
101        }
102        return -1;
103    } : null;
104
105    for (var i = 0; i < bandIndices.length; i++) {
106        var bandIndex = bandIndices[i];
107        timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(filterMatcher);
108        timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(highlightMatcher);
109    }
110    timeline.paint();
111}
112function clearAll(timeline, bandIndices, table) {
113    var tr = table.rows[1];
114    for (var x = 0; x < tr.cells.length - 1; x++) {
115        tr.cells[x].firstChild.value = "";
116    }
117
118    for (var i = 0; i < bandIndices.length; i++) {
119        var bandIndex = bandIndices[i];
120        timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(null);
121        timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(null);
122    }
123    timeline.paint();
124}
125