ImageClipper.js 11 KB

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