statusmessage.js 3.9 KB

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