ImageClipper.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if( clipperType == "unrestricted" ){
  76. ratio = 0;
  77. }else if( clipperType == "size" ){
  78. var width = this.json.imageWidth.toInt();
  79. var height = this.json.imageHeight.toInt();
  80. ratio = width / height
  81. }else if( clipperType == "ratio" ){
  82. ratio = this.json.imageRatio || 1
  83. }
  84. MWF.xDesktop.requireApp("process.Xform", "widget.ImageClipper", function(){
  85. this.imageClipper = new MWF.xApplication.process.Xform.widget.ImageClipper(this.form.app, {
  86. "style": "default",
  87. "aspectRatio" : ratio,
  88. "imageUrl" : d ? MWF.xDesktop.getImageSrc( d ) : "",
  89. "reference" : this.form.businessData.work.job,
  90. "referenceType": "processPlatformJob",
  91. "onChange" : function(){
  92. callback( { src : this.imageClipper.imageSrc, id : this.imageClipper.imageId } );
  93. }.bind(this)
  94. });
  95. this.imageClipper.load();
  96. }.bind(this));
  97. },
  98. createErrorNode: function(text){
  99. var node = new Element("div");
  100. var iconNode = new Element("div", {
  101. "styles": {
  102. "width": "20px",
  103. "height": "20px",
  104. "float": "left",
  105. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  106. }
  107. }).inject(node);
  108. var textNode = new Element("div", {
  109. "styles": {
  110. "line-height": "20px",
  111. "margin-left": "20px",
  112. "color": "red"
  113. },
  114. "text": text
  115. }).inject(node);
  116. return node;
  117. },
  118. notValidationMode: function(text){
  119. if (!this.isNotValidationMode){
  120. this.isNotValidationMode = true;
  121. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  122. this.node.setStyle("border", "1px solid red");
  123. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  124. this.showNotValidationMode(this.node);
  125. }
  126. },
  127. showNotValidationMode: function(node){
  128. var p = node.getParent("div");
  129. if (p){
  130. if (p.get("MWFtype") == "tab$Content"){
  131. if (p.getParent("div").getStyle("display")=="none"){
  132. var contentAreaNode = p.getParent("div").getParent("div");
  133. var tabAreaNode = contentAreaNode.getPrevious("div");
  134. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  135. var tabNode = tabAreaNode.getChildren()[idx];
  136. tabNode.click();
  137. p = tabAreaNode.getParent("div");
  138. }
  139. }
  140. this.showNotValidationMode(p);
  141. }
  142. },
  143. validationMode: function(){
  144. if (this.isNotValidationMode){
  145. this.isNotValidationMode = false;
  146. this.node.setStyles(this.node.retrieve("borderStyle"));
  147. if (this.errNode){
  148. this.errNode.destroy();
  149. this.errNode = null;
  150. }
  151. }
  152. },
  153. validationConfigItem: function(routeName, data){
  154. var flag = (data.status=="all") ? true: (routeName == data.decision);
  155. if (flag){
  156. var n = this.getData();
  157. var v = (data.valueType=="value") ? n : n.length;
  158. switch (data.operateor){
  159. case "isnull":
  160. if (!v){
  161. this.notValidationMode(data.prompt);
  162. return false;
  163. }
  164. break;
  165. case "notnull":
  166. if (v){
  167. this.notValidationMode(data.prompt);
  168. return false;
  169. }
  170. break;
  171. case "gt":
  172. if (v>data.value){
  173. this.notValidationMode(data.prompt);
  174. return false;
  175. }
  176. break;
  177. case "lt":
  178. if (v<data.value){
  179. this.notValidationMode(data.prompt);
  180. return false;
  181. }
  182. break;
  183. case "equal":
  184. if (v==data.value){
  185. this.notValidationMode(data.prompt);
  186. return false;
  187. }
  188. break;
  189. case "neq":
  190. if (v!=data.value){
  191. this.notValidationMode(data.prompt);
  192. return false;
  193. }
  194. break;
  195. case "contain":
  196. if (v.indexOf(data.value)!=-1){
  197. this.notValidationMode(data.prompt);
  198. return false;
  199. }
  200. break;
  201. case "notcontain":
  202. if (v.indexOf(data.value)==-1){
  203. this.notValidationMode(data.prompt);
  204. return false;
  205. }
  206. break;
  207. }
  208. }
  209. return true;
  210. },
  211. validationConfig: function(routeName, opinion){
  212. if (this.json.validationConfig){
  213. if (this.json.validationConfig.length){
  214. for (var i=0; i<this.json.validationConfig.length; i++) {
  215. var data = this.json.validationConfig[i];
  216. if (!this.validationConfigItem(routeName, data)) return false;
  217. }
  218. }
  219. return true;
  220. }
  221. return true;
  222. },
  223. validation: function(routeName, opinion){
  224. if (!this.validationConfig(routeName, opinion)) return false;
  225. if (!this.json.validation) return true;
  226. if (!this.json.validation.code) return true;
  227. var flag = this.form.Macro.exec(this.json.validation.code, this);
  228. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  229. if (flag.toString()!="true"){
  230. this.notValidationMode(flag);
  231. return false;
  232. }
  233. return true;
  234. }
  235. });