DashboardOptionsMenu.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import PropTypes from 'prop-types'
  20. import I18n from 'i18n!dashboard'
  21. import axios from 'axios'
  22. import ScreenReaderContent from 'instructure-ui/lib/components/ScreenReaderContent'
  23. import PopoverMenu from 'instructure-ui/lib/components/PopoverMenu'
  24. import { MenuItem, MenuItemGroup, MenuItemSeparator } from 'instructure-ui/lib/components/Menu'
  25. import Button from 'instructure-ui/lib/components/Button'
  26. import IconSettings2Line from 'instructure-icons/lib/Line/IconSettings2Line'
  27. import {sharedDashboardInstance} from '../dashboardPlannerHelper'
  28. export default class DashboardOptionsMenu extends React.Component {
  29. static propTypes = {
  30. recent_activity_dashboard: PropTypes.bool,
  31. hide_dashcard_color_overlays: PropTypes.bool,
  32. planner_enabled: PropTypes.bool,
  33. planner_selected: PropTypes.bool,
  34. onDashboardChange: PropTypes.func
  35. }
  36. static defaultProps = {
  37. hide_dashcard_color_overlays: false,
  38. planner_enabled: false,
  39. planner_selected: false,
  40. recent_activity_dashboard: false,
  41. onDashboardChange: () => {}
  42. }
  43. constructor (props) {
  44. super(props)
  45. sharedDashboardInstance.init(this.changeToCardView)
  46. let view;
  47. if (props.planner_enabled && props.planner_selected) {
  48. view = ['planner']
  49. } else if (props.recent_activity_dashboard) {
  50. view = ['activity']
  51. } else {
  52. view = ['cards']
  53. }
  54. this.state = {
  55. view,
  56. colorOverlays: props.hide_dashcard_color_overlays ? [] : ['colorOverlays']
  57. }
  58. }
  59. changeToCardView = () => {
  60. this.setState({view: ['cards']}, () => {
  61. this.toggleDashboardView(this.state.view)
  62. this.postDashboardToggle()
  63. })
  64. }
  65. handleViewOptionSelect = (e, newSelected) => {
  66. if (newSelected.length === 0) {
  67. return
  68. }
  69. this.setState({view: newSelected}, () => {
  70. this.toggleDashboardView(this.state.view)
  71. this.postDashboardToggle()
  72. })
  73. }
  74. handleColorOverlayOptionSelect = (e, newSelected) => {
  75. this.setState({
  76. colorOverlays: newSelected
  77. }, this.toggleColorOverlays)
  78. this.postToggleColorOverlays()
  79. }
  80. toggleDashboardView (newView) {
  81. const fakeObj = {
  82. style: {}
  83. }
  84. const dashboardPlanner = document.getElementById('dashboard-planner') || fakeObj
  85. const dashboardPlannerHeader = document.getElementById('dashboard-planner-header') || fakeObj
  86. const dashboardActivity = document.getElementById('dashboard-activity')
  87. const dashboardCards = document.getElementById('DashboardCard_Container')
  88. const rightSideContent = document.getElementById('right-side-wrapper') || fakeObj
  89. if (newView[0] === 'planner') {
  90. dashboardPlanner.style.display = 'block'
  91. dashboardPlannerHeader.style.display = 'block'
  92. dashboardActivity.style.display = 'none'
  93. dashboardCards.style.display = 'none'
  94. rightSideContent.style.display = 'none'
  95. } else if (newView[0] === 'activity') {
  96. dashboardPlanner.style.display = 'none'
  97. dashboardPlannerHeader.style.display = 'none'
  98. dashboardActivity.style.display = 'block'
  99. dashboardCards.style.display = 'none'
  100. rightSideContent.style.display = 'block'
  101. } else {
  102. dashboardPlanner.style.display = 'none'
  103. dashboardPlannerHeader.style.display = 'none'
  104. dashboardActivity.style.display = 'none'
  105. dashboardCards.style.display = 'block'
  106. rightSideContent.style.display = 'block'
  107. }
  108. this.props.onDashboardChange(newView[0]);
  109. }
  110. toggleColorOverlays () {
  111. const dashcardHeaders = Array.from(document.getElementsByClassName('ic-DashboardCard__header'))
  112. dashcardHeaders.forEach((dashcardHeader) => {
  113. const dashcardImageHeader = dashcardHeader.getElementsByClassName('ic-DashboardCard__header_image')[0]
  114. if (dashcardImageHeader) {
  115. const dashcardOverlay = dashcardImageHeader.getElementsByClassName('ic-DashboardCard__header_hero')[0]
  116. dashcardOverlay.style.opacity = this.colorOverlays ? 0.6 : 0
  117. const headerButtonBg = dashcardHeader.getElementsByClassName('ic-DashboardCard__header-button-bg')[0]
  118. headerButtonBg.style.opacity = this.colorOverlays ? 0 : 1
  119. }
  120. })
  121. }
  122. postDashboardToggle () {
  123. axios.put('/dashboard/view', {
  124. dashboard_view: this.state.view[0]
  125. })
  126. }
  127. postToggleColorOverlays () {
  128. axios.post('/users/toggle_hide_dashcard_color_overlays');
  129. }
  130. get cardView () {
  131. return this.state.view.includes('cards')
  132. }
  133. get colorOverlays () {
  134. return this.state.colorOverlays.includes('colorOverlays')
  135. }
  136. render () {
  137. const cardView = this.cardView
  138. return (
  139. <PopoverMenu
  140. trigger={
  141. <Button variant="icon">
  142. <ScreenReaderContent>{I18n.t('Dashboard Options')}</ScreenReaderContent>
  143. <IconSettings2Line />
  144. </Button>
  145. }
  146. contentRef={(el) => { this.menuContentRef = el; }}
  147. >
  148. <MenuItemGroup
  149. label={I18n.t('Dashboard View')}
  150. onSelect={this.handleViewOptionSelect}
  151. selected={this.state.view}
  152. >
  153. <MenuItem value="cards">{I18n.t('Card View')}</MenuItem>
  154. {
  155. (this.props.planner_enabled) && (
  156. <MenuItem value="planner">{I18n.t('List View')}</MenuItem>
  157. )
  158. }
  159. <MenuItem value="activity">{I18n.t('Recent Activity')}</MenuItem>
  160. </MenuItemGroup>
  161. { cardView && <MenuItemSeparator /> }
  162. { cardView && (
  163. <MenuItemGroup
  164. label={
  165. <ScreenReaderContent>
  166. {I18n.t('Toggle course card color overlays')}
  167. </ScreenReaderContent>
  168. }
  169. onSelect={this.handleColorOverlayOptionSelect}
  170. selected={this.state.colorOverlays}
  171. >
  172. <MenuItem value="colorOverlays">
  173. { I18n.t('Color Overlay') }
  174. </MenuItem>
  175. </MenuItemGroup>
  176. )}
  177. </PopoverMenu>
  178. )
  179. }
  180. }