ImageClipper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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{
  69. window.webkit.messageHandlers.uploadImage2FileStorage.postMessage(jsonString);
  70. }
  71. }else{
  72. this.selectImage( d, function(data){
  73. this.setData( data ? data.id : "" );
  74. this.validation();
  75. }.bind(this));
  76. }
  77. }.bind(this));
  78. },
  79. getTextData : function(){
  80. var value = this._getBusinessData() || "";
  81. return {"value": [value], "text": [value]};
  82. },
  83. getData: function( data ){
  84. return this._getBusinessData() || "";
  85. },
  86. setData: function( data ){
  87. this._setBusinessData(data);
  88. var img = this.node.getElements("img");
  89. if( img && img.length )img.destroy();
  90. if( !data )return;
  91. var img = new Element("img",{
  92. src : MWF.xDesktop.getImageSrc( data )
  93. }).inject( this.node, "top" );
  94. if (layout.mobile || COMMON.Browser.Platform.isMobile) {
  95. img.setStyles({
  96. "max-width": "90%"
  97. })
  98. }else if( this.json.clipperType == "size" ){
  99. var width = this.json.imageWidth;
  100. var height = this.json.imageHeight;
  101. if (width && height) {
  102. img.setStyles({
  103. width: width + "px",
  104. height: height + "px"
  105. })
  106. }
  107. }
  108. },
  109. selectImage: function(d, callback){
  110. var clipperType = this.json.clipperType || "unrestricted";
  111. var ratio = 1;
  112. var description = "";
  113. var maxSize = 800;
  114. if( clipperType == "unrestricted" ){
  115. ratio = 0;
  116. }else if( clipperType == "size" ){
  117. var width = this.json.imageWidth.toInt();
  118. var height = this.json.imageHeight.toInt();
  119. ratio = width / height;
  120. maxSize = Math.max( width, height );
  121. if( !isNaN( width ) && !isNaN( height ) ){
  122. description = MWF.LP.widget.pictureSize.replace(/{width}/g, width).replace(/{height}/g, height);
  123. }
  124. }else if( clipperType == "ratio" ){
  125. ratio = this.json.imageRatio || 1;
  126. description = MWF.LP.widget.pictureRatio.replace(/{ratio}/g, ratio);
  127. }
  128. MWF.xDesktop.requireApp("process.Xform", "widget.ImageClipper", function(){
  129. this.imageClipper = new MWF.xApplication.process.Xform.widget.ImageClipper(this.form.app, {
  130. "style": "default",
  131. "aspectRatio" : ratio,
  132. "description" : description,
  133. "imageUrl" : d ? MWF.xDesktop.getImageSrc( d ) : "",
  134. "reference" : this.form.businessData.work.job,
  135. "referenceType": "processPlatformJob",
  136. "resultMaxSize" : maxSize,
  137. "onChange" : function(){
  138. callback( { src : this.imageClipper.imageSrc, id : this.imageClipper.imageId } );
  139. }.bind(this)
  140. });
  141. this.imageClipper.load();
  142. }.bind(this));
  143. },
  144. createErrorNode: function(text){
  145. var node = new Element("div");
  146. var iconNode = new Element("div", {
  147. "styles": {
  148. "width": "20px",
  149. "height": "20px",
  150. "float": "left",
  151. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  152. }
  153. }).inject(node);
  154. var textNode = new Element("div", {
  155. "styles": {
  156. "line-height": "20px",
  157. "margin-left": "20px",
  158. "color": "red",
  159. "word-break": "keep-all"
  160. },
  161. "text": text
  162. }).inject(node);
  163. return node;
  164. },
  165. notValidationMode: function(text){
  166. if (!this.isNotValidationMode){
  167. this.isNotValidationMode = true;
  168. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  169. this.node.setStyle("border", "1px solid red");
  170. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  171. this.showNotValidationMode(this.node);
  172. if (!this.node.isIntoView()) this.node.scrollIntoView();
  173. }
  174. },
  175. showNotValidationMode: function(node){
  176. var p = node.getParent("div");
  177. if (p){
  178. if (p.get("MWFtype") == "tab$Content"){
  179. if (p.getParent("div").getStyle("display")=="none"){
  180. var contentAreaNode = p.getParent("div").getParent("div");
  181. var tabAreaNode = contentAreaNode.getPrevious("div");
  182. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  183. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  184. tabNode.click();
  185. p = tabAreaNode.getParent("div");
  186. }
  187. }
  188. this.showNotValidationMode(p);
  189. }
  190. },
  191. validationMode: function(){
  192. if (this.isNotValidationMode){
  193. this.isNotValidationMode = false;
  194. this.node.setStyles(this.node.retrieve("borderStyle"));
  195. if (this.errNode){
  196. this.errNode.destroy();
  197. this.errNode = null;
  198. }
  199. }
  200. },
  201. validationConfigItem: function(routeName, data){
  202. var flag = (data.status=="all") ? true: (routeName == data.decision);
  203. if (flag){
  204. var n = this.getData();
  205. var v = (data.valueType=="value") ? n : n.length;
  206. switch (data.operateor){
  207. case "isnull":
  208. if (!v){
  209. this.notValidationMode(data.prompt);
  210. return false;
  211. }
  212. break;
  213. case "notnull":
  214. if (v){
  215. this.notValidationMode(data.prompt);
  216. return false;
  217. }
  218. break;
  219. case "gt":
  220. if (v>data.value){
  221. this.notValidationMode(data.prompt);
  222. return false;
  223. }
  224. break;
  225. case "lt":
  226. if (v<data.value){
  227. this.notValidationMode(data.prompt);
  228. return false;
  229. }
  230. break;
  231. case "equal":
  232. if (v==data.value){
  233. this.notValidationMode(data.prompt);
  234. return false;
  235. }
  236. break;
  237. case "neq":
  238. if (v!=data.value){
  239. this.notValidationMode(data.prompt);
  240. return false;
  241. }
  242. break;
  243. case "contain":
  244. if (v.indexOf(data.value)!=-1){
  245. this.notValidationMode(data.prompt);
  246. return false;
  247. }
  248. break;
  249. case "notcontain":
  250. if (v.indexOf(data.value)==-1){
  251. this.notValidationMode(data.prompt);
  252. return false;
  253. }
  254. break;
  255. }
  256. }
  257. return true;
  258. },
  259. validationConfig: function(routeName, opinion){
  260. if (this.json.validationConfig){
  261. if (this.json.validationConfig.length){
  262. for (var i=0; i<this.json.validationConfig.length; i++) {
  263. var data = this.json.validationConfig[i];
  264. if (!this.validationConfigItem(routeName, data)) return false;
  265. }
  266. }
  267. return true;
  268. }
  269. return true;
  270. },
  271. validation: function(routeName, opinion){
  272. if (!this.validationConfig(routeName, opinion)) return false;
  273. if (!this.json.validation) return true;
  274. if (!this.json.validation.code) return true;
  275. var flag = this.form.Macro.exec(this.json.validation.code, this);
  276. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  277. if (flag.toString()!="true"){
  278. this.notValidationMode(flag);
  279. return false;
  280. }
  281. return true;
  282. }
  283. });