logstoolbar_controller.js 3.9 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
B
bryk 已提交
25
   * @param {!backendApi.Logs} podLogs
26 27 28
   * @param {!../logs_service.LogColorInversionService} logsColorInversionService
   * @ngInject
   */
29
  constructor($state, $stateParams, podLogs, podContainers, logsColorInversionService) {
30 31 32 33 34 35 36
    /** @private {!ui.router.$state} */
    this.state_ = $state;

    /**
     * Service to notify logs controller if any changes on toolbar.
     * @private {!../logs_service.LogColorInversionService}
     */
B
bryk 已提交
37
    this.logsColorInversionService_ = logsColorInversionService;
38

39 40
    /** @export {!Array<string>} */
    this.containers = podContainers.containers;
41

42 43
    /** @export {string} */
    this.container = $stateParams.container || this.containers[0];
44

45
    /** @export {../logs_state.StateParams} */
46
    this.stateParams = $stateParams;
47 48 49

    /** @export */
    this.i18n = i18n;
50 51
  }

B
bryk 已提交
52 53 54 55 56 57 58 59
  /**
   * Indicates state of log area color.
   * If false: black text is placed on white area. Otherwise colors are inverted.
   * @export
   * @return {boolean}
   */
  isTextColorInverted() { return this.logsColorInversionService_.getInverted(); }

60 61 62 63 64 65 66
  /**
   * Execute a code when a user changes the selected option of a container element.
   * @param {string} container
   * @return {string}
   * @export
   */
  onContainerChange(container) {
67 68 69
    return this.state_.go(
        logs,
        new StateParams(this.stateParams.objectNamespace, this.stateParams.objectName, container));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  }

  /**
   * 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
   */
B
bryk 已提交
89
  onTextColorChange() { this.logsColorInversionService_.invert(); }
90 91 92 93

  /**
   * Find Pod by name.
   * Return object or undefined if can not find a object.
94
   * @param {!Array<!backendApi.ReplicationControllerPodWithContainers>} array
95
   * @param {!string} name
96
   * @return {!backendApi.ReplicationControllerPodWithContainers|undefined}
97 98
   * @private
   */
99 100 101 102 103 104 105 106
  findPodByName_(array, name) {
    for (let i = 0; i < array.length; i++) {
      if (array[i].name === name) {
        return array[i];
      }
    }
    return undefined;
  }
107 108 109 110 111

  /**
   * Find Container by name.
   * Return object or undefined if can not find a object.
   * @param {!Array<!backendApi.PodContainer>} array
B
bryk 已提交
112 113
   * @param {string} name
   * @return {!backendApi.PodContainer}
114 115
   * @private
   */
B
bryk 已提交
116
  initializeContainer_(array, name) {
117 118 119 120 121 122 123
    let container = undefined;
    for (let i = 0; i < array.length; i++) {
      if (array[i].name === name) {
        container = array[i];
        break;
      }
    }
B
bryk 已提交
124 125 126 127 128
    if (!container) {
      container = array[0];
    }
    return container;
  }
129
}
130 131 132 133 134 135 136

const i18n = {
  /** @export {string} @desc Label 'Pod' on the toolbar of the logs page. Ends with colon. */
  MSG_LOGS_POD_LABEL: goog.getMsg('Pod:'),
  /** @export {string} @desc Label 'Container' on the toolbar of the logs page. Ends with colon. */
  MSG_LOGS_CONTAINER_LABEL: goog.getMsg('Container:'),
};