DashboardCardBox.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 $ from 'jquery'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import DashboardCard from './DashboardCard'
  22. import DraggableDashboardCard from './DraggableDashboardCard'
  23. import DashboardCardBackgroundStore from './DashboardCardBackgroundStore'
  24. import MovementUtils from './MovementUtils'
  25. const DashboardCardBox = React.createClass({
  26. displayName: 'DashboardCardBox',
  27. propTypes: {
  28. courseCards: PropTypes.array,
  29. reorderingEnabled: PropTypes.bool,
  30. hideColorOverlays: PropTypes.bool,
  31. connectDropTarget: PropTypes.func
  32. },
  33. componentWillMount () {
  34. this.setState({
  35. courseCards: this.props.courseCards
  36. });
  37. },
  38. componentDidMount: function(){
  39. DashboardCardBackgroundStore.addChangeListener(this.colorsUpdated);
  40. DashboardCardBackgroundStore.setDefaultColors(this.allCourseAssetStrings());
  41. },
  42. componentWillReceiveProps: function (newProps) {
  43. DashboardCardBackgroundStore.setDefaultColors(this.allCourseAssetStrings());
  44. this.setState({
  45. courseCards: newProps.courseCards
  46. });
  47. },
  48. getDefaultProps: function () {
  49. return {
  50. courseCards: [],
  51. hideColorOverlays: false
  52. };
  53. },
  54. colorsUpdated: function(){
  55. if(this.isMounted()){
  56. this.forceUpdate();
  57. }
  58. },
  59. allCourseAssetStrings: function(){
  60. return this.props.courseCards.map(card => card.assetString);
  61. },
  62. colorForCard: function(assetString){
  63. return DashboardCardBackgroundStore.colorForCourse(assetString);
  64. },
  65. handleColorChange: function(assetString, newColor){
  66. DashboardCardBackgroundStore.setColorForCourse(assetString, newColor);
  67. },
  68. getOriginalIndex (assetString) {
  69. return this.state.courseCards.findIndex(c => c.assetString === assetString);
  70. },
  71. moveCard (assetString, atIndex, cb) {
  72. const cardIndex = this.state.courseCards.findIndex(card => card.assetString === assetString);
  73. let newCards = this.state.courseCards.slice();
  74. newCards.splice(atIndex, 0, newCards.splice(cardIndex, 1)[0]);
  75. newCards = newCards.map((card, index) => {
  76. const newCard = Object.assign({}, card);
  77. newCard.position = index;
  78. return newCard;
  79. });
  80. this.setState({
  81. courseCards: newCards
  82. }, () => {
  83. MovementUtils.updatePositions(this.state.courseCards, window.ENV.current_user_id);
  84. if (typeof cb === 'function') {
  85. cb()
  86. }
  87. });
  88. },
  89. render: function () {
  90. const Component = (this.props.reorderingEnabled) ? DraggableDashboardCard : DashboardCard;
  91. const cards = this.state.courseCards.map((card, index) => {
  92. const position = (card.position != null) ? card.position : this.getOriginalIndex.bind(this, card.assetString)
  93. return (
  94. <Component
  95. key={card.id}
  96. shortName={card.shortName}
  97. originalName={card.originalName}
  98. courseCode={card.courseCode}
  99. id={card.id}
  100. href={card.href}
  101. links={card.links}
  102. term={card.term}
  103. assetString={card.assetString}
  104. backgroundColor={this.colorForCard(card.assetString)}
  105. handleColorChange={this.handleColorChange.bind(this, card.assetString)}
  106. image={card.image}
  107. imagesEnabled={card.imagesEnabled}
  108. reorderingEnabled={this.props.reorderingEnabled}
  109. hideColorOverlays={this.props.hideColorOverlays}
  110. position={position}
  111. currentIndex={index}
  112. moveCard={this.moveCard}
  113. totalCards={this.state.courseCards.length}
  114. />
  115. );
  116. });
  117. const dashboardCardBox = (
  118. <div className="ic-DashboardCard__box">
  119. {cards}
  120. </div>
  121. );
  122. if (this.props.reorderingEnabled) {
  123. const { connectDropTarget } = this.props;
  124. return connectDropTarget(dashboardCardBox);
  125. }
  126. return dashboardCardBox;
  127. }
  128. });
  129. export default DashboardCardBox