summaryrefslogtreecommitdiff
path: root/codemirror_ui/js/codemirror-ui-find.js
blob: c1cc8c5ac766f597deb5b3565c188f5078bc044b (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
/**
 * @author jgreen
 */
var cursor = null;

function setupFindReplace(){
    document.getElementById('closeButton').onclick = closeWindow;
    document.getElementById('findButton').onclick = find;
    document.getElementById('replaceButton').onclick = replace;
	document.getElementById('replaceAllButton').onclick = replaceAll;
    document.getElementById('replaceFindButton').onclick = replaceFind;
}

function closeWindow(){
    codeMirrorUI.searchWindow = null;
    window.close();
}

function find(){
    var findString = document.getElementById('find').value;
    if (findString == null || findString == '') {
        alert('You must enter something to search for.');
        return;
    }
	
	if(document.getElementById('regex').checked){
		findString = new RegExp(findString);
	}
	
	cursor = codeMirrorUI.mirror.getSearchCursor(findString, true);
    var found = moveCursor(cursor);
	
	//if we didn't find anything, let's check to see if we should start from the top
	if(!found && document.getElementById('wrap').checked){
		cursor = codeMirrorUI.mirror.getSearchCursor(findString, false);
		found = moveCursor(cursor);
	}
	
	if(found){
		cursor.select();
	}else{
		alert("No instances found. (Maybe you need to enable 'Wrap Search'?)");
	}
	
}

function moveCursor(cursor){
	var found = false;
	if( getFindDirection() == "forward" ){
		found = cursor.findNext();
    }else{
		found = cursor.findPrevious();
	}
	return found;
}


function getFindDirection(){
    var dRadio = document.forms[0].elements['direction'];
    
    for (var i = 0; i < dRadio.length; i++) {
        if (dRadio[i].checked) {
            return dRadio[i].value;
        }
    }
    
    return 'no-value?';
    
}


function replaceAll(){
	var cursor = codeMirrorUI.mirror.getSearchCursor(document.getElementById('find').value, false);
    while (cursor.findNext())
      cursor.replace(document.getElementById('replace').value);
}


function replace(){
    cursor.replace(document.getElementById('replace').value);
	//codeMirrorUI.replaceSelection(document.getElementById('replace').value);
    setTimeout(window.focus, 100);
    //alert('replaced!');
}

function replaceFind(){
    replace();
    find();
}