<commit_before>/**
 * @module Jmx
 */
module Jmx {

  export var propertiesColumnDefs = [
    {field: 'name', displayName: 'Property', width: "27%",
      cellTemplate: '<div class="ngCellText" title="{{row.entity.attrDesc}}" ' +
        'data-placement="bottom"><div ng-show="!inDashboard" class="inline" compile="getDashboardWidgets(row.entity)"></div>{{row.entity.name}}</div>'},
    {field: 'value', displayName: 'Value', width: "70%",
      cellTemplate: '<div class="ngCellText" ng-click="onViewAttribute(row.entity)" title="{{row.entity.attrDesc}}" ng-bind-html-unsafe="row.entity.summary"></div>'
    }
  ];

  export var foldersColumnDefs = [
    {
      displayName: 'Name',
      cellTemplate: '<div class="ngCellText"><a href="{{folderHref(row)}}"><i class="{{folderIconClass(row)}}"></i> {{row.getProperty("title")}}</a></div>'
    }
  ];

  export function AttributesController($scope, $element, $location, workspace:Workspace, jolokia, jmxWidgets, jmxWidgetTypes) {
    $scope.searchText = '';
    $scope.columnDefs = [];
    $scope.selectedItems = [];

    $scope.lastKey = null;
    $scope.attributesInfoCache = {};

    $scope.entity = {};
    $scope.attributeSchema = {};

    var attributeSchemaBasic = {
      properties: {
        'key': {
          description: 'Key',
          tooltip: 'Attribute key',
          type: 'string',
          readOnly: 'true'
        },
        'description': {
          description: 'Description',
          tooltip: 'Attribute description',
          type: 'string',
          formTemplate: "<textarea class='input-xlarge' rows='2' readonly='true'></textarea>"
        },
        'type': {
          description: 'Type',
          tooltip: 'Attribute type',
          type: 'string',
          readOnly: 'true'
        }
      }
    };

    $scope.gridOptions = {
      selectedItems: $scope.selectedItems,
      //plugins: [$scope.selectToggle],
      showFilter: false,
      //showColumnMenu: false,
      canSelectRows: false,
      enableRowSelection: false,
      keepLastSelected: false,
      multiSelect: false,
      showColumnMenu: true,
      displaySelectionCheckbox: false,
      filterOptions: {
        filterText: ''
      },
      // TODO disabled for now as it causes https://github.com/hawtio/hawtio/issues/262
      //sortInfo: { field: 'name', direction: 'asc'},
      data: 'gridData',
      columnDefs: 'columnDefs'
    };

    $scope.$on("$routeChangeSuccess", function (event, current, previous) {
      // lets do this asynchronously to avoid Error: $digest already in progress
      setTimeout(updateTableContents, 50);
    });

    $scope.$watch('workspace.selection', function () {
      if (workspace.moveIfViewInvalid()) {
        Core.unregister(jolokia, $scope);
        return;
      }
      updateTableContents();
    });

    $scope.hasWidget = (row) => {
      console.log("Row: ", row);
      return true;
    };

    $scope.onCancelAttribute = () => {
      // clear entity
      $scope.entity = {};
    }

    $scope.onUpdateAttribute = () => {
      var value = $scope.entity["attrValueEdit"];
      var key = $scope.entity["key"];

      // clear entity
      $scope.entity = {};

      // TODO: check if value changed

      // update the attribute on the mbean
      var mbean = workspace.getSelectedMBeanName();
      if (mbean) {
        jolokia.setAttribute(mbean, key, value,
          onSuccess((response) => {
              notification("success", "Updated attribute " + key);
            }
          ));
      }
    };

    $scope.onViewAttribute = (row) => {
      // create entity and populate it with data from the selected row
      $scope.entity = {};
      $scope.entity["key"] = row.key;
      $scope.entity["description"] = row.attrDesc;
      $scope.entity["type"] = row.type;
      $scope.entity["rw"] = row.rw;
      var type = asJsonSchemaType(row.type, row.key);
      var readOnly = !row.rw;

      // clone the new map
      if (readOnly) {
        $scope.entity["attrValueView"] = row.summary;
        $scope.attributeSchemaView = {};
        for (var i in attributeSchemaBasic) {
          $scope.attributeSchemaView[i] = attributeSchemaBasic[i];
        }
        // and add the new attrValue which is dynamic computed
        $scope.attributeSchemaView.properties.attrValueView = {
          description: 'Value',
          label: "Value",
          tooltip: 'Attribute value',
          type: 'string',
          readOnly: 'true'
        }
        delete $scope.attributeSchemaEdit.properties.attrValueEdit;
      } else {
        $scope.entity["attrValueEdit"] = row.summary;
        $scope.attributeSchemaEdit = {};
        for (var i in attributeSchemaBasic) {
          $scope.attributeSchemaEdit[i] = attributeSchemaBasic[i];
        }
        // and add the new attrValue which is dynamic computed
        $scope.attributeSchemaEdit.properties.attrValueEdit = {
          description: 'Value',
          label: "Value",
          tooltip: 'Attribute value',
          type: 'string'
        }
        delete $scope.attributeSchemaEdit.properties.attrValueView;
      }

      log.info("Read only " + readOnly);

      $scope.showAttributeDialog = true;
    }

    $scope.getDashboardWidgets = (row) => {
      var mbean = workspace.getSelectedMBeanName();
      if (!mbean) {
        return '';
      }
      var potentialCandidates = jmxWidgets.filter((widget) => {<commit_msg>#883: JMX attribute dialog show type, and work on being able to edit the value.<commit_after>/**
 * @module Jmx
 */
module Jmx {

  export var propertiesColumnDefs = [
    {field: 'name', displayName: 'Property', width: "27%",
      cellTemplate: '<div class="ngCellText" title="{{row.entity.attrDesc}}" ' +
        'data-placement="bottom"><div ng-show="!inDashboard" class="inline" compile="getDashboardWidgets(row.entity)"></div>{{row.entity.name}}</div>'},
    {field: 'value', displayName: 'Value', width: "70%",
      cellTemplate: '<div class="ngCellText" ng-click="onViewAttribute(row.entity)" title="{{row.entity.attrDesc}}" ng-bind-html-unsafe="row.entity.summary"></div>'
    }
  ];

  export var foldersColumnDefs = [
    {
      displayName: 'Name',
      cellTemplate: '<div class="ngCellText"><a href="{{folderHref(row)}}"><i class="{{folderIconClass(row)}}"></i> {{row.getProperty("title")}}</a></div>'
    }
  ];

  export function AttributesController($scope, $element, $location, workspace:Workspace, jolokia, jmxWidgets, jmxWidgetTypes) {
    $scope.searchText = '';
    $scope.columnDefs = [];
    $scope.selectedItems = [];

    $scope.lastKey = null;
    $scope.attributesInfoCache = {};

    $scope.entity = {};
    $scope.attributeSchema = {};

    var attributeSchemaBasic = {
      properties: {
        'key': {
          description: 'Key',
          tooltip: 'Attribute key',
          type: 'string',
          readOnly: 'true'
        },
        'description': {
          description: 'Description',
          tooltip: 'Attribute description',
          type: 'string',
          formTemplate: "<textarea class='input-xlarge' rows='2' readonly='true'></textarea>"
        },
        'type': {
          description: 'Type',
          tooltip: 'Attribute type',
          type: 'string',
          readOnly: 'true'
        }
      }
    };

    $scope.gridOptions = {
      selectedItems: $scope.selectedItems,
      //plugins: [$scope.selectToggle],
      showFilter: false,
      //showColumnMenu: false,
      canSelectRows: false,
      enableRowSelection: false,
      keepLastSelected: false,
      multiSelect: false,
      showColumnMenu: true,
      displaySelectionCheckbox: false,
      filterOptions: {
        filterText: ''
      },
      // TODO disabled for now as it causes https://github.com/hawtio/hawtio/issues/262
      //sortInfo: { field: 'name', direction: 'asc'},
      data: 'gridData',
      columnDefs: 'columnDefs'
    };

    $scope.$on("$routeChangeSuccess", function (event, current, previous) {
      // lets do this asynchronously to avoid Error: $digest already in progress
      setTimeout(updateTableContents, 50);
    });

    $scope.$watch('workspace.selection', function () {
      if (workspace.moveIfViewInvalid()) {
        Core.unregister(jolokia, $scope);
        return;
      }
      updateTableContents();
    });

    $scope.hasWidget = (row) => {
      console.log("Row: ", row);
      return true;
    };

    $scope.onCancelAttribute = () => {
      // clear entity
      $scope.entity = {};
    }

    $scope.onUpdateAttribute = () => {
      var value = $scope.entity["attrValueEdit"];
      var key = $scope.entity["key"];

      // clear entity
      $scope.entity = {};

      // TODO: check if value changed

      // update the attribute on the mbean
      var mbean = workspace.getSelectedMBeanName();
      if (mbean) {
        jolokia.setAttribute(mbean, key, value,
          onSuccess((response) => {
              notification("success", "Updated attribute " + key);
            }
          ));
      }
    };

    $scope.onViewAttribute = (row) => {
      // create entity and populate it with data from the selected row
      $scope.entity = {};
      $scope.entity["key"] = row.key;
      $scope.entity["description"] = row.attrDesc;
      $scope.entity["type"] = row.type;
      $scope.entity["rw"] = row.rw;
      var type = asJsonSchemaType(row.type, row.key);
      var readOnly = !row.rw;

      // calculate a textare with X number of rows that usually fit the value to display
      var len = row.summary.length;
      var rows = (len / 40) + 1;
      if (rows > 10) {
        // cap at most 10 rows to not make the dialog too large
        rows = 10;
      }

      // clone the new map
      if (readOnly) {
        $scope.entity["attrValueView"] = row.summary;

        $scope.attributeSchemaView = {};
        for (var i in attributeSchemaBasic) {
          $scope.attributeSchemaView[i] = attributeSchemaBasic[i];
        }

        // and add the new attrValue which is dynamic computed
        $scope.attributeSchemaView.properties.attrValueView = {
          description: 'Value',
          label: "Value",
          tooltip: 'Attribute value',
          type: 'string',
          formTemplate: "<textarea class='input-xlarge' rows='" + rows + "' readonly='true'></textarea>"
        }
        if ($scope.attributeSchemaView) {
          delete $scope.attributeSchemaView.properties.attrValueEdit;
        }
      } else {
        $scope.entity["attrValueEdit"] = row.summary;
        $scope.attributeSchemaEdit = {};
        for (var i in attributeSchemaBasic) {
          $scope.attributeSchemaEdit[i] = attributeSchemaBasic[i];
        }
        // and add the new attrValue which is dynamic computed
        $scope.attributeSchemaEdit.properties.attrValueEdit = {
          description: 'Value',
          label: "Value",
          tooltip: 'Attribute value',
          type: 'string',
          formTemplate: "<textarea class='input-xlarge' rows='" + rows + "'></textarea>"
        }
        if ($scope.attributeSchemaEdit) {
          delete $scope.attributeSchemaEdit.properties.attrValueView;
        }
      }

      $scope.showAttributeDialog = true;
    }

    $scope.getDashboardWidgets = (row) => {
      var mbean = workspace.getSelectedMBeanName();
      if (!mbean) {
        return '';
      }
      var potentialCandidates = jmxWidgets.filter((widget) => {
{'fraction_non_alphanumeric': 0.1449618521441726, 'fraction_numerical': 0.002806279049372972, 'mean_word_length': 2.4062126642771804, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885501', 'n_tokens_mistral': 3319, 'n_tokens_neox': 3227, 'n_words': 713}