summaryrefslogtreecommitdiffstats
path: root/auth/auth-gui/theme/onap/console.js
blob: fe4f64948c8486f22e082bebd23daeba7a9a90d6 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */
function getCommand() {
	if(typeof String.prototype.trim !== 'function') {
		String.prototype.trim = function() {
			return this.replace(/^\s+|\s+$/g, ''); 
		};
	}

	var cmds = [];
	cmds = document.querySelector("#command_field").value.split(" ");
	var cleanCmd = "";
	if (document.querySelector("#details_img").getAttribute("class") == "selected") 
		cleanCmd += "details ";
	for (var i = 0; i < cmds.length;i++) {
		var trimmed = cmds[i].trim();
		if (trimmed != "")
			cleanCmd += trimmed + " ";
	}
	
	return cleanCmd.trim();
}

function moveCommandToDiv() {

	var textInput = document.querySelector("#command_field");
	var content = document.createTextNode(textInput.value);
	var parContent = document.createElement("p");
	var consoleDiv = document.querySelector("#console_area");
	var commandCount = consoleDiv.querySelectorAll(".command").length;
	parContent.setAttribute("class", "command");
	parContent.appendChild(content);
	consoleDiv.appendChild(parContent);

	textInput.value = "";
}

function printResponse(response) {
	var parContent = document.createElement("p");
	parContent.setAttribute("class", "response");
	var preTag = document.createElement("pre");
	parContent.appendChild(preTag);
	var content = document.createTextNode(response);
	preTag.appendChild(content);
	var consoleDiv = document.querySelector("#console_area");
	consoleDiv.appendChild(parContent);
	
	consoleDiv.scrollTop = consoleDiv.scrollHeight;
}

function clearHistory() {
	var consoleDiv = document.querySelector("#console_area");
	var curr;
	while (curr=consoleDiv.firstChild) {
		consoleDiv.removeChild(curr);
	}
	document.querySelector("#command_field").value = "";
	currentCmd = 0;
}

function buttonChangeFontSize(direction) {
	var slider = document.querySelector("#text_size_slider");
	var currentSize = parseInt(slider.value);
	var newSize;
	if (direction == "inc") {
		newSize = currentSize + 10;
	} else {
		newSize = currentSize - 10;
	}
	if (newSize > slider.max) newSize = parseInt(slider.max);
	if (newSize < slider.min) newSize = parseInt(slider.min);
	slider.value = newSize;
	changeFontSize(newSize);
}

function changeFontSize(size) {
	var consoleDiv = document.querySelector("#console_area");
	consoleDiv.style.fontSize = size + "%";
}

function handleDivHiding(id, img) {
	var options_link = document.querySelector("#options_link");
	var divHeight = toggleVisibility(document.querySelector("#"+id));

	if (id == 'options') {
		if (options_link.getAttribute("class") == "open") {
			changeImg(document.querySelector("#options_img"), "../../theme/onap/options_down.png");
			options_link.setAttribute("class", "closed");
		} else {
			changeImg(document.querySelector("#options_img"), "../../theme/onap/options_up.png");
			options_link.setAttribute("class", "open");
		}
		moveToggleImg(options_link, divHeight);
	} else { //id=text_slider
		selectOption(img,divHeight);
	}

}

function selectOption(img, divHeight) {
	var options_link = document.querySelector("#options_link");
	var anySelected;
	if (img.getAttribute("class") != "selected") {
		anySelected = document.querySelectorAll(".selected").length>0;
		if (anySelected == false)
			divHeight += 4;
		img.setAttribute("class", "selected");
	} else {
		img.setAttribute("class", "");
		anySelected = document.querySelectorAll(".selected").length>0;
		if (anySelected == false)
			divHeight -= 4;

	}

	moveToggleImg(options_link, divHeight);
}

function toggleVisibility(element) {
	var divHeight;
    if(element.style.display == 'block') {
    	divHeight = 0 - element.clientHeight;
    	element.style.display = 'none';
    } else { 
    	element.style.display = 'block';
    	divHeight = element.clientHeight;
    }
    return divHeight;
}

function moveToggleImg(element, height) {
	var curTop = (element.style.top == "" ? 0 : parseInt(element.style.top));
	element.style.top = curTop + height;   
}

function changeImg(img, loc) {
	img.src = loc;
}

