Viewing File: /usr/local/cpanel/base/frontend/jupiter/site-monitor/directives/monitorDirective.js

/*
# site-monitor/directives/monitorDirective.js                    Copyright(c) 2021 cPanel, L.L.C.
#                                                                            All rights reserved.
# copyright@cpanel.net                                                          http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
*/

/* global define: false */

/** @namespace cpanel.siteMonitor.directive.monitor */

define(
    [
        "angular",
        "lodash",
        "cjt/core",
        "cjt/util/locale",
        "app/models/monitor-state.enum",
        "cjt/directives/spinnerDirective",
    ],
    function(angular, _, CJT, LOCALE, MonitorState) {

        "use strict";

        var module = angular.module("cpanel.siteMonitor.monitorDirective", [
            "cjt2.directives.spinner",
        ]);

        module.directive("monitor", ["spinnerAPI", function factory(spinnerAPI) {

            /**
             * Generates a monitor line in the table
             *
             * @module docroot
             * @restrict E
             *
             * @param  {string} id an id attribute for the element.
             * @param  {Monitor} data reference to the monitor to render.
             *
             * @example
             * <li ng-repeat="monitor in ::site.monitors trackby monitor.url()"
             *    id="{{ ::monitor.name}}" >
             *    <monitor data="monitor" />
             * </li>
             *
             */

            var TEMPLATE_PATH = "directives/monitorDirective.phtml";
            var RELATIVE_PATH = "site-monitor/" + TEMPLATE_PATH;

            return {
                templateUrl: CJT.config.debug ? CJT.buildFullPath(RELATIVE_PATH) : TEMPLATE_PATH,
                restrict: "E",
                require: "^monitors",
                scope: {
                    monitor: "=data",
                    parentID: "@parentID",
                },
                link: function(scope, element, attrs) {
                    // Watch the state property to update the spinners
                    var unwatch = scope.$watch("monitor.state", function(newState, oldState, scope) {
                        var spinnerId = scope.makeSpinnerId(scope.monitor);
                        switch(newState) {
                            case MonitorState.Creating:
                            case MonitorState.Removing:
                                spinnerAPI.start(spinnerId);
                                break;
                            default:
                                spinnerAPI.stop(spinnerId);
                                break;
                        }
                    }, true);
                },
                controller: ["$scope", "spinnerAPI", function($scope, spinnerAPI) {

                    $scope.LOCALE = LOCALE;
                    $scope.MonitorState = MonitorState;

                    $scope.standardPorts = {
                        http: 80,
                        https: 443,
                    };

                    /**
                     * Check if the port is one of the standard ones.
                     *
                     * @param {string} protocol - the protocol
                     * @param {number} port - the port number on the monitor
                     * @returns
                     */
                    $scope.hasStandardPort = function(protocol, port) {
                        return $scope.standardPorts[protocol] === port;
                    };

                    /**
                     * Generate the monitors spinner id
                     *
                     * @param {Monitor} monitor
                     */
                    $scope.makeSpinnerId = function(monitor) {
                        return $scope.parentID + "_spinner_" + monitor.url();
                    };

                    /**
                     * Build the correct text string for the monitor state.
                     *
                     * @param {MonitorState} state - the state of the current monitor.
                     * @returns {string} Message to print
                     */
                    $scope.monitorStateMessage = function monitorStateMessage(state) {
                        switch(state) {
                            case MonitorState.Creating:
                                return LOCALE.maketext("Configuring…")
                            case MonitorState.Removing:
                                return LOCALE.maketext("Removing…")
                            case MonitorState.Ready:
                                return LOCALE.maketext("Active");
                            case MonitorState.Error:
                                return LOCALE.maketext("Failed");
                            case MonitorState.None:
                            default:
                                return '';
                        }
                    }
                }]
            };

        }]);
    }
);
Back to Directory File Manager