Textarea.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class Textarea 多行文本组件。
  3. * @o2cn 多行文本输入
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var field = this.form.get("fieldId"); //获取组件对象
  8. * //方法2
  9. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  10. *
  11. * var data = field.getData(); //获取值
  12. * field.setData("字符串值"); //设置值
  13. * field.hide(); //隐藏字段
  14. * var id = field.json.id; //获取字段标识
  15. * var flag = field.isEmpty(); //字段是否为空
  16. * @extends MWF.xApplication.process.Xform.$Input
  17. * @o2category FormComponents
  18. * @o2range {Process|CMS|Portal}
  19. * @hideconstructor
  20. */
  21. MWF.xApplication.process.Xform.Textarea = MWF.APPTextarea = new Class({
  22. Implements: [Events],
  23. Extends: MWF.APP$Input,
  24. _loadNode: function(){
  25. if (this.isReadonly()){
  26. this._loadNodeRead();
  27. }else{
  28. this._loadNodeEdit();
  29. }
  30. },
  31. _loadNodeRead: function(){
  32. this.node.empty();
  33. this.node.set({
  34. "nodeId": this.json.id,
  35. "MWFType": this.json.type
  36. });
  37. },
  38. _loadMergeEditNodeByDefault: function(){
  39. var data = this.getSortedSectionData();
  40. data = data.map(function(d){ return d.data; });
  41. this._setBusinessData( data.join("\n") );
  42. this._loadNode();
  43. },
  44. getInputData: function(){
  45. if( this.isReadonly()) {
  46. return this._getBusinessData();
  47. }else if (this.node.getFirst()){
  48. return this.node.getFirst().get("value");
  49. }else{
  50. return this._getBusinessData();
  51. }
  52. },
  53. toHtml: function(value){
  54. var reg = new RegExp("\n","g");
  55. var reg2 = new RegExp("\u003c","g"); //尖括号转义,否则内容会截断
  56. var reg3 = new RegExp("\u003e","g");
  57. return ( value || "").replace(reg2,"&lt").replace(reg3,"&gt").replace(reg,"<br/>");
  58. },
  59. __setData: function(data){
  60. var old = this.getInputData();
  61. this._setBusinessData(data);
  62. if( this.isReadonly()){
  63. this.node.set("html", this.toHtml(data));
  64. }else if (this.node.getFirst()){
  65. this.node.getFirst().set("value", data);
  66. this.checkDescription();
  67. this.validationMode();
  68. }else{
  69. this.node.set("html", this.toHtml(data));
  70. }
  71. if (old!==data) this.fireEvent("change");
  72. this.moduleValueAG = null;
  73. },
  74. _setValue: function(value){
  75. if (!value) value = "";
  76. var p = o2.promiseAll(value).then(function(v){
  77. if (o2.typeOf(v)=="array") v = v[0];
  78. this._setBusinessData(v);
  79. if (this.node.getFirst()) this.node.getFirst().set("value", v || "");
  80. if (this.isReadonly()){
  81. this.node.set("html", this.toHtml(value));
  82. }
  83. this.fieldModuleLoaded = true;
  84. //this.__setValue(v);
  85. }.bind(this), function(){});
  86. this.moduleValueAG = p;
  87. p.then(function(){
  88. this.moduleValueAG = null;
  89. }.bind(this), function(){
  90. this.moduleValueAG = null;
  91. }.bind(this));
  92. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  93. // this.moduleValueAG = null;
  94. // if (o2.typeOf(v)=="array") v = v[0];
  95. // this._setBusinessData(v);
  96. // if (this.node.getFirst()) this.node.getFirst().set("value", v || "");
  97. // if (this.readonly || this.json.isReadonly){
  98. // var reg = new RegExp("\n","g");
  99. // var reg2 = new RegExp("\u003c","g"); //尖括号转义,否则内容会截断
  100. // var reg3 = new RegExp("\u003e","g");
  101. // var text = value.replace(reg2,"&lt").replace(reg3,"&gt").replace(reg,"<br/>");
  102. // this.node.set("html", text);
  103. // }
  104. // }.bind(this));
  105. //
  106. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  107. // this.moduleValueAG = null;
  108. // }.bind(this));
  109. return value;
  110. // if (value && value.isAG){
  111. // this.moduleValueAG = value;
  112. // value.addResolve(function(v){
  113. // this._setValue(v);
  114. // }.bind(this));
  115. // }else{
  116. // this._setBusinessData(value);
  117. // if (this.node.getFirst()) this.node.getFirst().set("value", value || "");
  118. // if (this.readonly || this.json.isReadonly){
  119. // var reg = new RegExp("\n","g");
  120. // var reg2 = new RegExp("\u003c","g"); //尖括号转义,否则内容会截断
  121. // var reg3 = new RegExp("\u003e","g");
  122. // var text = value.replace(reg2,"&lt").replace(reg3,"&gt").replace(reg,"<br/>");
  123. // this.node.set("html", text);
  124. // }
  125. // return value;
  126. // }
  127. },
  128. getTextData: function(){
  129. //var value = this.node.get("value");
  130. //var text = this.node.get("text");
  131. if (this.isReadonly()){
  132. var value = this._getBusinessData();
  133. return {"value": [value || ""] , "text": [value || ""]};
  134. }else{
  135. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  136. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  137. return {"value": [value || ""] , "text": [text || value || ""]};
  138. }
  139. },
  140. // _setValue: function(value){
  141. // this._setBusinessData(value);
  142. // if (this.node.getFirst()) this.node.getFirst().set("value", value || "");
  143. // if (this.readonly || this.json.isReadonly){
  144. // var reg = new RegExp("\n","g");
  145. // var reg2 = new RegExp("\u003c","g"); //尖括号转义,否则内容会截断
  146. // var reg3 = new RegExp("\u003e","g");
  147. // var text = value.replace(reg2,"&lt").replace(reg3,"&gt").replace(reg,"<br/>");
  148. // this.node.set("html", text);
  149. // }
  150. // },
  151. _resetNodeEdit: function(){
  152. var input = new Element("textarea", {"styles": {
  153. "background": "transparent",
  154. "width": (this.json.inputStyles && this.json.inputStyles.width) ? this.json.inputStyles.width : "100%",
  155. "border": "0px"
  156. }});
  157. var node = new Element("div", {"styles": {
  158. // "ovwrflow": (this.json.styles && this.json.styles.overflow) ? this.json.styles.overflow : "hidden",
  159. "position": "relative",
  160. "padding-right": "2px"
  161. }}).inject(this.node, "after");
  162. input.inject(node);
  163. this.node.destroy();
  164. this.node = node;
  165. },
  166. _loadNodeEdit: function(){
  167. if (!this.json.preprocessing) this._resetNodeEdit();
  168. var input = this.node.getFirst();
  169. if( !input && this.nodeHtml ){
  170. this.node.set("html", this.nodeHtml);
  171. input = this.node.getFirst();
  172. }
  173. input.set(this.json.properties);
  174. if( this.form.json.textareaDisableResize )input.setStyle("resize","none");
  175. this.node.set({
  176. "id": this.json.id,
  177. "MWFType": this.json.type
  178. });
  179. this.node.addEvent("change", function(){
  180. this._setBusinessData(this.getInputData());
  181. this.fireEvent("change");
  182. }.bind(this));
  183. this.node.addEvent("input", function(e){
  184. var v=e.target.get("value");
  185. this._setBusinessData(v);
  186. }.bind(this));
  187. this.node.getFirst().addEvent("blur", function(){
  188. this.validation();
  189. }.bind(this));
  190. this.node.getFirst().addEvent("keyup", function(){
  191. this.validationMode();
  192. }.bind(this));
  193. },
  194. _afterLoaded: function(){
  195. if (!this.readonly){
  196. this.loadDescription();
  197. }
  198. },
  199. loadDescription: function(){
  200. if (this.isReadonly())return;
  201. var v = this._getBusinessData();
  202. if (!v){
  203. if (this.json.description){
  204. var size, w;
  205. if( this.node.offsetParent === null ){ //隐藏
  206. size = { y: 26 }
  207. }else{
  208. size = this.node.getFirst().getSize();
  209. w = size.x-3;
  210. if( this.json.showIcon!='no' && !this.form.json.hideModuleIcon ){
  211. w = size.x-23;
  212. }
  213. }
  214. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  215. this.descriptionNode.setStyles({
  216. "height": ""+size.y+"px",
  217. "line-height": ""+size.y+"px"
  218. });
  219. if( w )this.descriptionNode.setStyles({
  220. "width": ""+w+"px"
  221. });
  222. this.setDescriptionEvent();
  223. }
  224. }
  225. },
  226. setDescriptionEvent: function(){
  227. if (this.descriptionNode){
  228. if (COMMON.Browser.Platform.name==="ios"){
  229. this.descriptionNode.addEvents({
  230. "click": function(){
  231. this.descriptionNode.setStyle("display", "none");
  232. this.node.getFirst().focus();
  233. }.bind(this)
  234. });
  235. }else if (COMMON.Browser.Platform.name==="android"){
  236. this.descriptionNode.addEvents({
  237. "click": function(){
  238. this.descriptionNode.setStyle("display", "none");
  239. this.node.getFirst().focus();
  240. }.bind(this)
  241. });
  242. }else{
  243. this.descriptionNode.addEvents({
  244. "click": function(){
  245. this.descriptionNode.setStyle("display", "none");
  246. this.node.getFirst().focus();
  247. }.bind(this)
  248. });
  249. }
  250. this.node.getFirst().addEvents({
  251. "focus": function(){
  252. this.descriptionNode.setStyle("display", "none");
  253. }.bind(this),
  254. "blur": function(){
  255. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  256. }.bind(this)
  257. });
  258. }
  259. },
  260. setExcelData: function (d) {
  261. var value = d.replace(/&#10;/g,"\n"); //换行符&#10;
  262. this.excelData = value;
  263. this.setData(value, true);
  264. }
  265. });