devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
328 lines (321 loc) • 10.9 kB
JavaScript
/**
* DevExtreme (cjs/__internal/viz/core/title.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.plugin = exports.Title = void 0;
var _m_extend = require("../../core/utils/m_extend");
var _m_type = require("../../core/utils/m_type");
var _layout_element = require("../../viz/core/layout_element");
var _utils = require("./utils");
const _Number = Number;
const parseHorizontalAlignment = (0, _utils.enumParser)(["left", "center", "right"]);
const parseVerticalAlignment = (0, _utils.enumParser)(["top", "bottom"]);
const DEFAULT_MARGIN = 10;
function hasText(text) {
return !!(text && String(text).length > 0)
}
function processTitleLength(elem, text, width, options, placeholderSize) {
if (elem.attr({
text: text
}).setMaxSize(width, placeholderSize, options).textChanged) {
elem.setTitle(text)
}
}
function pickMarginValue(value) {
return value >= 0 ? _Number(value) : 10
}
function validateMargin(margin) {
let result;
if (margin >= 0) {
result = {
left: _Number(margin),
top: _Number(margin),
right: _Number(margin),
bottom: _Number(margin)
}
} else {
margin = margin || {};
result = {
left: pickMarginValue(margin.left),
top: pickMarginValue(margin.top),
right: pickMarginValue(margin.right),
bottom: pickMarginValue(margin.bottom)
}
}
return result
}
function checkRect(rect, boundingRect) {
return rect[2] - rect[0] < boundingRect.width || rect[3] - rect[1] < boundingRect.height
}
let Title = exports.Title = class extends _layout_element.LayoutElement {
constructor(params) {
super();
this._params = params;
this._group = params.renderer.g().attr({
class: params.cssClass
}).linkOn(params.root || params.renderer.root, "title");
this._hasText = false
}
dispose() {
this._group.linkRemove();
this._group.linkOff();
if (this._titleElement) {
this._clipRect.dispose();
this._titleElement = this._subtitleElement = this._clipRect = null
}
this._params = this._group = this._options = null
}
_updateOptions(options) {
this._options = options;
this._options.horizontalAlignment = parseHorizontalAlignment(options.horizontalAlignment, "center");
this._options.verticalAlignment = parseVerticalAlignment(options.verticalAlignment, "top");
this._options.margin = validateMargin(options.margin)
}
_updateStructure() {
const renderer = this._params.renderer;
const group = this._group;
const options = this._options;
const align = options.horizontalAlignment;
if (!this._titleElement) {
this._titleElement = renderer.text().append(group);
this._subtitleElement = renderer.text();
this._clipRect = renderer.clipRect();
group.attr({
"clip-path": this._clipRect.id
})
}
this._titleElement.attr({
align: align,
class: options.cssClass
});
this._subtitleElement.attr({
align: align,
class: options.subtitle.cssClass
});
group.linkAppend();
hasText(options.subtitle.text) ? this._subtitleElement.append(group) : this._subtitleElement.remove()
}
_updateTexts() {
const options = this._options;
const subtitleOptions = options.subtitle;
const titleElement = this._titleElement;
const subtitleElement = this._subtitleElement;
let titleBox;
titleElement.attr({
text: "A",
y: 0
}).css((0, _utils.patchFontOptions)(options.font));
titleBox = titleElement.getBBox();
this._baseLineCorrection = titleBox.height + titleBox.y;
titleElement.attr({
text: options.text
});
titleBox = titleElement.getBBox();
const y = -titleBox.y;
titleElement.attr({
y: y
});
if (hasText(subtitleOptions.text)) {
subtitleElement.attr({
text: subtitleOptions.text,
y: 0
}).css((0, _utils.patchFontOptions)(subtitleOptions.font))
}
}
_shiftSubtitle() {
const titleBox = this._titleElement.getBBox();
const element = this._subtitleElement;
const offset = this._options.subtitle.offset;
element.move(0, titleBox.y + titleBox.height - element.getBBox().y - offset)
}
_updateBoundingRectAlignment() {
const boundingRect = this._boundingRect;
const options = this._options;
boundingRect.verticalAlignment = options.verticalAlignment;
boundingRect.horizontalAlignment = options.horizontalAlignment;
boundingRect.cutLayoutSide = options.verticalAlignment;
boundingRect.cutSide = "vertical";
boundingRect.position = {
horizontal: options.horizontalAlignment,
vertical: options.verticalAlignment
}
}
hasText() {
return this._hasText
}
update(themeOptions, userOptions) {
const options = (0, _m_extend.extend)(true, {}, themeOptions, processTitleOptions(userOptions));
const _hasText = hasText(options.text);
const isLayoutChanged = _hasText || _hasText !== this._hasText;
this._baseLineCorrection = 0;
this._updateOptions(options);
this._boundingRect = {};
if (_hasText) {
this._updateStructure();
this._updateTexts()
} else {
this._group.linkRemove()
}
this._updateBoundingRect();
this._updateBoundingRectAlignment();
this._hasText = _hasText;
return isLayoutChanged
}
draw(width, height) {
if (this._hasText) {
this._group.linkAppend();
this._correctTitleLength(width);
if (this._group.getBBox().height > height) {
this.freeSpace()
}
}
return this
}
_correctTitleLength(width) {
const options = this._options;
const margin = options.margin;
const maxWidth = width - margin.left - margin.right;
let placeholderSize = options.placeholderSize;
processTitleLength(this._titleElement, options.text, maxWidth, options, placeholderSize);
if (this._subtitleElement) {
if (_Number(placeholderSize) > 0) {
placeholderSize -= this._titleElement.getBBox().height
}
processTitleLength(this._subtitleElement, options.subtitle.text, maxWidth, options.subtitle, placeholderSize);
this._shiftSubtitle()
}
this._updateBoundingRect();
const {
x: x,
y: y,
height: height
} = this.getCorrectedLayoutOptions();
this._clipRect.attr({
x: x,
y: y,
width: width,
height: height
})
}
getLayoutOptions() {
return this._boundingRect || null
}
shift(x, y) {
const box = this.getLayoutOptions();
this._group.move(x - box.x, y - box.y);
return this
}
_updateBoundingRect() {
const options = this._options;
const margin = options.margin;
const boundingRect = this._boundingRect;
const box = this._hasText ? this._group.getBBox() : {
width: 0,
height: 0,
x: 0,
y: 0,
isEmpty: true
};
if (!box.isEmpty) {
box.height += margin.top + margin.bottom - this._baseLineCorrection;
box.width += margin.left + margin.right;
box.x -= margin.left;
box.y += this._baseLineCorrection - margin.top
}
if (options.placeholderSize > 0) {
box.height = options.placeholderSize
}
boundingRect.height = box.height;
boundingRect.width = box.width;
boundingRect.x = box.x;
boundingRect.y = box.y
}
getCorrectedLayoutOptions() {
const srcBox = this.getLayoutOptions();
const correction = this._baseLineCorrection;
return (0, _m_extend.extend)({}, srcBox, {
y: srcBox.y - correction,
height: srcBox.height + correction
})
}
layoutOptions() {
if (!this._hasText) {
return null
}
return {
horizontalAlignment: this._boundingRect.horizontalAlignment,
verticalAlignment: this._boundingRect.verticalAlignment,
priority: 0
}
}
measure(size) {
this.draw(size[0], size[1]);
return [this._boundingRect.width, this._boundingRect.height]
}
move(rect, fitRect) {
const boundingRect = this._boundingRect;
if (checkRect(rect, boundingRect)) {
this.shift(fitRect[0], fitRect[1])
} else {
this.shift(Math.round(rect[0]), Math.round(rect[1]))
}
}
freeSpace() {
this._params.incidentOccurred("W2103");
this._group.linkRemove();
this._boundingRect.width = this._boundingRect.height = 0
}
getOptions() {
return this._options
}
changeLink(root) {
this._group.linkRemove();
this._group.linkOn(root, "title")
}
};
function processTitleOptions(options) {
const newOptions = (0, _m_type.isString)(options) ? {
text: options
} : options || {};
newOptions.subtitle = (0, _m_type.isString)(newOptions.subtitle) ? {
text: newOptions.subtitle
} : newOptions.subtitle || {};
return newOptions
}
const plugin = exports.plugin = {
name: "title",
init() {
this._title = new Title({
renderer: this._renderer,
cssClass: `${this._rootClassPrefix}-title`,
incidentOccurred: this._incidentOccurred
});
this._layout.add(this._title)
},
dispose() {
this._title.dispose();
this._title = null
},
customize(constructor) {
constructor.addChange({
code: "TITLE",
handler() {
if (this._title.update(this._themeManager.theme("title"), this.option("title"))) {
this._change(["LAYOUT"])
}
},
isThemeDependent: true,
option: "title",
isOptionChange: true
})
},
fontFields: ["title.font", "title.subtitle.font"]
};