summaryrefslogtreecommitdiff
path: root/production_static_files/yaksh/js/question.js
blob: 75baf7686598f9cbe9640eaa9fcd87572dc3c01f (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
function submitCode()
{
    document.forms["code"].submit();
    var x = document.getElementById("status");
    x.innerHTML = "<strong>Checking answer ...</strong>";
    x = document.getElementById("check");
    x.disabled = true;
    x.value = "Checking Answer ...";
    document.getElementById("skip").disabled = true;
}

function setSelectionRange(input, selectionStart, selectionEnd) 
{
    if (input.setSelectionRange) 
    {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) 
    {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
    }
}

function replaceSelection (input, replaceString) 
{
    if (input.setSelectionRange) 
    {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
		if (selectionStart != selectionEnd)
		{ 
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		}
		else
		{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}
    }
    else if (document.selection) 
    {
		var range = document.selection.createRange();
		if (range.parentElement() == input) 
		{
			var isCollapsed = range.text == '';
			range.text = replaceString;
			if (!isCollapsed)  
			{
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
    }
}

function autoresize()
{
    var ta = document.getElementById('answer');
    var divta = document.getElementById('AnswerWithLines');
    ta.style.height="";
    var height = ta.scrollHeight;
    ta.style.height = 'auto';
    ta.style.height = height+'px';
    divta.style.height = height+'px';
}

function catchTab(item,e)
{
    if(navigator.userAgent.match("Gecko"))
    {
		c=e.which;
    }
    else
    {
		c=e.keyCode;
    }
    if(c==9)
    {
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
		return false;
    }
}

var lineObjOffsetTop = 0;

function addLineNumbers(id)
{
    var el = document.createElement('DIV');
    var ta = document.getElementById(id);
    var content = document.getElementById('snippet').value;
    ta.parentNode.insertBefore(el,ta);
    el.appendChild(ta);
    el.style.width = (ta.offsetWidth + 30) + 'px';
    ta.style.position = 'absolute';
    ta.style.left = '30px';
    ta.scrollHeight=""
    el.style.border = 'none';
    el.style.overflow='hidden';
    el.style.position = 'relative';
    el.style.width = (ta.offsetWidth + 30) + 'px';
    var lineObj = document.createElement('DIV');
    lineObj.style.position = 'absolute';
    lineObj.style.top = lineObjOffsetTop + 'px';
    lineObj.style.width = '27px';
    lineObj.style.fontSize= '18px';
    lineObj.style.foregroundColor='black';
    el.insertBefore(lineObj,ta);
    lineObj.style.textAlign = 'right';
    lineObj.className='lineObj';
    var string = '';
    split_content = content.split('\n');
    if(id == "answer")
    {
                el.className='AnswerWithLines';
                el.id='AnswerWithLines';
                el.style.height = (ta.scrollHeight) + 'px';
		for(var no=split_content.length+1;no<1000;no++)
		{
			if(string.length>0)string = string + '<br>';
			string = string + no;
		}
	}
	else
	{
                el.className='SnippetWithLines';
                el.id='SnippetWithLines';
                el.style.height = (ta.scrollHeight) + 'px';
		for(var no=1;no<=split_content.length;no++)
		{
			if(string.length>0)string = string + '<br>';
			string = string + no;
		}
	}
	ta.onmousedown = function() { positionLineObj(lineObj,ta); };
    ta.onscroll = function() { positionLineObj(lineObj,ta); };
    ta.onblur = function() { positionLineObj(lineObj,ta); };
    ta.onfocus = function() { positionLineObj(lineObj,ta); };
    ta.onmouseover = function() { positionLineObj(lineObj,ta); };
    ta.onkeyup = function(){autoresize();};
    lineObj.innerHTML = string;
}
       
function positionLineObj(obj,ta)
{
    obj.style.top = (ta.scrollTop * -1 + lineObjOffsetTop) + 'px';  
}