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