__vhook_integration.js 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var mgIntegrationHelper = {
  2. //help determine what part of app should be integrated
  3. getIngegrationInsertType: function(isIntegration) {
  4. var allowedInsertTypes= ['full', 'content', 'mc_content'];
  5. var intParam = $(isIntegration).attr('mg-integration-insert-type');
  6. if (allowedInsertTypes.includes(intParam)) {
  7. return intParam;
  8. } else {
  9. return 'full';
  10. }
  11. },
  12. //get integration HTML code to be passed on the page
  13. getIngegrationCode: function(selector, integrationInsertType) {
  14. if(integrationInsertType === 'full'){
  15. return $(selector).parents('.mg-integration-container').children('#layers').first()[0].outerHTML;
  16. } else if (integrationInsertType === 'mc_content') {
  17. return $(selector)[0].innerHTML;
  18. } else {
  19. return $(selector)[0].outerHTML;
  20. }
  21. },
  22. removeOldContainer: function(selector, integrationInsertType, integrationTarget) {
  23. return $(selector)[0].remove();
  24. },
  25. afterInsertActions: function(selector, integrationInsertType, integrationTarget) {
  26. if (integrationInsertType === 'full') {
  27. var modalContId = $('#' + $(selector).attr('id') + '_modal');
  28. $(integrationTarget).find(modalContId).first().remove();
  29. }
  30. }
  31. }
  32. mgEventHandler.on('AppsPreLoad', null, function(id, params){
  33. if (typeof params.appContainers !== 'undefined') {
  34. //check all app container, looking for integration containers
  35. for (var key in params.appContainers) {
  36. if (!params.appContainers.hasOwnProperty(key)) {
  37. continue;
  38. }
  39. //each integration container needs to have 'mg-integration-container' class
  40. var isIntegration = $(params.appContainers[key]).parents('.mg-integration-container');
  41. if (isIntegration.length === 1) {
  42. var integrationInsertType = mgIntegrationHelper.getIngegrationInsertType(isIntegration);
  43. var tempIntCode = mgIntegrationHelper.getIngegrationCode(params.appContainers[key], integrationInsertType);
  44. var integrationType = $(isIntegration).attr('mg-integration-type');
  45. var integrationTarget = $(isIntegration).attr('mg-integration-target');
  46. if (integrationType === 'append') {
  47. mgIntegrationHelper.removeOldContainer(params.appContainers[key], integrationInsertType, integrationTarget);
  48. $(integrationTarget).append(tempIntCode);
  49. mgIntegrationHelper.afterInsertActions(params.appContainers[key], integrationInsertType, integrationTarget);
  50. } else if(integrationType === 'replace') {
  51. mgIntegrationHelper.removeOldContainer(params.appContainers[key], integrationInsertType, integrationTarget);
  52. $(integrationTarget).replaceWith(tempIntCode);
  53. mgIntegrationHelper.afterInsertActions(params.appContainers[key], integrationInsertType, integrationTarget);
  54. } else if(integrationType === 'after') {
  55. mgIntegrationHelper.removeOldContainer(params.appContainers[key], integrationInsertType, integrationTarget);
  56. $(integrationTarget).after(tempIntCode);
  57. mgIntegrationHelper.afterInsertActions(params.appContainers[key], integrationInsertType, integrationTarget);
  58. } else if(integrationType === 'before') {
  59. mgIntegrationHelper.removeOldContainer(params.appContainers[key], integrationInsertType, integrationTarget);
  60. $(integrationTarget).before(tempIntCode);
  61. mgIntegrationHelper.afterInsertActions(params.appContainers[key], integrationInsertType, integrationTarget);
  62. } else if(integrationType === 'prepend') {
  63. mgIntegrationHelper.removeOldContainer(params.appContainers[key], integrationInsertType, integrationTarget);
  64. $(integrationTarget).prepend(tempIntCode);
  65. mgIntegrationHelper.afterInsertActions(params.appContainers[key], integrationInsertType, integrationTarget);
  66. } else if(integrationType === 'custom') {
  67. var contId = $(params.appContainers[key]).attr('id');
  68. var integrationFunction = $(isIntegration).attr('mg-integration-function');
  69. if (integrationFunction && typeof window[integrationFunction] === "function") {
  70. window[integrationFunction](integrationTarget, contId);
  71. }
  72. if (integrationTarget !== 'null') {
  73. $(params.appContainers[key])[0].remove();
  74. $(integrationTarget).addClass('vue-app-main-container');
  75. if (typeof $(integrationTarget).attr('id') === 'undefined'){
  76. $(integrationTarget).attr('id', contId);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }, 3000);