statusmessage.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. statusPostButton.addEventListener('click', function() {
  66. let textBox = document.getElementById('edit-message');
  67. console.dir(textBox);
  68. comparison = textBox.value === 0;
  69. console.log(comparison);
  70. if (textBox.value.length === 0) {
  71. event.preventDefault();
  72. event.stopPropagation();
  73. event.stopImmediatePropagation();
  74. alert('Enter a status message');
  75. } else {
  76. textBox.value = "";
  77. }
  78. });
  79. });
  80. // let uploadButton = $('#edit-media-upload');
  81. // uploadButton.hide();
  82. // $('.status-media-upload').click(function() {
  83. // console.log('This is firing');
  84. // if (uploadButton.is(':visible')) {
  85. // uploadButton.hide();
  86. // } else {
  87. // uploadButton.show();
  88. // }
  89. // });
  90. })(jQuery, Drupal, drupalSettings);