thumbnailViewer.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var thumbnailViewer = function(options){
  2. var getSVGDocument = function(objectElem){
  3. var svgDoc = objectElem.contentDocument;
  4. if(! svgDoc){
  5. svgDoc = objectElem.getSVGDocument();
  6. }
  7. return svgDoc;
  8. }
  9. var bindThumbnail = function(main, thumb){
  10. if(! window.main && main){
  11. window.main = main;
  12. }
  13. if(! window.thumb && thumb){
  14. window.thumb = thumb;
  15. }
  16. if(! window.main || ! window.thumb){
  17. return;
  18. }
  19. var resizeTimer;
  20. var interval = 300; //msec
  21. window.addEventListener('resize', function(event){
  22. if (resizeTimer !== false) {
  23. clearTimeout(resizeTimer);
  24. }
  25. resizeTimer = setTimeout(function () {
  26. window.main.resize();
  27. window.thumb.resize();
  28. }, interval);
  29. });
  30. window.main.setOnZoom(function(level){
  31. window.thumb.updateThumbScope();
  32. if(options.onZoom){
  33. options.onZoom(window.main, window.thumb, level);
  34. }
  35. });
  36. window.main.setOnPan(function(point){
  37. window.thumb.updateThumbScope();
  38. if(options.onPan){
  39. options.onPan(window.main, window.thumb, point);
  40. }
  41. });
  42. var _updateThumbScope = function (main, thumb, scope, line1, line2){
  43. var mainPanX = main.getPan().x
  44. , mainPanY = main.getPan().y
  45. , mainWidth = main.getSizes().width
  46. , mainHeight = main.getSizes().height
  47. , mainZoom = main.getSizes().realZoom
  48. , thumbPanX = thumb.getPan().x
  49. , thumbPanY = thumb.getPan().y
  50. , thumbZoom = thumb.getSizes().realZoom;
  51. var thumByMainZoomRatio = thumbZoom / mainZoom;
  52. var scopeX = thumbPanX - mainPanX * thumByMainZoomRatio;
  53. var scopeY = thumbPanY - mainPanY * thumByMainZoomRatio;
  54. var scopeWidth = mainWidth * thumByMainZoomRatio;
  55. var scopeHeight = mainHeight * thumByMainZoomRatio;
  56. scope.setAttribute("x", scopeX + 1);
  57. scope.setAttribute("y", scopeY + 1);
  58. scope.setAttribute("width", scopeWidth - 2);
  59. scope.setAttribute("height", scopeHeight - 2);
  60. /*
  61. line1.setAttribute("x1", scopeX + 1);
  62. line1.setAttribute("y1", scopeY + 1);
  63. line1.setAttribute("x2", scopeX + 1 + scopeWidth - 2);
  64. line1.setAttribute("y2", scopeY + 1 + scopeHeight - 2);
  65. line2.setAttribute("x1", scopeX + 1);
  66. line2.setAttribute("y1", scopeY + 1 + scopeHeight - 2);
  67. line2.setAttribute("x2", scopeX + 1 + scopeWidth - 2);
  68. line2.setAttribute("y2", scopeY + 1);
  69. */
  70. };
  71. window.thumb.updateThumbScope = function(){
  72. var scope = document.getElementById('scope');
  73. var line1 = document.getElementById('line1');
  74. var line2 = document.getElementById('line2');
  75. _updateThumbScope(window.main, window.thumb, scope, line1, line2);
  76. }
  77. window.thumb.updateThumbScope();
  78. var _updateMainViewPan = function(clientX, clientY, scopeContainer, main, thumb){
  79. var dim = scopeContainer.getBoundingClientRect()
  80. , mainWidth = main.getSizes().width
  81. , mainHeight = main.getSizes().height
  82. , mainZoom = main.getSizes().realZoom
  83. , thumbWidth = thumb.getSizes().width
  84. , thumbHeight = thumb.getSizes().height
  85. , thumbZoom = thumb.getSizes().realZoom;
  86. var thumbPanX = clientX - dim.left - thumbWidth / 2;
  87. var thumbPanY = clientY - dim.top - thumbHeight / 2;
  88. var mainPanX = - thumbPanX * mainZoom / thumbZoom;
  89. var mainPanY = - thumbPanY * mainZoom / thumbZoom;
  90. main.pan({x:mainPanX, y:mainPanY});
  91. };
  92. var updateMainViewPan = function(evt){
  93. if(evt.which == 0 && evt.button == 0){
  94. return false;
  95. }
  96. var scopeContainer = document.getElementById('scopeContainer');
  97. _updateMainViewPan(evt.clientX, evt.clientY, scopeContainer, window.main, window.thumb);
  98. }
  99. var scopeContainer = document.getElementById('scopeContainer');
  100. scopeContainer.addEventListener('click', function(evt){
  101. updateMainViewPan(evt);
  102. });
  103. scopeContainer.addEventListener('mousemove', function(evt){
  104. updateMainViewPan(evt);
  105. });
  106. };
  107. var mainViewObjectElem = document.getElementById(options.mainViewId);
  108. mainViewObjectElem.addEventListener("load", function(){
  109. var mainViewSVGDoc = getSVGDocument(mainViewObjectElem);
  110. if(options.onMainViewSVGLoaded){
  111. options.onMainViewSVGLoaded(mainViewSVGDoc);
  112. }
  113. var beforePan = function(oldPan, newPan){
  114. var stopHorizontal = false
  115. , stopVertical = false
  116. , gutterWidth = 100
  117. , gutterHeight = 100
  118. // Computed variables
  119. , sizes = this.getSizes()
  120. , leftLimit = -((sizes.viewBox.x + sizes.viewBox.width) * sizes.realZoom) + gutterWidth
  121. , rightLimit = sizes.width - gutterWidth - (sizes.viewBox.x * sizes.realZoom)
  122. , topLimit = -((sizes.viewBox.y + sizes.viewBox.height) * sizes.realZoom) + gutterHeight
  123. , bottomLimit = sizes.height - gutterHeight - (sizes.viewBox.y * sizes.realZoom);
  124. customPan = {};
  125. customPan.x = Math.max(leftLimit, Math.min(rightLimit, newPan.x));
  126. customPan.y = Math.max(topLimit, Math.min(bottomLimit, newPan.y));
  127. return customPan;
  128. };
  129. var main = svgPanZoom('#'+options.mainViewId, {
  130. zoomEnabled: true,
  131. controlIconsEnabled: true,
  132. fit: true,
  133. center: true,
  134. beforePan: beforePan
  135. });
  136. bindThumbnail(main, undefined);
  137. if(options.onMainViewShown){
  138. options.onMainViewShown(mainViewSVGDoc, main);
  139. }
  140. }, false);
  141. var thumbViewObjectElem = document.getElementById(options.thumbViewId);
  142. thumbViewObjectElem.addEventListener("load", function(){
  143. var thumbViewSVGDoc = getSVGDocument(thumbViewObjectElem);
  144. if(options.onThumbnailSVGLoaded){
  145. options.onThumbnailSVGLoaded(thumbViewSVGDoc);
  146. }
  147. var thumb = svgPanZoom('#'+options.thumbViewId, {
  148. zoomEnabled: false,
  149. panEnabled: false,
  150. controlIconsEnabled: false,
  151. dblClickZoomEnabled: false,
  152. preventMouseEventsDefault: true,
  153. });
  154. bindThumbnail(undefined, thumb);
  155. if(options.onThumbnailShown){
  156. options.onThumbnailShown(thumbViewSVGDoc, thumb);
  157. }
  158. }, false);
  159. };