import React, { Component } from 'react' import PropTypes from 'prop-types' import _ from 'lodash' import VehicleJourney from './VehicleJourney' import StopAreaHeaderManager from '../../helpers/stop_area_header_manager' export default class VehicleJourneys extends Component { constructor(props){ super(props) this.headerManager = new StopAreaHeaderManager( _.map(this.stopPoints(), (sp)=>{return sp.object_id}), this.stopPoints(), this.props.filters.features ) this.togglePurchaseWindows = this.togglePurchaseWindows.bind(this) this.toggleTimetables = this.toggleTimetables.bind(this) } isReturn() { return this.props.routeUrl != undefined } vehicleJourneysList() { if(this.isReturn()){ return this.props.returnVehicleJourneys } else{ return this.props.vehicleJourneys } } stopPoints() { if(this.isReturn()){ return this.props.returnStopPointsList } else{ return this.props.stopPointsList } } componentDidMount() { this.props.onLoadFirstPage(this.props.filters, this.props.routeUrl) } hasFeature(key) { return this.props.filters.features[key] } showHeader(object_id) { return this.headerManager.showHeader(object_id) } getPurchaseWindowsAndTimeTables(){ let timetables_keys = [] let windows_keys = [] this._allTimeTables = [] this._allPurchaseWindows = [] this.vehicleJourneysList().map((vj, index) => { vj.time_tables.map((tt, _) => { if(timetables_keys.indexOf(tt.id) < 0){ timetables_keys.push(tt.id) this._allTimeTables.push(tt) } }) vj.purchase_windows.map((tt, _) => { if(windows_keys.indexOf(tt.id) < 0){ windows_keys.push(tt.id) this._allPurchaseWindows.push(tt) } }) }) } allTimeTables() { if(! this._allTimeTables){ this.getPurchaseWindowsAndTimeTables() } return this._allTimeTables } allPurchaseWindows() { if(!this._allPurchaseWindows){ this.getPurchaseWindowsAndTimeTables() } return this._allPurchaseWindows } toggleTimetables(e) { let root = $(this.refs['vehicleJourneys']) root.find('.table-2entries .detailed-timetables').toggleClass('hidden') root.find('.table-2entries .detailed-timetables-bt').toggleClass('active') this.componentDidUpdate() e.preventDefault() false } togglePurchaseWindows(e) { let root = $(this.refs['vehicleJourneys']) root.find('.table-2entries .detailed-purchase-windows').toggleClass('hidden') root.find('.table-2entries .detailed-purchase-windows-bt').toggleClass('active') this.componentDidUpdate() e.preventDefault() false } componentDidUpdate(prevProps, prevState) { if(this.props.status.isFetching == false){ $('.table-2entries').each(function() { $(this).find('.th').css('height', 'auto') var refH = [] var refCol = [] $(this).find('.t2e-head').children('div').each(function() { var h = this.getBoundingClientRect().height; refH.push(h) }); var i = 0 $(this).find('.t2e-item').children('div').each(function() { var h = this.getBoundingClientRect().height; if(refCol.length < refH.length){ refCol.push(h) } else { if(h > refCol[i]) { refCol[i] = h } } if(i == (refH.length - 1)){ i = 0 } else { i++ } }); for(var n = 0; n < refH.length; n++) { if(refCol[n] < refH[n]) { refCol[n] = refH[n] } } $(this).find('.th').css('height', refCol[0]); for(var nth = 1; nth < refH.length; nth++) { $(this).find('.td:nth-child('+ (nth + 1) +')').css('height', refCol[nth]); } }); } } timeTableURL(tt) { let refURL = window.location.pathname.split('/', 3).join('/') let ttURL = refURL + '/time_tables/' + tt.id return ( {tt.days || tt.comment} ) } purchaseWindowURL(tt) { let refURL = window.location.pathname.split('/', 3).join('/') let ttURL = refURL + '/purchase_windows/' + tt.id return ( {tt.name} ) } extraHeaderLabel(header) { if(header["type"] == "custom_field"){ return this.props.customFields[header["name"]]["name"] } else{ return I18n.attribute_name("vehicle_journey", header) } } render() { this.previousBreakpoint = undefined this._allTimeTables = null this._allPurchaseWindows = null let detailed_calendars = this.hasFeature('detailed_calendars') && !this.isReturn() && (this.allTimeTables().length > 0) let detailed_purchase_windows = this.hasFeature('detailed_purchase_windows') && !this.isReturn() && (this.allPurchaseWindows().length > 0) if(this.props.status.isFetching == true) { return (
{this.purchaseWindowURL(tt)}
{tt.bounding_dates.join(' > ')}
{this.timeTableURL(tt)}
{tt.bounding_dates.split(' ').join(' > ')}