ImageClipper.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. MWF.xApplication.process.Xform.widget = MWF.xApplication.process.Xform.widget || {};
  2. MWF.require("MWF.widget.ImageClipper", null, false);
  3. MWF.xApplication.process.Xform.widget.ImageClipper = new Class({
  4. Implements: [Options, Events],
  5. Extends: MWF.widget.Common,
  6. options: {
  7. "reference" : "",
  8. "referenceType" : "",
  9. "imageUrl" : "",
  10. "resultMaxSize" : 800,
  11. "description" : "",
  12. "title": "Select Image",
  13. "style": "default",
  14. "aspectRatio": 1
  15. },
  16. initialize: function(designer, options){
  17. this.setOptions(options);
  18. this.app = designer;
  19. this.path = "../x_component_process_Xform/widget/$ImageClipper/";
  20. this.cssPath = "../x_component_process_Xform/widget/$ImageClipper/"+this.options.style+"/css.wcss";
  21. this._loadCss();
  22. },
  23. load: function(data){
  24. this.data = data;
  25. var options = {};
  26. var width = "668";
  27. var height = "510";
  28. width = width.toInt();
  29. height = height.toInt();
  30. var outBody = this.app.content;
  31. if (layout.mobile){
  32. outBody = $(document.body);
  33. }
  34. var size = outBody.getSize();
  35. var x = (size.x-width)/2;
  36. var y = (size.y-height)/2;
  37. if (x<0) x = 0;
  38. if (y<0) y = 0;
  39. if (layout.mobile){
  40. x = 20;
  41. y = 0;
  42. }
  43. var _self = this;
  44. if (layout.mobile) { //移动端直接选择图片
  45. this.fileNode = new Element("input.file", {
  46. "type" : "file",
  47. "accept":" .jpeg, .jpg, .png",
  48. "multiple": false,
  49. "styles" : {"display":"none"}
  50. }).inject(outBody);
  51. this.fileNode.addEvent("change", function(event){
  52. var file=this.fileNode.files[0];
  53. if(file) {
  54. this.uploadImageForH5(file);
  55. }
  56. this.fileNode.destroy();
  57. this.fileNode = null;
  58. }.bind(this));
  59. setTimeout( function(){ this.fileNode.click(); }.bind(this), 100);
  60. }else {
  61. MWF.require("MWF.xDesktop.Dialog", function() {
  62. var dlg = new MWF.xDesktop.Dialog({
  63. "title": this.options.title || "Select Image",
  64. "style": options.style || "image",
  65. "top": y,
  66. "left": x - 20,
  67. "fromTop": y,
  68. "fromLeft": x - 20,
  69. "width": width,
  70. "height": height,
  71. "html": "<div></div>",
  72. "maskNode": outBody,
  73. "container": outBody,
  74. "buttonList": [
  75. {
  76. "text": MWF.LP.process.button.ok,
  77. "action": function () {
  78. if( _self.image.getResizedImage() ){
  79. _self.image.uploadImage( function( json ){
  80. _self.imageSrc = MWF.xDesktop.getImageSrc( json.id );
  81. _self.imageId = json.id;
  82. _self.fireEvent("change");
  83. this.close();
  84. }.bind(this));
  85. }else{
  86. _self.imageSrc = "";
  87. _self.imageId = "";
  88. _self.fireEvent("change");
  89. this.close();
  90. }
  91. }
  92. },
  93. {
  94. "text": MWF.LP.process.button.cancel,
  95. "action": function () {
  96. this.close();
  97. }
  98. }
  99. ]
  100. });
  101. dlg.show();
  102. this.image = new MWF.widget.ImageClipper(dlg.content.getFirst(), {
  103. "aspectRatio": this.options.aspectRatio,
  104. "description" : this.options.description,
  105. "imageUrl" : this.options.imageUrl,
  106. "resultMaxSize" : this.options.resultMaxSize,
  107. "reference" : this.options.reference,
  108. "referenceType": this.options.referenceType,
  109. "resetEnable" : true
  110. });
  111. this.image.load(this.data);
  112. }.bind(this));
  113. }
  114. },
  115. //移动端h5 上传图片
  116. uploadImageForH5:function(file) {
  117. //公共图片上传服务
  118. var maxSize = this.options.resultMaxSize ? this.options.resultMaxSize : 800;
  119. var formData = new FormData();
  120. formData.append('file', file, file.name);
  121. // if( this.options.data ){
  122. // Object.each(this.options.data, function(v, k){
  123. // formData.append(k, v)
  124. // });
  125. // }
  126. o2.xDesktop.uploadImageByScale(
  127. this.options.reference,
  128. this.options.referenceType,
  129. maxSize,
  130. formData,
  131. file,
  132. function(json){
  133. this.imageSrc = MWF.xDesktop.getImageSrc( json.id );
  134. this.imageId = json.id;
  135. this.fireEvent("change");
  136. }.bind(this),
  137. function(json) {
  138. this.imageSrc = "";
  139. this.imageId = "";
  140. this.fireEvent("change");
  141. }.bind(this)
  142. );
  143. }
  144. });