ThemeEditorAccordion.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 I18n from 'i18n!theme_editor'
  21. import ThemeEditorColorRow from './ThemeEditorColorRow'
  22. import ThemeEditorImageRow from './ThemeEditorImageRow'
  23. import RangeInput from './RangeInput'
  24. import customTypes from './PropTypes'
  25. import $ from 'jquery'
  26. import 'jqueryui/accordion'
  27. const activeIndexKey = 'Theme__editor-accordion-index'
  28. export default React.createClass({
  29. displayName: 'ThemeEditorAccordion',
  30. propTypes: {
  31. variableSchema: customTypes.variableSchema,
  32. brandConfigVariables: PropTypes.object.isRequired,
  33. changedValues: PropTypes.object.isRequired,
  34. changeSomething: PropTypes.func.isRequired,
  35. getDisplayValue: PropTypes.func.isRequired
  36. },
  37. componentDidMount() {
  38. this.initAccordion()
  39. },
  40. getStoredAccordionIndex(){
  41. // Note that "Number(null)" returns 0
  42. return Number(window.sessionStorage.getItem(activeIndexKey))
  43. },
  44. setStoredAccordionIndex(index){
  45. window.sessionStorage.setItem(activeIndexKey, index)
  46. },
  47. // Returns the index of the current accordion pane open
  48. getCurrentIndex(){
  49. return $(this.getDOMNode()).accordion('option','active')
  50. },
  51. // Remembers which accordion pane is open
  52. rememberActiveIndex(){
  53. var index = this.getCurrentIndex()
  54. this.setStoredAccordionIndex(index)
  55. },
  56. initAccordion(){
  57. const index = this.getStoredAccordionIndex()
  58. $(this.getDOMNode()).accordion({
  59. active: index,
  60. header: "h3",
  61. heightStyle: "content",
  62. beforeActivate: function ( event, ui) {
  63. var previewIframe = $('#previewIframe');
  64. if ($.trim(ui.newHeader[0].innerText) === 'Login Screen') {
  65. var loginPreview = previewIframe.contents().find('#login-preview');
  66. if (loginPreview) previewIframe.scrollTo(loginPreview);
  67. } else {
  68. previewIframe.scrollTo(0);
  69. }
  70. },
  71. activate: this.rememberActiveIndex
  72. });
  73. },
  74. renderRow(varDef) {
  75. var props = {
  76. key: varDef.variable_name,
  77. currentValue: this.props.brandConfigVariables[varDef.variable_name],
  78. userInput: this.props.changedValues[varDef.variable_name],
  79. onChange: this.props.changeSomething.bind(null, varDef.variable_name),
  80. placeholder: this.props.getDisplayValue(varDef.variable_name),
  81. varDef: varDef
  82. };
  83. switch (varDef.type) {
  84. case 'color':
  85. return <ThemeEditorColorRow {...props} />
  86. case 'image':
  87. return <ThemeEditorImageRow {...props} />
  88. case 'percentage':
  89. const defaultValue = props.currentValue || props.placeholder;
  90. return <RangeInput
  91. key={varDef.variable_name}
  92. labelText={varDef.human_name}
  93. min={0}
  94. max={1}
  95. step={0.1}
  96. defaultValue={defaultValue ? parseFloat(defaultValue) : 0.5}
  97. name={'brand_config[variables][' + varDef.variable_name + ']'}
  98. onChange={value => props.onChange(value)}
  99. formatValue={value => I18n.toPercentage(value * 100, {precision: 0})}
  100. />
  101. default:
  102. return null
  103. }
  104. },
  105. render() {
  106. return (
  107. <div className="accordion ui-accordion--mini Theme__editor-accordion">
  108. {this.props.variableSchema.map(variableGroup =>
  109. [
  110. <h3>
  111. <a href="#">
  112. <div className="te-Flex">
  113. <span className="te-Flex__block">{variableGroup.group_name}</span>
  114. <i className="Theme__editor-accordion-icon icon-mini-arrow-right" />
  115. </div>
  116. </a>
  117. </h3>
  118. ,
  119. <div>
  120. {variableGroup.variables.map(this.renderRow)}
  121. </div>
  122. ]
  123. )}
  124. </div>
  125. )
  126. }
  127. })