All Downloads are FREE. Search and download functionalities are using the official Maven repository.

webapp.scripts.services.cubismService.js Maven / Gradle / Ivy

The newest version!
Box.Application.addService('cubism-graphs', function(application) {
  'use strict';
  var d3        = application.getGlobal('cubism');
  var cubism    = application.getGlobal('cubism');
  var windowSvc = application.getService('window');
  var utils = application.getService('utils');
  var cubismContext = cubism.context();
  var focusEvents   = {};

  cubismContext.on('focus', function(index) {
    for (var moduleId in focusEvents) {
      focusEvents[moduleId].call(cubismContext, index);
    }
  }, cubismContext);

  // TODO: this may need debouncing, each graph will call this on it's reset
  function resetCubismContext(step, widthInPx, timescale) {
    widthInPx = (typeof widthInPx === 'number') ? widthInPx : $(window).width();
    timescale = (typeof timescale === 'number') ? timescale : windowSvc.getTimescale();
    step = (typeof step === 'number') ? step : utils.getStep(timescale, widthInPx);

    cubismContext.step(step).size(widthInPx);
    application.broadcast("cubism-context-reset");
  }

  // create a context when service initializes
  //FIXME service should not be dependent on the DOM on browser state
  resetCubismContext();

  // TODO should we only wrap cubism context handling into this service
  // or should we set default configurations such as the axis layout?
  return {
    resetCubismContext: resetCubismContext,
    createGraphAxis: function () {
      return cubismContext.axis();
    },
    createRulerOverGraphs: function() {
      return cubismContext.rule();
    },
    onFocus: function(eventHandler, moduleId) {
      // there can only be one focus event per context:
      // event cache will take care of executing all events
      focusEvents[moduleId] = eventHandler;
    },
    removeOnFocus: function(moduleId) {
      delete focusEvents[moduleId];
    },
    createMetrics: function(dataSourceRequest, name) {
      return cubismContext.metric(dataSourceRequest, name);
    },
    createHorizonGraph: function() {
      return cubismContext.horizon();
    },
    start: function() {
      return cubismContext.start();
    },
    stop: function() {
      return cubismContext.stop();
    },
    xToTime: function(pageX) {
      var timeStart = cubismContext.scale.domain()[0].getTime();
      return timeStart + (pageX * cubismContext.step());
    }
  };
});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy