github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/asset_client/src/components/data.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const m = require('mithril')
    20  const Chart = require('chart.js')
    21  const GoogleMapsLoader = require('google-maps')
    22  const modals = require('./modals')
    23  const api = require('../services/api')
    24  
    25  GoogleMapsLoader.KEY = null
    26  let google = null
    27  
    28  // If maps key is missing, asks server for it, and then finally the user
    29  const setMapsApiKey = () => {
    30    return Promise.resolve()
    31      .then(() => {
    32        if (GoogleMapsLoader.KEY) return
    33        return api.get('info')
    34          .then(({ mapsApiKey }) => {
    35            if (mapsApiKey) {
    36              GoogleMapsLoader.KEY = mapsApiKey
    37              return
    38            }
    39  
    40            return modals.show(modals.BasicModal, {
    41              title: 'No API Key',
    42              acceptText: 'Set Key',
    43              body: m('.container', [
    44                m('.mb-4',
    45                  'Oh no! This server has not been configured with an API key ',
    46                  'to use Google Maps. Fortunately, you can easily ',
    47                  m('a', {
    48                    href: 'https://developers.google.com/maps/documentation/javascript/get-api-key',
    49                    target: '_blank'
    50                  }, 'request a free developer key from Google'),
    51                  ' and input it into the field below'),
    52                m('input.form-control', {
    53                  type: 'text',
    54                  oninput: m.withAttr('value', value => { mapsApiKey = value })
    55                })
    56              ])
    57            })
    58              .then(() => {
    59                GoogleMapsLoader.KEY = mapsApiKey
    60                api.post('info/mapsApiKey', { mapsApiKey })
    61              })
    62              .catch(() => {})
    63          })
    64      })
    65  }
    66  
    67  const LineGraphWidget = {
    68    view (vnode) {
    69      return m('canvas#graph-container', { width: '100%' })
    70    },
    71  
    72    parseUpdates (updates) {
    73      return updates.map(d => ({
    74        t: d.timestamp * 1000,
    75        y: d.value,
    76        reporter: d.reporter.name
    77      }))
    78    },
    79  
    80    oncreate (vnode) {
    81      const ctx = document.getElementById('graph-container').getContext('2d')
    82  
    83      vnode.state.graph = new Chart(ctx, {
    84        type: 'line',
    85        data: {
    86          datasets: [{
    87            data: this.parseUpdates(vnode.attrs.updates),
    88            fill: false,
    89            pointStyle: 'triangle',
    90            pointRadius: 8,
    91            borderColor: '#ff0000',
    92            lineTension: 0
    93          }]
    94        },
    95        options: {
    96          legend: {
    97            display: false
    98          },
    99          tooltips: {
   100            bodyFontSize: 14,
   101            displayColors: false,
   102            custom: model => {
   103              if (model.body) {
   104                const index = model.dataPoints[0].index
   105                const reporter = vnode.state.graph.data.datasets[0]
   106                  .data[index].reporter
   107                const value = model.body[0].lines[0]
   108                model.body[0].lines[0] = `${value} (from ${reporter})`
   109              }
   110            }
   111          },
   112          responsive: true,
   113          scales: {
   114            xAxes: [{
   115              type: 'time',
   116              offset: true,
   117              display: true,
   118              time: {
   119                minUnit: 'second',
   120                tooltipFormat: 'MM/DD/YYYY, h:mm:ss a'
   121              },
   122              ticks: {
   123                major: {
   124                  fontStyle: 'bold',
   125                  fontColor: '#ff0000'
   126                }
   127              }
   128            }],
   129            yAxes: [{
   130              type: 'linear',
   131              offset: true,
   132              display: true
   133            }]
   134          }
   135        }
   136      })
   137    },
   138  
   139    onupdate (vnode) {
   140      const data = this.parseUpdates(vnode.attrs.updates)
   141      vnode.state.graph.data.datasets[0].data = data
   142      vnode.state.graph.update()
   143    }
   144  }
   145  
   146  const MapWidget = {
   147    view (vnode) {
   148      return m('#map-container')
   149    },
   150  
   151    oncreate (vnode) {
   152      setMapsApiKey()
   153        .then(() => {
   154          GoogleMapsLoader.load(goog => {
   155            google = goog
   156            const coordinates = vnode.attrs.coordinates.map(coord => ({
   157              lat: coord.latitude,
   158              lng: coord.longitude
   159            }))
   160  
   161            const container = document.getElementById('map-container')
   162            vnode.state.map = new google.maps.Map(container, { zoom: 4 })
   163            vnode.state.markers = coordinates.map(position => {
   164              return new google.maps.Marker({ position, map: vnode.state.map })
   165            })
   166  
   167            vnode.state.path = new google.maps.Polyline({
   168              map: vnode.state.map,
   169              path: coordinates,
   170              geodesic: true,
   171              strokeColor: '#FF0000'
   172            })
   173  
   174            vnode.state.bounds = new google.maps.LatLngBounds()
   175            coordinates.forEach(position => vnode.state.bounds.extend(position))
   176            vnode.state.map.fitBounds(vnode.state.bounds)
   177          })
   178        })
   179    },
   180  
   181    onbeforeupdate (vnode, old) {
   182      // Coordinates exist and have changed
   183      return vnode.attrs.coordinates &&
   184        vnode.attrs.coordinates.length !== old.attrs.coordinates.length
   185    },
   186  
   187    onupdate (vnode) {
   188      const coordinates = vnode.attrs.coordinates.map(coord => ({
   189        lat: coord.latitude,
   190        lng: coord.longitude
   191      }))
   192  
   193      vnode.state.markers.forEach(marker => marker.setMap(null))
   194      vnode.state.markers = coordinates.map(position => {
   195        return new google.maps.Marker({ position, map: vnode.state.map })
   196      })
   197  
   198      vnode.state.path.setPath(coordinates)
   199      coordinates.forEach(position => vnode.state.bounds.extend(position))
   200      vnode.state.map.fitBounds(vnode.state.bounds)
   201    }
   202  }
   203  
   204  module.exports = {
   205    LineGraphWidget,
   206    MapWidget
   207  }