Missing Licenses
[aaf/authz.git] / auth / auth-gui / theme / onap / console.js
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21 function getCommand() {
22         if(typeof String.prototype.trim !== 'function') {
23                 String.prototype.trim = function() {
24                         return this.replace(/^\s+|\s+$/g, ''); 
25                 };
26         }
27
28         var cmds = [];
29         cmds = document.querySelector("#command_field").value.split(" ");
30         var cleanCmd = "";
31         if (document.querySelector("#details_img").getAttribute("class") == "selected") 
32                 cleanCmd += "set details=true ";
33         for (var i = 0; i < cmds.length;i++) {
34                 var trimmed = cmds[i].trim();
35                 if (trimmed != "")
36                         cleanCmd += trimmed + " ";
37         }
38         
39         return cleanCmd.trim();
40 }
41
42 function moveCommandToDiv() {
43
44         var textInput = document.querySelector("#command_field");
45         var content = document.createTextNode(textInput.value);
46         var parContent = document.createElement("p");
47         var consoleDiv = document.querySelector("#console_area");
48         var commandCount = consoleDiv.querySelectorAll(".command").length;
49         parContent.setAttribute("class", "command");
50         parContent.appendChild(content);
51         consoleDiv.appendChild(parContent);
52
53         textInput.value = "";
54 }
55
56 function printResponse(response) {
57         var parContent = document.createElement("p");
58         parContent.setAttribute("class", "response");
59         var preTag = document.createElement("pre");
60         parContent.appendChild(preTag);
61         var content = document.createTextNode(response);
62         preTag.appendChild(content);
63         var consoleDiv = document.querySelector("#console_area");
64         consoleDiv.appendChild(parContent);
65         
66         consoleDiv.scrollTop = consoleDiv.scrollHeight;
67 }
68
69 function clearHistory() {
70         var consoleDiv = document.querySelector("#console_area");
71         var curr;
72         while (curr=consoleDiv.firstChild) {
73                 consoleDiv.removeChild(curr);
74         }
75         document.querySelector("#command_field").value = "";
76         currentCmd = 0;
77 }
78
79 function buttonChangeFontSize(direction) {
80         var slider = document.querySelector("#text_size_slider");
81         var currentSize = parseInt(slider.value);
82         var newSize;
83         if (direction == "inc") {
84                 newSize = currentSize + 10;
85         } else {
86                 newSize = currentSize - 10;
87         }
88         if (newSize > slider.max) newSize = parseInt(slider.max);
89         if (newSize < slider.min) newSize = parseInt(slider.min);
90         slider.value = newSize;
91         changeFontSize(newSize);
92 }
93
94 function changeFontSize(size) {
95         var consoleDiv = document.querySelector("#console_area");
96         consoleDiv.style.fontSize = size + "%";
97 }
98
99 function handleDivHiding(id, img) {
100         var options_link = document.querySelector("#options_link");
101         var divHeight = toggleVisibility(document.querySelector("#"+id));
102
103         if (id == 'options') {
104                 if (options_link.getAttribute("class") == "open") {
105                         changeImg(document.querySelector("#options_img"), "../../theme/onap/options_down.png");
106                         options_link.setAttribute("class", "closed");
107                 } else {
108                         changeImg(document.querySelector("#options_img"), "../../theme/onap/options_up.png");
109                         options_link.setAttribute("class", "open");
110                 }
111                 moveToggleImg(options_link, divHeight);
112         } else { //id=text_slider
113                 selectOption(img,divHeight);
114         }
115
116 }
117
118 function selectOption(img, divHeight) {
119         var options_link = document.querySelector("#options_link");
120         var anySelected;
121         if (img.getAttribute("class") != "selected") {
122                 anySelected = document.querySelectorAll(".selected").length>0;
123                 if (anySelected == false)
124                         divHeight += 4;
125                 img.setAttribute("class", "selected");
126         } else {
127                 img.setAttribute("class", "");
128                 anySelected = document.querySelectorAll(".selected").length>0;
129                 if (anySelected == false)
130                         divHeight -= 4;
131
132         }
133
134         moveToggleImg(options_link, divHeight);
135 }
136
137 function toggleVisibility(element) {
138         var divHeight;
139     if(element.style.display == 'block') {
140         divHeight = 0 - element.clientHeight;
141         element.style.display = 'none';
142     } else { 
143         element.style.display = 'block';
144         divHeight = element.clientHeight;
145     }
146     return divHeight;
147 }
148
149 function moveToggleImg(element, height) {
150         var curTop = (element.style.top == "" ? 0 : parseInt(element.style.top));
151         element.style.top = curTop + height;   
152 }
153
154 function changeImg(img, loc) {
155         img.src = loc;
156 }
157
158 var currentCmd = 0;
159 function keyPressed() {
160         document.querySelector("#command_field").onkeyup=function(e) {
161                 if (!e) e = window.event;
162                 var keyCode = e.which || e.keyCode;
163                 if (keyCode == 38 || keyCode == 40 || keyCode == 13 || keyCode == 27) {
164                         var cmdHistoryList = document.querySelectorAll(".command");
165                         switch (keyCode) {
166                         case 13:
167                                 // press enter 
168
169                                 if (getCommand().toLowerCase()=="clear") {
170                                         clearHistory();
171                                 } else {
172                                         currentCmd = cmdHistoryList.length + 1;
173                                         document.querySelector("#submit").click();
174                                 }
175                                 break;
176                                 
177                         case 27:
178                                 //press escape
179                                 currentCmd = cmdHistoryList.length;
180                                 document.querySelector("#command_field").value = "";
181                                 break;
182         
183                         case 38:
184                                 // press arrow up       
185                                 if (currentCmd != 0)
186                                         currentCmd -= 1;
187                                 if (cmdHistoryList.length != 0) 
188                                         document.querySelector("#command_field").value = cmdHistoryList[currentCmd].innerHTML;
189                                 break;
190                         case 40:
191                                 // press arrow down
192                                 var cmdText = "";
193                                 currentCmd = (currentCmd == cmdHistoryList.length) ? currentCmd : currentCmd + 1;
194                                 if (currentCmd < cmdHistoryList.length) 
195                                         cmdText = cmdHistoryList[currentCmd].innerHTML;
196                                 
197                                 document.querySelector("#command_field").value = cmdText;
198                                 break;
199                         }
200                 }
201         }
202 }
203
204 function saveToFile() {
205         var commands = document.querySelectorAll(".command");
206         var responses = document.querySelectorAll(".response");
207         var textToWrite = "";
208         for (var i = 0; i < commands.length; i++) {
209                 textToWrite += "> " + commands[i].innerHTML + "\r\n";
210                 textToWrite += prettyResponse(responses[i].firstChild.innerHTML);
211         }
212         
213     var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/);
214     var ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/);
215     var ieVer=(ie ? ie[1] : (ie11 ? 11 : -1));
216     
217 //    if (ie && ieVer<10) {
218 //        console.log("No blobs on IE ver<10");
219 //        return;
220 //    }
221
222         var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
223         var fileName = "AAFcommands.log";
224         
225         if (ieVer >= 10) {
226 //              window.navigator.msSaveBlob(textFileAsBlob, fileName);
227                 window.navigator.msSaveOrOpenBlob(textFileAsBlob, fileName); 
228         } else {
229                 var downloadLink = document.createElement("a");
230                 downloadLink.download = fileName;
231                 downloadLink.innerHTML = "Download File";
232                 if (window.webkitURL != null) {
233                         // Chrome allows the link to be clicked
234                         // without actually adding it to the DOM.
235                         downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
236                 } else {
237                         // Firefox requires the link to be added to the DOM
238                         // before it can be clicked.
239                         downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
240                         downloadLink.onclick = destroyClickedElement;
241                         downloadLink.style.display = "none";
242                         document.body.appendChild(downloadLink);
243                 }
244         
245                 downloadLink.click();
246         }
247 }
248
249 function prettyResponse(response) {
250         var lines = response.split('\n');
251         var cleanResponse = "";
252         for (var i=0; i < lines.length; i++) {
253                 cleanResponse += lines[i] + "\r\n";
254         }
255         cleanResponse = cleanResponse.replace(/(&lt;)/g,"<").replace(/(&gt;)/g,">");
256         return cleanResponse;
257 }
258
259 function destroyClickedElement(event){
260         document.body.removeChild(event.target);
261 }
262
263 function fakePlaceholder() {
264         document.querySelector("#command_field").setAttribute("value", "Type your AAFCLI commands here");
265 }
266
267 function maximizeConsole(img) {
268         var footer = document.querySelector("#footer");
269         var console_area = document.querySelector("#console_area");
270         var content = document.querySelector("#content");
271         var input_area = document.querySelector("#input_area");
272         var help_msg = document.querySelector("#help_msg");
273         var console_space = document.documentElement.clientHeight;
274         console_space -= input_area.outerHeight;
275         console_space -= help_msg.outerHeight;
276     var height = getStyle(console_area,'paddingTop') + getStyle(console_area,'paddingBottom');
277         console_space -= height;
278         
279         
280         if (content.getAttribute("class") != "maximized") {
281                 content.setAttribute("class", "maximized");
282                 footer.style.display="none";
283                 console_area.style.resize="none";
284                 console_area.style.height=console_space.toString()+"px";
285         } else {
286                 content.removeAttribute("class");
287                 footer.style.display="";
288                 console_area.style.resize="vertical";
289                 console_area.style.height="300px";
290         }
291         selectOption(img,0);
292 }