ImageClipper.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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": MWF.xApplication.process.Xform.LP ? MWF.xApplication.process.Xform.LP.imageClipper : "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 = "678";
  27. var height = "520";
  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.download || "下载",
  77. // "action": function () {
  78. // _self.download();
  79. // }
  80. // },
  81. // {
  82. // "text": MWF.LP.process.button.remove || "删除",
  83. // "action": function () {
  84. // _self.remove();
  85. // }
  86. // },
  87. {
  88. "text": MWF.LP.process.button.ok,
  89. "action": function () {
  90. if( _self.image.getResizedImage() ){
  91. _self.image.uploadImage( function( json ){
  92. _self.imageSrc = MWF.xDesktop.getImageSrc( json.id );
  93. _self.imageId = json.id;
  94. _self.fireEvent("change");
  95. this.close();
  96. }.bind(this));
  97. }else{
  98. _self.imageSrc = "";
  99. _self.imageId = "";
  100. _self.fireEvent("change");
  101. this.close();
  102. }
  103. }
  104. },
  105. {
  106. "text": MWF.LP.process.button.cancel,
  107. "action": function () {
  108. this.close();
  109. }
  110. }
  111. ]
  112. });
  113. dlg.show();
  114. this.image = new MWF.widget.ImageClipper(dlg.content.getFirst(), {
  115. "aspectRatio": this.options.aspectRatio,
  116. "description" : this.options.description,
  117. "imageUrl" : this.options.imageUrl,
  118. "resultMaxSize" : this.options.resultMaxSize,
  119. "reference" : this.options.reference,
  120. "referenceType": this.options.referenceType,
  121. "resetEnable" : true
  122. });
  123. this.image.load(this.data);
  124. }.bind(this));
  125. }
  126. },
  127. //移动端h5 上传图片
  128. uploadImageForH5:function(file) {
  129. //公共图片上传服务
  130. var maxSize = this.options.resultMaxSize ? this.options.resultMaxSize : 800;
  131. var formData = new FormData();
  132. formData.append('file', file, file.name);
  133. // if( this.options.data ){
  134. // Object.each(this.options.data, function(v, k){
  135. // formData.append(k, v)
  136. // });
  137. // }
  138. o2.xDesktop.uploadImageByScale(
  139. this.options.reference,
  140. this.options.referenceType,
  141. maxSize,
  142. formData,
  143. file,
  144. function(json){
  145. this.imageSrc = MWF.xDesktop.getImageSrc( json.id );
  146. this.imageId = json.id;
  147. this.fireEvent("change");
  148. }.bind(this),
  149. function(json) {
  150. this.imageSrc = "";
  151. this.imageId = "";
  152. this.fireEvent("change");
  153. }.bind(this)
  154. );
  155. }
  156. // download: function () {
  157. // if( this.image ){
  158. // this.image.downloadSourceImage();
  159. // }else{
  160. // var src = this.imageSrc || this.options.imageUrl;
  161. // if(src)window.open( src, "_blank" );
  162. // }
  163. // }
  164. });