Home Reference Source

src/index.js

import Map from 'map';
const supportedAPI = ['init', 'sdk'];
/**
    The main entry of the application
*/

/**
    Method that handles all API calls
    */
function apiHandler(api, params) {
  if (!api) throw Error('API method required');
  api = api.toLowerCase();
  if (supportedAPI.indexOf(api) === -1) throw Error(`Method ${api} is not supported`);

  console.log(`Handling API call ${api}`);

  switch (api) {
    // TODO: add API implementation
    case 'sdk':
      console.log('sdk');
      window.freeSeasMap = Map;
      break;
    default:
      console.warn(`No handler defined for ${api}`);
  }
}

function app(window) {

  // set default configurations
  let configurations = {
  };

  // all methods that were called till now and stored in queue
  // needs to be called now
  let globalObject = window[window['JS-Widget']];
  let queue = globalObject.q;

  if (queue) {
    for (var i = 0; i < queue.length; i++) {
      if (queue[i][0].toLowerCase() === 'init') {
        configurations = queue[i][1];
        let freeSeasMap = new Map(configurations);

        freeSeasMap.render();
      } else {
        apiHandler(queue[i][0], queue[i][1]);
      }
    }
  }

  // override temporary (until the app loaded) handler
  // for widget's API calls
  globalObject = apiHandler;
  globalObject.configurations = configurations;
}

app(window);

export default Map;