Label.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Label 文本组件。
  3. * @o2cn 文本组件
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var label = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var label = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS|Portal}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Label = MWF.APPLabel = new Class(
  16. /** @lends MWF.xApplication.process.Xform.Label# */
  17. {
  18. Implements: [Events],
  19. Extends: MWF.APP$Module,
  20. _loadUserInterface: function(){
  21. if (this.json.valueType == "text"){
  22. this.node.set("text", this.json.text || "");
  23. }
  24. if (this.json.valueType == "script"){
  25. var code = (this.json.script) ? this.json.script.code : "";
  26. if (code){
  27. var value = this.form.Macro.exec(code, this);
  28. this._setNodeText(value);
  29. //this.node.set("text", this.form.Macro.exec(code, this) || "");
  30. }
  31. }
  32. if (this.json.prefixIcon || this.json.suffixIcon){
  33. var text = this.node.get("text");
  34. this.node.empty();
  35. var tNode = new Element("div", {"styles": {
  36. "margin-left": (this.json.prefixIcon) ? "20px" : "0px",
  37. "margin-right": (this.json.suffixIcon) ? "20px" : "0px",
  38. "height": "100%"
  39. }, "text": text}).inject(this.node);
  40. var height = (this.node.offsetParent === null) ? "20" : this.node.getSize().y;
  41. if (this.json.prefixIcon){
  42. var node = new Element("div", {"styles": {
  43. "float": "left",
  44. "width": "20px",
  45. "height": ""+height+"px",
  46. "background": "url("+this.json.prefixIcon+") center center no-repeat"
  47. }}).inject(tNode, "before");
  48. }
  49. if (this.json.suffixIcon){
  50. var node = new Element("div", {"styles": {
  51. "float": "right",
  52. "width": "20px",
  53. "height": ""+height+"px",
  54. "background": "url("+this.json.suffixIcon+") center center no-repeat"
  55. }}).inject(tNode, "before");
  56. }
  57. }
  58. },
  59. _setNodeText: function(value){
  60. if (value && value.isAG){
  61. value.addResolve(function(v){
  62. this._setNodeText(v);
  63. }.bind(this));
  64. }else{
  65. o2.promiseAll(value).then(function(v){
  66. this.node.set("text", v || "");
  67. }.bind(this), function(){});
  68. //this.node.set("text", value || "");
  69. }
  70. },
  71. /**当参数为Promise的时候,请参考文档: {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
  72. * @summary 为组件设置文本,该文本不会被保存到后台。
  73. * @param text{String|Promise} .
  74. * @example
  75. * this.form.get("fieldId").setText("test"); //赋文本值
  76. * @example
  77. * //使用Promise
  78. * var field = this.form.get("fieldId");
  79. * var dict = new this.Dict("test"); //test为数据字典名称
  80. * var promise = dict.get("tools", true); //异步使用数据字典的get方法时返回Promise,参数true表示异步
  81. * field.setText( promise );
  82. */
  83. setText: function(text){
  84. if (!!text){
  85. o2.promiseAll(text).then(function(v){
  86. this.node.set("text", v || "");
  87. }.bind(this), function(){});
  88. }else{
  89. this.node.set("text", text || "");
  90. }
  91. //this.node.set("text", text);
  92. }
  93. });