simple-image-view.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //简单多张图片排列显示模块
  2. var single_image_view = function(option, success) {
  3. var object = new SingleImageView(option);
  4. success();
  5. return object;
  6. }
  7. function SingleImageView(option) {
  8. this.markControl = option.markControl;
  9. this.defaultZoom = 100;
  10. this.loading = false;
  11. this.init();
  12. this.markControl.on('step.board.show', this, function(event, context, eventObject) {
  13. this.container.removeClass('span12');
  14. this.container.addClass('span10');
  15. this.container.perfectScrollbar('update');
  16. if (this.iviewer != undefined) {
  17. this.iviewer.iviewer('update');
  18. }
  19. });
  20. this.markControl.on('step.board.hide', this, function(event, context, eventObject) {
  21. this.container.removeClass('span10');
  22. this.container.addClass('span12');
  23. this.container.perfectScrollbar('update');
  24. if (this.iviewer != undefined) {
  25. this.iviewer.iviewer('update');
  26. }
  27. });
  28. this.markControl.on('center.width.change', this, function(event, context, eventObject) {
  29. this.container.perfectScrollbar('update');
  30. if (this.iviewer != undefined) {
  31. this.iviewer.iviewer('update');
  32. this.iviewer.iviewer('fit');
  33. }
  34. });
  35. this.markControl.on('view.sidebar.open', this, function(event, context, eventObject) {
  36. this.imageControl.hide();
  37. });
  38. this.markControl.on('view.sidebar.close', this, function(event, context, eventObject) {
  39. this.imageControl.show();
  40. });
  41. this.markControl.on('task.get.before', this, function(event, context, eventObject) {
  42. this.task = undefined;
  43. this.render();
  44. });
  45. this.markControl.on('task.get.success', this, function(event, context, eventObject) {
  46. this.task = context.task;
  47. this.render();
  48. });
  49. this.markControl.on('task.get.none', this, function(event, context, eventObject) {
  50. this.task = undefined;
  51. this.render();
  52. });
  53. this.markControl.on('image.position.change', this, function(event, context, topPercent) {
  54. if (this.task != undefined && this.loading == false) {
  55. this.updateScrollTop(topPercent);
  56. }
  57. });
  58. }
  59. SingleImageView.prototype.init = function() {
  60. this.configCache = {};
  61. //this.container = getDom(this.container_dom).appendTo(this.markControl.container.imageContent);
  62. this.container = this.markControl.container.imageContent;
  63. this.container.height(this.markControl.container.centerContent.height());
  64. this.container.perfectScrollbar({
  65. wheelSpeed: 20
  66. });
  67. this.imageHolder = getDom(this.image_holder_dom, this.markControl).appendTo(this.container);
  68. this.imageHolder.width('100%');
  69. this.imageControl = getDom(this.image_control_dom, this.markControl).insertBefore(this.markControl.container.assistantButton.parent());
  70. this.imageControl.find('.zoom-out-button').click(this, function(event) {
  71. var iviewer = event.data.iviewer;
  72. if (iviewer != undefined) {
  73. iviewer.iviewer('zoom_by', 1);
  74. }
  75. });
  76. this.imageControl.find('.zoom-in-button').click(this, function(event) {
  77. var iviewer = event.data.iviewer;
  78. if (iviewer != undefined) {
  79. iviewer.iviewer('zoom_by', -1);
  80. }
  81. });
  82. this.imageControl.find('.zoom-fit-button').click(this, function(event) {
  83. var iviewer = event.data.iviewer;
  84. if (iviewer != undefined) {
  85. iviewer.iviewer('fit');
  86. }
  87. });
  88. this.imageControl.find('.zoom-origin-button').click(this, function(event) {
  89. var iviewer = event.data.iviewer;
  90. if (iviewer != undefined) {
  91. iviewer.iviewer('set_zoom', 100);
  92. }
  93. });
  94. this.imageControl.find('.rotate-button').click(this, function(event) {
  95. var iviewer = event.data.iviewer;
  96. if (iviewer != undefined) {
  97. iviewer.iviewer('angle', 90);
  98. }
  99. });
  100. var self = this;
  101. this.container.scroll(function() {
  102. if (self.currentConfig != undefined) {
  103. self.currentConfig.scrollTop = self.container.scrollTop() / self.imageHolder.height();
  104. self.currentConfig.scrollLeft = self.container.scrollLeft() / self.imageHolder.width();
  105. }
  106. });
  107. }
  108. SingleImageView.prototype.render = function() {
  109. if (this.task != undefined) {
  110. var config = this.configCache[this.task.blockId];
  111. if (config == undefined) {
  112. config = {
  113. zoom: this.defaultZoom
  114. };
  115. this.configCache[this.task.blockId] = config;
  116. }
  117. this.currentConfig = config;
  118. this.loadImage();
  119. // this.imageControl.show();
  120. // this.imageHolder.show();
  121. // this.imageHolder.trigger('mouseenter');
  122. } else {
  123. this.imageControl.hide();
  124. this.imageHolder.hide();
  125. this.currentConfig = undefined;
  126. }
  127. }
  128. SingleImageView.prototype.loadImage = function() {
  129. if (this.task.imageData != undefined && this.loading == false) {
  130. this.loading = true;
  131. if (this.iviewer == undefined) {
  132. var self = this;
  133. this.iviewer = this.imageHolder.iviewer({
  134. src: self.task.imageData,
  135. zoom_delta: 1.2,
  136. zoom: 100,
  137. zoom_min: 10,
  138. ui_disabled: true,
  139. mousewheel: false,
  140. zoom_animation: false,
  141. onAfterZoom: function(ev, new_zoom) {
  142. self.onZoomSet(new_zoom);
  143. },
  144. onStopDrag: function(ev, point) {
  145. self.onStopDrag(point);
  146. },
  147. onFinishLoad: function(ev, url) {
  148. self.onImageLoad();
  149. },
  150. angle: function(ev, info) {
  151. self.onRotate(info.angle);
  152. }
  153. });
  154. this.imageHolder.css('overflow', '');
  155. } else {
  156. this.iviewer.iviewer('loadImage', this.task.imageData);
  157. }
  158. }
  159. }
  160. SingleImageView.prototype.onImageLoad = function() {
  161. this.loading = false;
  162. this.container.scrollTop(0);
  163. if (this.currentConfig != undefined && this.currentConfig.zoom != undefined && this.currentConfig.zoom != this.defaultZoom) {
  164. if (this.currentConfig.rotate != undefined && this.currentConfig.rotate > 0) {
  165. this.iviewer.iviewer('angle', this.currentConfig.rotate);
  166. }
  167. if (this.currentConfig.zoom != undefined && this.currentConfig.zoom != this.defaultZoom) {
  168. this.iviewer.iviewer('set_zoom', this.currentConfig.zoom);
  169. }
  170. }
  171. //this.imageHolder.find('img').css('border', '2px solid');
  172. // if (this.currentConfig.dx != undefined && this.currentConfig.dy != undefined) {
  173. // this.iviewer.iviewer('moveTo', this.currentConfig.dx, this.currentConfig.dy);
  174. // }
  175. this.imageControl.show();
  176. this.imageHolder.show();
  177. this.imageHolder.trigger('mouseenter');
  178. this.markControl.trigger('task.load.finish');
  179. }
  180. SingleImageView.prototype.onZoomSet = function(zoom) {
  181. if (this.loading == false && this.currentConfig != undefined) {
  182. this.currentConfig.zoom = zoom;
  183. this.updateScrollTop(this.currentConfig.scrollTop);
  184. this.updateScrollLeft(this.currentConfig.scrollLeft);
  185. }
  186. }
  187. SingleImageView.prototype.onStopDrag = function(point) {
  188. if (this.loading == false) {
  189. this.currentConfig.dx = point.x;
  190. this.currentConfig.dy = point.y;
  191. }
  192. }
  193. SingleImageView.prototype.onRotate = function(angle) {
  194. if (this.loading == false) {
  195. this.currentConfig.rotate = angle;
  196. }
  197. }
  198. SingleImageView.prototype.updateScrollTop = function(scrollTopPercent) {
  199. var height = this.imageHolder.height();
  200. var minHeight = this.container.height();
  201. if (scrollTopPercent != undefined && scrollTopPercent >= 0 && scrollTopPercent <= 1 && height > minHeight) {
  202. var left = height * (1 - scrollTopPercent);
  203. var scrollTop = left > minHeight ? (height - left) : (height - minHeight);
  204. this.container.scrollTop(scrollTop);
  205. } else {
  206. this.container.scrollTop(0);
  207. }
  208. }
  209. SingleImageView.prototype.updateScrollLeft = function(scrollLeftPercent) {
  210. var width = this.imageHolder.width();
  211. var minWidth = this.container.width();
  212. if (scrollLeftPercent != undefined && scrollLeftPercent >= 0 && scrollLeftPercent <= 1 && width > minWidth) {
  213. var left = width * (1 - scrollLeftPercent);
  214. var scrollLeft = left > minWidth ? (width - left) : (width - minWidth);
  215. this.container.scrollLeft(scrollLeft);
  216. } else {
  217. this.container.scrollLeft(0);
  218. }
  219. }
  220. SingleImageView.prototype.container_dom = '<div class="image-content"></div>';
  221. SingleImageView.prototype.image_control_dom = '<em>\
  222. <a href="#" class="btn zoom-out-button">放大</a>\
  223. <a href="#" class="btn zoom-in-button">缩小</a>\
  224. <a href="#" class="btn zoom-fit-button">适应</a>\
  225. <a href="#" class="btn zoom-origin-button">原始</a>\
  226. <a href="#" class="btn rotate-button">旋转</a>\
  227. </em>';
  228. SingleImageView.prototype.image_holder_dom = '<div style="position: relative;"></div>';