Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <bus-tracker></bus-tracker>

<script>
window.googleAPI = new Promise(function(resolve) {
	const script = document.createElement("script");
	script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyD7POAQA-i16Vws48h4yRFVGBZzIExOAJI";
	document.body.appendChild(script);
	script.onload = resolve;
});
</script>
              
            
!

CSS

              
                html,
body {
  height: 100%;
}
body {
  font-family: 'Catamaran', sans-serif;
  background-color: #f2f2f2;
  display: flex;
  flex-direction: column;
  margin: 0;
}
bus-tracker {
  display: flex;
  flex-direction: column;
}
.top {
  flex-grow: 1;
  overflow-y: auto;
  height: 10%;
  display: flex;
  flex-direction: column;
}
.bottom {
  height: 250px;
  position: relative;
}
.gmap {
  width: 100%;
  height: 250px;
  background-color: grey;
}
.header {
  box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, 0.1);
  background-color: #313131;
  color: white;
  min-height: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.2;
}
.header h1 {
  text-align: center;
  font-size: 18px;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin: 0;
}
.route-selected {
  line-height: 1;
  position: absolute;
  z-index: 1;
  text-align: right;
  background: rgba(6, 6, 6, 0.6);
  top: 10px;
  right: 10px;
  padding: 6px 10px;
  color: white;
  border-radius: 2px;
  cursor: pointer;
}
.route-selected small {
  display: block;
  font-size: 14px;
  color: #ddd;
}
.route-selected .error-message {
  font-size: 14px;
  background-color: #FF5722;
  border-radius: 10px;
  padding: 4px 8px 1px;
  margin-top: 5px;
}
.routes-list {
  padding: 20px 0;
  margin: 0;
  overflow-y: auto;
}
.routes-list li {
  list-style: none;
  cursor: pointer;
  background: white;
  border: 1px solid #dedede;
  padding: 8px 8px 6px;
  margin: 1% 2%;
  border-radius: 25px;
  color: #2196F3;
  width: 41%;
  display: inline-flex;
  font-size: 14px;
  line-height: 1.2;
}
.routes-list li:hover {
  border-color: transparent;
  background-color: #008eff;
  color: white;
  box-shadow: 0px 5px 20px 0px rgba(0, 0, 0, 0.2);
}
.routes-list li .check {
  display: none;
}
.routes-list li.active {
  color: #666;
  background-color: #e8e8e8;
}
.routes-list li.active .check {
  display: inline-block;
  margin-left: 5px;
  color: #2cc532;
}
.routes-list li.active:hover {
  border-color: #dedede;
  box-shadow: none;
}
.route-number {
  display: inline-block;
  border-right: 1px solid #dedede;
  padding-right: 5px;
  margin-right: 5px;
  min-width: 18px;
  text-align: right;
}
p {
  text-align: center;
  margin: 0;
  color: #ccc;
  font-size: 14px;
}

              
            
!

JS

              
                import { StacheElement, type } from "//unpkg.com/can@pre/core.mjs";

const proxyUrl = "https://can-cors.herokuapp.com/";
const token = "?key=piRYHjJ5D2Am39C9MxduHgRZc&format=json";
const apiRoot = "http://www.ctabustracker.com/bustime/api/v2/";
const getRoutesEnpoint = apiRoot + "getroutes" + token;
const getVehiclesEndpoint = apiRoot + "getvehicles" + token;

class GoogleMapView extends StacheElement {
  static view = `<div this:to="this.mapElement" class="gmap"></div>`;

  static props = {
    map: type.Any,
    mapElement: type.maybeConvert(HTMLElement),
    vehicles: type.Any,
    markers: type.Any
  };

  connected() {
    googleAPI.then(() => {
      this.map = new google.maps.Map(this.mapElement, {
        zoom: 10,
        center: {
          lat: 41.881,
          lng: -87.623
        }
      });
    });
    this.listenTo("vehicles", (ev, newVehicles) => {
      if (Array.isArray(this.markers)) {
        this.markers.forEach(marker => {
          marker.setMap(null);
        });
        this.markers = null;
      }
      if (newVehicles) {
        this.markers = newVehicles.map(vehicle => {
          return new google.maps.Marker({
            position: {
              lat: parseFloat(vehicle.lat),
              lng: parseFloat(vehicle.lon)
            },
            map: this.map
          });
        });
      }
    });
  }
}

customElements.define("google-map-view", GoogleMapView);

class BusTracker extends StacheElement {
  static view = `
    <div class="top">
      <div class="header">
        <h1>{{this.title}}</h1>
        {{# if(this.routesPromise.isPending) }}<p>Loading routes…</p>{{/ if }}
        {{# if(this.vehiclesPromise.isPending) }}<p>Loading vehicles…</p>{{/ if }}
      </div>
      <ul class="routes-list">
        {{# for(route of this.routesPromise.value) }}
          <li on:click="this.pickRoute(route)" {{# eq(route, this.route) }}class="active"{{/ eq }}>
            <span class="route-number">{{ route.rt }}</span>
            <span class="route-name">{{ route.rtnm }}</span>
            <span class="check">✔</span>
          </li>
        {{/ for }}
      </ul>
    </div>
    <div class="bottom">
      {{# if(this.route) }}
        <div class="route-selected" on:click="this.pickRoute(route)">
          <small>Route {{ this.route.rt }}:</small> {{ this.route.rtnm }}
          {{# if(this.vehiclesPromise.isRejected) }}
            <div class="error-message">No vehicles available for this route</div>
          {{/ if }}
        </div>
      {{/ if }}
      <google-map-view vehicles:from="this.vehiclesPromise.value"/>
    </div>
  `;

  static props = {
    title: {
      default: "Chicago CTA Bus Tracker"
    },

    routesPromise: {
      get default() {
        return fetch(proxyUrl + getRoutesEnpoint)
          .then(response => response.json())
          .then(data => data["bustime-response"].routes);
      }
    },

    route: type.Any,
    vehiclesPromise: type.Any
  };

  pickRoute(route) {
    this.route = route;
    this.vehiclesPromise = fetch(
      proxyUrl + getVehiclesEndpoint + "&rt=" + route.rt
    )
      .then(response => response.json())
      .then(data => {
        if (data["bustime-response"].error) {
          return Promise.reject(data["bustime-response"].error[0]);
        } else {
          return data["bustime-response"].vehicle;
        }
      });
  }
}

customElements.define("bus-tracker", BusTracker);

              
            
!
999px

Console