video.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (function($)
  2. {
  3. $.Redactor.prototype.video = function()
  4. {
  5. return {
  6. reUrlYoutube: /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig,
  7. reUrlVimeo: /https?:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/,
  8. getTemplate: function()
  9. {
  10. return String()
  11. + '<section id="redactor-modal-video-insert">'
  12. + '<label>' + this.lang.get('video_html_code') + '</label>'
  13. + '<textarea id="redactor-insert-video-area" style="height: 160px;"></textarea>'
  14. + '</section>';
  15. },
  16. init: function()
  17. {
  18. var button = this.button.addAfter('image', 'video', this.lang.get('video'));
  19. this.button.addCallback(button, this.video.show);
  20. },
  21. show: function()
  22. {
  23. this.modal.addTemplate('video', this.video.getTemplate());
  24. this.modal.load('video', this.lang.get('video'), 700);
  25. this.modal.createCancelButton();
  26. var button = this.modal.createActionButton(this.lang.get('insert'));
  27. button.on('click', this.video.insert);
  28. this.selection.save();
  29. this.modal.show();
  30. $('#redactor-insert-video-area').focus();
  31. },
  32. insert: function()
  33. {
  34. var data = $('#redactor-insert-video-area').val();
  35. if (!data.match(/<iframe|<video/gi))
  36. {
  37. data = this.clean.stripTags(data);
  38. // parse if it is link on youtube & vimeo
  39. var iframeStart = '<iframe style="width: 500px; height: 281px;" src="',
  40. iframeEnd = '" frameborder="0" allowfullscreen></iframe>';
  41. if (data.match(this.video.reUrlYoutube))
  42. {
  43. data = data.replace(this.video.reUrlYoutube, iframeStart + '//www.youtube.com/embed/$1' + iframeEnd);
  44. }
  45. else if (data.match(this.video.reUrlVimeo))
  46. {
  47. data = data.replace(this.video.reUrlVimeo, iframeStart + '//player.vimeo.com/video/$2' + iframeEnd);
  48. }
  49. }
  50. this.selection.restore();
  51. this.modal.close();
  52. var current = this.selection.getBlock() || this.selection.getCurrent();
  53. if (current) $(current).after(data);
  54. else
  55. {
  56. this.insert.html(data);
  57. }
  58. this.code.sync();
  59. }
  60. };
  61. };
  62. })(jQuery);