fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.1 KiB

8 years ago
/**
* td布局
* @class BI.TdLayout
* @extends BI.Layout
*/
BI.TdLayout = BI.inherit(BI.Layout, {
4 years ago
props: function (props) {
8 years ago
return BI.extend(BI.TdLayout.superclass.props.apply(this, arguments), {
4 years ago
baseCls: "bi-td" + (props.verticalAlign === BI.VerticalAlign.Stretch ? " bi-h-fill" : ""),
4 years ago
columnSize: [],
4 years ago
rowSize: [],
verticalAlign: BI.VerticalAlign.Middle,
4 years ago
horizontalAlign: BI.HorizontalAlign.Stretch,
8 years ago
hgap: 0,
vgap: 0,
4 years ago
tgap: 0,
bgap: 0,
lgap: 0,
rgap: 0,
items: []
8 years ago
});
},
8 years ago
render: function () {
BI.TdLayout.superclass.render.apply(this, arguments);
4 years ago
var self = this, o = this.options;
this.$table = BI.Widget._renderEngine.createElement("<table>").attr({cellspacing: 0, cellpadding: 0}).css({
7 years ago
position: "relative",
4 years ago
width: (o.horizontalAlign === BI.HorizontalAlign.Center || o.horizontalAlign === BI.HorizontalAlign.Stretch) ? "100%" : "auto",
height: (o.verticalAlign !== BI.VerticalAlign.Top) ? "100%" : "auto",
8 years ago
"border-spacing": "0px",
7 years ago
border: "none",
8 years ago
"border-collapse": "separate"
8 years ago
});
8 years ago
this.rows = 0;
this.populate(this.options.items);
},
_addElement: function (idx, arr) {
var o = this.options;
7 years ago
function firstElement (item, row, col) {
8 years ago
if (row === 0) {
7 years ago
item.addClass("first-row");
8 years ago
}
if (col === 0) {
item.addClass("first-col");
}
item.addClass(BI.isOdd(row + 1) ? "odd-row" : "even-row");
item.addClass(BI.isOdd(col + 1) ? "odd-col" : "even-col");
item.addClass("center-element");
}
7 years ago
function firstObject (item, row, col) {
8 years ago
var cls = "";
if (row === 0) {
cls += " first-row";
}
if (col === 0) {
cls += " first-col";
}
BI.isOdd(row + 1) ? (cls += " odd-row") : (cls += " even-row");
BI.isOdd(col + 1) ? (cls += " odd-col") : (cls += " even-col");
item.cls = (item.cls || "") + cls + " center-element";
}
7 years ago
function first (item, row, col) {
8 years ago
if (item instanceof BI.Widget) {
firstElement(item.element, row, col);
} else if (item.el instanceof BI.Widget) {
firstElement(item.el.element, row, col);
} else if (item.el) {
7 years ago
firstObject(item.el, row, col);
8 years ago
} else {
firstObject(item, row, col);
}
}
4 years ago
var height = o.rowSize[idx] === "" ? "" : (o.rowSize[idx] < 1 ? ((o.rowSize[idx] * 100).toFixed(1) + "%") : o.rowSize[idx]);
4 years ago
var rowHeight = BI.isNumber(o.rowSize[idx]) ? (o.rowSize[idx] <= 1 ? height : height / BI.pixRatio + BI.pixUnit) : height;
5 years ago
var tr = BI._lazyCreateWidget({
8 years ago
type: "bi.default",
4 years ago
tagName: "tr",
height: height,
css: {
4 years ago
"max-height": rowHeight,
"min-height": rowHeight
4 years ago
}
7 years ago
});
8 years ago
for (var i = 0; i < arr.length; i++) {
5 years ago
var w = BI._lazyCreateWidget(arr[i]);
4 years ago
if (o.verticalAlign === BI.VerticalAlign.Stretch) {
w.element.addClass("h-fill-item");
}
7 years ago
w.element.css({position: "relative", top: "0", left: "0", margin: "0px auto"});
4 years ago
var item = arr[i];
if (o.vgap + o.tgap + (item.tgap || 0) + (item.vgap || 0) !== 0) {
w.element.css({
"margin-top": (o.vgap + o.tgap + (item.tgap || 0) + (item.vgap || 0)) / BI.pixRatio + BI.pixUnit
});
8 years ago
}
4 years ago
if (o.hgap + o.lgap + (item.lgap || 0) + (item.hgap || 0) !== 0) {
w.element.css({
"margin-left": ((i === 0 ? o.hgap : 0) + o.lgap + (item.lgap || 0) + (item.hgap || 0)) / BI.pixRatio + BI.pixUnit
});
8 years ago
}
4 years ago
if (o.hgap + o.rgap + (item.rgap || 0) + (item.hgap || 0) !== 0) {
w.element.css({
"margin-right": (o.hgap + o.rgap + (item.rgap || 0) + (item.hgap || 0)) / BI.pixRatio + BI.pixUnit
});
8 years ago
}
4 years ago
if (o.vgap + o.bgap + (item.bgap || 0) + (item.vgap || 0) !== 0) {
w.element.css({
"margin-bottom": (o.vgap + o.bgap + (item.bgap || 0) + (item.vgap || 0)) / BI.pixRatio + BI.pixUnit
});
8 years ago
}
8 years ago
first(w, this.rows++, i);
4 years ago
var width = "";
var columnSize = o.columnSize.length > 0 ? o.columnSize[i] : item.width;
if (columnSize > 0) {
width = columnSize < 1 ?
((columnSize * 100).toFixed(1) + "%")
: (columnSize + (i === 0 ? o.hgap : 0) + o.hgap + o.lgap + o.rgap);
}
4 years ago
function hasFill() {
if (o.columnSize.length > 0) {
return o.columnSize.indexOf("fill") >= 0;
}
return BI.some(arr, function (i, item) {
if (item.width === "fill") {
return true;
}
});
}
if ((BI.isNull(columnSize) || columnSize === "") && hasFill()) {
4 years ago
width = 2;
}
5 years ago
var td = BI._lazyCreateWidget({
7 years ago
type: "bi.default",
width: width,
7 years ago
tagName: "td",
8 years ago
items: [w]
7 years ago
});
// 对于表现为td的元素设置最大宽度,有几点需要注意
// 1、由于直接对td设置最大宽度是在规范中未定义的, 所以要使用类似td:firstChild来迂回实现
// 2、不能给多个td设置最大宽度,这样只会平分宽度
// 3、多百分比宽度就算了
4 years ago
if (columnSize > 0) {
4 years ago
columnSize = columnSize < 1 ? width : width / BI.pixRatio + BI.pixUnit;
td.element.css({
"max-width": columnSize,
"min-width": columnSize
});
4 years ago
}
8 years ago
td.element.css({
7 years ago
position: "relative",
"vertical-align": o.verticalAlign,
7 years ago
margin: "0",
padding: "0",
border: "none"
8 years ago
});
tr.addItem(td);
}
this.addWidget(this._getChildName(idx), tr);
8 years ago
return tr;
},
7 years ago
appendFragment: function (frag) {
this.$table.append(frag);
this.element.append(this.$table);
8 years ago
},
8 years ago
resize: function () {
// console.log("td布局不需要resize");
},
addItem: function (arr) {
if (!BI.isArray(arr)) {
throw new Error("必须是数组", arr);
8 years ago
}
return BI.TdLayout.superclass.addItem.apply(this, arguments);
},
6 years ago
update: function () {
},
8 years ago
populate: function (items) {
BI.TdLayout.superclass.populate.apply(this, arguments);
8 years ago
this._mount();
8 years ago
}
});
5 years ago
BI.shortcut("bi.td", BI.TdLayout);