ThemeEditorImageRow.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2015 - 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 customTypes from './PropTypes'
  21. import I18n from 'i18n!theme_editor'
  22. // consider anything other than null or undefined (including '') as "set"
  23. function isSet(val) {
  24. return val === null || val === undefined
  25. }
  26. export default React.createClass({
  27. displayName: 'ThemeEditorImageRow',
  28. propTypes: {
  29. varDef: customTypes.image,
  30. userInput: customTypes.userVariableInput,
  31. onChange: PropTypes.func.isRequired,
  32. currentValue: PropTypes.string,
  33. placeholder: PropTypes.string
  34. },
  35. getDefaultProps(){
  36. return {
  37. userInput: {}
  38. }
  39. },
  40. // valid input: null, '', or an HTMLInputElement
  41. setValue(inputElementOrNewValue) {
  42. var chosenValue = inputElementOrNewValue
  43. if (!inputElementOrNewValue) { //if it's null or ''
  44. // if they hit the "Undo" or "Use Default" button,
  45. // we want to also clear out the value of the <input type=file>
  46. // but we don't want to mess with its value otherwise
  47. this.refs.fileInput.getDOMNode().value = ''
  48. } else {
  49. chosenValue = window.URL.createObjectURL(inputElementOrNewValue.files[0])
  50. }
  51. this.props.onChange(chosenValue)
  52. },
  53. render() {
  54. var inputName = 'brand_config[variables][' + this.props.varDef.variable_name + ']'
  55. var imgSrc = this.props.userInput.val || this.props.placeholder
  56. return (
  57. <section className="Theme__editor-accordion_element Theme__editor-upload">
  58. <div className="te-Flex">
  59. <div className="Theme__editor-form--upload">
  60. <div className="Theme__editor-upload_header">
  61. <h4 className="Theme__editor-upload_title" >
  62. { this.props.varDef.human_name }
  63. </h4>
  64. <span className="Theme__editor-upload_restrictions">
  65. { this.props.varDef.helper_text }
  66. </span>
  67. </div>
  68. <div className={'Theme__editor_preview-img-container Theme__editor_preview-img-container--' + this.props.varDef.variable_name}>
  69. {/* ^ this utility class is to control the background color that shows behind the images you can customize in theme editor - see theme_editor.scss */}
  70. <div className="Theme__editor_preview-img">
  71. { imgSrc && <img src={imgSrc} className="Theme__editor-placeholder" alt="" /> }
  72. </div>
  73. </div>
  74. <div className="Theme__editor-image_upload">
  75. <input
  76. type="hidden"
  77. name={!this.props.userInput.val && inputName}
  78. value={(this.props.userInput.val === '') ? '' : this.props.currentValue}
  79. />
  80. <label className="Theme__editor-image_upload-label">
  81. <span className="screenreader-only">
  82. { this.props.varDef.human_name }
  83. </span>
  84. <input
  85. type="file"
  86. className="Theme__editor-input_upload"
  87. name={this.props.userInput.val && inputName}
  88. accept={this.props.varDef.accept}
  89. onChange={event => this.setValue(event.target)}
  90. ref="fileInput"
  91. />
  92. <span className="Theme__editor-button_upload Button Button--link" aria-hidden="true">
  93. { I18n.t('Select Image') }
  94. </span>
  95. </label>
  96. {this.props.userInput.val || this.props.currentValue ? (
  97. <button
  98. type="button"
  99. className="Button Button--link"
  100. onClick={() => this.setValue(isSet(this.props.userInput.val) ? '' : null)}
  101. >
  102. { isSet(this.props.userInput.val) ? I18n.t('Use Default') : I18n.t('Undo') }
  103. </button>
  104. ) : (
  105. null
  106. )}
  107. </div>
  108. </div>
  109. </div>
  110. </section>
  111. )
  112. }
  113. })