Textfield.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class process.Textfield 文本输入框。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var field = this.form.get("fieldName"); //获取组件对象
  7. * //方法2
  8. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  9. *
  10. * var data = field.getData(); //获取值
  11. * field.setData("字符串值"); //设置值
  12. * field.hide(); //隐藏字段
  13. * var id = field.json.id; //获取字段标识
  14. * var flag = field.isEmpty(); //字段是否为空
  15. * field.resetData(); //重置字段的值为默认值或置空
  16. * @extends MWF.xApplication.process.Xform.$Input
  17. * @hideconstructor
  18. */
  19. MWF.xApplication.process.Xform.Textfield = MWF.APPTextfield = new Class({
  20. Implements: [Events],
  21. Extends: MWF.APP$Input,
  22. iconStyle: "textFieldIcon",
  23. _loadUserInterface: function(){
  24. this._loadNode();
  25. if (this.json.compute === "show"){
  26. this._setValue(this._computeValue());
  27. }else{
  28. this._loadValue();
  29. }
  30. },
  31. _loadNode: function(){
  32. if (this.readonly || this.json.isReadonly){
  33. this._loadNodeRead();
  34. }else{
  35. this._loadNodeEdit();
  36. }
  37. },
  38. loadDescription: function(){
  39. if (this.readonly || this.json.isReadonly)return;
  40. var v = this._getBusinessData();
  41. if (!v){
  42. if (this.json.description){
  43. var size = this.node.getFirst().getSize();
  44. var w = size.x-3;
  45. if( this.json.showIcon!='no' && !this.form.json.hideModuleIcon ){
  46. if (COMMON.Browser.safari) w = w-20;
  47. }
  48. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  49. this.descriptionNode.setStyles({
  50. "width": ""+w+"px",
  51. "height": ""+size.y+"px",
  52. "line-height": ""+size.y+"px"
  53. });
  54. this.setDescriptionEvent();
  55. }
  56. }
  57. },
  58. setDescriptionEvent: function(){
  59. if (this.descriptionNode){
  60. if (COMMON.Browser.Platform.name==="ios"){
  61. this.descriptionNode.addEvents({
  62. "click": function(){
  63. this.descriptionNode.setStyle("display", "none");
  64. this.node.getFirst().focus();
  65. this.node.getFirst().fireEvent("click");
  66. }.bind(this)
  67. });
  68. }else if (COMMON.Browser.Platform.name==="android"){
  69. this.descriptionNode.addEvents({
  70. "click": function(){
  71. this.descriptionNode.setStyle("display", "none");
  72. this.node.getFirst().focus();
  73. this.node.getFirst().fireEvent("click");
  74. }.bind(this)
  75. });
  76. }else{
  77. this.descriptionNode.addEvents({
  78. "click": function(){
  79. this.descriptionNode.setStyle("display", "none");
  80. this.node.getFirst().focus();
  81. this.node.getFirst().fireEvent("click");
  82. }.bind(this)
  83. });
  84. }
  85. this.node.getFirst().addEvents({
  86. "focus": function(){
  87. this.descriptionNode.setStyle("display", "none");
  88. }.bind(this),
  89. "blur": function(){
  90. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  91. }.bind(this)
  92. });
  93. }
  94. },
  95. _loadNodeRead: function(){
  96. this.node.empty();
  97. this.node.set({
  98. "nodeId": this.json.id,
  99. "MWFType": this.json.type
  100. });
  101. },
  102. _resetNodeEdit: function(){
  103. var input = new Element("input", {
  104. "styles": {
  105. "background": "transparent",
  106. "width": "100%",
  107. "display": "block",
  108. "border": "0px"
  109. }
  110. });
  111. var node = new Element("div", {"styles": {
  112. "overflow": "hidden",
  113. "position": "relative",
  114. "margin-right": "20px",
  115. "padding-right": "4px"
  116. }}).inject(this.node, "after");
  117. input.inject(node);
  118. this.node.destroy();
  119. this.node = node;
  120. },
  121. _loadNodeEdit: function(){
  122. if (!this.json.preprocessing) this._resetNodeEdit();
  123. var input = this.node.getFirst();
  124. input.set(this.json.properties);
  125. this.node.set({
  126. "id": this.json.id,
  127. "MWFType": this.json.type,
  128. "events": {
  129. "click": this.clickSelect.bind(this)
  130. }
  131. });
  132. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon){
  133. this.iconNode = new Element("div", {
  134. "styles": this.form.css[this.iconStyle]
  135. }).inject(this.node, "before");
  136. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  137. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon);
  138. }
  139. this.node.getFirst().addEvent("change", function(){
  140. var v = this.getInputData("change");
  141. this._setBusinessData(v);
  142. this.validationMode();
  143. if (this.validation()) this._setBusinessData(v);
  144. }.bind(this));
  145. if (this.json.ANNModel){
  146. this.node.getFirst().addEvent("focus", function(){
  147. o2.Actions.get("x_query_assemble_surface").calculateNeural(this.json.ANNModel, this.form.businessData.work.id, function(json){
  148. var arr = json.data.filter(function(d){
  149. var value = this.node.getFirst().get("value");
  150. return d.score>0.1 && (value.indexOf(d.value)===-1)
  151. }.bind(this));
  152. if (arr.length){
  153. if (!this.modelNode) this.createModelNode();
  154. this.modelNode.getLast().empty();
  155. this.modelNode.show();
  156. this.modelNode.position({ "relativeTo": this.node, "position": "bottomLeft", "edge": 'upperLeft' });
  157. arr.each(function(v){
  158. var node = new Element("div", {"text": v.value, "styles": this.form.css.modelItemNode}).inject(this.modelNode.getLast());
  159. node.addEvents({
  160. "mouseover": function(){this.setStyle("color", "#0000ff");},
  161. "mouseout": function(){this.setStyle("color", "#a31515");},
  162. "mousedown": function(e){
  163. var str = this.node.getFirst().get("value")
  164. this.node.getFirst().set("value", ((str) ? str+", "+e.target.get("text") : e.target.get("text")));
  165. this.modelNode.hide();
  166. }.bind(this)
  167. });
  168. }.bind(this));
  169. }
  170. }.bind(this));
  171. }.bind(this));
  172. this.node.getFirst().addEvent("blur", function(){
  173. if (this.modelNode) this.modelNode.hide();
  174. }.bind(this));
  175. }
  176. this.node.getFirst().addEvent("blur", function(){
  177. this.validation();
  178. }.bind(this));
  179. this.node.getFirst().addEvent("keyup", function(){
  180. this.validationMode();
  181. }.bind(this));
  182. },
  183. createModelNode: function(){
  184. this.modelNode = new Element("div", {"styles": this.form.css.modelNode}).inject(this.node, "after");
  185. new Element("div", {"styles": this.form.css.modelNodeTitle, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  186. new Element("div", {"styles": this.form.css.modelNodeContent, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  187. },
  188. getInputData: function(){
  189. if (this.node.getFirst()){
  190. var v = this.node.getElement("input").get("value");
  191. if (this.json.dataType=="number"){
  192. var n = v.toFloat();
  193. return (isNaN(n)) ? 0 : n;
  194. }
  195. }else{
  196. return this._getBusinessData();
  197. }
  198. return v;
  199. }
  200. });