sheet-view.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //查看原始题卡模块
  2. var sheet_view = function(option, success) {
  3. var object = new SheetView(option);
  4. success();
  5. return object;
  6. }
  7. function SheetView(option) {
  8. this.markControl = option.markControl;
  9. this.server = option.server;
  10. if (option.sheetConfig) {
  11. this.sheetConfig = JSON.parse(option.sheetConfig);
  12. } else {
  13. this.sheetConfig = [];
  14. }
  15. this.init();
  16. this.markControl.on('task.get.success', this, function(event, context, eventObject) {
  17. this.render(context.task.sheetUrls);
  18. });
  19. this.markControl.on('task.get.none', this, function(event, context, eventObject) {
  20. this.render();
  21. });
  22. this.markControl.on('task.get.before', this, function(event, context, eventObject) {
  23. this.render();
  24. });
  25. this.markControl.on('view.sidebar.open', this, function(event, context, eventObject) {
  26. if (context.task != undefined) {
  27. this.render(context.task.sheetUrls);
  28. }
  29. });
  30. this.markControl.on('view.sidebar.close', this, function(event, context, eventObject) {
  31. if (context.task != undefined) {
  32. this.render(context.task.sheetUrls);
  33. }
  34. });
  35. }
  36. SheetView.prototype.init = function() {
  37. var self = this;
  38. this.markControl.initMarkFunction();
  39. this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container);
  40. this.container.width(this.maxWidth);
  41. this.container.header = getDom(this.container_header_dom, this.markControl).appendTo(this.container);
  42. this.container.header.width('100%');
  43. this.container.content = getDom(this.container_content_dom, this.markControl).appendTo(this.container);
  44. this.container.content.width('100%');
  45. this.container.header.find('.image-close').click(function() {
  46. self.toggle(false);
  47. });
  48. this.control = getDom(this.control_dom, this.markControl).appendTo(this.markControl.container.assistant);
  49. this.control.find('#show-sheet-button').click(function() {
  50. self.markControl.container.assistant.hide();
  51. self.toggle(true);
  52. });
  53. this.control.find('#hide-sheet-button').click(function() {
  54. self.markControl.container.assistant.hide();
  55. self.toggle(false);
  56. });
  57. self.toggle(false);
  58. }
  59. SheetView.prototype.render = function(urls) {
  60. var self = this;
  61. self.container.header.find('.image-switch').empty();
  62. self.container.content.empty();
  63. if (urls != undefined && urls.length > 0) {
  64. for (var i = 0; i < urls.length; i++) {
  65. var url = urls[i];
  66. var sheetId = "sheet" + i;
  67. var nav = $('<a href="#" data-url="' + url + '" id="' + sheetId + '"><span>' + (i + 1) + '</span></a>').appendTo(this.container.header.find('.image-switch'));
  68. }
  69. this.container.header.find('.image-switch a').click(function() {
  70. var url = this.getAttribute("data-url");
  71. var sheetId = this.getAttribute("id");
  72. self.renderContent(self.container.content, sheetId);
  73. });
  74. this.container.header.find('.image-switch a').first().click();
  75. } else {
  76. this.container.hide();
  77. }
  78. }
  79. SheetView.prototype.renderContent = function(content, sheetId) {
  80. content.width(this.markControl.container.centerContent.width());
  81. content.height(this.markControl.container.centerContent.height());
  82. content.empty();
  83. var url = document.getElementById(sheetId).getAttribute("data-url");
  84. var show = false;
  85. var i = sheetId.split("sheet")[1];
  86. // if(i%2==0){
  87. // show = true;
  88. // }
  89. content.append($('<canvas id="sheet-canvas"></canvas>'));
  90. var image = new Image();
  91. image.crossOrigin = '';
  92. image.src = this.server + url;
  93. image.canvas = document.getElementById('sheet-canvas');
  94. image.content = content;
  95. var self = this;
  96. image.onload = function() {
  97. var ctx = image.canvas.getContext("2d");
  98. image.canvas.width = Math.min(image.width, image.content.width());
  99. var scale = image.canvas.width / image.width;
  100. image.canvas.height = image.height * scale;
  101. ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, image.canvas.width, image.canvas.height);
  102. ctx.fillStyle = "#FFFFFF";
  103. if(self.sheetConfig.length!=0){
  104. for (var j = 0; j < self.sheetConfig.length; j++) {
  105. var config = self.sheetConfig[j];
  106. var index = parseNumber(i) + 1;
  107. if (config.i == index) {
  108. //兼容简单模式1:0:0:0:0
  109. if(config.x==0 && config.y==0 && config.w==0 && config.h==0){
  110. ctx.fillRect(0, 0, image.width * scale, image.height * scale);
  111. }else{
  112. ctx.fillRect(config.x * scale, config.y * scale, config.w * scale, config.h * scale);
  113. }
  114. }
  115. }
  116. }else{
  117. ctx.fillRect(0, 0, this.canvas.width/2, this.canvas.height*3.7/10);
  118. }
  119. //this.canvas.toDataURL("image/jpeg");
  120. }
  121. }
  122. SheetView.prototype.toggle = function(show) {
  123. if (show == true) {
  124. this.show = true;
  125. this.container.show();
  126. } else {
  127. this.show = false;
  128. this.container.hide();
  129. }
  130. }
  131. SheetView.prototype.control_dom = '<h3 class="popover-title">查看原卷</h3>\
  132. <div class="popover-content"><p id="sheet-list" class="popover-list">\
  133. <a href="#" id="show-sheet-button">打开</a><a href="#" id="hide-sheet-button">关闭</a>\
  134. </p></div>';
  135. SheetView.prototype.container_dom = '<div class="image-view-popover sheet" style=" width:100%; height:100vh;"></div>';
  136. SheetView.prototype.container_header_dom = '<div class="popover-header">\
  137. <p class="title">原图切换</p>\
  138. <p class="image-switch"></p>\
  139. <p class="image-close"><img src="{staticServer}/mark-new/images/images-close-b.png" /></p></div>';
  140. SheetView.prototype.container_content_dom = '<div class="popover-cont"></div>';