{"size": 2771, "ext": "js", "max_stars_repo_path": "www/app/modules/biospecimen/participant/specimen/overview.js", "max_stars_repo_name": "krishagni/openspecimen", "max_stars_repo_stars_event_min_datetime": "2015-01-09T14:47:22.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T08:51:45.000Z", "max_issues_repo_path": "www/app/modules/biospecimen/participant/specimen/overview.js", "max_issues_repo_name": "krishagni/openspecimen", "max_issues_repo_issues_event_min_datetime": "2017-08-03T01:02:40.000Z", "max_issues_repo_issues_event_max_datetime": "2020-01-27T14:11:20.000Z", "max_forks_repo_path": "www/app/modules/biospecimen/participant/specimen/overview.js", "max_forks_repo_name": "krishagni/openspecimen", "max_forks_repo_forks_event_min_datetime": "2015-02-05T15:00:21.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-08T07:13:59.000Z"}
{"max_stars_count": 48, "max_issues_count": 3, "max_forks_count": 59, "avg_line_length": 29.1684210526, "max_line_length": 110, "alphanum_fraction": 0.590761458}

angular.module('os.biospecimen.specimen.overview', ['os.biospecimen.models'])
  .controller('SpecimenOverviewCtrl', function(
    $scope, $rootScope, $modal, hasDict, userRole, cpr, visit, specimen, showSpmnActivity,
    osRightDrawerSvc, Specimen, SpecimenLabelPrinter, Util, ExtensionsUtil) {

    function init() {
      if (hasDict) {
        ExtensionsUtil.createExtensionFieldMap(specimen, hasDict);
      }

      $scope.spmnCtx = {
        obj: {cpr: cpr, visit: visit, specimen: specimen, userRole: userRole},
        inObjs: ['specimen', 'calcSpecimen',],
        exObjs: ['specimen.events'],
        watcher: ['specimen.storageLocation', 'specimen.collectionEvent.user', 'specimen.receivedEvent.user'],
        showActivity: showSpmnActivity
      }

      loadActivities();

      $scope.$watch('specimen.activityStatus', function(newVal, oldVal) {
        if (newVal == oldVal) {
          return;
        }

        if (newVal == 'Closed') {
          loadActivities();
        }
      });

      if (showSpmnActivity) {
        osRightDrawerSvc.open();
      }
    }

    function loadActivities() {
      $scope.activities = [];
      specimen.getEvents(
        function(activities) {
          $scope.activities = activities.map(
            function(activity) {
              return angular.extend({global: $rootScope.global}, activity);
            }
          );
        }
      );
    };

    function printSpecimenLabels(includeChildren, children) {
      var ids = [];
      if (includeChildren) {
        ids = (children || []).map(function(spmn) { return spmn.id; });
      }

      ids.unshift(specimen.id);

      var ts = Util.formatDate(Date.now(), 'yyyyMMdd_HHmmss');
      var outputFilename = [
        cpr.cpShortTitle, cpr.ppid,
        visit.name || visit.id,
        specimen.label || specimen.id,
        ts].join('_') + '.csv';
      SpecimenLabelPrinter.printLabels({specimenIds: ids}, outputFilename);
    }

    $scope.toggleShowActivity = function() {
      $scope.spmnCtx.showActivity = !$scope.spmnCtx.showActivity;
    }

    $scope.printSpecimenLabels = function() {
      var children = Specimen.flatten($scope.treeSpecimens);
      if (!children || children.length <= 0) {
        printSpecimenLabels(false);
        return;
      }

      $modal.open({
        templateUrl: 'modules/biospecimen/participant/specimen/confirm-print.html',
        controller: function($scope, $modalInstance) {
          $scope.printSpecimenLabels = function(includeChildren) {
            printSpecimenLabels(includeChildren, children);
            $modalInstance.dismiss('cancel');
          }

          $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
          }
        }
      });
    }

    init();
  });
