LockManager.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2017 - 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 I18n from 'i18n!blueprint_courses'
  19. import $ from 'jquery'
  20. import React from 'react'
  21. import ReactDOM from 'react-dom'
  22. import 'jquery.instructure_misc_plugins'
  23. import get from 'lodash/get'
  24. import buildProps from '../buildLockProps'
  25. import ApiClient from '../apiClient'
  26. import LockBanner from '../components/LockBanner'
  27. import LockToggle from '../components/LockToggle'
  28. export default class LockManager {
  29. constructor () {
  30. this.state = {
  31. isLocked: false,
  32. itemLocks: [],
  33. isMasterContent: false,
  34. isChildContent: false,
  35. itemId: ''
  36. }
  37. }
  38. init (options) {
  39. if (!this.shouldInit()) return;
  40. this.props = buildProps(options)
  41. this.setupState()
  42. if (this.state.itemId !== undefined) { // will be undefined if creating a new assignment, discussion, etc.
  43. this.render()
  44. }
  45. }
  46. shouldInit () {
  47. return ENV.MASTER_COURSE_DATA &&
  48. (ENV.MASTER_COURSE_DATA.is_master_course_master_content ||
  49. ENV.MASTER_COURSE_DATA.is_master_course_child_content)
  50. }
  51. setupState () {
  52. this.state = {
  53. isLocked: ENV.MASTER_COURSE_DATA.restricted_by_master_course,
  54. itemLocks: ENV.MASTER_COURSE_DATA.master_course_restrictions || ENV.MASTER_COURSE_DATA.default_restrictions,
  55. isMasterContent: ENV.MASTER_COURSE_DATA.is_master_course_master_content,
  56. isChildContent: ENV.MASTER_COURSE_DATA.is_master_course_child_content,
  57. courseId: ENV.COURSE_ID,
  58. itemId: get(ENV, this.props.itemIdPath),
  59. }
  60. }
  61. setState (newState) {
  62. this.state = Object.assign(this.state, newState)
  63. this.render()
  64. }
  65. getItemLocks () {
  66. return { ...this.state.itemLocks }
  67. }
  68. isMasterContent () {
  69. return this.state.isMasterContent
  70. }
  71. isChildContent () {
  72. return this.state.isChildContent
  73. }
  74. toggleLocked = () => {
  75. const { itemType } = this.props
  76. const { courseId, isLocked, itemId } = this.state
  77. ApiClient.toggleLocked({ courseId, itemType, itemId, isLocked: !isLocked })
  78. .then((res) => {
  79. if (res.data.success) {
  80. this.setState({
  81. isLocked: !isLocked,
  82. })
  83. } else {
  84. this.showToggleError()
  85. }
  86. })
  87. .catch(() => {
  88. this.showToggleError()
  89. })
  90. }
  91. showToggleError () {
  92. $.flashError(I18n.t('There was a problem toggling the content lock.'))
  93. }
  94. setupToggle (cb) {
  95. if (!this.props.toggleWrapperSelector) return;
  96. if (!this.toggleNode) {
  97. LockToggle.setupRootNode(this.props.toggleWrapperSelector, this.props.toggleWrapperChildIndex || 0, (node) => {
  98. this.toggleNode = node
  99. cb()
  100. })
  101. } else {
  102. cb()
  103. }
  104. }
  105. renderLockToggle () {
  106. if (!this.props.toggleWrapperSelector) return;
  107. this.setupToggle(() => {
  108. ReactDOM.render(
  109. <LockToggle
  110. isLocked={this.state.isLocked}
  111. isToggleable={this.props.page === 'show' && this.state.isMasterContent}
  112. onClick={this.toggleLocked}
  113. />, this.toggleNode)
  114. })
  115. }
  116. renderBanner () {
  117. if (!this.bannerNode) this.bannerNode = LockBanner.setupRootNode()
  118. ReactDOM.render(
  119. <LockBanner
  120. isLocked={this.state.isLocked}
  121. itemLocks={this.state.itemLocks}
  122. />, this.bannerNode)
  123. }
  124. render () {
  125. this.renderBanner()
  126. this.renderLockToggle()
  127. }
  128. }