Label.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Label.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * This class creates a label element. A label is a simple text control
  12. * that can be bound to other controls.
  13. *
  14. * @-x-less Label.less
  15. * @class tinymce.ui.Label
  16. * @extends tinymce.ui.Widget
  17. */
  18. define("tinymce/ui/Label", [
  19. "tinymce/ui/Widget",
  20. "tinymce/ui/DomUtils"
  21. ], function(Widget, DomUtils) {
  22. "use strict";
  23. return Widget.extend({
  24. /**
  25. * Constructs a instance with the specified settings.
  26. *
  27. * @constructor
  28. * @param {Object} settings Name/value object with settings.
  29. * @param {Boolean} multiline Multiline label.
  30. */
  31. init: function(settings) {
  32. var self = this;
  33. self._super(settings);
  34. self.addClass('widget');
  35. self.addClass('label');
  36. self.canFocus = false;
  37. if (settings.multiline) {
  38. self.addClass('autoscroll');
  39. }
  40. if (settings.strong) {
  41. self.addClass('strong');
  42. }
  43. },
  44. /**
  45. * Initializes the current controls layout rect.
  46. * This will be executed by the layout managers to determine the
  47. * default minWidth/minHeight etc.
  48. *
  49. * @method initLayoutRect
  50. * @return {Object} Layout rect instance.
  51. */
  52. initLayoutRect: function() {
  53. var self = this, layoutRect = self._super();
  54. if (self.settings.multiline) {
  55. var size = DomUtils.getSize(self.getEl());
  56. // Check if the text fits within maxW if not then try word wrapping it
  57. if (size.width > layoutRect.maxW) {
  58. layoutRect.minW = layoutRect.maxW;
  59. self.addClass('multiline');
  60. }
  61. self.getEl().style.width = layoutRect.minW + 'px';
  62. layoutRect.startMinH = layoutRect.h = layoutRect.minH = Math.min(layoutRect.maxH, DomUtils.getSize(self.getEl()).height);
  63. }
  64. return layoutRect;
  65. },
  66. /**
  67. * Repaints the control after a layout operation.
  68. *
  69. * @method repaint
  70. */
  71. repaint: function() {
  72. var self = this;
  73. if (!self.settings.multiline) {
  74. self.getEl().style.lineHeight = self.layoutRect().h + 'px';
  75. }
  76. return self._super();
  77. },
  78. /**
  79. * Sets/gets the current label text.
  80. *
  81. * @method text
  82. * @param {String} [text] New label text.
  83. * @return {String|tinymce.ui.Label} Current text or current label instance.
  84. */
  85. text: function(text) {
  86. var self = this;
  87. if (self._rendered && text) {
  88. this.innerHtml(self.encode(text));
  89. }
  90. return self._super(text);
  91. },
  92. /**
  93. * Renders the control as a HTML string.
  94. *
  95. * @method renderHtml
  96. * @return {String} HTML representing the control.
  97. */
  98. renderHtml: function() {
  99. var self = this, forId = self.settings.forId;
  100. return (
  101. '<label id="' + self._id + '" class="' + self.classes() + '"' + (forId ? ' for="' + forId + '"' : '') + '>' +
  102. self.encode(self._text) +
  103. '</label>'
  104. );
  105. }
  106. });
  107. });