ImageClipper.js 14 KB

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