From dea213e9a1b11cc5b34b5ae146c881aa5a5968b6 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 10 Dec 2014 14:51:57 -0800 Subject: [PATCH] GUAC-932: Further cleanup history display code. --- .../manage/templates/editableConnection.html | 6 +- .../app/manage/types/HistoryEntryDuration.js | 94 +++++++++++++++++++ .../app/manage/types/HistoryEntryWrapper.js | 84 +++++------------ 3 files changed, 120 insertions(+), 64 deletions(-) create mode 100644 guacamole/src/main/webapp/app/manage/types/HistoryEntryDuration.js diff --git a/guacamole/src/main/webapp/app/manage/templates/editableConnection.html b/guacamole/src/main/webapp/app/manage/templates/editableConnection.html index a78f498b1..f9ee65458 100644 --- a/guacamole/src/main/webapp/app/manage/templates/editableConnection.html +++ b/guacamole/src/main/webapp/app/manage/templates/editableConnection.html @@ -97,11 +97,7 @@ THE SOFTWARE. {{wrapper.entry.username}} {{wrapper.entry.startDate | date:'short'}} - - {{'manage.edit.connection.history.formattedDuration' | translate:"{VALUE: wrapper.duration.value, UNIT: wrapper.duration.unit}"}} - {{'manage.edit.connection.history.activeNow' | translate}} - {{'manage.edit.connection.history.unknownEnd' | translate}} - + {{wrapper.durationText | translate:"{VALUE: wrapper.duration.value, UNIT: wrapper.duration.unit}"}} diff --git a/guacamole/src/main/webapp/app/manage/types/HistoryEntryDuration.js b/guacamole/src/main/webapp/app/manage/types/HistoryEntryDuration.js new file mode 100644 index 000000000..86995baa2 --- /dev/null +++ b/guacamole/src/main/webapp/app/manage/types/HistoryEntryDuration.js @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2014 Glyptodon LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * A service for defining the HistoryEntryDuration class. + */ +angular.module('manage').factory('HistoryEntryDuration', [function defineHistoryEntryDuration() { + + /** + * Value/unit pair representing the length of time that a connection was + * used. + * + * @constructor + * @param {Number} milliseconds + * The number of milliseconds that the associated connection was used. + */ + var HistoryEntryDuration = function HistoryEntryDuration(milliseconds) { + + var seconds = milliseconds / 1000; + + /** + * Rounds the given value to the nearest tenth. + * + * @param {Number} value The value to round. + * @returns {Number} The given value, rounded to the nearest tenth. + */ + var round = function round(value) { + return Math.round(value * 10) / 10; + }; + + // Days + if (seconds >= 86400) { + this.value = round(seconds / 86400); + this.unit = 'day'; + } + + // Hours + else if (seconds >= 3600) { + this.value = round(seconds / 3600); + this.unit = 'hour'; + } + + // Minutes + else if (seconds >= 60) { + this.value = round(seconds / 60); + this.unit = 'minute'; + } + + // Seconds + else { + + /** + * The number of seconds (or minutes, or hours, etc.) that the + * connection was used. The units associated with this value are + * represented by the unit property. + * + * @type Number + */ + this.value = round(seconds); + + /** + * The units associated with the value of this duration. Valid + * units are 'second', 'minute', 'hour', and 'day'. + * + * @type String + */ + this.unit = 'second'; + + } + + }; + + return HistoryEntryDuration; + +}]); \ No newline at end of file diff --git a/guacamole/src/main/webapp/app/manage/types/HistoryEntryWrapper.js b/guacamole/src/main/webapp/app/manage/types/HistoryEntryWrapper.js index 6d12c57bf..d7aa9b588 100644 --- a/guacamole/src/main/webapp/app/manage/types/HistoryEntryWrapper.js +++ b/guacamole/src/main/webapp/app/manage/types/HistoryEntryWrapper.js @@ -21,63 +21,11 @@ */ /** - * A service for generating new guacClient properties objects. + * A service for defining the HistoryEntryWrapper class. */ -angular.module('manage').factory('HistoryEntryWrapper', [function defineHistoryEntryWrapper() { +angular.module('manage').factory('HistoryEntryWrapper', ['HistoryEntryDuration', + function defineHistoryEntryWrapper(HistoryEntryDuration) { - /** - * Given a number of milliseconds, returns an object containing a unit and value - * for that history entry duration. - * - * @param {Number} milliseconds The number of milliseconds. - * @return {Object} A unit and value pair representing a history entry duration. - */ - var formatMilliseconds = function formatMilliseconds(milliseconds) { - - var seconds = milliseconds / 1000; - - /** - * Rounds the given value to the nearest tenth. - * - * @param {Number} value The value to round. - * @returns {Number} The given value, rounded to the nearest tenth. - */ - var round = function round(value) { - return Math.round(value * 10) / 10; - }; - - // Seconds - if (seconds < 60) { - return { - value : round(seconds), - unit : "second" - }; - } - - // Minutes - if (seconds < 3600) { - return { - value : round(seconds / 60 ), - unit : "minute" - }; - } - - // Hours - if (seconds < 86400) { - return { - value : round(seconds / 3600), - unit : "hour" - }; - } - - // Days - return { - value : round(seconds / 86400), - unit : "day" - }; - - }; - /** * Wrapper for ConnectionHistoryEntry which adds display-specific * properties, such as the connection duration. @@ -99,13 +47,31 @@ angular.module('manage').factory('HistoryEntryWrapper', [function defineHistoryE * An object providing value and unit properties, denoting the duration * and its corresponding units. * - * @type Object + * @type HistoryEntryDuration */ this.duration = null; - // Set the duration if the necessary information is present and the entry is not still active - if (historyEntry.endDate && historyEntry.startDate && !historyEntry.active) - this.duration = formatMilliseconds(historyEntry.endDate - historyEntry.startDate); + /** + * The string to display as the duration of this history entry. If a + * duration is available, its value and unit will be exposed to any + * given translation string as the VALUE and UNIT substitution + * variables respectively. + * + * @type String + */ + this.durationText = 'manage.edit.connection.history.formattedDuration'; + + // Notify if connection is active right now + if (historyEntry.active) + this.durationText = 'manage.edit.connection.history.activeNow'; + + // If connection is not active, inform use if end date is not known + else if (!historyEntry.endDate) + this.durationText = 'manage.edit.connection.history.unknownEnd'; + + // Set the duration if the necessary information is present + if (historyEntry.endDate && historyEntry.startDate) + this.duration = new HistoryEntryDuration(historyEntry.endDate - historyEntry.startDate); };