ImageClipper.js 11 KB

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