Giter Site home page Giter Site logo

derhuerst / vbb-journey-ui Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 3.0 267 KB

UI component for displaying a journey like in Google Maps.

Home Page: https://github.com/derhuerst/vbb-journey-ui

License: ISC License

JavaScript 76.32% CSS 23.68%
vbb bvg public-transport transit ui component virtual-dom

vbb-journey-ui's Introduction

vbb-journey-ui

virtual-dom UI components for displaying a journey like in Google Maps.

npm version build status ISC-licensed support me via GitHub Sponsors chat with me on Twitter

screenshot of vbb-journey-ui

Installing

npm install vbb-journey-ui

Usage

renderJourney returns a virtual-dom tree, which you can put into the DOM or convert to an HTML string.

const {DateTime} = require('luxon')
const ms = require('ms')
const createVbbClient = require('vbb-hafas')
const createRenderJourney = require('vbb-journey-ui')
const toString = require('virtual-dom-stringify')

const formatTime = (when) => {
	return DateTime.fromJSDate(when, {
		zone: 'Europe/Berlin',
		locale: 'de-DE'
	}).toLocaleString(DateTime.TIME_SIMPLE)
}

const formatDelay = (delay) => {
	if (delay === 0) return null // todo: show +0
	const color = Math.abs(delay) >= 30 ? '#c0392b' : '#27ae60'
	const text = delay < 0
		? '-' + ms(-delay * 1000)
		: '+' + ms(delay * 1000)
	return h('span', {style: {color}}, [text])
}

const renderJourney = createRenderJourney(formatTime, formatDelay, {})

const vbb = createVbbClient('my-awesome-program')
vbb.journeys('900000003201', '900000024101', {results: 1, stopovers: true})
.then((journeys) => {
	const tree = renderJourney(journeys[0])
	console.log(toString(tree))
})
.catch(console.error)

API

createRenderJourney(formatTime, formatDelay, [actions]) => renderJourney
renderJourney(journey, [detailsFor], [actions]) => virtualDomTree

formatTime must be a with the signature (when: Date) => string.

formatDelay must be a with the signature (delay: number) => string.

actions may be an object with the following methods:

  • actions.hideLegDetails(i)
  • actions.showLegDetails(i)
  • actions.selectStop(stopId, [stationId])

journey must be a Friendly Public Transport Format 1.0.1 journey object.

detailsFor may be an array of journey.legs indices that additional info like passed stations shall be shown for.

renderJourney returns a virtual-dom tree.

Contributing

If you have a question or have difficulties using vbb-journey-ui, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.

vbb-journey-ui's People

Contributors

derhuerst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vbb-journey-ui's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.