ImageClipper.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class ImageClipper 图片编辑组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var attachment = this.form.get("name"); //获取组件
  7. * //方法2
  8. * var attachment = this.target; //在组件事件脚本中获取
  9. * @extends MWF.xApplication.process.Xform.$Module
  10. * @category FormComponents
  11. * @hideconstructor
  12. */
  13. MWF.xApplication.process.Xform.ImageClipper = MWF.APPImageClipper = new Class(
  14. /** @lends MWF.xApplication.process.Xform.ImageClipper# */
  15. {
  16. Implements: [Events],
  17. Extends: MWF.APP$Module,
  18. initialize: function(node, json, form, options){
  19. this.node = $(node);
  20. this.node.store("module", this);
  21. this.json = json;
  22. this.form = form;
  23. this.field = true;
  24. },
  25. _loadUserInterface: function(){
  26. this.field = true;
  27. this.node.empty();
  28. var data = this._getBusinessData();
  29. if( data ){
  30. var img = new Element("img",{
  31. src : MWF.xDesktop.getImageSrc( data )
  32. });
  33. if (layout.mobile || COMMON.Browser.Platform.isMobile) {
  34. img.setStyles({
  35. "max-width": "90%"
  36. })
  37. }else if( this.json.clipperType == "size" ){
  38. var width = this.json.imageWidth;
  39. var height = this.json.imageHeight;
  40. if( width && height ){
  41. img.setStyles({
  42. width : width+"px",
  43. height : height+"px"
  44. })
  45. }
  46. }
  47. img.inject( this.node );
  48. }
  49. if( this.readonly || this.json.isReadonly )return;
  50. var divBottom = new Element("div").inject( this.node );
  51. var button = new Element("button").inject(divBottom);
  52. button.set({
  53. //"id": this.json.id,
  54. "text": this.json.name || this.json.id,
  55. "styles": this.form.css.buttonStyles,
  56. "MWFType": this.json.type
  57. });
  58. button.addEvent("click", function(){
  59. this.validationMode();
  60. var d = this._getBusinessData();
  61. if (layout.mobile){
  62. o2.imageClipperCallback = function( str ){
  63. var data = JSON.parse( str );
  64. this.setData( data ? data.fileId : "" );
  65. this.validation();
  66. o2.imageClipperCallback = null;
  67. }.bind(this);
  68. // 兼容cms编辑表单
  69. var referencetype = "processPlatformJob";
  70. if(this.form.businessData.work && this.form.businessData.work.referencetype) {
  71. referencetype = this.form.businessData.work.referencetype
  72. }
  73. var jsonString = JSON.stringify({
  74. "mwfId" : this.json.id,
  75. "callback" : "o2.imageClipperCallback",
  76. "referencetype": referencetype,
  77. "reference": this.form.businessData.work.job
  78. });
  79. if( window.o2android && window.o2android.uploadImage2FileStorage ){
  80. window.o2android.uploadImage2FileStorage(jsonString)
  81. }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.uploadImage2FileStorage){
  82. window.webkit.messageHandlers.uploadImage2FileStorage.postMessage(jsonString);
  83. }else {
  84. this.selectImage( d, function(data){
  85. this.setData( data ? data.id : "" );
  86. this.validation();
  87. }.bind(this));
  88. }
  89. }else{
  90. this.selectImage( d, function(data){
  91. this.setData( data ? data.id : "" );
  92. this.validation();
  93. }.bind(this));
  94. }
  95. }.bind(this));
  96. },
  97. getTextData : function(){
  98. var value = this._getBusinessData() || "";
  99. return {"value": [value], "text": [value]};
  100. },
  101. /**
  102. * @summary 判断组件值是否为空.
  103. * @example
  104. * if( this.form.get('fieldName').isEmpty() ){
  105. * this.form.notice('请上传图片', 'warn');
  106. * }
  107. * @return {Boolean} 值是否为空.
  108. */
  109. isEmpty : function(){
  110. return !this.getData();
  111. },
  112. /**
  113. * 获取上传的图片ID。
  114. * @summary 获取上传的图片ID。
  115. * @example
  116. * var id = this.form.get('fieldName').getData(); //获取上传的图片id
  117. * var url = MWF.xDesktop.getImageSrc( id ); //获取图片的url
  118. */
  119. getData: function( data ){
  120. return this._getBusinessData() || "";
  121. },
  122. setData: function( data ){
  123. this._setBusinessData(data);
  124. var img = this.node.getElements("img");
  125. if( img && img.length )img.destroy();
  126. if( !data )return;
  127. var img = new Element("img",{
  128. src : MWF.xDesktop.getImageSrc( data )
  129. }).inject( this.node, "top" );
  130. if (layout.mobile || COMMON.Browser.Platform.isMobile) {
  131. img.setStyles({
  132. "max-width": "90%"
  133. })
  134. }else if( this.json.clipperType == "size" ){
  135. var width = this.json.imageWidth;
  136. var height = this.json.imageHeight;
  137. if (width && height) {
  138. img.setStyles({
  139. width: width + "px",
  140. height: height + "px"
  141. })
  142. }
  143. }
  144. },
  145. selectImage: function(d, callback){
  146. var clipperType = this.json.clipperType || "unrestricted";
  147. var ratio = 1;
  148. var description = "";
  149. var maxSize = 800;
  150. if( clipperType == "unrestricted" ){
  151. ratio = 0;
  152. }else if( clipperType == "size" ){
  153. var width = (this.json.imageWidth) ? this.json.imageWidth.toInt() : 600;
  154. var height = (this.json.imageHeight) ? this.json.imageHeight.toInt() : 500;
  155. ratio = width / height;
  156. maxSize = Math.max( width, height );
  157. if( !isNaN( width ) && !isNaN( height ) ){
  158. description = MWF.LP.widget.pictureSize.replace(/{width}/g, width).replace(/{height}/g, height);
  159. }
  160. }else if( clipperType == "ratio" ){
  161. ratio = this.json.imageRatio || 1;
  162. description = MWF.LP.widget.pictureRatio.replace(/{ratio}/g, ratio);
  163. }
  164. MWF.xDesktop.requireApp("process.Xform", "widget.ImageClipper", function(){
  165. this.imageClipper = new MWF.xApplication.process.Xform.widget.ImageClipper(this.form.app, {
  166. "style": "default",
  167. "aspectRatio" : ratio,
  168. "description" : description,
  169. "imageUrl" : d ? MWF.xDesktop.getImageSrc( d ) : "",
  170. "reference" : this.form.businessData.work.job,
  171. "referenceType": "processPlatformJob",
  172. "resultMaxSize" : maxSize,
  173. "onChange" : function(){
  174. callback( { src : this.imageClipper.imageSrc, id : this.imageClipper.imageId } );
  175. }.bind(this)
  176. });
  177. this.imageClipper.load();
  178. }.bind(this));
  179. },
  180. createErrorNode: function(text){
  181. var node = new Element("div");
  182. var iconNode = new Element("div", {
  183. "styles": {
  184. "width": "20px",
  185. "height": "20px",
  186. "float": "left",
  187. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  188. }
  189. }).inject(node);
  190. var textNode = new Element("div", {
  191. "styles": {
  192. "line-height": "20px",
  193. "margin-left": "20px",
  194. "color": "red",
  195. "word-break": "keep-all"
  196. },
  197. "text": text
  198. }).inject(node);
  199. return node;
  200. },
  201. notValidationMode: function(text){
  202. if (!this.isNotValidationMode){
  203. this.isNotValidationMode = true;
  204. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  205. this.node.setStyle("border", "1px solid red");
  206. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  207. this.showNotValidationMode(this.node);
  208. if (!this.node.isIntoView()) this.node.scrollIntoView();
  209. }
  210. },
  211. showNotValidationMode: function(node){
  212. var p = node.getParent("div");
  213. if (p){
  214. if (p.get("MWFtype") == "tab$Content"){
  215. if (p.getParent("div").getStyle("display")=="none"){
  216. var contentAreaNode = p.getParent("div").getParent("div");
  217. var tabAreaNode = contentAreaNode.getPrevious("div");
  218. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  219. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  220. tabNode.click();
  221. p = tabAreaNode.getParent("div");
  222. }
  223. }
  224. this.showNotValidationMode(p);
  225. }
  226. },
  227. validationMode: function(){
  228. if (this.isNotValidationMode){
  229. this.isNotValidationMode = false;
  230. this.node.setStyles(this.node.retrieve("borderStyle"));
  231. if (this.errNode){
  232. this.errNode.destroy();
  233. this.errNode = null;
  234. }
  235. }
  236. },
  237. validationConfigItem: function(routeName, data){
  238. var flag = (data.status=="all") ? true: (routeName == data.decision);
  239. if (flag){
  240. var n = this.getData();
  241. var v = (data.valueType=="value") ? n : n.length;
  242. switch (data.operateor){
  243. case "isnull":
  244. if (!v){
  245. this.notValidationMode(data.prompt);
  246. return false;
  247. }
  248. break;
  249. case "notnull":
  250. if (v){
  251. this.notValidationMode(data.prompt);
  252. return false;
  253. }
  254. break;
  255. case "gt":
  256. if (v>data.value){
  257. this.notValidationMode(data.prompt);
  258. return false;
  259. }
  260. break;
  261. case "lt":
  262. if (v<data.value){
  263. this.notValidationMode(data.prompt);
  264. return false;
  265. }
  266. break;
  267. case "equal":
  268. if (v==data.value){
  269. this.notValidationMode(data.prompt);
  270. return false;
  271. }
  272. break;
  273. case "neq":
  274. if (v!=data.value){
  275. this.notValidationMode(data.prompt);
  276. return false;
  277. }
  278. break;
  279. case "contain":
  280. if (v.indexOf(data.value)!=-1){
  281. this.notValidationMode(data.prompt);
  282. return false;
  283. }
  284. break;
  285. case "notcontain":
  286. if (v.indexOf(data.value)==-1){
  287. this.notValidationMode(data.prompt);
  288. return false;
  289. }
  290. break;
  291. }
  292. }
  293. return true;
  294. },
  295. validationConfig: function(routeName, opinion){
  296. if (this.json.validationConfig){
  297. if (this.json.validationConfig.length){
  298. for (var i=0; i<this.json.validationConfig.length; i++) {
  299. var data = this.json.validationConfig[i];
  300. if (!this.validationConfigItem(routeName, data)) return false;
  301. }
  302. }
  303. return true;
  304. }
  305. return true;
  306. },
  307. /**
  308. * @summary 根据组件的校验设置进行校验。
  309. * @param {String} [routeName] - 可选,路由名称.
  310. * @example
  311. * if( !this.form.get('fieldName').validation() ){
  312. * return false;
  313. * }
  314. * @return {Boolean} 是否通过校验
  315. */
  316. validation: function(routeName, opinion){
  317. if (!this.validationConfig(routeName, opinion)) return false;
  318. if (!this.json.validation) return true;
  319. if (!this.json.validation.code) return true;
  320. this.currentRouteName = routeName;
  321. var flag = this.form.Macro.exec(this.json.validation.code, this);
  322. this.currentRouteName = "";
  323. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  324. if (flag.toString()!="true"){
  325. this.notValidationMode(flag);
  326. return false;
  327. }
  328. return true;
  329. }
  330. });