sheet-view.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. this.init();
  11. this.markControl.on('task.get.success', this, function(event, context, eventObject) {
  12. this.render(context.task.sheetUrls);
  13. });
  14. this.markControl.on('task.get.none', this, function(event, context, eventObject) {
  15. this.render();
  16. });
  17. this.markControl.on('task.get.before', this, function(event, context, eventObject) {
  18. this.render();
  19. });
  20. }
  21. SheetView.prototype.init = function() {
  22. var self = this;
  23. this.markControl.initMarkFunction();
  24. this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container);
  25. this.container.width(this.maxWidth);
  26. this.container.header = getDom(this.container_header_dom, this.markControl).appendTo(this.container);
  27. this.container.header.width('100%');
  28. this.container.content = getDom(this.container_content_dom, this.markControl).appendTo(this.container);
  29. this.container.content.width('100%');
  30. this.container.header.find('.image-close').click(function () {
  31. self.toggle(false);
  32. });
  33. this.control = getDom(this.control_dom, this.markControl).appendTo(this.markControl.container.assistant);
  34. this.control.find('#show-sheet-button').click(function () {
  35. self.markControl.container.assistant.hide();
  36. self.toggle(true);
  37. });
  38. this.control.find('#hide-sheet-button').click(function () {
  39. self.markControl.container.assistant.hide();
  40. self.toggle(false);
  41. });
  42. self.toggle(false);
  43. }
  44. SheetView.prototype.render = function(urls) {
  45. var self = this;
  46. if (urls != undefined && urls.length > 0) {
  47. for (var i = 0; i < urls.length; i++) {
  48. var url = urls[i];
  49. var sheetId = "sheet"+i;
  50. var nav = $('<a href="#" data-url="'+url+'" id="'+sheetId+'"><span>'+(i+1)+'</span></a>').appendTo(this.container.header.find('.image-switch'));
  51. }
  52. this.container.header.find('.image-switch a').click(function(){
  53. var url = this.getAttribute("data-url");
  54. var sheetId = this.getAttribute("id");
  55. self.renderContent(self.container.content, sheetId);
  56. });
  57. this.container.header.find('.image-switch a').first().click();
  58. } else {
  59. this.container.hide();
  60. }
  61. }
  62. SheetView.prototype.renderContent = function(content, sheetId) {
  63. content.width(this.markControl.container.centerContent.width());
  64. content.height(this.markControl.container.centerContent.height());
  65. content.empty();
  66. var url = document.getElementById(sheetId).getAttribute("data-url");
  67. var show = false;
  68. var i = sheetId.split("sheet")[1];
  69. if(i%2==0){
  70. show = true;
  71. }
  72. content.append($('<canvas id="sheet-canvas"></canvas>'));
  73. var image = new Image();
  74. image.crossOrigin = '';
  75. image.src = this.server + url ;
  76. image.canvas = document.getElementById('sheet-canvas');
  77. image.content = content;
  78. image.onload = function() {
  79. var ctx = this.canvas.getContext("2d");
  80. this.canvas.width = Math.min(this.width, this.content.width());
  81. this.canvas.height = this.canvas.width * this.height / this.width;
  82. ctx.drawImage(image, 0,0,this.width,this.height,0, 0, this.canvas.width, this.canvas.height);
  83. if(show){
  84. ctx.fillStyle = "#FFFFFF";
  85. ctx.fillRect(0, 0, this.canvas.width/2, this.canvas.height*3.7/10);
  86. }
  87. this.canvas.toDataURL("image/jpeg");
  88. }
  89. }
  90. SheetView.prototype.toggle = function(show) {
  91. if (show == true) {
  92. this.show = true;
  93. this.container.show();
  94. } else {
  95. this.show = false;
  96. this.container.hide();
  97. }
  98. }
  99. SheetView.prototype.control_dom = '<h3 class="popover-title">查看原卷</h3>\
  100. <div class="popover-content"><p id="sheet-list" class="popover-list">\
  101. <a href="#" id="show-sheet-button">打开</a><a href="#" id="hide-sheet-button">关闭</a>\
  102. </p></div>';
  103. SheetView.prototype.container_dom = '<div class="image-view-popover sheet" style=" width:100%; height:100vh;"></div>';
  104. SheetView.prototype.container_header_dom = '<div class="popover-header">\
  105. <p class="title">原图切换</p>\
  106. <p class="image-switch"></p>\
  107. <p class="image-close"><img src="{staticServer}/mark-new/images/images-close-b.png" /></p></div>';
  108. SheetView.prototype.container_content_dom = '<div class="popover-cont"></div>';