Textfield.js 8.4 KB

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