'use strict'; /* global moment */ // create main application module var app = angular.module('Application', ['angular-md5', 'ui-notification']); app.controller('LogsController', ['$scope', '$timeout', '$location', 'Client', function ($scope, $timeout, $location, Client) { var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {}); $scope.initialized = false; $scope.installedApps = Client.getInstalledApps(); $scope.client = Client; $scope.logs = []; $scope.selected = ''; $scope.activeEventSource = null; $scope.lines = 10; $scope.selectedAppInfo = null; // Add built-in log types for now $scope.logs.push({ name: 'System (All)', type: 'platform', value: 'all', url: Client.makeURL('/api/v1/cloudron/logs?units=all') }); $scope.logs.push({ name: 'Box', type: 'platform', value: 'box', url: Client.makeURL('/api/v1/cloudron/logs?units=box') }); $scope.logs.push({ name: 'Mail', type: 'platform', value: 'mail', url: Client.makeURL('/api/v1/cloudron/logs?units=mail') }); $scope.error = function (error) { console.error(error); window.location.href = '/error.html'; }; function ab2str(buf) { return String.fromCharCode.apply(null, new Uint16Array(buf)); } $scope.clear = function () { var logViewer = $('.logs-container'); logViewer.empty(); }; $scope.showTerminal = function () { if (!$scope.selected) return; window.open('/terminal.html?id=' + $scope.selected.value, 'Cloudron Terminal', 'width=1024,height=800'); }; function showLogs() { if (!$scope.selected) return; var func = $scope.selected.type === 'platform' ? Client.getPlatformLogs : Client.getAppLogs; func($scope.selected.value, true, $scope.lines, function handleLogs(error, result) { if (error) return console.error(error); $scope.activeEventSource = result; result.onmessage = function handleMessage(message) { var data; try { data = JSON.parse(message.data); } catch (e) { return console.error(e); } // check if we want to auto scroll (this is before the appending, as that skews the check) var tmp = $('.logs-container'); var autoScroll = tmp[0].scrollTop > (tmp[0].scrollTopMax - 24); var logLine = $('