statusmessage.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Created by logicp on 6/05/17.
  3. */
  4. (function($, Drupal, drupalSettings) {
  5. Drupal.behaviors.status= {
  6. attach: function (context, settings) {
  7. if (Drupal.AjaxCommands){
  8. Drupal.AjaxCommands.prototype.viewsScrollTop = null;
  9. }
  10. Drupal.AjaxCommands.prototype.generatePreview = function(ajax, response, status) {
  11. if (validateUrl(response.url)) {
  12. var cleanUrl = response.url.replace(/^http(s?):\/\//i, "");
  13. // console.log(cleanUrl);
  14. $.ajax({
  15. type: 'POST',
  16. url: '/statusmessage/generate-preview/build' ,
  17. data: {'data': cleanUrl},
  18. success: function (response) {
  19. // console.log(response.data);
  20. if (response.data != null) {
  21. var parser = new DOMParser();
  22. // var doc = parser.parseFromString(response.data, "text/html");
  23. let markup = document.createElement('div');
  24. markup.innerHTML = response.data;
  25. let statusTextBox = document.getElementById('edit-message');
  26. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  27. if (oldPreviewIframe !== null) {
  28. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  29. }
  30. previewIframe = document.createElement('iframe');
  31. previewIframe.classList.add('statusmessage-preview-iframe');
  32. statusTextBox.parentNode.appendChild(previewIframe);
  33. previewIframe.contentWindow.document.open();
  34. previewIframe.contentWindow.document.appendChild(markup);
  35. previewIframe.contentWindow.document.close();
  36. }
  37. }
  38. });
  39. }
  40. };
  41. function validateUrl(input) {
  42. if (input !== null) {
  43. 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]+)?(/.*)?"));
  44. }
  45. }
  46. Drupal.AjaxCommands.prototype.clearPreview = function(ajax, response, status) {
  47. if (response.clear == true) {
  48. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  49. if (oldPreviewIframe !== null) {
  50. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  51. }
  52. }
  53. }
  54. let statusPostButton = document.getElementById('edit-post');
  55. statusPostButton.addEventListener('click', function() {
  56. let textBox = document.getElementById('edit-message');
  57. textBox.value = "";
  58. })
  59. }
  60. };
  61. })(jQuery, Drupal, drupalSettings);