Textfield.js 8.4 KB

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