CourseImagePicker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 I18n from 'i18n!course_images'
  20. import _ from 'underscore'
  21. import UploadArea from './UploadArea'
  22. import FlickrSearch from '../../shared/FlickrSearch'
  23. import Spinner from 'instructure-ui/lib/components/Spinner'
  24. class CourseImagePicker extends React.Component {
  25. constructor (props) {
  26. super(props);
  27. this.state = {
  28. draggingFile: false
  29. };
  30. this.onDrop = this.onDrop.bind(this);
  31. this.onDragLeave = this.onDragLeave.bind(this);
  32. this.onDragEnter = this.onDragEnter.bind(this);
  33. this.shouldAcceptDrop = this.shouldAcceptDrop.bind(this);
  34. }
  35. onDrop (e) {
  36. this.setState({draggingFile: false});
  37. this.props.handleFileUpload(e, this.props.courseId);
  38. e.preventDefault();
  39. e.stopPropagation();
  40. }
  41. onDragLeave () {
  42. this.setState({draggingFile: false});
  43. }
  44. onDragEnter (e) {
  45. if (this.shouldAcceptDrop(e.dataTransfer)) {
  46. this.setState({draggingFile: true});
  47. e.preventDefault();
  48. e.stopPropagation();
  49. }
  50. }
  51. shouldAcceptDrop (dataTransfer) {
  52. if (dataTransfer) {
  53. return (_.indexOf(dataTransfer.types, 'Files') >= 0);
  54. }
  55. }
  56. render () {
  57. return (
  58. <div className="CourseImagePicker"
  59. onDrop={this.onDrop}
  60. onDragLeave={this.onDragLeave}
  61. onDragOver={this.onDragEnter}
  62. onDragEnter={this.onDragEnter}>
  63. { this.props.uploadingImage ?
  64. <div className="CourseImagePicker__Overlay">
  65. <Spinner title="Loading"/>
  66. </div>
  67. :
  68. null
  69. }
  70. { this.state.draggingFile ?
  71. <div className="DraggingOverlay CourseImagePicker__Overlay">
  72. <div className="DraggingOverlay__Content">
  73. <div className="DraggingOverlay__Icon">
  74. <i className="icon-upload" />
  75. </div>
  76. <div className="DraggingOverlay__Instructions">
  77. {I18n.t('Drop Image')}
  78. </div>
  79. </div>
  80. </div>
  81. :
  82. null
  83. }
  84. <div className="ic-Action-header CourseImagePicker__Header">
  85. <h3 className="ic-Action-header__Heading">{I18n.t('Change Image')}</h3>
  86. <div className="ic-Action-header__Secondary">
  87. <button
  88. className="CourseImagePicker__CloseBtn"
  89. onClick={this.props.handleClose}
  90. type="button"
  91. >
  92. <i className="icon-x" />
  93. <span className="screenreader-only">
  94. {I18n.t('Close')}
  95. </span>
  96. </button>
  97. </div>
  98. </div>
  99. <div className="CourseImagePicker__Content">
  100. <UploadArea
  101. courseId={this.props.courseId}
  102. handleFileUpload={this.props.handleFileUpload}/>
  103. <FlickrSearch selectImage={(flickrUrl) => this.props.handleFlickrUrlUpload(flickrUrl)} />
  104. </div>
  105. </div>
  106. );
  107. }
  108. }
  109. export default CourseImagePicker