QuickRef.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Accordion, AccordionItem, SVGIcon, Link } from 'ca-ui-toolkit';
  4. import './QuickRef.scss';
  5. import isString from 'lodash/isString';
  6. export class QuickRef extends Component {
  7. static propTypes = {
  8. glassContext: PropTypes.object,
  9. itemData: PropTypes.object,
  10. contentData: PropTypes.array
  11. };
  12. static defaultProps = {
  13. itemData: {},
  14. contentData: []
  15. }
  16. itemChanged = false;
  17. constructor(props) {
  18. super(props);
  19. this.data = this._addContentToItem(props.itemData, props.contentData);
  20. this.state = {
  21. openItem: 0
  22. };
  23. }
  24. shouldComponentUpdate() {
  25. if (this.itemChanged) {
  26. return true;
  27. }
  28. }
  29. _addContentToItem = (itemData, contentData) => {
  30. const data = Object.assign({}, itemData);
  31. for (let i=0; i<contentData.length; i++) {
  32. data.panels[contentData[i].panelIndex-1].references.push(contentData[i]);
  33. }
  34. return data;
  35. }
  36. closeOtherTabs = (index) => {
  37. const { openItem } = this.state;
  38. this.itemChanged = true;
  39. if(openItem === index) {
  40. this.setState({ openItem: -1 });
  41. } else {
  42. this.setState({ openItem: index });
  43. }
  44. }
  45. _mapItem = (item,index) => {
  46. return (
  47. <AccordionItem key={index} itemName={item.label} open={index===this.state.openItem ? true : false} onChange={this.closeOtherTabs.bind(this, index)}>
  48. {item.references.map(this._mapContent)}
  49. </AccordionItem>);
  50. }
  51. // TODO: here!!!!!
  52. _mapContent = (reference) => {
  53. if (reference.type === 'video') {
  54. return this._createVideoItem(reference);
  55. } else if (reference.type === 'html'){
  56. return this._createHtmlItem(reference);
  57. } else {
  58. return this._createItem(reference);
  59. }
  60. }
  61. _createVideoItem = (data) => {
  62. return (<Link style={{ display: 'block' }} className='refVideoLink__targetLink' href= {data.href} target='_blank'>
  63. {this._createItemIcon()}{data.label}
  64. </Link>);
  65. }
  66. _createItemIcon = () => {
  67. let svgHref = 'ca_home-video_16';
  68. return (<SVGIcon className='clItemLinkIcon' iconId={svgHref}></SVGIcon>);
  69. }
  70. _createHtmlItem = () => {
  71. // TODO: if glass return html type, what should insert here??
  72. return <div></div>;
  73. }
  74. _createItem = (data) => {
  75. let url = data.href;
  76. let locale = this.props.glassContext && this.props.glassContext.getCoreSvc('.UserProfile').preferences.productLocale;
  77. // inject locale into URL where required
  78. if (isString(url) && isString(locale)) {
  79. url = url.replace(/{locale}/, locale);
  80. }
  81. return (<Link className='refLinkTarget' href={url} target='_blank'>{data.label}</Link>);
  82. }
  83. render() {
  84. const accordionItems = this.data.panels && this.data.panels.map(this._mapItem);
  85. return (
  86. <div className="ca-home-accordion">
  87. <div className='clRefCaption'>{this.data.label}</div>
  88. <Accordion id="accordion-left">
  89. {
  90. accordionItems
  91. }
  92. </Accordion>
  93. </div>
  94. );
  95. }
  96. }