<commit_before>/*
Copyright 2016 First People's Cultural Council

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.
*/
import React, { Component, PropTypes } from 'react'
// import ReactDOM from 'react-dom'

// REDUX
import { connect } from 'react-redux'
import { changeTheme } from 'providers/redux/reducers/navigation'
import { nuxeoConnect, getCurrentUser } from 'providers/redux/reducers/nuxeo'
import { fetchDialect2 } from 'providers/redux/reducers/fvDialect'

import selectn from 'selectn'

import ProviderHelpers from 'common/ProviderHelpers'
// import StringHelpers from 'common/StringHelpers'
import UIHelpers from 'common/UIHelpers'

// import AuthorizationFilter from 'views/components/Document/AuthorizationFilter'

import AppFrontController from './AppFrontController'

import Shepherd from 'tether-shepherd'

// import FontIcon from 'material-ui/lib/font-icon'
// import Paper from 'material-ui/lib/paper'
// import FlatButton from 'material-ui/lib/flat-button'

import IntlService from 'views/services/intl'

// const getPosition = function getPosition() {
//   const doc = document
//   const w = window
//   let x
//   let y
//   let docEl

//   if (typeof w.pageYOffset === 'number') {
//     x = w.pageXOffset
//     y = w.pageYOffset
//   } else {
//     docEl = doc.compatMode && doc.compatMode === 'CSS1Compat' ? doc.documentElement : doc.body
//     x = docEl.scrollLeft
//     y = docEl.scrollTop
//   }
//   return { x: x, y: y }
// }

/**
 * Finds the xPath for a component, leading up to the 'app-wrapper'.
 * Used as a utility for creating Shepherd tours
 */
function findComponentParents(_el) {
  let parents = []
  let el = _el
  const originalEl = el

  if (!el.getAttribute('data-component-id')) {
    return null
  }

  while (el.parentNode) {
    el = el.parentNode

    if (el.getAttribute('id') === 'app-wrapper') {
      break
    }

    if (el.tagName) {
      const appendClass = el.className ? '.' + el.className.replace(/\s/g, '.').replace(/\:/g, '\\:') : '' // eslint-disable-line
      const appendData = el.getAttribute('data-component-id')
        ? "[data-component-id='" + el.getAttribute('data-component-id') + "']"
        : ''

      parents.push(el.tagName.toLowerCase() + appendClass + appendData)
    }
  }

  parents = parents.reverse()
  parents.push(
    originalEl.tagName.toLowerCase() + "[data-component-id='" + originalEl.getAttribute('data-component-id') + "']"
  )

  return parents.join(' ')
}

const getPreferences = function getPreferences(login, dialect) {
  const preferenceString = selectn('response.properties.preferences', login)
  const parsedPreferences = preferenceString ? JSON.parse(preferenceString) : {}
  const flattenedPreferences = {}

  for (const preferenceCat in parsedPreferences) {
    for (const preference in parsedPreferences[preferenceCat]) {
      flattenedPreferences[preference] = parsedPreferences[preferenceCat][preference]
    }
  }

  // Dialect assignment
  flattenedPreferences.primary_dialect_path = selectn('path', dialect)

  return flattenedPreferences
}

const { array, func, object, string } = PropTypes
class AppWrapper extends Component {
  intl = IntlService.instance
  intlBaseKey = 'views'

  static propTypes = {
    // REDUX: actions/dispatch/func
    changeTheme: func.isRequired,
    fetchDialect2: func.isRequired,
    getCurrentUser: func.isRequired,
    nuxeoConnect: func.isRequired,
    // REDUX: reducers/state
    computeDialect2: object.isRequired,
    computeLogin: object.isRequired,
    properties: object.isRequired,
    splitWindowPath: array.isRequired,
    windowPath: string.isRequired,
  }

  static childContextTypes = {
    muiTheme: React.PropTypes.object,
  }

  // react-redux-provide will pass context such as providers (Note: this is only needed for debugging the store atm)
  static contextTypes = {
    providers: PropTypes.object,
  }

  // TODO: The legacy context API will be removed in a future major version.
  // TODO: Use the new context API introduced with version 16.3.
  // TODO: The legacy API will continue working for all 16.x releases.
  // via: https://reactjs.org/docs/legacy-context.html

  /**
   * Pass essential context to all children
   */
  getChildContext() {
    const newContext = {
      muiTheme: this.props.properties.theme.palette,
    }

    return newContext
  }

