ImageClipper.js 13 KB

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