recursionHelper.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* eslint-disable
  2. max-len,
  3. no-undef,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. //
  13. // * An Angular service which helps with creating recursive directives.
  14. // * @author Mark Lagendijk
  15. // * @license MIT
  16. //
  17. // From: https://github.com/marklagendijk/angular-recursion
  18. angular.module('RecursionHelper', []).factory('RecursionHelper', [
  19. '$compile',
  20. function($compile) {
  21. /*
  22. Manually compiles the element, fixing the recursion loop.
  23. @param element
  24. @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
  25. @returns An object containing the linking functions.
  26. */
  27. return {
  28. compile(element, link) {
  29. // Normalize the link parameter
  30. if (angular.isFunction(link)) {
  31. link = { post: link }
  32. }
  33. // Break the recursion loop by removing the contents
  34. const contents = element.contents().remove()
  35. let compiledContents
  36. return {
  37. pre: link && link.pre ? link.pre : null,
  38. /*
  39. Compiles and re-adds the contents
  40. */
  41. post(scope, element) {
  42. // Compile the contents
  43. if (!compiledContents) {
  44. compiledContents = $compile(contents)
  45. }
  46. // Re-add the compiled contents to the element
  47. compiledContents(scope, function(clone) {
  48. element.append(clone)
  49. })
  50. // Call the post-linking function, if any
  51. if (link && link.post) {
  52. link.post.apply(null, arguments)
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. ])