<commit_before>require('./style/index.less');

var React = require('react');
var Main = require('client/components/main/index');

var SecurityDetail = require('client/components/security_detail/index');

var deleteModal = require('client/components/modal_delete/index');
var createSecurityGroup = require('./pop/create_security_group/index');
var modifySecurityGroup = require('./pop/modify_security_group/index');
var createRule = require('./pop/create_rule/index');

var config = require('./config.json');
var request = require('./request');
var router = require('client/utils/router');
var __ = require('locale/client/dashboard.lang.json');

class Model extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      config: config
    };

    ['onInitialize', 'onAction'].forEach((m) => {
      this[m] = this[m].bind(this);
    });
  }

  componentWillMount() {
  }

  shouldComponentUpdate(nextProps, nextState) {
    if (this.props.style.display === 'none' && !nextState.config.table.loading) {
      return false;
    }
    return true;
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.style.display !== 'none' && this.props.style.display === 'none') {
      if (this.state.config.table.loading) {
        this.loadingTable();
      } else {
        this.getTableData(false);
      }
    }
  }

  onInitialize(params) {
    this.getTableData(false);
  }

  getTableData(forceUpdate, detailRefresh) {
    request.getList(forceUpdate).then((res) => {
      var table = this.state.config.table;
      table.data = res;
      table.loading = false;

      var detail = this.refs.dashboard.refs.detail;
      if (detail && detail.state.loading) {
        detail.setState({
          loading: false
        });
      }

      this.setState({
        config: config
      }, () => {
        if (detail && detailRefresh) {
          detail.refresh();
        }
      });
    });
  }

  onAction(field, actionType, refs, data) {
    switch (field) {
      case 'btnList':
        this.onClickBtnList(data.key, refs, data);
        break;
      case 'table':
        this.onClickTable(actionType, refs, data);
        break;
      case 'detail':
        this.onClickDetailTabs(actionType, refs, data);
        break;
      default:
        break;
    }
  }

  onClickBtnList(key, refs, data) {
    var rows = data.rows;
    var that = this;
    switch (key) {
      case 'create':
        createSecurityGroup(null, function() {
          that.refresh(null, true);
        });
        break;
      case 'delete':
        deleteModal({
          __: __,
          action: 'delete',
          type: 'security_group',
          data: rows,
          onDelete: function(_data, cb) {
            request.deleteSecurityGroup(rows).then(() => {
              if (router.getPathList()[2]) {
                router.replaceState('/dashboard/security-group');
              }
              that.refresh(null, true);
            });
            cb(true);
          }
        });
        break;
      case 'refresh':
        this.refresh({
          tableLoading: true,
          detailLoading: true,
          clearState: true,
          detailRefresh: true
        }, true);
        break;
      case 'modify':
        modifySecurityGroup(rows[0], null, function(_data) {
          that.refresh(null, true);
        });
        break;
      default:
        break;
    }
  }

  onClickTable(actionType, refs, data) {
    switch (actionType) {
      case 'check':
        this.onClickTableCheckbox(refs, data);
        break;
      default:
        break;
    }
  }

  onClickTableCheckbox(refs, data) {
    var {rows} = data,
      btnList = refs.btnList,
      btns = btnList.state.btns;

    btnList.setState({
      btns: this.btnListRender(rows, btns)
    });
  }

  btnListRender(rows, btns) {
    var noDefault = true;
    rows.forEach((ele) => {
      noDefault = noDefault && (ele.name === 'default' ? false : true);
    });
    for(let key in btns) {
      switch (key) {
        case 'modify':
          btns[key].disabled = (rows.length === 1 && noDefault) ? false : true;
          break;
        case 'delete':
          btns[key].disabled = (rows.length > 0 && noDefault) ? false : true;
          break;
        default:
          break;
      }
    }

    return btns;
  }

  onClickDetailTabs(tabKey, refs, data) {
    var {rows} = data;
    var detail = refs.detail;
    var contents = detail.state.contents;

    var isAvailableView = (_rows) => {
      if (_rows.length > 1) {
        contents[tabKey] = (
          <div className="no-data-desc">
            <p>{__.view_is_unavailable}</p>
          </div>
        );
        return false;
      } else {
        return true;
      }
    };

    switch(tabKey) {
      case 'description':
        if (isAvailableView(rows)) {
          var itemKeys = ['ingress', 'egress'];
          var items = this.getSecurityDetailData(rows[0], this);

          contents[tabKey] = (
            <SecurityDetail
              __={__}
              title={__.security_group + __.rules}
              defaultUnfold={true}
              itemKeys={itemKeys}
              defaultKey="ingress"
              tabKey="description"
              items={items}
              rawItem={rows[0]}
              onClick={this.onDetailAction.bind(this)} />
          );
        }
        break;
      default:
        break;
    }

    detail.setState({
      contents: contents<commit_msg>add params in SecurityDetail<commit_after>require('./style/index.less');

var React = require('react');
var Main = require('client/components/main/index');

var SecurityDetail = require('client/components/security_detail/index');

var deleteModal = require('client/components/modal_delete/index');
var createSecurityGroup = require('./pop/create_security_group/index');
var modifySecurityGroup = require('./pop/modify_security_group/index');
var createRule = require('./pop/create_rule/index');

var config = require('./config.json');
var request = require('./request');
var router = require('client/utils/router');
var __ = require('locale/client/dashboard.lang.json');

class Model extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      config: config
    };

    ['onInitialize', 'onAction'].forEach((m) => {
      this[m] = this[m].bind(this);
    });
  }

  componentWillMount() {
  }

  shouldComponentUpdate(nextProps, nextState) {
    if (this.props.style.display === 'none' && !nextState.config.table.loading) {
      return false;
    }
    return true;
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.style.display !== 'none' && this.props.style.display === 'none') {
      if (this.state.config.table.loading) {
        this.loadingTable();
      } else {
        this.getTableData(false);
      }
    }
  }

  onInitialize(params) {
    this.getTableData(false);
  }

  getTableData(forceUpdate, detailRefresh) {
    request.getList(forceUpdate).then((res) => {
      var table = this.state.config.table;
      table.data = res;
      table.loading = false;

      var detail = this.refs.dashboard.refs.detail;
      if (detail && detail.state.loading) {
        detail.setState({
          loading: false
        });
      }

      this.setState({
        config: config
      }, () => {
        if (detail && detailRefresh) {
          detail.refresh();
        }
      });
    });
  }

  onAction(field, actionType, refs, data) {
    switch (field) {
      case 'btnList':
        this.onClickBtnList(data.key, refs, data);
        break;
      case 'table':
        this.onClickTable(actionType, refs, data);
        break;
      case 'detail':
        this.onClickDetailTabs(actionType, refs, data);
        break;
      default:
        break;
    }
  }

  onClickBtnList(key, refs, data) {
    var rows = data.rows;
    var that = this;
    switch (key) {
      case 'create':
        createSecurityGroup(null, function() {
          that.refresh(null, true);
        });
        break;
      case 'delete':
        deleteModal({
          __: __,
          action: 'delete',
          type: 'security_group',
          data: rows,
          onDelete: function(_data, cb) {
            request.deleteSecurityGroup(rows).then(() => {
              if (router.getPathList()[2]) {
                router.replaceState('/dashboard/security-group');
              }
              that.refresh(null, true);
            });
            cb(true);
          }
        });
        break;
      case 'refresh':
        this.refresh({
          tableLoading: true,
          detailLoading: true,
          clearState: true,
          detailRefresh: true
        }, true);
        break;
      case 'modify':
        modifySecurityGroup(rows[0], null, function(_data) {
          that.refresh(null, true);
        });
        break;
      default:
        break;
    }
  }

  onClickTable(actionType, refs, data) {
    switch (actionType) {
      case 'check':
        this.onClickTableCheckbox(refs, data);
        break;
      default:
        break;
    }
  }

  onClickTableCheckbox(refs, data) {
    var {rows} = data,
      btnList = refs.btnList,
      btns = btnList.state.btns;

    btnList.setState({
      btns: this.btnListRender(rows, btns)
    });
  }

  btnListRender(rows, btns) {
    var noDefault = true;
    rows.forEach((ele) => {
      noDefault = noDefault && (ele.name === 'default' ? false : true);
    });
    for(let key in btns) {
      switch (key) {
        case 'modify':
          btns[key].disabled = (rows.length === 1 && noDefault) ? false : true;
          break;
        case 'delete':
          btns[key].disabled = (rows.length > 0 && noDefault) ? false : true;
          break;
        default:
          break;
      }
    }

    return btns;
  }

  onClickDetailTabs(tabKey, refs, data) {
    var {rows} = data;
    var detail = refs.detail;
    var contents = detail.state.contents;

    var isAvailableView = (_rows) => {
      if (_rows.length > 1) {
        contents[tabKey] = (
          <div className="no-data-desc">
            <p>{__.view_is_unavailable}</p>
          </div>
        );
        return false;
      } else {
        return true;
      }
    };

    switch(tabKey) {
      case 'description':
        if (isAvailableView(rows)) {
          var itemKeys = ['ingress', 'egress'];
          var items = this.getSecurityDetailData(rows[0], this);

          contents[tabKey] = (
            <SecurityDetail
              title={__.security_group + __.rules}
              btnValue={__.add_ + __.security_group + __.rules}
              defaultUnfold={true}
              itemKeys={itemKeys}
              defaultKey="ingress"
              tabKey="description"
              items={items}
              rawItem={rows[0]}
              onClick={this.onDetailAction.bind(this)} />
          );
        }
        break;
      default:
        break;
    }

    detail.setState({
      contents: contents
{'fraction_non_alphanumeric': 0.1375261672886138, 'fraction_numerical': 0.0012742331846727951, 'mean_word_length': 1.960926973861493, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '2341513', 'n_tokens_mistral': 3278, 'n_tokens_neox': 3245, 'n_words': 617}