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