  constructor(props, context) {
    super(props, context)

    // Connect to Nuxeo
    this.props.nuxeoConnect()
    this.props.getCurrentUser()

    this.state = {
      adminGuideStarted: false,
      dialect: null,<commit_msg>AppWrapper: Added a `TODO` note<commit_after>/*
Copyright 2016 First People's Cultural Council

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.
*/
import React, { Component, PropTypes } from 'react'
// import ReactDOM from 'react-dom'

// REDUX
import { connect } from 'react-redux'
import { changeTheme } from 'providers/redux/reducers/navigation'
import { nuxeoConnect, getCurrentUser } from 'providers/redux/reducers/nuxeo'
import { fetchDialect2 } from 'providers/redux/reducers/fvDialect'

import selectn from 'selectn'

import ProviderHelpers from 'common/ProviderHelpers'
// import StringHelpers from 'common/StringHelpers'
import UIHelpers from 'common/UIHelpers'

// import AuthorizationFilter from 'views/components/Document/AuthorizationFilter'

import AppFrontController from './AppFrontController'

import Shepherd from 'tether-shepherd'

// import FontIcon from 'material-ui/lib/font-icon'
// import Paper from 'material-ui/lib/paper'
// import FlatButton from 'material-ui/lib/flat-button'

import IntlService from 'views/services/intl'

// const getPosition = function getPosition() {
//   const doc = document
//   const w = window
//   let x
//   let y
//   let docEl

//   if (typeof w.pageYOffset === 'number') {
//     x = w.pageXOffset
//     y = w.pageYOffset
//   } else {
//     docEl = doc.compatMode && doc.compatMode === 'CSS1Compat' ? doc.documentElement : doc.body
//     x = docEl.scrollLeft
//     y = docEl.scrollTop
//   }
//   return { x: x, y: y }
// }

/**
 * Finds the xPath for a component, leading up to the 'app-wrapper'.
 * Used as a utility for creating Shepherd tours
 */
function findComponentParents(_el) {
  let parents = []
  let el = _el
  const originalEl = el

  if (!el.getAttribute('data-component-id')) {
    return null
  }

  while (el.parentNode) {
    el = el.parentNode

    if (el.getAttribute('id') === 'app-wrapper') {
      break
    }

    if (el.tagName) {
      const appendClass = el.className ? '.' + el.className.replace(/\s/g, '.').replace(/\:/g, '\\:') : '' // eslint-disable-line
      const appendData = el.getAttribute('data-component-id')
        ? "[data-component-id='" + el.getAttribute('data-component-id') + "']"
        : ''

      parents.push(el.tagName.toLowerCase() + appendClass + appendData)
    }
  }

  parents = parents.reverse()
  parents.push(
    originalEl.tagName.toLowerCase() + "[data-component-id='" + originalEl.getAttribute('data-component-id') + "']"
  )

  return parents.join(' ')
}

const getPreferences = function getPreferences(login, dialect) {
  const preferenceString = selectn('response.properties.preferences', login)
  const parsedPreferences = preferenceString ? JSON.parse(preferenceString) : {}
  const flattenedPreferences = {}

  for (const preferenceCat in parsedPreferences) {
    for (const preference in parsedPreferences[preferenceCat]) {
      flattenedPreferences[preference] = parsedPreferences[preferenceCat][preference]
    }
  }

  // Dialect assignment
  flattenedPreferences.primary_dialect_path = selectn('path', dialect)

  return flattenedPreferences
}

const { array, func, object, string } = PropTypes
class AppWrapper extends Component {
  intl = IntlService.instance
  intlBaseKey = 'views'

  static propTypes = {
    // REDUX: actions/dispatch/func
    changeTheme: func.isRequired,
    fetchDialect2: func.isRequired,
    getCurrentUser: func.isRequired,
    nuxeoConnect: func.isRequired,
    // REDUX: reducers/state
    computeDialect2: object.isRequired,
    computeLogin: object.isRequired,
    properties: object.isRequired,
    splitWindowPath: array.isRequired,
    windowPath: string.isRequired,
  }

  static childContextTypes = {
    muiTheme: React.PropTypes.object,
  }

  // TODO: SEE IF THIS IS AN ISSUE AFTER SWITCH TO REDUX
  // react-redux-provide will pass context such as providers (Note: this is only needed for debugging the store atm)
  static contextTypes = {
    providers: PropTypes.object,
  }

  // TODO: The legacy context API will be removed in a future major version.
  // TODO: Use the new context API introduced with version 16.3.
  // TODO: The legacy API will continue working for all 16.x releases.
  // via: https://reactjs.org/docs/legacy-context.html

  /**
   * Pass essential context to all children
   */
  getChildContext() {
    const newContext = {
      muiTheme: this.props.properties.theme.palette,
    }

    return newContext
  }

  constructor(props, context) {
    super(props, context)

    // Connect to Nuxeo
    this.props.nuxeoConnect()
    this.props.getCurrentUser()

    this.state = {
      adminGuideStarted: false,
      dialect: null,
{'fraction_non_alphanumeric': 0.11287304245050724, 'fraction_numerical': 0.0033487639121441938, 'mean_word_length': 3.455462922334357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5277485', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2997, 'n_words': 858}