recursionHelper.js 2.3 KB

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