trumbowyg.resizimg.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (function ($) {
  2. 'use strict';
  3. var defaultOptions = {
  4. minSize: 32,
  5. step: 4
  6. };
  7. var preventDefault = function (ev) {
  8. return ev.preventDefault();
  9. };
  10. $.extend(true, $.trumbowyg, {
  11. plugins: {
  12. resizimg: {
  13. init: function (trumbowyg) {
  14. trumbowyg.o.plugins.resizimg = $.extend(true, {},
  15. defaultOptions,
  16. trumbowyg.o.plugins.resizimg || {},
  17. {
  18. resizable: {
  19. resizeWidth: false,
  20. onDragStart: function (ev, $el) {
  21. var opt = trumbowyg.o.plugins.resizimg;
  22. var x = ev.pageX - $el.offset().left;
  23. var y = ev.pageY - $el.offset().top;
  24. if (x < $el.width() - opt.minSize || y < $el.height() - opt.minSize) {
  25. return false;
  26. }
  27. },
  28. onDrag: function (ev, $el, newWidth, newHeight) {
  29. var opt = trumbowyg.o.plugins.resizimg;
  30. if (newHeight < opt.minSize) {
  31. newHeight = opt.minSize;
  32. }
  33. newHeight -= newHeight % opt.step;
  34. $el.height(newHeight);
  35. return false;
  36. },
  37. onDragEnd: function () {
  38. trumbowyg.$c.trigger('tbwchange');
  39. }
  40. }
  41. }
  42. );
  43. function initResizable() {
  44. trumbowyg.$ed.find('img:not(.resizable)')
  45. .resizable(trumbowyg.o.plugins.resizimg.resizable)
  46. .on('mousedown', preventDefault);
  47. }
  48. function destroyResizable() {
  49. trumbowyg.$ed.find('img.resizable')
  50. .resizable('destroy')
  51. .off('mousedown', preventDefault);
  52. trumbowyg.syncTextarea();
  53. }
  54. trumbowyg.$c.on('tbwinit', initResizable);
  55. trumbowyg.$c.on('tbwfocus', initResizable);
  56. trumbowyg.$c.on('tbwchange', initResizable);
  57. trumbowyg.$c.on('tbwblur', destroyResizable);
  58. trumbowyg.$c.on('tbwclose', destroyResizable);
  59. }
  60. }
  61. }
  62. });
  63. })(jQuery);