logstoolbar_controller.js 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16
import {StateParams, stateName as logs} from './../logs_state';

17 18 19 20 21 22 23
/**
 * Controller for the logs view.
 * @final
 */
export default class LogsToolbarController {
  /**
   * @param {!ui.router.$state} $state
24
   * @param {!StateParams} $stateParams
25 26 27 28
   * @param {!backendApi.ReplicaSetPods} replicaSetPods
   * @param {!../logs_service.LogColorInversionService} logsColorInversionService
   * @ngInject
   */
29
  constructor($state, $stateParams, replicaSetPods, logsColorInversionService) {
30 31 32
    /** @private {!ui.router.$state} */
    this.state_ = $state;

33 34
    /** @private {!StateParams} */
    this.params = $stateParams;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    /**
     * Service to notify logs controller if any changes on toolbar.
     * @private {!../logs_service.LogColorInversionService}
     */
    this.logsColorInversionService = logsColorInversionService;

    /** @export {!Array<!backendApi.ReplicaSetPodWithContainers>} */
    this.pods = replicaSetPods.pods || [];

    /**
     * Currently chosen pod.
     * @export {!backendApi.ReplicaSetPodWithContainers|undefined}
     */
    this.pod = this.findPodByName_(this.pods, this.params.podId);

    /** @export {!Array<!backendApi.PodContainer>} */
    this.containers = this.pod.podContainers || [];

    /** @export {!backendApi.PodContainer|undefined} */
    this.container = this.findContainerByName_(this.containers, this.params.container);

    /**
     * Pod creation time.
59
     * @export {?string}
60
     */
61
    this.podCreationTime = this.pod.startTime;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    /**
     * Namespace.
     * @private {string}
     */
    this.namespace = this.params.namespace;

    /**
     * Replica Set name.
     * @private {string}
     */
    this.replicaSetName = this.params.replicaSet;

    /**
     * Indicates state of log area color.
     * If false: black text is placed on white area. Otherwise colors are inverted.
     * @export
     * @return {boolean}
     */
    this.isTextColorInverted = function() { return this.logsColorInversionService.getInverted(); };
  }

  /**
   * Execute a code when a user changes the selected option of a pod element.
   * @param {string} podId
   * @return {string}
   * @export
   */
  onPodChange(podId) {
91 92
    return this.state_.transitionTo(
        logs, new StateParams(this.namespace, this.replicaSetName, podId, this.container.name));
93 94 95 96 97 98 99 100 101
  }

  /**
   * Execute a code when a user changes the selected option of a container element.
   * @param {string} container
   * @return {string}
   * @export
   */
  onContainerChange(container) {
102 103
    return this.state_.transitionTo(
        logs, new StateParams(this.namespace, this.replicaSetName, this.pod.name, container));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  }

  /**
   * Return proper style class for icon.
   * @export
   * @returns {string}
   */
  getStyleClass() {
    const logsTextColor = 'kd-logs-color-icon';
    if (this.isTextColorInverted()) {
      return `${logsTextColor}-invert`;
    }
    return `${logsTextColor}`;
  }

  /**
   * Execute a code when a user changes the selected option for console color.
   * @export
   */
  onTextColorChange() { this.logsColorInversionService.invert(); }

  /**
   * Find Pod by name.
   * Return object or undefined if can not find a object.
   * @param {!Array<!backendApi.ReplicaSetPodWithContainers>} array
   * @param {!string} name
   * @return {!backendApi.ReplicaSetPodWithContainers|undefined}
   * @private
   */
  findPodByName_(array, name) { return array.find((element) => element.name === name); }

  /**
   * Find Container by name.
   * Return object or undefined if can not find a object.
   * @param {!Array<!backendApi.PodContainer>} array
   * @param {!string} name
   * @return {!backendApi.PodContainer|undefined}
   * @private
   */
  findContainerByName_(array, name) { return array.find((element) => element.name === name); }
}