Textfield.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if( w )this.descriptionNode.setStyles({
  61. "width": ""+w+"px"
  62. });
  63. this.setDescriptionEvent();
  64. }
  65. }
  66. },
  67. setDescriptionEvent: function(){
  68. if (this.descriptionNode){
  69. if (COMMON.Browser.Platform.name==="ios"){
  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 if (COMMON.Browser.Platform.name==="android"){
  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. }else{
  86. this.descriptionNode.addEvents({
  87. "click": function(){
  88. this.descriptionNode.setStyle("display", "none");
  89. this.node.getFirst().focus();
  90. this.node.getFirst().fireEvent("click");
  91. }.bind(this)
  92. });
  93. }
  94. this.node.getFirst().addEvents({
  95. "focus": function(){
  96. this.descriptionNode.setStyle("display", "none");
  97. }.bind(this),
  98. "blur": function(){
  99. if (!this.node.getFirst().get("value")) this.descriptionNode.setStyle("display", "block");
  100. }.bind(this)
  101. });
  102. }
  103. },
  104. _loadNodeRead: function(){
  105. this.node.empty();
  106. this.node.set({
  107. "nodeId": this.json.id,
  108. "MWFType": this.json.type
  109. });
  110. },
  111. _resetNodeEdit: function(){
  112. var input = new Element("input", {
  113. "styles": {
  114. "background": "transparent",
  115. "width": (this.json.inputStyles && this.json.inputStyles.width) ? this.json.inputStyles.width : "100%",
  116. "display": "block",
  117. "border": "0px"
  118. }
  119. });
  120. var node = new Element("div", {"styles": {
  121. "overflow": (this.json.styles && this.json.styles.overflow) ? this.json.styles.overflow : "hidden",
  122. "position": "relative",
  123. "margin-right": this.hasIcon() ? "20px" : "0px",
  124. "padding-right": "4px"
  125. }}).inject(this.node, "after");
  126. input.inject(node);
  127. this.node.destroy();
  128. this.node = node;
  129. },
  130. _loadNodeEdit: function(){
  131. if (!this.json.preprocessing) this._resetNodeEdit();
  132. var input = this.node.getFirst();
  133. if( !input && this.nodeHtml){
  134. this.node.set("html", this.nodeHtml);
  135. input = this.node.getFirst();
  136. }
  137. input.set(this.json.properties);
  138. this.node.set({
  139. "id": this.json.id,
  140. "MWFType": this.json.type,
  141. "events": {
  142. "click": this.clickSelect.bind(this)
  143. }
  144. });
  145. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon){
  146. this.iconNode = new Element("div", {
  147. "styles": this.form.css[this.iconStyle]
  148. }).inject(this.node, "before");
  149. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  150. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon);
  151. }
  152. this.node.getFirst().addEvent("change", function(){
  153. var v = this.getInputData("change");
  154. //this._setBusinessData(v);
  155. this.validationMode();
  156. if (this.validation()) {
  157. this._setBusinessData(v);
  158. this.fireEvent("change");
  159. }
  160. }.bind(this));
  161. var inputNode = this.node.getFirst();
  162. if (inputNode) inputNode.addEvent("input", function(e){
  163. var v=e.target.get("value");
  164. this._setBusinessData(v);
  165. }.bind(this));
  166. if (this.json.ANNModel){
  167. this.node.getFirst().addEvent("focus", function(){
  168. o2.Actions.get("x_query_assemble_surface").calculateNeural(this.json.ANNModel, this.form.businessData.work.id, function(json){
  169. var arr = json.data.filter(function(d){
  170. var value = this.node.getFirst().get("value");
  171. return d.score>0.1 && (value.indexOf(d.value)===-1)
  172. }.bind(this));
  173. if (arr.length){
  174. if (!this.modelNode) this.createModelNode();
  175. this.modelNode.getLast().empty();
  176. this.modelNode.show();
  177. this.modelNode.position({ "relativeTo": this.node, "position": "bottomLeft", "edge": 'upperLeft' });
  178. arr.each(function(v){
  179. var node = new Element("div", {"text": v.value, "styles": this.form.css.modelItemNode}).inject(this.modelNode.getLast());
  180. node.addEvents({
  181. "mouseover": function(){this.setStyle("color", "#0000ff");},
  182. "mouseout": function(){this.setStyle("color", "#a31515");},
  183. "mousedown": function(e){
  184. var str = this.node.getFirst().get("value")
  185. this.node.getFirst().set("value", ((str) ? str+", "+e.target.get("text") : e.target.get("text")));
  186. this.modelNode.hide();
  187. }.bind(this)
  188. });
  189. }.bind(this));
  190. }
  191. }.bind(this));
  192. }.bind(this));
  193. this.node.getFirst().addEvent("blur", function(){
  194. if (this.modelNode) this.modelNode.hide();
  195. }.bind(this));
  196. }
  197. this.node.getFirst().addEvent("blur", function(){
  198. this.validation();
  199. }.bind(this));
  200. this.node.getFirst().addEvent("keyup", function(){
  201. this.validationMode();
  202. }.bind(this));
  203. },
  204. createModelNode: function(){
  205. this.modelNode = new Element("div", {"styles": this.form.css.modelNode}).inject(this.node, "after");
  206. new Element("div", {"styles": this.form.css.modelNodeTitle, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  207. new Element("div", {"styles": this.form.css.modelNodeContent, "text": MWF.xApplication.process.Xform.LP.ANNInput}).inject(this.modelNode);
  208. },
  209. getInputData: function(){
  210. if (this.node.getFirst()){
  211. var v = this.node.getElement("input").get("value");
  212. if (this.json.dataType=="number"){
  213. var n = v.toFloat();
  214. return (isNaN(n)) ? 0 : n;
  215. }
  216. }else{
  217. return this._getBusinessData();
  218. }
  219. return v;
  220. }
  221. });