sheet-view.js 4.9 KB

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