github.com/hzck/speedroute@v0.0.0-20201115191102-403b7d0e443f/old-public/js/angular-vis.js (about)

     1  angular.module('ngVis', [])
     2  
     3      .factory('VisDataSet', function () {
     4          'use strict';
     5          return function (data, options) {
     6              // Create the new dataSets
     7              return new vis.DataSet(data, options);
     8          };
     9      })
    10  
    11  /**
    12   * TimeLine directive
    13   */
    14      .directive('visTimeline', function () {
    15          'use strict';
    16          return {
    17              restrict: 'EA',
    18              transclude: false,
    19              scope: {
    20                  data: '=',
    21                  options: '=',
    22                  events: '='
    23              },
    24              link: function (scope, element, attr) {
    25                  var timelineEvents = [
    26                      'rangechange',
    27                      'rangechanged',
    28                      'timechange',
    29                      'timechanged',
    30                      'select',
    31                      'doubleClick',
    32                      'click',
    33                      'contextmenu'
    34                  ];
    35  
    36                  // Declare the timeline
    37                  var timeline = null;
    38  
    39                  scope.$watch('data', function () {
    40                      // Sanity check
    41                      if (scope.data == null) {
    42                          return;
    43                      }
    44  
    45                      // If we've actually changed the data set, then recreate the graph
    46                      // We can always update the data by adding more data to the existing data set
    47                      if (timeline != null) {
    48                          timeline.destroy();
    49                      }
    50  
    51                      // Create the timeline object
    52                      timeline = new vis.Timeline(element[0], scope.data.items, scope.data.groups, scope.options);
    53  
    54                      // Attach an event handler if defined
    55                      angular.forEach(scope.events, function (callback, event) {
    56                          if (timelineEvents.indexOf(String(event)) >= 0) {
    57                              timeline.on(event, callback);
    58                          }
    59                      });
    60  
    61                      // onLoad callback
    62                      if (scope.events != null && scope.events.onload != null &&
    63                          angular.isFunction(scope.events.onload)) {
    64                          scope.events.onload(timeline);
    65                      }
    66                  });
    67  
    68                  scope.$watchCollection('options', function (options) {
    69                      if (timeline == null) {
    70                          return;
    71                      }
    72                      timeline.setOptions(options);
    73                  });
    74              }
    75          };
    76      })
    77  
    78  /**
    79   * Directive for network chart.
    80   */
    81      .directive('visNetwork', function () {
    82          return {
    83              restrict: 'EA',
    84              transclude: false,
    85              scope: {
    86                  data: '=',
    87                  options: '=',
    88                  events: '='
    89              },
    90              link: function (scope, element, attr) {
    91                  var networkEvents = [
    92                      'click',
    93                      'doubleClick',
    94                      'oncontext',
    95                      'hold',
    96                      'release',
    97                      'selectNode',
    98                      'selectEdge',
    99                      'deselectNode',
   100                      'deselectEdge',
   101                      'dragStart',
   102                      'dragging',
   103                      'dragEnd',
   104                      'hoverNode',
   105                      'blurNode',
   106                      'zoom',
   107                      'showPopup',
   108                      'hidePopup',
   109                      'startStabilizing',
   110                      'stabilizationProgress',
   111                      'stabilizationIterationsDone',
   112                      'stabilized',
   113                      'resize',
   114                      'initRedraw',
   115                      'beforeDrawing',
   116                      'afterDrawing',
   117                      'animationFinished'
   118  
   119                  ];
   120  
   121                  var network = null;
   122  
   123                  scope.$watch('data', function () {
   124                      // Sanity check
   125                      if (scope.data == null) {
   126                          return;
   127                      }
   128  
   129                      // If we've actually changed the data set, then recreate the graph
   130                      // We can always update the data by adding more data to the existing data set
   131                      if (network != null) {
   132                          network.destroy();
   133                      }
   134  
   135                      // Create the graph2d object
   136                      network = new vis.Network(element[0], scope.data, scope.options);
   137  
   138                      // Attach an event handler if defined
   139                      angular.forEach(scope.events, function (callback, event) {
   140                          if (networkEvents.indexOf(String(event)) >= 0) {
   141                              network.on(event, callback);
   142                          }
   143                      });
   144  
   145                      // onLoad callback
   146                      if (scope.events != null && scope.events.onload != null &&
   147                          angular.isFunction(scope.events.onload)) {
   148                          scope.events.onload(network);
   149                      }
   150                  });
   151  
   152                  scope.$watchCollection('options', function (options) {
   153                      if (network == null) {
   154                          return;
   155                      }
   156                      network.setOptions(options);
   157                  });
   158              }
   159          };
   160      })
   161  
   162  /**
   163   * Directive for graph2d.
   164   */
   165      .directive('visGraph2d', function () {
   166          'use strict';
   167          return {
   168              restrict: 'EA',
   169              transclude: false,
   170              scope: {
   171                  data: '=',
   172                  options: '=',
   173                  events: '='
   174              },
   175              link: function (scope, element, attr) {
   176                  var graphEvents = [
   177                      'rangechange',
   178                      'rangechanged',
   179                      'timechange',
   180                      'timechanged',
   181                      'finishedRedraw'
   182                  ];
   183  
   184                  // Create the chart
   185                  var graph = null;
   186  
   187                  scope.$watch('data', function () {
   188                      // Sanity check
   189                      if (scope.data == null) {
   190                          return;
   191                      }
   192  
   193                      // If we've actually changed the data set, then recreate the graph
   194                      // We can always update the data by adding more data to the existing data set
   195                      if (graph != null) {
   196                          graph.destroy();
   197                      }
   198  
   199                      // Create the graph2d object
   200                      graph = new vis.Graph2d(element[0], scope.data.items, scope.data.groups, scope.options);
   201  
   202                      // Attach an event handler if defined
   203                      angular.forEach(scope.events, function (callback, event) {
   204                          if (graphEvents.indexOf(String(event)) >= 0) {
   205                              graph.on(event, callback);
   206                          }
   207                      });
   208  
   209                      // onLoad callback
   210                      if (scope.events != null && scope.events.onload != null &&
   211                          angular.isFunction(scope.events.onload)) {
   212                          scope.events.onload(graph);
   213                      }
   214                  });
   215  
   216                  scope.$watchCollection('options', function (options) {
   217                      if (graph == null) {
   218                          return;
   219                      }
   220                      graph.setOptions(options);
   221                  });
   222              }
   223          };
   224      })
   225  ;