statusmessage.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Created by logicp on 6/05/17.
  3. */
  4. (function($, Drupal, drupalSettings) {
  5. Drupal.behaviors.status= {
  6. attach: function (context, settings) {
  7. Drupal.AjaxCommands.prototype.generatePreview = function(ajax, response, status) {
  8. if (validateUrl(response.url)) {
  9. var cleanUrl = response.url.replace(/^http(s?):\/\//i, "");
  10. // console.log(cleanUrl);
  11. $.ajax({
  12. type: 'POST',
  13. url: '/statusmessage/generate-preview/build' ,
  14. data: {'data': cleanUrl},
  15. success: function (response) {
  16. // console.log(response.data);
  17. if (response.data != null) {
  18. var parser = new DOMParser();
  19. // var doc = parser.parseFromString(response.data, "text/html");
  20. let markup = document.createElement('div');
  21. markup.innerHTML = response.data;
  22. let statusBlock = document.getElementById('block-statusblock');
  23. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  24. if (oldPreviewIframe !== null) {
  25. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  26. }
  27. previewIframe = document.createElement('iframe');
  28. previewIframe.classList.add('statusmessage-preview-iframe');
  29. statusBlock.appendChild(previewIframe);
  30. previewIframe.contentWindow.document.open();
  31. previewIframe.contentWindow.document.appendChild(markup);
  32. previewIframe.contentWindow.document.close();
  33. }
  34. }
  35. });
  36. }
  37. };
  38. function validateUrl(input) {
  39. if (input !== null) {
  40. return input.match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?"));
  41. }
  42. }
  43. Drupal.AjaxCommands.prototype.clearPreview = function(ajax, response, status) {
  44. if (response.clear == true) {
  45. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  46. if (oldPreviewIframe !== null) {
  47. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  48. }
  49. }
  50. }
  51. }
  52. };
  53. })(jQuery, Drupal, drupalSettings);