ImageClipper.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. },
  123. "text": text
  124. }).inject(node);
  125. return node;
  126. },
  127. notValidationMode: function(text){
  128. if (!this.isNotValidationMode){
  129. this.isNotValidationMode = true;
  130. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  131. this.node.setStyle("border", "1px solid red");
  132. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  133. this.showNotValidationMode(this.node);
  134. }
  135. },
  136. showNotValidationMode: function(node){
  137. var p = node.getParent("div");
  138. if (p){
  139. if (p.get("MWFtype") == "tab$Content"){
  140. if (p.getParent("div").getStyle("display")=="none"){
  141. var contentAreaNode = p.getParent("div").getParent("div");
  142. var tabAreaNode = contentAreaNode.getPrevious("div");
  143. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  144. var tabNode = tabAreaNode.getChildren()[idx];
  145. tabNode.click();
  146. p = tabAreaNode.getParent("div");
  147. }
  148. }
  149. this.showNotValidationMode(p);
  150. }
  151. },
  152. validationMode: function(){
  153. if (this.isNotValidationMode){
  154. this.isNotValidationMode = false;
  155. this.node.setStyles(this.node.retrieve("borderStyle"));
  156. if (this.errNode){
  157. this.errNode.destroy();
  158. this.errNode = null;
  159. }
  160. }
  161. },
  162. validationConfigItem: function(routeName, data){
  163. var flag = (data.status=="all") ? true: (routeName == data.decision);
  164. if (flag){
  165. var n = this.getData();
  166. var v = (data.valueType=="value") ? n : n.length;
  167. switch (data.operateor){
  168. case "isnull":
  169. if (!v){
  170. this.notValidationMode(data.prompt);
  171. return false;
  172. }
  173. break;
  174. case "notnull":
  175. if (v){
  176. this.notValidationMode(data.prompt);
  177. return false;
  178. }
  179. break;
  180. case "gt":
  181. if (v>data.value){
  182. this.notValidationMode(data.prompt);
  183. return false;
  184. }
  185. break;
  186. case "lt":
  187. if (v<data.value){
  188. this.notValidationMode(data.prompt);
  189. return false;
  190. }
  191. break;
  192. case "equal":
  193. if (v==data.value){
  194. this.notValidationMode(data.prompt);
  195. return false;
  196. }
  197. break;
  198. case "neq":
  199. if (v!=data.value){
  200. this.notValidationMode(data.prompt);
  201. return false;
  202. }
  203. break;
  204. case "contain":
  205. if (v.indexOf(data.value)!=-1){
  206. this.notValidationMode(data.prompt);
  207. return false;
  208. }
  209. break;
  210. case "notcontain":
  211. if (v.indexOf(data.value)==-1){
  212. this.notValidationMode(data.prompt);
  213. return false;
  214. }
  215. break;
  216. }
  217. }
  218. return true;
  219. },
  220. validationConfig: function(routeName, opinion){
  221. if (this.json.validationConfig){
  222. if (this.json.validationConfig.length){
  223. for (var i=0; i<this.json.validationConfig.length; i++) {
  224. var data = this.json.validationConfig[i];
  225. if (!this.validationConfigItem(routeName, data)) return false;
  226. }
  227. }
  228. return true;
  229. }
  230. return true;
  231. },
  232. validation: function(routeName, opinion){
  233. if (!this.validationConfig(routeName, opinion)) return false;
  234. if (!this.json.validation) return true;
  235. if (!this.json.validation.code) return true;
  236. var flag = this.form.Macro.exec(this.json.validation.code, this);
  237. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  238. if (flag.toString()!="true"){
  239. this.notValidationMode(flag);
  240. return false;
  241. }
  242. return true;
  243. }
  244. });