var currentCmd = 0;
function keyPressed() {
	document.querySelector("#command_field").onkeyup=function(e) {
		if (!e) e = window.event;
		var keyCode = e.which || e.keyCode;
		if (keyCode == 38 || keyCode == 40 || keyCode == 13 || keyCode == 27) {
			var cmdHistoryList = document.querySelectorAll(".command");
			switch (keyCode) {
			case 13:
				// press enter 

				if (getCommand().toLowerCase()=="clear") {
					clearHistory();
				} else {
					currentCmd = cmdHistoryList.length + 1;
					document.querySelector("#submit").click();
				}
				break;
				
			case 27:
				//press escape
				currentCmd = cmdHistoryList.length;
				document.querySelector("#command_field").value = "";
				break;
	
			case 38:
				// press arrow up	
				if (currentCmd != 0)
					currentCmd -= 1;
				if (cmdHistoryList.length != 0) 
					document.querySelector("#command_field").value = cmdHistoryList[currentCmd].innerHTML;
				break;
			case 40:
				// press arrow down
				var cmdText = "";
				currentCmd = (currentCmd == cmdHistoryList.length) ? currentCmd : currentCmd + 1;
				if (currentCmd < cmdHistoryList.length) 
					cmdText = cmdHistoryList[currentCmd].innerHTML;
				
				document.querySelector("#command_field").value = cmdText;
				break;
			}
		}
	}
}

function saveToFile() {
	var commands = document.querySelectorAll(".command");
	var responses = document.querySelectorAll(".response");
	var textToWrite = "";
	for (var i = 0; i < commands.length; i++) {
		textToWrite += "> " + commands[i].innerHTML + "\r\n";
		textToWrite += prettyResponse(responses[i].firstChild.innerHTML);
	}
	
    var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/);
    var ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/);
    var ieVer=(ie ? ie[1] : (ie11 ? 11 : -1));
    
//    if (ie && ieVer<10) {
//        console.log("No blobs on IE ver<10");
//        return;
//    }

	var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
	var fileName = "AAFcommands.log";
	
	if (ieVer >= 10) {
//		window.navigator.msSaveBlob(textFileAsBlob, fileName);
		window.navigator.msSaveOrOpenBlob(textFileAsBlob, fileName); 
	} else {
		var downloadLink = document.createElement("a");
		downloadLink.download = fileName;
		downloadLink.innerHTML = "Download File";
		if (window.webkitURL != null) {
			// Chrome allows the link to be clicked
			// without actually adding it to the DOM.
			downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
		} else {
			// Firefox requires the link to be added to the DOM
			// before it can be clicked.
			downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
			downloadLink.onclick = destroyClickedElement;
			downloadLink.style.display = "none";
			document.body.appendChild(downloadLink);
		}
	
		downloadLink.click();
	}
}

function prettyResponse(response) {
	var lines = response.split('\n');
	var cleanResponse = "";
	for (var i=0; i < lines.length; i++) {
		cleanResponse += lines[i] + "\r\n";
	}
	cleanResponse = cleanResponse.replace(/(&lt;)/g,"<").replace(/(&gt;)/g,">");
	return cleanResponse;
}

function destroyClickedElement(event){
	document.body.removeChild(event.target);
}

function fakePlaceholder() {
	document.querySelector("#command_field").setAttribute("value", "Type your AAFCLI commands here");
}

function maximizeConsole(img) {
	var footer = document.querySelector("#footer");
	var console_area = document.querySelector("#console_area");
	var content = document.querySelector("#content");
	var input_area = document.querySelector("#input_area");
	var help_msg = document.querySelector("#help_msg");
	var console_space = document.documentElement.clientHeight;
	console_space -= input_area.outerHeight;
	console_space -= help_msg.outerHeight;
    var height = getStyle(console_area,'paddingTop') + getStyle(console_area,'paddingBottom');
	console_space -= height;
	
	
	if (content.getAttribute("class") != "maximized") {
		content.setAttribute("class", "maximized");
		footer.style.display="none";
		console_area.style.resize="none";
		console_area.style.height=console_space.toString()+"px";
	} else {
		content.removeAttribute("class");
		footer.style.display="";
		console_area.style.resize="vertical";
		console_area.style.height="600px";
	}
	selectOption(img,0);
}