1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

Job stdout - Safari support

Safari's DOMParser object does not support parsing HTML. When the app initializes we now detect the browser type and define $rootScope.browser accordingly. If the browser is Safari, then we do some ugly looping and regex'ing to separate out the html and style sheet from the stdout API response.
This commit is contained in:
Chris Houseknecht 2014-06-17 11:53:33 -04:00 committed by Luke Sneeringer
parent a28d90a1ed
commit e8337cffd4
2 changed files with 72 additions and 10 deletions

View File

@ -426,7 +426,32 @@ angular.module('Tower', [
$rootScope.crumbCache = [];
$rootScope.sessionTimer = Timer.init();
function detectBrowser() {
var ua = window.navigator.userAgent,
browser;
if (ua.search("MSIE") >= 0) {
browser = "MSIE";
}
else if (navigator.userAgent.search("Chrome") >= 0) {
browser = "CHROME";
}
else if (navigator.userAgent.search("Firefox") >= 0) {
browser = "FF";
}
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
browser = "SAFARI";
}
else if (navigator.userAgent.search("Opera") >= 0) {
browser = "OPERA";
}
return browser;
}
$rootScope.browser = detectBrowser();
$rootScope.$on("$routeChangeStart", function (event, next) {
var base;
// Before navigating away from current tab, make sure the primary view is visible
if ($('#stream-container').is(':visible')) {
HideStream();
@ -456,7 +481,7 @@ angular.module('Tower', [
}
// Make the correct tab active
var base = $location.path().replace(/^\//, '').split('/')[0];
base = $location.path().replace(/^\//, '').split('/')[0];
if (base === '') {
base = 'home';
} else {
@ -464,6 +489,7 @@ angular.module('Tower', [
base = (base === 'job_events' || base === 'job_host_summaries') ? 'jobs' : base;
}
$('.nav-tabs a[href="#' + base + '"]').tab('show');
});
if (!Authorization.getToken()) {
@ -515,7 +541,6 @@ angular.module('Tower', [
setTimeout(function() {
$rootScope.$apply(function() {
sock.checkStatus();
//$rootScope.$emit('SocketErrorEncountered');
$log.debug('socket status: ' + $rootScope.socketStatus);
});
},2000);

View File

@ -7,7 +7,7 @@
'use strict';
function JobStdoutController ($scope, $compile, $routeParams, ClearScope, GetBasePath, Wait, Rest, ProcessErrors, Socket) {
function JobStdoutController ($rootScope, $scope, $compile, $routeParams, ClearScope, GetBasePath, Wait, Rest, ProcessErrors, Socket) {
ClearScope();
@ -36,14 +36,50 @@ function JobStdoutController ($scope, $compile, $routeParams, ClearScope, GetBas
Rest.setUrl(stdout_url + '?format=html');
Rest.get()
.success(function(data) {
var lines, styles=[], html=[], found=false, doc, style, pre, parser;
api_complete = true;
Wait('stop');
var doc, style, pre, parser = new DOMParser();
doc = parser.parseFromString(data, "text/html");
pre = doc.getElementsByTagName('pre');
style = doc.getElementsByTagName('style');
$('#style-sheet-container').empty().html(style[0]);
$('#pre-container-content').empty().html($(pre[0]).html());
if ($rootScope.browser === "SAFARI") {
// Safari's DOMParser will not parse HTML, so we have to do our best to extract the
// parts we want.
lines = data.split("\n");
// Get the style sheet
lines.forEach(function(line) {
if (/<style.*/.test(line)) {
found = true;
}
if (found) {
styles.push(line);
}
if (/<\/style>/.test(line)) {
found = false;
}
});
found = false;
// Get all the bits between <pre> and </pre>
lines.forEach(function(line) {
if (/<pre>/.test(line)) {
found = true;
}
else if (/<\/pre>/.test(line)) {
found = false;
}
else if (found) {
html.push(line);
}
});
$('#style-sheet-container').empty().html(styles.join("\n"));
$('#pre-container-content').empty().html(html.join("\n"));
}
else {
parser = new DOMParser();
doc = parser.parseFromString(data, "text/html");
pre = doc.getElementsByTagName('pre');
style = doc.getElementsByTagName('style');
$('#style-sheet-container').empty().html(style[0]);
$('#pre-container-content').empty().html($(pre[0]).html());
}
setTimeout(function() { $('#pre-container').mCustomScrollbar("scrollTo", 'bottom'); }, 1000);
})
.error(function(data, status) {
@ -83,4 +119,5 @@ function JobStdoutController ($scope, $compile, $routeParams, ClearScope, GetBas
});
}
JobStdoutController.$inject = [ '$scope', '$compile', '$routeParams', 'ClearScope', 'GetBasePath', 'Wait', 'Rest', 'ProcessErrors', 'Socket' ];
JobStdoutController.$inject = [ '$rootScope', '$scope', '$compile', '$routeParams', 'ClearScope', 'GetBasePath', 'Wait', 'Rest', 'ProcessErrors', 'Socket' ];