statusmessage.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 statusTextBox = document.getElementById('edit-message');
  23. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  24. if (oldPreviewIframe !== null) {
  25. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  26. }
  27. var cssLink = document.createElement("link")
  28. cssLink.href = "/modules/statusmessage/css/preview.css";
  29. cssLink .rel = "stylesheet";
  30. cssLink .type = "text/css";
  31. previewIframe = document.createElement('iframe');
  32. previewIframe.classList.add('statusmessage-preview-iframe');
  33. statusTextBox.parentNode.appendChild(previewIframe);
  34. previewIframe.contentWindow.document.open();
  35. previewIframe.contentWindow.document.appendChild(markup);
  36. previewIframe.contentWindow.document.documentElement.appendChild(cssLink);
  37. previewIframe.contentWindow.document.close();
  38. }
  39. }
  40. });
  41. }
  42. };
  43. function validateUrl(input) {
  44. if (input !== null) {
  45. 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]+)?(/.*)?"));
  46. }
  47. }
  48. Drupal.AjaxCommands.prototype.clearPreview = function(ajax, response, status) {
  49. if (response.clear == true) {
  50. let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
  51. if (oldPreviewIframe !== null) {
  52. oldPreviewIframe.parentNode.removeChild(oldPreviewIframe);
  53. Drupal.attachBehaviors();
  54. }
  55. }
  56. };
  57. }
  58. };
  59. $(document).ready(function() {
  60. //Place listener on status post button
  61. //If status message is empty, prevent submit and alert the user
  62. let statusPostButton = document.getElementById('edit-post');
  63. let alertDialog = document.createElement('div');
  64. alertDialog.id = 'status-dialog';
  65. alertDialog.title = 'Status Alert';
  66. alertDialog.innerHTML = '<p>Enter a status message</p>';
  67. statusPostButton.addEventListener('click', function() {
  68. let textBox = document.getElementById('edit-message');
  69. console.dir(textBox);
  70. comparison = textBox.value === 0;
  71. console.log(comparison);
  72. if (textBox.value.length === 0) {
  73. event.preventDefault();
  74. event.stopPropagation();
  75. event.stopImmediatePropagation();
  76. // alert('Enter a status message');
  77. $(alertDialog).dialog();
  78. } else {
  79. textBox.value = "";
  80. }
  81. });
  82. });
  83. // let uploadButton = $('#edit-media-upload');
  84. // uploadButton.hide();
  85. // $('.status-media-upload').click(function() {
  86. // console.log('This is firing');
  87. // if (uploadButton.is(':visible')) {
  88. // uploadButton.hide();
  89. // } else {
  90. // uploadButton.show();
  91. // }
  92. // });
  93. })(jQuery, Drupal, drupalSettings);