<commit_before>'use strict';

angular.module('algorea')
  .directive('includeTask', function () {
    return {
      restrict: 'EA',
      scope: false,
      template: function(elem, attrs) {
        var userItemVarStr = attrs.userItemVar ? 'user-item-var="'+attrs.userItemVar+'"' : '';
        return '<span ng-show="loadingError">{{loadingError}}</span><span build-task><iframe ng-if="taskUrl && !loadingError" ng-src="{{taskUrl}}" class="iframe-task" id="{{taskName}}" '+userItemVarStr+' allowfullscreen allow="microphone"></iframe></span>';
      },
      link: function(scope, elem, attrs) {
         // user-item-var can be used to take a variable other than
         // $scope.user_item for the user_item. This is used in the forum.
         if (attrs.userItemVar) {
            scope.user_item = scope[attrs.userItemVar];
         }
         if (attrs.readOnly && attrs.readOnly != 'false') {
            scope.readOnly = true;
         }
         if (attrs.taskName) {
            scope.taskName = attrs.taskName;
         }
      }
    };
});

angular.module('algorea')
  .directive('buildTask', ['$sce', '$http', '$timeout', '$rootScope', '$interval', '$injector', '$i18next', 'itemService', 'pathService', 'tabsService', function ($sce, $http, $timeout, $rootScope, $interval, $injector, $i18next, itemService, pathService, tabsService) {
   var mapService = null;
   if (config.domains.current.useMap) {
      mapService = $injector.get('mapService');
   }

   // Steps :
   // -initTask : loads the URL into the iframe
   // -initIframe : wait for the task DOM to be loaded
   // -loadTask : connect to the task object
   // -configureTask : configure the platform and start task.load
   // and then the controller logic takes over

   function initTask(scope, elem, sameUrl) {
      scope.currentView = null;
      // ID of the current instance, allows to avoid callbacks from old tasks
      scope.currentId = Math.random() * 1000000000;
      if (scope.item.sUrl) {
         if (scope.item.bUsesAPI) {
            var itemUrl = scope.item.sUrl;
            scope.taskUrl = $sce.trustAsResourceUrl(TaskProxyManager.getUrl(itemUrl, (scope.user_item ? scope.user_item.sToken : ''), 'http://algorea.pem.dev', name, $rootScope.sLocale));
            scope.itemUrl = itemUrl;
         } else {
            scope.taskUrl = $sce.trustAsResourceUrl(scope.item.sUrl);
            scope.itemUrl = null; // Reload the iframe each time
         }

         // Let a $digest happen before continuing
         $timeout(function() { initIframe(scope, elem, sameUrl); });
      } else {
         loadTask(scope, null, false);
      }
   }

   function initIframe(scope, elem, sameUrl) {
      scope.taskIframe = elem.find('iframe');
//      scope.taskIframe[0].src = scope.taskUrl;
      var timeout = $timeout(function() {
          loadTask(scope, scope.taskIframe, sameUrl);
          }, 3000);
      scope.taskIframe[0].onload = function() {
         if($timeout.cancel(timeout)) {
            loadTask(scope, scope.taskIframe, sameUrl);
         }
      };
   }

   function loadTask(scope, elem, sameUrl) {
      scope.item_strings = scope.item.strings[0];
      scope.item_strings_compare = null;
      if(!scope.item.sUrl) {
         scope.setTabs({}, true);
         scope.taskLoaded = true;
         return;
      }
      scope.loadingError = false;
      if (scope.item.sType == 'Task') {
         elem.addClass('iframe-task');
      } else {
         elem.addClass('iframe-course');
      }
      if (!scope.item.bUsesAPI) {
         elem.addClass('fullscreen-iframe');
         scope.taskLoaded = true;
         return;
      }
      var currentId = scope.currentId;
      scope.loadOpacity = 1;
      var loadMsgTimeout = $timeout(function () {
         scope.slowLoading = true;
      }, 5000);
      TaskProxyManager.getTaskProxy(scope.taskName, function(task) {
         if(scope.currentId != currentId) { return; }
         $timeout.cancel(loadMsgTimeout);
         scope.task = task;
         configureTask(scope, elem, sameUrl);
      }, !sameUrl, function() {
         if(scope.currentId != currentId) { return; }
         scope.taskLoaded = true;
         scope.loadingError = $i18next.t('task_communicate_error');
         scope.$apply();
      });
   }

   function configureTask(scope, elem, sameUrl) {
      scope.loadedUserItemID = scope.user_item.ID;
      scope.task.unloaded = false;
      // not sure the following line is still necessary<commit_msg>task: Apply scope on early returns<commit_after>'use strict';

angular.module('algorea')
  .directive('includeTask', function () {
    return {
      restrict: 'EA',
      scope: false,
      template: function(elem, attrs) {
        var userItemVarStr = attrs.userItemVar ? 'user-item-var="'+attrs.userItemVar+'"' : '';
        return '<span ng-show="loadingError">{{loadingError}}</span><span build-task><iframe ng-if="taskUrl && !loadingError" ng-src="{{taskUrl}}" class="iframe-task" id="{{taskName}}" '+userItemVarStr+' allowfullscreen allow="microphone"></iframe></span>';
      },
      link: function(scope, elem, attrs) {
         // user-item-var can be used to take a variable other than
         // $scope.user_item for the user_item. This is used in the forum.
         if (attrs.userItemVar) {
            scope.user_item = scope[attrs.userItemVar];
         }
         if (attrs.readOnly && attrs.readOnly != 'false') {
            scope.readOnly = true;
         }
         if (attrs.taskName) {
            scope.taskName = attrs.taskName;
         }
      }
    };
});

angular.module('algorea')
  .directive('buildTask', ['$sce', '$http', '$timeout', '$rootScope', '$interval', '$injector', '$i18next', 'itemService', 'pathService', 'tabsService', function ($sce, $http, $timeout, $rootScope, $interval, $injector, $i18next, itemService, pathService, tabsService) {
   var mapService = null;
   if (config.domains.current.useMap) {
      mapService = $injector.get('mapService');
   }

   // Steps :
   // -initTask : loads the URL into the iframe
   // -initIframe : wait for the task DOM to be loaded
   // -loadTask : connect to the task object
   // -configureTask : configure the platform and start task.load
   // and then the controller logic takes over

   function initTask(scope, elem, sameUrl) {
      scope.currentView = null;
      // ID of the current instance, allows to avoid callbacks from old tasks
      scope.currentId = Math.random() * 1000000000;
      if (scope.item.sUrl) {
         if (scope.item.bUsesAPI) {
            var itemUrl = scope.item.sUrl;
            scope.taskUrl = $sce.trustAsResourceUrl(TaskProxyManager.getUrl(itemUrl, (scope.user_item ? scope.user_item.sToken : ''), 'http://algorea.pem.dev', name, $rootScope.sLocale));
            scope.itemUrl = itemUrl;
         } else {
            scope.taskUrl = $sce.trustAsResourceUrl(scope.item.sUrl);
            scope.itemUrl = null; // Reload the iframe each time
         }

         // Let a $digest happen before continuing
         $timeout(function() { initIframe(scope, elem, sameUrl); });
      } else {
         loadTask(scope, null, false);
      }
   }

   function initIframe(scope, elem, sameUrl) {
      scope.taskIframe = elem.find('iframe');
//      scope.taskIframe[0].src = scope.taskUrl;
      var timeout = $timeout(function() {
          loadTask(scope, scope.taskIframe, sameUrl);
          }, 3000);
      scope.taskIframe[0].onload = function() {
         if($timeout.cancel(timeout)) {
            loadTask(scope, scope.taskIframe, sameUrl);
         }
      };
   }

   function loadTask(scope, elem, sameUrl) {
      scope.item_strings = scope.item.strings[0];
      scope.item_strings_compare = null;
      if(!scope.item.sUrl) {
         scope.setTabs({}, true);
         scope.taskLoaded = true;
         scope.$apply();
         return;
      }
      scope.loadingError = false;
      if (scope.item.sType == 'Task') {
         elem.addClass('iframe-task');
      } else {
         elem.addClass('iframe-course');
      }
      if (!scope.item.bUsesAPI) {
         elem.addClass('fullscreen-iframe');
         scope.taskLoaded = true;
         scope.$apply();
         return;
      }
      var currentId = scope.currentId;
      scope.loadOpacity = 1;
      var loadMsgTimeout = $timeout(function () {
         scope.slowLoading = true;
      }, 5000);
      TaskProxyManager.getTaskProxy(scope.taskName, function(task) {
         if(scope.currentId != currentId) { return; }
         $timeout.cancel(loadMsgTimeout);
         scope.task = task;
         configureTask(scope, elem, sameUrl);
      }, !sameUrl, function() {
         if(scope.currentId != currentId) { return; }
         scope.taskLoaded = true;
         scope.loadingError = $i18next.t('task_communicate_error');
         scope.$apply();
      });
   }

   function configureTask(scope, elem, sameUrl) {
      scope.loadedUserItemID = scope.user_item.ID;
      scope.task.unloaded = false;
      // not sure the following line is still necessary
{'fraction_non_alphanumeric': 0.13978970669618151, 'fraction_numerical': 0.006198118428334256, 'mean_word_length': 2.4700460829493087, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77474', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2686, 'n_words': 590}