Textfield.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class Textfield 文本输入框。
  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.Textfield = MWF.APPTextfield = new Class({
  22. Implements: [Events],
  23. Extends: MWF.APP$Input,
  24. iconStyle: "textFieldIcon",
  25. // _loadUserInterface: function(){
  26. // this._loadNode();
  27. // if (this.json.compute === "show"){
  28. // this._setValue(this._computeValue());
  29. // }else{
  30. // this._loadValue();
  31. // }
  32. // },
  33. _loadNode: function(){
  34. if (this.isReadonly()){
  35. this._loadNodeRead();
  36. }else{
  37. this._loadNodeEdit();
  38. }
  39. },
  40. loadDescription: function(){
  41. if (this.isReadonly())return;
  42. var v = this._getBusinessData();
  43. if (!v && v!==0){
  44. if (this.json.description){
  45. var size, w;
  46. if( this.node.offsetParent === null ){ //隐藏
  47. size = { y: 26 }
  48. }else{
  49. size = this.node.getFirst().getSize();
  50. // w = size.x-3;
  51. // if( this.hasIcon() ){
  52. // if (COMMON.Browser.safari) w = w-20;
  53. // }
  54. }
  55. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  56. this.descriptionNode.setStyles({
  57. "height": ""+size.y+"px",
  58. "line-height": ""+size.y+"px"
  59. });
  60. this.descriptionNode.setStyles({
  61. "width": "auto",
  62. "overflow": "auto"
  63. });
  64. // if( w )this.descriptionNode.setStyles({
  65. // "width": ""+w+"px"
  66. // });
  67. this.setDescriptionEvent();
  68. }
  69. }
  70. },
  71. setDescriptionEvent: function(){
  72. if (this.descriptionNode){
  73. if (COMMON.Browser.Platform.name==="ios"){
  74. this.descriptionNode.addEvents({
  75. "click": function(){
  76. this.descriptionNode.setStyle("display", "none");
  77. this.node.getFirst().focus();
  78. this.node.getFirst().fireEvent("click");
  79. }.bind(this)
  80. });
  81. }else if (COMMON.Browser.Platform.name==="android"){
  82. this.descriptionNode.addEvents({
  83. "click": function(){
  84. this.descriptionNode.setStyle("display", "none");
  85. this.node.getFirst().focus();
  86. this.node.getFirst().fireEvent("click");
  87. }.bind(this)
  88. });
  89. }else{
  90. this.descriptionNode.addEvents({
  91. "click": function(){
  92. this.descriptionNode.setStyle("display", "none");
  93. this.node.getFirst().focus();
  94. this.node.getFirst().fireEvent("click");
  95. }.bind(this)
  96. });
  97. }
  98. this.node.getFirst().addEvents({
  99. "focus": function(){
  100. this.descriptionNode.setStyle("display", "none");
  101. }.bind(this),
  102. "blur": function(){
  103. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  104. }.bind(this)
  105. });
  106. }
  107. },
  108. _loadNodeRead: function(){
  109. this.node.empty();
  110. this.node.set({
  111. "nodeId": this.json.id,
  112. "MWFType": this.json.type
  113. });
  114. this.clearDefaultMargin();
  115. },
  116. _resetNodeEdit: function(){
  117. var input = new Element("input", {
  118. "styles": {
  119. "background": "transparent",
  120. "width": (this.json.inputStyles && this.json.inputStyles.width) ? this.json.inputStyles.width : "100%",
  121. "display": "block",
  122. "border": "0px"
  123. }
  124. });
  125. var node = new Element("div", {"styles": {
  126. "overflow": (this.json.styles && this.json.styles.overflow) ? this.json.styles.overflow : "hidden",
  127. "position": "relative",
  128. "margin-right": this.hasIcon() ? "20px" : "0px",
  129. "padding-right": "4px"
  130. }}).inject(this.node, "after");
  131. input.inject(node);
  132. this.node.destroy();
  133. this.node = node;
  134. },
  135. _loadNodeEdit: function(){
  136. if (!this.json.preprocessing) this._resetNodeEdit();
  137. var input = this.node.getFirst();
  138. if( !input && this.nodeHtml){
  139. this.node.set("html", this.nodeHtml);
  140. input = this.node.getFirst();
  141. }
  142. input.set(this.json.properties);
  143. this.node.set({
  144. "id": this.json.id,
  145. "MWFType": this.json.type,
  146. "events": {
  147. "click": this.clickSelect.bind(this)
  148. }
  149. });
  150. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon){
  151. this.iconNode = new Element("div", {
  152. "styles": this.form.css[this.iconStyle]
  153. }).inject(this.node, "before");
  154. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  155. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon);
  156. }
  157. this.node.getFirst().addEvent("change", function(){
  158. var v = this.getInputData("change");
  159. //this._setBusinessData(v);
  160. this.validationMode();
  161. if (this.validation()) {
  162. this._setBusinessData(v);
  163. this.fireEvent("change");
  164. }
  165. }.bind(this));
  166. var inputNode = this.node.getFirst();
  167. if (inputNode) inputNode.addEvent("input", function(e){
  168. var v=e.target.get("value");
  169. this._setBusinessData(v);
  170. }.bind(this));
  171. if (this.json.ANNModel){
  172. this.node.getFirst().addEvent("focus", function(){
  173. o2.Actions.get("x_query_assemble_surface").calculateNeural(this.json.ANNModel, this.form.businessData.work.id, function(json){
  174. var arr = json.data.filter(function(d){
  175. var value = this.node.getFirst().get("value");
  176. return d.score>0.1 && (value.indexOf(d.value)===-1)
  177. }.bind(this));
  178. if (arr.length){
  179. if (!this.modelNode) this.createModelNode();
  180. this.modelNode.getLast().empty();
  181. this.modelNode.show();
  182. this.modelNode.position({ "relativeTo": this.node, "position": "bottomLeft", "edge": 'upperLeft' });
  183. arr.each(function(v){
  184. var node = new Element("div", {"text": v.value, "styles": this.form.css.modelItemNode}).inject(this.modelNode.getLast());
  185. node.addEvents({
  186. "mouseover": function(){this.setStyle("color", "#0000ff");},
  187. "mouseout": function(){this.setStyle("color", "#a31515");},
  188. "mousedown": function(e){
  189. var str = this.node.getFirst().get("value")
  190. this.node.getFirst().set("value", ((str) ? str+", "+e.target.get("text") : e.target.get("text")));
  191. this.modelNode.hide();
  192. }.bind(this)
  193. });
  194. }.bind(this));
  195. }
  196. }.bind(this));
  197. }.bind(this));
  198. this.node.getFirst().addEvent("blur", function(){
  199. if (this.modelNode) this.modelNode.hide();
  200. }.bind(this));
  201. }
  202. this.node.getFirst().addEvent("blur", function(){
  203. this.validation();
  204. }.bind(this));
  205. this.node.getFirst().addEvent("keyup", function(){
  206. this.validationMode();
  207. }.bind(this));
  208. },
  209. createModelNode: function(){
  210. this.modelNode = new Element("div", {"styles": this.form.css.modelNode}).inject(this.node, "after");
  211. new Element("div", {"styles": this.form.css.modelNodeTitle, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  212. new Element("div", {"styles": this.form.css.modelNodeContent, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  213. },
  214. getInputData: function(){
  215. if (this.node.getFirst()){
  216. var v = this.node.getElement("input").get("value");
  217. if (this.json.dataType=="number"){
  218. var n = v.toFloat();
  219. return (isNaN(n)) ? 0 : n;
  220. }
  221. }else{
  222. return this._getBusinessData();
  223. }
  224. return v;
  225. }
  226. });