Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Fix part of oppia#3826: Extracted recursionHelper-> NestedDirectivesR…
Browse files Browse the repository at this point in the history
…ecursionTimeoutPreventionService into separate file. (oppia#4518)

* Extracted  recursionHelper → NestedDirectivesRecursionTimeoutPreventionService

* Fixed indentation error

* Fixed review errors
  • Loading branch information
Kunalgarg2100 authored and kevinlee12 committed Jan 14, 2018
1 parent 45af240 commit fc05972
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 73 deletions.
45 changes: 0 additions & 45 deletions core/templates/dev/head/components/forms/FormBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,48 +365,3 @@ oppia.directive('requireIsFloat', ['$filter', function($filter) {
}
};
}]);

// Prevents timeouts due to recursion in nested directives. See:
//
// http://stackoverflow.com/q/14430655
oppia.factory('recursionHelper', ['$compile', function($compile) {
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param {DOM element} element
* @param {function|object} link - A post-link function, or an object with
* function(s) registered via pre and post properties.
* @return {object} An object containing the linking functions.
*/
compile: function(element, link) {
// Normalize the link parameter
if (angular.isFunction(link)) {
link = {
post: link
};
}

// Break the recursion loop by removing the contents,
var contents = element.contents().remove();
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
post: function(scope, element) {
// Compile the contents.
if (!compiledContents) {
compiledContents = $compile(contents);
}
// Re-add the compiled contents to the element.
compiledContents(scope, function(clone) {
element.append(clone);
});

// Call the post-linking function, if any.
if (link && link.post) {
link.post.apply(null, arguments);
}
}
};
}
};
}]);
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedChoicesEditor', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -33,7 +36,7 @@ oppia.directive('schemaBasedChoicesEditor', [
'/components/forms/schema_editors/' +
'schema_based_choices_editor_directive.html'),
restrict: 'E',
compile: recursionHelper.compile,
compile: NestedDirectivesRecursionTimeoutPreventionService.compile,
controller: ['$scope', function($scope) {
$scope.getReadonlySchema = function() {
var readonlySchema = angular.copy($scope.schema());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedCustomEditor', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -29,7 +32,7 @@ oppia.directive('schemaBasedCustomEditor', [
'/components/forms/schema_editors/' +
'schema_based_custom_editor_directive.html'),
restrict: 'E',
compile: recursionHelper.compile
compile: NestedDirectivesRecursionTimeoutPreventionService.compile
};
}
]);
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedDictEditor', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -32,7 +35,7 @@ oppia.directive('schemaBasedDictEditor', [
'/components/forms/schema_editors/' +
'schema_based_dict_editor_directive.html'),
restrict: 'E',
compile: recursionHelper.compile,
compile: NestedDirectivesRecursionTimeoutPreventionService.compile,
controller: [
'$scope', 'IdGenerationService',
function($scope, IdGenerationService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/

oppia.directive('schemaBasedListEditor', [
'SchemaDefaultValueService', 'recursionHelper', 'FocusManagerService',
'SchemaUndefinedLastElementService', 'IdGenerationService',
'UrlInterpolationService', function(
SchemaDefaultValueService, recursionHelper, FocusManagerService,
SchemaUndefinedLastElementService, IdGenerationService,
UrlInterpolationService) {
'SchemaDefaultValueService',
'NestedDirectivesRecursionTimeoutPreventionService',
'FocusManagerService', 'SchemaUndefinedLastElementService',
'IdGenerationService', 'UrlInterpolationService',
function(
SchemaDefaultValueService,
NestedDirectivesRecursionTimeoutPreventionService,
FocusManagerService, SchemaUndefinedLastElementService,
IdGenerationService, UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -41,7 +44,7 @@ oppia.directive('schemaBasedListEditor', [
'/components/forms/schema_editors/' +
'schema_based_list_editor_directive.html'),
restrict: 'E',
compile: recursionHelper.compile,
compile: NestedDirectivesRecursionTimeoutPreventionService.compile,
controller: ['$scope', function($scope) {
var baseFocusLabel = (
$scope.labelForFocusTarget() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedCustomViewer', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -29,7 +32,7 @@ oppia.directive('schemaBasedCustomViewer', [
'/components/forms/schema_viewers/' +
'schema_based_custom_viewer_directive.html'),
restrict: 'E',
compile: recursionHelper.compile
compile: NestedDirectivesRecursionTimeoutPreventionService.compile
};
}
]);
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedDictViewer', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -30,7 +33,7 @@ oppia.directive('schemaBasedDictViewer', [
'/components/forms/schema_viewers/' +
'schema_based_dict_viewer.html'),
restrict: 'E',
compile: recursionHelper.compile,
compile: NestedDirectivesRecursionTimeoutPreventionService.compile,
controller: ['$scope', function($scope) {
$scope.getHumanReadablePropertyDescription = function(property) {
return property.description || '[' + property.name + ']';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('schemaBasedListViewer', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
localValue: '=',
Expand All @@ -29,7 +32,7 @@ oppia.directive('schemaBasedListViewer', [
'/components/forms/schema_viewers/' +
'schema_based_list_viewer_directive.html'),
restrict: 'E',
compile: recursionHelper.compile
compile: NestedDirectivesRecursionTimeoutPreventionService.compile
};
}
]);
1 change: 1 addition & 0 deletions core/templates/dev/head/pages/admin/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<script src="{{TEMPLATE_DIR_PREFIX}}/services/RteHelperService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaDefaultValueService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaUndefinedLastElementService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/NestedDirectivesRecursionTimeoutPreventionService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/contextual/DeviceInfoService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/contextual/UrlService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/stateful/FocusManagerService.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
<script src="{{TEMPLATE_DIR_PREFIX}}/services/LocalStorageService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaDefaultValueService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaUndefinedLastElementService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/NestedDirectivesRecursionTimeoutPreventionService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/pages/exploration_editor/EditorNavbarBreadcrumbDirective.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/pages/exploration_editor/EditorNavigationDirective.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/pages/exploration_editor/EditorServices.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ <h1 class="oppia-exploration-h1"><span class="protractor-test-exploration-header
<script src="{{TEMPLATE_DIR_PREFIX}}/services/ComputeGraphService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaDefaultValueService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaUndefinedLastElementService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/NestedDirectivesRecursionTimeoutPreventionService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/attribution_guide/AttributionGuideDirective.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/embed_modal/ExplorationEmbedButtonService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/profile_link/ProfileLinkImageDirective.js"></script>
Expand Down
1 change: 1 addition & 0 deletions core/templates/dev/head/pages/moderator/moderator.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ <h3>Activities to feature in the library</h3>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/forms/FormBuilder.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaDefaultValueService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/SchemaUndefinedLastElementService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/services/NestedDirectivesRecursionTimeoutPreventionService.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/forms/schema_editors/SchemaBasedChoicesEditorDirective.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/forms/schema_editors/SchemaBasedDictEditorDirective.js"></script>
<script src="{{TEMPLATE_DIR_PREFIX}}/components/forms/schema_editors/SchemaBasedEditorDirective.js"></script>
Expand Down
9 changes: 6 additions & 3 deletions core/templates/dev/head/pages/tests/FormBuilderTestPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

oppia.directive('formOverlay', [
'recursionHelper', 'UrlInterpolationService',
function(recursionHelper, UrlInterpolationService) {
'NestedDirectivesRecursionTimeoutPreventionService',
'UrlInterpolationService',
function(
NestedDirectivesRecursionTimeoutPreventionService,
UrlInterpolationService) {
return {
scope: {
definition: '=',
Expand All @@ -28,7 +31,7 @@ oppia.directive('formOverlay', [
templateUrl: UrlInterpolationService.getDirectiveTemplateUrl(
'/pages/tests/form_entry_point_modal_directive.html'),
restrict: 'E',
compile: recursionHelper.compile,
compile: NestedDirectivesRecursionTimeoutPreventionService.compile,
controller: ['$scope', function($scope) {
$scope.$watch('savedValue', function() {
$scope.localValue = angular.copy($scope.savedValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2018 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Service prevents timeouts due to recursion
* in nested directives. See: http://stackoverflow.com/q/14430655
*/

oppia.factory('NestedDirectivesRecursionTimeoutPreventionService', [
'$compile',
function($compile) {
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param {DOM element} element
* @param {function|object} link - A post-link function, or an object with
* function(s) registered via pre and post properties.
* @return {object} An object containing the linking functions.
*/
compile: function(element, link) {
// Normalize the link parameter
if (angular.isFunction(link)) {
link = {
post: link
};
}

// Break the recursion loop by removing the contents,
var contents = element.contents().remove();
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
post: function(scope, element) {
// Compile the contents.
if (!compiledContents) {
compiledContents = $compile(contents);
}
// Re-add the compiled contents to the element.
compiledContents(scope, function(clone) {
element.append(clone);
});

// Call the post-linking function, if any.
if (link && link.post) {
link.post.apply(null, arguments);
}
}
};
}
};
}]);

0 comments on commit fc05972

Please sign in to comment.