ImageClipper.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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( this.json.clipperType == "size" ){
  21. var width = this.json.imageWidth;
  22. var height = this.json.imageHeight;
  23. if( width && height ){
  24. img.setStyles({
  25. width : width+"px",
  26. height : height+"px"
  27. })
  28. }
  29. }
  30. img.inject( this.node );
  31. }
  32. if( this.readonly )return;
  33. var divBottom = new Element("div").inject( this.node );
  34. var button = new Element("button").inject(divBottom);
  35. button.set({
  36. //"id": this.json.id,
  37. "text": this.json.name || this.json.id,
  38. "styles": this.form.css.buttonStyles,
  39. "MWFType": this.json.type
  40. });
  41. button.addEvent("click", function(){
  42. this.validationMode();
  43. var d = this._getBusinessData();
  44. this.selectImage( d, function(data){
  45. this.setData( data ? data.id : "" );
  46. this.validation();
  47. }.bind(this));
  48. }.bind(this));
  49. },
  50. getData: function( data ){
  51. return this._getBusinessData() || "";
  52. },
  53. setData: function( data ){
  54. this._setBusinessData(data);
  55. var img = this.node.getElements("img");
  56. if( img && img.length )img.destroy();
  57. if( !data )return;
  58. var img = new Element("img",{
  59. src : MWF.xDesktop.getImageSrc( data )
  60. }).inject( this.node, "top" );
  61. if( this.json.clipperType == "size" ){
  62. var width = this.json.imageWidth;
  63. var height = this.json.imageHeight;
  64. if( width && height ){
  65. img.setStyles({
  66. width : width+"px",
  67. height : height+"px"
  68. })
  69. }
  70. }
  71. },
  72. selectImage: function(d, callback){
  73. var clipperType = this.json.clipperType || "unrestricted";
  74. var ratio = 1;
  75. var description = "";
  76. var maxSize = 800;
  77. if( clipperType == "unrestricted" ){
  78. ratio = 0;
  79. }else if( clipperType == "size" ){
  80. var width = this.json.imageWidth.toInt();
  81. var height = this.json.imageHeight.toInt();
  82. ratio = width / height;
  83. maxSize = Math.max( width, height );
  84. if( !isNaN( width ) && !isNaN( height ) ){
  85. description = MWF.LP.widget.pictureSize.replace(/{width}/g, width).replace(/{height}/g, height);
  86. }
  87. }else if( clipperType == "ratio" ){
  88. ratio = this.json.imageRatio || 1;
  89. description = MWF.LP.widget.pictureRatio.replace(/{ratio}/g, ratio);
  90. }
  91. MWF.xDesktop.requireApp("process.Xform", "widget.ImageClipper", function(){
  92. this.imageClipper = new MWF.xApplication.process.Xform.widget.ImageClipper(this.form.app, {
  93. "style": "default",
  94. "aspectRatio" : ratio,
  95. "description" : description,
  96. "imageUrl" : d ? MWF.xDesktop.getImageSrc( d ) : "",
  97. "reference" : this.form.businessData.work.job,
  98. "referenceType": "processPlatformJob",
  99. "resultMaxSize" : maxSize,
  100. "onChange" : function(){
  101. callback( { src : this.imageClipper.imageSrc, id : this.imageClipper.imageId } );
  102. }.bind(this)
  103. });
  104. this.imageClipper.load();
  105. }.bind(this));
  106. },
  107. createErrorNode: function(text){
  108. var node = new Element("div");
  109. var iconNode = new Element("div", {
  110. "styles": {
  111. "width": "20px",
  112. "height": "20px",
  113. "float": "left",
  114. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  115. }
  116. }).inject(node);
  117. var textNode = new Element("div", {
  118. "styles": {
  119. "line-height": "20px",
  120. "margin-left": "20px",
  121. "color": "red",
  122. "word-break": "keep-all"
  123. },
  124. "text": text
  125. }).inject(node);
  126. return node;
  127. },
  128. notValidationMode: function(text){
  129. if (!this.isNotValidationMode){
  130. this.isNotValidationMode = true;
  131. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  132. this.node.setStyle("border", "1px solid red");
  133. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  134. this.showNotValidationMode(this.node);
  135. }
  136. },
  137. showNotValidationMode: function(node){
  138. var p = node.getParent("div");
  139. if (p){
  140. if (p.get("MWFtype") == "tab$Content"){
  141. if (p.getParent("div").getStyle("display")=="none"){
  142. var contentAreaNode = p.getParent("div").getParent("div");
  143. var tabAreaNode = contentAreaNode.getPrevious("div");
  144. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  145. var tabNode = tabAreaNode.getChildren()[idx];
  146. tabNode.click();
  147. p = tabAreaNode.getParent("div");
  148. }
  149. }
  150. this.showNotValidationMode(p);
  151. }
  152. },
  153. validationMode: function(){
  154. if (this.isNotValidationMode){
  155. this.isNotValidationMode = false;
  156. this.node.setStyles(this.node.retrieve("borderStyle"));
  157. if (this.errNode){
  158. this.errNode.destroy();
  159. this.errNode = null;
  160. }
  161. }
  162. },
  163. validationConfigItem: function(routeName, data){
  164. var flag = (data.status=="all") ? true: (routeName == data.decision);
  165. if (flag){
  166. var n = this.getData();
  167. var v = (data.valueType=="value") ? n : n.length;
  168. switch (data.operateor){
  169. case "isnull":
  170. if (!v){
  171. this.notValidationMode(data.prompt);
  172. return false;
  173. }
  174. break;
  175. case "notnull":
  176. if (v){
  177. this.notValidationMode(data.prompt);
  178. return false;
  179. }
  180. break;
  181. case "gt":
  182. if (v>data.value){
  183. this.notValidationMode(data.prompt);
  184. return false;
  185. }
  186. break;
  187. case "lt":
  188. if (v<data.value){
  189. this.notValidationMode(data.prompt);
  190. return false;
  191. }
  192. break;
  193. case "equal":
  194. if (v==data.value){
  195. this.notValidationMode(data.prompt);
  196. return false;
  197. }
  198. break;
  199. case "neq":
  200. if (v!=data.value){
  201. this.notValidationMode(data.prompt);
  202. return false;
  203. }
  204. break;
  205. case "contain":
  206. if (v.indexOf(data.value)!=-1){
  207. this.notValidationMode(data.prompt);
  208. return false;
  209. }
  210. break;
  211. case "notcontain":
  212. if (v.indexOf(data.value)==-1){
  213. this.notValidationMode(data.prompt);
  214. return false;
  215. }
  216. break;
  217. }
  218. }
  219. return true;
  220. },
  221. validationConfig: function(routeName, opinion){
  222. if (this.json.validationConfig){
  223. if (this.json.validationConfig.length){
  224. for (var i=0; i<this.json.validationConfig.length; i++) {
  225. var data = this.json.validationConfig[i];
  226. if (!this.validationConfigItem(routeName, data)) return false;
  227. }
  228. }
  229. return true;
  230. }
  231. return true;
  232. },
  233. validation: function(routeName, opinion){
  234. if (!this.validationConfig(routeName, opinion)) return false;
  235. if (!this.json.validation) return true;
  236. if (!this.json.validation.code) return true;
  237. var flag = this.form.Macro.exec(this.json.validation.code, this);
  238. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  239. if (flag.toString()!="true"){
  240. this.notValidationMode(flag);
  241. return false;
  242. }
  243. return true;
  244. }
  245. });