recursionHelper.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. angular.module('RecursionHelper', []).factory('RecursionHelper', [
  18. '$compile',
  19. function($compile) {
  20. /*
  21. Manually compiles the element, fixing the recursion loop.
  22. @param element
  23. @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
  24. @returns An object containing the linking functions.
  25. */
  26. return {
  27. compile(element, link) {
  28. // Normalize the link parameter
  29. if (angular.isFunction(link)) {
  30. link = { post: link }
  31. }
  32. // Break the recursion loop by removing the contents
  33. const contents = element.contents().remove()
  34. let compiledContents
  35. return {
  36. pre: link && link.pre ? link.pre : null,
  37. /*
  38. Compiles and re-adds the contents
  39. */
  40. post(scope, element) {
  41. // Compile the contents
  42. if (!compiledContents) {
  43. compiledContents = $compile(contents)
  44. }
  45. // Re-add the compiled contents to the element
  46. compiledContents(scope, function(clone) {
  47. element.append(clone)
  48. })
  49. // Call the post-linking function, if any
  50. if (link && link.post) {
  51. link.post.apply(null, arguments)
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. ])