<commit_before>/*
 * Copyright (C) 2016 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import $ from 'jquery'
import React from 'react'
import PropTypes from 'prop-types'
import {useScope as useI18nScope} from '@canvas/i18n'
import iframeAllowances from '@canvas/external-apps/iframeAllowances'
import CanvasModal from '@canvas/instui-bindings/react/Modal'

const I18n = useI18nScope('external_toolsModalLauncher')

export default class ExternalToolModalLauncher extends React.Component {
  static propTypes = {
    appElement: PropTypes.instanceOf(Element),
    title: PropTypes.string.isRequired,
    tool: PropTypes.object,
    isOpen: PropTypes.bool.isRequired,
    onRequestClose: PropTypes.func.isRequired,
    contextType: PropTypes.string.isRequired,
    contextId: PropTypes.number.isRequired,
    launchType: PropTypes.string.isRequired,
    onExternalContentReady: PropTypes.func
  }

  static defaultProps = {
    appElement: document.getElementById('application')
  }

  state = {
    beforeExternalContentAlertClass: 'screenreader-only',
    afterExternalContentAlertClass: 'screenreader-only',
    modalLaunchStyle: {}
  }

  getDimensions = () => {
    const dimensions = this.getLaunchDimensions()

    return {
      modalStyle: this.getModalStyle(dimensions),
      modalBodyStyle: this.getModalBodyStyle(dimensions),
      modalLaunchStyle: this.getModalLaunchStyle(dimensions)
    }
  }

  componentDidMount() {
    $(window).on('externalContentReady', this.onExternalToolCompleted)
    $(window).on('externalContentCancel', this.onExternalToolCompleted)
  }

  componentWillUnmount() {
    $(window).off('externalContentReady', this.onExternalToolCompleted)
    $(window).off('externalContentCancel', this.onExternalToolCompleted)
  }

  onExternalToolCompleted = (ev, data) => {
    if (this.props.onExternalContentReady) {
      this.props.onExternalContentReady(data)
    }
    this.props.onRequestClose()
  }

  getIframeSrc = () => {
    if (this.props.isOpen && this.props.tool) {
      return [
        '/',
        this.props.contextType,
        's/',
        this.props.contextId,
        '/external_tools/',
        this.props.tool.definition_id,
        '?display=borderless&launch_type=',
        this.props.launchType
      ].join('')
    }
  }

  getLaunchDimensions = () => {
    const dimensions = {
      width: 700,
      height: 700
    }<commit_msg>support context_module_id in tool modals<commit_after>/*
 * Copyright (C) 2016 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import $ from 'jquery'
import React from 'react'
import PropTypes from 'prop-types'
import {useScope as useI18nScope} from '@canvas/i18n'
import iframeAllowances from '@canvas/external-apps/iframeAllowances'
import CanvasModal from '@canvas/instui-bindings/react/Modal'

const I18n = useI18nScope('external_toolsModalLauncher')

export default class ExternalToolModalLauncher extends React.Component {
  static propTypes = {
    appElement: PropTypes.instanceOf(Element),
    title: PropTypes.string.isRequired,
    tool: PropTypes.object,
    isOpen: PropTypes.bool.isRequired,
    onRequestClose: PropTypes.func.isRequired,
    contextType: PropTypes.string.isRequired,
    contextId: PropTypes.number.isRequired,
    launchType: PropTypes.string.isRequired,
    contextModuleId: PropTypes.string,
    onExternalContentReady: PropTypes.func
  }

  static defaultProps = {
    appElement: document.getElementById('application')
  }

  state = {
    beforeExternalContentAlertClass: 'screenreader-only',
    afterExternalContentAlertClass: 'screenreader-only',
    modalLaunchStyle: {}
  }

  getDimensions = () => {
    const dimensions = this.getLaunchDimensions()

    return {
      modalStyle: this.getModalStyle(dimensions),
      modalBodyStyle: this.getModalBodyStyle(dimensions),
      modalLaunchStyle: this.getModalLaunchStyle(dimensions)
    }
  }

  componentDidMount() {
    $(window).on('externalContentReady', this.onExternalToolCompleted)
    $(window).on('externalContentCancel', this.onExternalToolCompleted)
  }

  componentWillUnmount() {
    $(window).off('externalContentReady', this.onExternalToolCompleted)
    $(window).off('externalContentCancel', this.onExternalToolCompleted)
  }

  onExternalToolCompleted = (ev, data) => {
    if (this.props.onExternalContentReady) {
      this.props.onExternalContentReady(data)
    }
    this.props.onRequestClose()
  }

  getIframeSrc = () => {
    if (this.props.isOpen && this.props.tool) {
      return [
        '/',
        this.props.contextType,
        's/',
        this.props.contextId,
        '/external_tools/',
        this.props.tool.definition_id,
        '?display=borderless&launch_type=',
        this.props.launchType,
        this.props.contextModuleId && '&context_module_id=',
        this.props.contextModuleId
      ].join('')
    }
  }

  getLaunchDimensions = () => {
    const dimensions = {
      width: 700,
      height: 700
    }
{'fraction_non_alphanumeric': 0.10204739642108657, 'fraction_numerical': 0.006126068031597614, 'mean_word_length': 3.685800604229607, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5218701', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1748, 'n_words': 443}