ImageClipper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.ImageClipper = MWF.APPImageClipper = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Module,
  5. initialize: function(node, json, form, options){
  6. this.node = $(node);
  7. this.node.store("module", this);
  8. this.json = json;
  9. this.form = form;
  10. this.field = true;
  11. },
  12. _loadUserInterface: function(){
  13. this.field = true;
  14. this.node.empty();
  15. var data = this._getBusinessData();
  16. if( data ){
  17. var img = new Element("img",{
  18. src : MWF.xDesktop.getImageSrc( data )
  19. });
  20. if (layout.mobile || COMMON.Browser.Platform.isMobile) {
  21. img.setStyles({
  22. "max-width": "90%"
  23. })
  24. }else if( this.json.clipperType == "size" ){
  25. var width = this.json.imageWidth;
  26. var height = this.json.imageHeight;
  27. if( width && height ){
  28. img.setStyles({
  29. width : width+"px",
  30. height : height+"px"
  31. })
  32. }
  33. }
  34. img.inject( this.node );
  35. }
  36. if( this.readonly || this.json.isReadonly )return;
  37. var divBottom = new Element("div").inject( this.node );
  38. var button = new Element("button").inject(divBottom);
  39. button.set({
  40. //"id": this.json.id,
  41. "text": this.json.name || this.json.id,
  42. "styles": this.form.css.buttonStyles,
  43. "MWFType": this.json.type
  44. });
  45. button.addEvent("click", function(){
  46. this.validationMode();
  47. var d = this._getBusinessData();
  48. if (layout.mobile){
  49. o2.imageClipperCallback = function( str ){
  50. var data = JSON.parse( str );
  51. this.setData( data ? data.fileId : "" );
  52. this.validation();
  53. o2.imageClipperCallback = null;
  54. }.bind(this);
  55. // 兼容cms编辑表单
  56. var referencetype = "processPlatformJob";
  57. if(this.form.businessData.work && this.form.businessData.work.referencetype) {
  58. referencetype = this.form.businessData.work.referencetype
  59. }
  60. var jsonString = JSON.stringify({
  61. "mwfId" : this.json.id,
  62. "callback" : "o2.imageClipperCallback",
  63. "referencetype": referencetype,
  64. "reference": this.form.businessData.work.job
  65. });
  66. if( window.o2android && window.o2android.uploadImage2FileStorage ){
  67. window.o2android.uploadImage2FileStorage(jsonString)
  68. }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.uploadImage2FileStorage){
  69. window.webkit.messageHandlers.uploadImage2FileStorage.postMessage(jsonString);
  70. }else {
  71. this.selectImage( d, function(data){
  72. this.setData( data ? data.id : "" );
  73. this.validation();
  74. }.bind(this));
  75. }
  76. }else{
  77. this.selectImage( d, function(data){
  78. this.setData( data ? data.id : "" );
  79. this.validation();
  80. }.bind(this));
  81. }
  82. }.bind(this));
  83. },
  84. getTextData : function(){
  85. var value = this._getBusinessData() || "";
  86. return {"value": [value], "text": [value]};
  87. },
  88. getData: function( data ){
  89. return this._getBusinessData() || "";
  90. },
  91. setData: function( data ){
  92. this._setBusinessData(data);
  93. var img = this.node.getElements("img");
  94. if( img && img.length )img.destroy();
  95. if( !data )return;
  96. var img = new Element("img",{
  97. src : MWF.xDesktop.getImageSrc( data )
  98. }).inject( this.node, "top" );
  99. if (layout.mobile || COMMON.Browser.Platform.isMobile) {
  100. img.setStyles({
  101. "max-width": "90%"
  102. })
  103. }else if( this.json.clipperType == "size" ){
  104. var width = this.json.imageWidth;
  105. var height = this.json.imageHeight;
  106. if (width && height) {
  107. img.setStyles({
  108. width: width + "px",
  109. height: height + "px"
  110. })
  111. }
  112. }
  113. },
  114. selectImage: function(d, callback){
  115. var clipperType = this.json.clipperType || "unrestricted";
  116. var ratio = 1;
  117. var description = "";
  118. var maxSize = 800;
  119. if( clipperType == "unrestricted" ){
  120. ratio = 0;
  121. }else if( clipperType == "size" ){
  122. var width = this.json.imageWidth.toInt();
  123. var height = this.json.imageHeight.toInt();
  124. ratio = width / height;
  125. maxSize = Math.max( width, height );
  126. if( !isNaN( width ) && !isNaN( height ) ){
  127. description = MWF.LP.widget.pictureSize.replace(/{width}/g, width).replace(/{height}/g, height);
  128. }
  129. }else if( clipperType == "ratio" ){
  130. ratio = this.json.imageRatio || 1;
  131. description = MWF.LP.widget.pictureRatio.replace(/{ratio}/g, ratio);
  132. }
  133. MWF.xDesktop.requireApp("process.Xform", "widget.ImageClipper", function(){
  134. this.imageClipper = new MWF.xApplication.process.Xform.widget.ImageClipper(this.form.app, {
  135. "style": "default",
  136. "aspectRatio" : ratio,
  137. "description" : description,
  138. "imageUrl" : d ? MWF.xDesktop.getImageSrc( d ) : "",
  139. "reference" : this.form.businessData.work.job,
  140. "referenceType": "processPlatformJob",
  141. "resultMaxSize" : maxSize,
  142. "onChange" : function(){
  143. callback( { src : this.imageClipper.imageSrc, id : this.imageClipper.imageId } );
  144. }.bind(this)
  145. });
  146. this.imageClipper.load();
  147. }.bind(this));
  148. },
  149. createErrorNode: function(text){
  150. var node = new Element("div");
  151. var iconNode = new Element("div", {
  152. "styles": {
  153. "width": "20px",
  154. "height": "20px",
  155. "float": "left",
  156. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  157. }
  158. }).inject(node);
  159. var textNode = new Element("div", {
  160. "styles": {
  161. "line-height": "20px",
  162. "margin-left": "20px",
  163. "color": "red",
  164. "word-break": "keep-all"
  165. },
  166. "text": text
  167. }).inject(node);
  168. return node;
  169. },
  170. notValidationMode: function(text){
  171. if (!this.isNotValidationMode){
  172. this.isNotValidationMode = true;
  173. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  174. this.node.setStyle("border", "1px solid red");
  175. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  176. this.showNotValidationMode(this.node);
  177. if (!this.node.isIntoView()) this.node.scrollIntoView();
  178. }
  179. },
  180. showNotValidationMode: function(node){
  181. var p = node.getParent("div");
  182. if (p){
  183. if (p.get("MWFtype") == "tab$Content"){
  184. if (p.getParent("div").getStyle("display")=="none"){
  185. var contentAreaNode = p.getParent("div").getParent("div");
  186. var tabAreaNode = contentAreaNode.getPrevious("div");
  187. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  188. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  189. tabNode.click();
  190. p = tabAreaNode.getParent("div");
  191. }
  192. }
  193. this.showNotValidationMode(p);
  194. }
  195. },
  196. validationMode: function(){
  197. if (this.isNotValidationMode){
  198. this.isNotValidationMode = false;
  199. this.node.setStyles(this.node.retrieve("borderStyle"));
  200. if (this.errNode){
  201. this.errNode.destroy();
  202. this.errNode = null;
  203. }
  204. }
  205. },
  206. validationConfigItem: function(routeName, data){
  207. var flag = (data.status=="all") ? true: (routeName == data.decision);
  208. if (flag){
  209. var n = this.getData();
  210. var v = (data.valueType=="value") ? n : n.length;
  211. switch (data.operateor){
  212. case "isnull":
  213. if (!v){
  214. this.notValidationMode(data.prompt);
  215. return false;
  216. }
  217. break;
  218. case "notnull":
  219. if (v){
  220. this.notValidationMode(data.prompt);
  221. return false;
  222. }
  223. break;
  224. case "gt":
  225. if (v>data.value){
  226. this.notValidationMode(data.prompt);
  227. return false;
  228. }
  229. break;
  230. case "lt":
  231. if (v<data.value){
  232. this.notValidationMode(data.prompt);
  233. return false;
  234. }
  235. break;
  236. case "equal":
  237. if (v==data.value){
  238. this.notValidationMode(data.prompt);
  239. return false;
  240. }
  241. break;
  242. case "neq":
  243. if (v!=data.value){
  244. this.notValidationMode(data.prompt);
  245. return false;
  246. }
  247. break;
  248. case "contain":
  249. if (v.indexOf(data.value)!=-1){
  250. this.notValidationMode(data.prompt);
  251. return false;
  252. }
  253. break;
  254. case "notcontain":
  255. if (v.indexOf(data.value)==-1){
  256. this.notValidationMode(data.prompt);
  257. return false;
  258. }
  259. break;
  260. }
  261. }
  262. return true;
  263. },
  264. validationConfig: function(routeName, opinion){
  265. if (this.json.validationConfig){
  266. if (this.json.validationConfig.length){
  267. for (var i=0; i<this.json.validationConfig.length; i++) {
  268. var data = this.json.validationConfig[i];
  269. if (!this.validationConfigItem(routeName, data)) return false;
  270. }
  271. }
  272. return true;
  273. }
  274. return true;
  275. },
  276. validation: function(routeName, opinion){
  277. if (!this.validationConfig(routeName, opinion)) return false;
  278. if (!this.json.validation) return true;
  279. if (!this.json.validation.code) return true;
  280. var flag = this.form.Macro.exec(this.json.validation.code, this);
  281. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  282. if (flag.toString()!="true"){
  283. this.notValidationMode(flag);
  284. return false;
  285. }
  286. return true;
  287. }
  288. });