CourseImageSelector.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Modal from 'react-modal'
  20. import I18n from 'i18n!course_images'
  21. import Actions from '../actions'
  22. import CourseImagePicker from './CourseImagePicker'
  23. import Spinner from 'instructure-ui/lib/components/Spinner'
  24. const modalOverrides = {
  25. content : {
  26. position: 'absolute',
  27. top: '0',
  28. left: '0',
  29. right: '0',
  30. bottom: '0',
  31. border: 'none',
  32. padding: '12px',
  33. maxWidth: '1420px',
  34. borderRadius: '0',
  35. background: '#ffffff'
  36. }
  37. };
  38. class CourseImageSelector extends React.Component {
  39. constructor (props) {
  40. super(props);
  41. this.state = props.store.getState();
  42. this.handleChange = this.handleChange.bind(this);
  43. this.handleModalClose = this.handleModalClose.bind(this);
  44. this.changeImage = this.changeImage.bind(this);
  45. this.removeImage = this.removeImage.bind(this);
  46. }
  47. componentWillMount () {
  48. this.props.store.subscribe(this.handleChange);
  49. this.props.store.dispatch(Actions.getCourseImage(this.props.courseId));
  50. this.setState({gettingImage: true});
  51. }
  52. handleChange () {
  53. this.setState(this.props.store.getState());
  54. }
  55. handleModalClose () {
  56. this.props.store.dispatch(Actions.setModalVisibility(false));
  57. }
  58. changeImage() {
  59. this.props.store.dispatch(Actions.setModalVisibility(true));
  60. }
  61. removeImage() {
  62. this.props.store.dispatch(Actions.putRemoveImage(this.props.courseId));
  63. }
  64. imageControls () {
  65. if (this.state.imageUrl) {
  66. return (
  67. <div>
  68. <div className="al-dropdown__container CourseImageSelector__EditDropdown">
  69. <a className="al-trigger" role="button" href="#">
  70. <i className="icon-settings"></i>
  71. <i className="icon-mini-arrow-down"></i>
  72. <span className="screenreader-only">{I18n.t('Course image settings')}</span>
  73. </a>
  74. <ul id="courseImage-editDropdown-1"
  75. ref="editDropdown"
  76. className="al-options"
  77. role="menu"
  78. tabIndex="0"
  79. aria-hidden="true"
  80. aria-expanded="false"
  81. aria-activedescendant="courseImage-editDropdown-2"
  82. >
  83. <li role="presentation">
  84. <a href="#"
  85. onClick={() => this.changeImage()}
  86. className="icon-compose icon-Line"
  87. id="courseImage-editDropdown-2"
  88. tabIndex="-1"
  89. ref="changeImage"
  90. role="menuitem"
  91. >
  92. {I18n.t('Change image')}
  93. </a>
  94. </li>
  95. <li role="presentation">
  96. <a href="#"
  97. onClick={() => this.removeImage()}
  98. className="icon-trash"
  99. id="courseImage-editDropdown-3"
  100. tabIndex="-1"
  101. ref="removeImage"
  102. role="menuitem"
  103. >
  104. {I18n.t('Remove image')}
  105. </a>
  106. </li>
  107. </ul>
  108. </div>
  109. </div>
  110. );
  111. }
  112. else {
  113. return (
  114. <button
  115. className="Button"
  116. type="button"
  117. onClick={() => this.props.store.dispatch(Actions.setModalVisibility(true))}
  118. >
  119. {I18n.t('Choose Image')}
  120. </button>
  121. );
  122. }
  123. }
  124. render () {
  125. const styles = {
  126. backgroundImage: `url(${this.state.imageUrl})`
  127. };
  128. return (
  129. <div>
  130. <div
  131. className="CourseImageSelector"
  132. style={(this.state.imageUrl) ? styles : {}}
  133. >
  134. { this.state.gettingImage || this.state.removingImage ?
  135. <div className="CourseImageSelector__Overlay">
  136. <Spinner title="Loading" size="small" />
  137. </div>
  138. :
  139. this.imageControls()
  140. }
  141. </div>
  142. <Modal
  143. isOpen={this.state.showModal}
  144. onRequestClose={this.handleModalClose}
  145. style={modalOverrides}
  146. >
  147. <CourseImagePicker
  148. courseId={this.props.courseId}
  149. handleClose={this.handleModalClose}
  150. handleFileUpload={(e, courseId) => this.props.store.dispatch(Actions.uploadFile(e, courseId))}
  151. handleFlickrUrlUpload={(flickrUrl) => this.props.store.dispatch(Actions.uploadFlickrUrl(flickrUrl, this.props.courseId))}
  152. uploadingImage={this.state.uploadingImage}
  153. />
  154. </Modal>
  155. </div>
  156. );
  157. }
  158. };
  159. export default CourseImageSelector