Browse Source

KERNEL-13841 refactor:base文件es6化箭头函数和this代码优化

es6
Joker.Wang-王顺 2 years ago
parent
commit
3db612aa38
  1. 24
      src/base/grid/grid.js
  2. 12
      src/base/layer/layer.drawer.js
  3. 16
      src/base/layer/layer.popover.js
  4. 4
      src/base/layer/layer.popup.js
  5. 16
      src/base/layer/layer.searcher.js
  6. 14
      src/base/list/listview.js
  7. 24
      src/base/list/virtualgrouplist.js
  8. 24
      src/base/list/virtuallist.js
  9. 10
      src/base/pager/pager.js
  10. 2
      src/base/single/tip/tip.toast.js
  11. 2
      src/base/single/tip/tip.tooltip.js

24
src/base/grid/grid.js

@ -30,7 +30,7 @@ export default class GridView extends Widget {
scrollLeft: 0,
scrollTop: 0,
items: [],
itemFormatter: (item, row, col)=> {
itemFormatter: (item, row, col) => {
return item;
},
});
@ -46,13 +46,13 @@ export default class GridView extends Widget {
this.renderedKeys = [];
this.renderRange = {};
this._scrollLock = false;
this._debounceRelease = BI.debounce(()=> {
this._debounceRelease = BI.debounce(() => {
this._scrollLock = false;
}, 1000 / 60);
this.container = BI._lazyCreateWidget({
type: "bi.absolute",
});
this.element.scroll(()=> {
this.element.scroll(() => {
if (this._scrollLock === true) {
return;
}
@ -85,7 +85,7 @@ export default class GridView extends Widget {
scrollx: scrollx,
items: [this.container],
});
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue)=> {
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue) => {
this.populate(newValue);
}) : o.items;
if (o.items.length > 0) {
@ -104,7 +104,7 @@ export default class GridView extends Widget {
}
destroyed() {
BI.each(this.renderedCells, (i, cell)=> {
BI.each(this.renderedCells, (i, cell) => {
cell.el._destroy();
})
}
@ -235,14 +235,14 @@ export default class GridView extends Widget {
}
// 已存在的, 需要添加的和需要删除的
const existSet = {}, addSet = {}, deleteArray = [];
BI.each(renderedKeys, (i, key)=> {
BI.each(renderedKeys, (i, key) => {
if (this.renderedKeys[i]) {
existSet[i] = key;
} else {
addSet[i] = key;
}
});
BI.each(this.renderedKeys, function (i, key) {
BI.each(this.renderedKeys, (i, key) => {
if (existSet[i]) {
return;
}
@ -251,12 +251,12 @@ export default class GridView extends Widget {
}
deleteArray.push(key[2]);
});
BI.each(deleteArray, (i, index)=> {
BI.each(deleteArray, (i, index) => {
// 性能优化,不调用destroy方法防止触发destroy事件
this.renderedCells[index].el._destroy();
});
const addedItems = [];
BI.each(addSet, function (index, key) {
BI.each(addSet, (index, key) => {
addedItems.push(renderedCells[key[2]]);
});
// 与listview一样, 给上下文
@ -370,7 +370,7 @@ export default class GridView extends Widget {
setOverflowX(b) {
if (this.options.overflowX !== !!b) {
this.options.overflowX = !!b;
BI.nextTick(()=> {
BI.nextTick(() => {
this.element.css({ overflowX: b ? "auto" : "hidden" });
});
}
@ -379,7 +379,7 @@ export default class GridView extends Widget {
setOverflowY(b) {
if (this.options.overflowY !== !!b) {
this.options.overflowY = !!b;
BI.nextTick(()=> {
BI.nextTick(() => {
this.element.css({ overflowY: b ? "auto" : "hidden" });
});
}
@ -418,7 +418,7 @@ export default class GridView extends Widget {
}
restore() {
BI.each(this.renderedCells, (i, cell)=> {
BI.each(this.renderedCells, (i, cell) => {
cell.el._destroy();
});
this._clearChildren();

12
src/base/layer/layer.drawer.js

@ -82,7 +82,7 @@ export default class Drawer extends Widget {
type: "bi.icon_button",
cls: "bi-message-close close-font",
height: headerHeight,
handler: ()=> {
handler: () => {
this.close();
},
} : {
@ -98,7 +98,7 @@ export default class Drawer extends Widget {
type: "bi.vertical",
scrolly: true,
cls: "drawer-body",
ref: (_ref)=> {
ref: (_ref) => {
this.body = _ref;
},
items: [{
@ -153,7 +153,7 @@ export default class Drawer extends Widget {
show(callback) {
const { placement } = this.options;
requestAnimationFrame(()=> {
requestAnimationFrame(() => {
const size = this._getSuitableSize();
switch (placement) {
case "right":
@ -185,7 +185,7 @@ export default class Drawer extends Widget {
hide(callback) {
const { placement } = this.options;
requestAnimationFrame(()=> {
requestAnimationFrame(() => {
switch (placement) {
case "right":
this.element.css({
@ -215,13 +215,13 @@ export default class Drawer extends Widget {
}
open() {
this.show(()=> {
this.show(() => {
this.fireEvent(Drawer.EVENT_OPEN);
});
}
close() {
this.hide(()=> {
this.hide(() => {
this.fireEvent(Drawer.EVENT_CLOSE);
});
}

16
src/base/layer/layer.popover.js

@ -47,7 +47,7 @@ export class Popover extends Widget {
this.startX = 0;
this.startY = 0;
const size = this._calculateSize();
this.tracker = new BI.MouseMoveTracker(function (deltaX, deltaY) {
this.tracker = new BI.MouseMoveTracker((deltaX, deltaY) => {
const W = BI.Widget._renderEngine.createElement("body").width();
const H = BI.Widget._renderEngine.createElement("body").height();
this.startX += deltaX;
@ -60,7 +60,7 @@ export class Popover extends Widget {
BI.Resizers._resize({
target: this.element[0],
});
}, ()=> {
}, () => {
this.tracker.releaseMouseMoves();
}, _global);
const items = [{
@ -70,7 +70,7 @@ export class Popover extends Widget {
items: [{
el: {
type: "bi.absolute",
ref: (_ref)=> {
ref: (_ref) => {
this.dragger = _ref;
},
items: [{
@ -95,7 +95,7 @@ export class Popover extends Widget {
type: "bi.icon_button",
cls: "bi-message-close close-font",
height: headerHeight,
handler: ()=> {
handler: () => {
this.close();
},
},
@ -109,7 +109,7 @@ export class Popover extends Widget {
type: "bi.vertical",
scrolly: true,
cls: "popover-body",
ref: (_ref)=> {
ref: (_ref) => {
this.body = _ref;
},
css: {
@ -164,7 +164,7 @@ export class Popover extends Widget {
}
// mounted之后绑定事件
mounted() {
this.dragger.element.mousedown(function (e) {
this.dragger.element.mousedown((e) => {
if (this.options.draggable !== false) {
this.startX = this.element[0].offsetLeft;
this.startY = this.element[0].offsetTop;
@ -258,7 +258,7 @@ export class BarPopover extends Popover {
text: this.options.btns[1],
value: 1,
level: "ignore",
handler: (v)=> {
handler: (v) => {
this.fireEvent(Popover.EVENT_CANCEL, v);
this.close(v);
},
@ -267,7 +267,7 @@ export class BarPopover extends Popover {
text: this.options.btns[0],
warningTitle: warningTitle,
value: 0,
handler: (v)=> {
handler: (v) => {
this.fireEvent(Popover.EVENT_CONFIRM, v);
this.close(v);
},

4
src/base/layer/layer.popup.js

@ -80,8 +80,8 @@ export default class PopupView extends Widget {
this.view = this._createView();
this.toolbar = this._createToolBar();
this.view.on(BI.Controller.EVENT_CHANGE, (type, ...args)=> {
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, args]);
this.view.on(BI.Controller.EVENT_CHANGE, (type, ...args) => {
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, ...args]);
if (type === BI.Events.CLICK) {
this.fireEvent(PopupView.EVENT_CHANGE);
}

16
src/base/layer/layer.searcher.js

@ -26,7 +26,7 @@ export default class SearcherView extends Pane {
matcher: { // 完全匹配的构造器
type: "bi.button_group",
behaviors: {
redmark: ()=> {
redmark: () => {
return true;
},
},
@ -38,7 +38,7 @@ export default class SearcherView extends Pane {
searcher: {
type: "bi.button_group",
behaviors: {
redmark: function () {
redmark: () => {
return true;
},
},
@ -56,7 +56,7 @@ export default class SearcherView extends Pane {
type: "bi.button_group",
chooseType,
behaviors: {
redmark: ()=> {
redmark: () => {
return true;
},
},
@ -65,8 +65,8 @@ export default class SearcherView extends Pane {
}],
value,
});
this.matcher.on(BI.Controller.EVENT_CHANGE, (type, val, ob)=> {
this.fireEvent(BI.Controller.EVENT_CHANGE, arguments);
this.matcher.on(BI.Controller.EVENT_CHANGE, (type, val, ob, ...args) => {
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, val, ob, ...args]);
if (type === BI.Events.CLICK) {
this.fireEvent(SearcherView.EVENT_CHANGE, val, ob);
}
@ -85,7 +85,7 @@ export default class SearcherView extends Pane {
type: "bi.button_group",
chooseType,
behaviors: {
redmark: ()=> {
redmark: () => {
return true;
},
},
@ -94,8 +94,8 @@ export default class SearcherView extends Pane {
}],
value,
});
this.searcher.on(BI.Controller.EVENT_CHANGE, (type, val, ob)=> {
this.fireEvent(BI.Controller.EVENT_CHANGE, arguments);
this.searcher.on(BI.Controller.EVENT_CHANGE, (type, val, ob, ...args) => {
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, val, ob, ...args]);
if (type === BI.Events.CLICK) {
this.fireEvent(BI.SearcherView.EVENT_CHANGE, val, ob);
}

14
src/base/list/listview.js

@ -16,7 +16,7 @@ export default class ListView extends Widget {
scrollTop: 0,
el: {},
items: [],
itemFormatter: (item, index)=> {
itemFormatter: (item, index) => {
return item;
},
};
@ -37,7 +37,7 @@ export default class ListView extends Widget {
items: [BI.extend({
type: "bi.vertical",
scrolly: false,
ref: (_ref)=> {
ref: (_ref) => {
this.container = _ref;
},
}, el)],
@ -49,17 +49,17 @@ export default class ListView extends Widget {
mounted() {
const o = this.options;
// 这里无法进行结构,因为存在赋值操作,如果使用结构则this.options的值不会跟随变化
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue)=> {
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue) => {
this.populate(newValue);
}) : o.items;
this._populate();
this.element.scroll((e)=> {
this.element.scroll((e) => {
o.scrollTop = this.element.scrollTop();
this._calculateBlocksToRender();
});
let lastWidth = this.element.width(),
lastHeight = this.element.height();
BI.ResizeDetector.addResizeListener(this, ()=> {
BI.ResizeDetector.addResizeListener(this, () => {
if (!this.element.is(":visible")) {
return;
}
@ -81,14 +81,14 @@ export default class ListView extends Widget {
let cnt = this.renderedIndex + 1;
let lastHeight;
const getElementHeight = ()=> {
const getElementHeight = () => {
return this.container.element.height();
}
lastHeight = getElementHeight();
while ((lastHeight) < minContentHeight && index < items.length) {
const itemsArr = items.slice(index, index + blockSize);
this.container.addItems(itemsArr.map((item, i)=> {
this.container.addItems(itemsArr.map((item, i) => {
return itemFormatter(item, index + i);
}), this);
const addedHeight = getElementHeight() - lastHeight;

24
src/base/list/virtualgrouplist.js

@ -18,7 +18,7 @@ export default class VirtualGroupList extends Widget {
rowHeight: "auto",
items: [],
el: {},
itemFormatter: (item, index)=> {
itemFormatter: (item, index) => {
return item;
},
};
@ -37,13 +37,13 @@ export default class VirtualGroupList extends Widget {
type: "bi.vertical",
items: [{
type: "bi.layout",
ref: ()=> {
ref: () => {
this.topBlank = this;
},
}, {
type: "bi.virtual_group",
height: rowHeight * items.length,
ref: ()=> {
ref: () => {
this.container = this;
},
layouts: [BI.extend({
@ -52,7 +52,7 @@ export default class VirtualGroupList extends Widget {
}, el)],
}, {
type: "bi.layout",
ref: ()=> {
ref: () => {
this.bottomBlank = this;
},
}],
@ -63,22 +63,22 @@ export default class VirtualGroupList extends Widget {
mounted() {
// 这里无法进行结构,因为存在赋值操作,如果使用结构则this.options的值不会跟随变化
const o = this.options;
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue)=> {
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue) => {
this.populate(newValue);
}) : o.items;
this._populate();
this.ticking = false;
this.element.scroll(()=> {
this.element.scroll(() => {
o.scrollTop = this.element.scrollTop();
if (!this.ticking) {
requestAnimationFrame(function () {
requestAnimationFrame(() => {
this._calculateBlocksToRender();
this.ticking = false;
});
this.ticking = true;
}
});
BI.ResizeDetector.addResizeListener(this, ()=> {
BI.ResizeDetector.addResizeListener(this, () => {
if (this.element.is(":visible")) {
this._calculateBlocksToRender();
}
@ -95,13 +95,13 @@ export default class VirtualGroupList extends Widget {
const minContentHeight = scrollTop + height + overscanHeight;
let index = (this.renderedIndex + 1) * blockSize, cnt = this.renderedIndex + 1;
let lastHeight;
const getElementHeight = ()=> {
const getElementHeight = () => {
return this.container.element.height() + this.topBlank.element.height() + this.bottomBlank.element.height();
}
lastHeight = this.renderedIndex === -1 ? 0 : getElementHeight();
while (lastHeight < minContentHeight && index < items.length) {
const itemsArr = items.slice(index, index + blockSize);
this.container[this.renderedIndex === -1 ? "populate" : "addItems"](itemsArr.map((item, i)=> {
this.container[this.renderedIndex === -1 ? "populate" : "addItems"](itemsArr.map((item, i) => {
return itemFormatter(item, index + i);
}), this);
const elementHeight = getElementHeight();
@ -138,7 +138,7 @@ export default class VirtualGroupList extends Widget {
}
}
this.bottomBlank.setHeight(this.tree.sumTo(this.renderedIndex) - this.tree.sumTo(Math.min(end, this.renderedIndex)) + "px");
this.container.populate(itemsArr.map((item, i)=> {
this.container.populate(itemsArr.map((item, i) => {
return itemFormatter(item, (start < 0 ? 0 : start) * blockSize + i);
}));
} else {
@ -149,7 +149,7 @@ export default class VirtualGroupList extends Widget {
}
}
this.container.element.height(rowHeight * items.length - topHeight);
this.container.populate(itemsArr.map((item, i)=> {
this.container.populate(itemsArr.map((item, i) => {
return itemFormatter(item, (start < 0 ? 0 : start) * blockSize + i);
}));
}

24
src/base/list/virtuallist.js

@ -16,7 +16,7 @@ export default class VirtualList extends Widget {
blockSize: 10,
scrollTop: 0,
items: [],
itemFormatter: (item, index)=> {
itemFormatter: (item, index) => {
return item;
},
};
@ -34,18 +34,18 @@ export default class VirtualList extends Widget {
type: "bi.vertical",
items: [{
type: "bi.layout",
ref: (_ref)=> {
ref: (_ref) => {
this.topBlank = _ref;
},
}, {
type: "bi.vertical",
scrolly: false,
ref: (_ref)=> {
ref: (_ref) => {
this.container = _ref;
},
}, {
type: "bi.layout",
ref: (_ref)=> {
ref: (_ref) => {
this.bottomBlank = _ref;
},
}],
@ -55,15 +55,15 @@ export default class VirtualList extends Widget {
mounted() {
// 这里无法进行结构,因为存在赋值操作,如果使用结构则this.options的值不会跟随变化
const o = this.options;
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue)=> {
o.items = BI.isFunction(o.items) ? this.__watch(o.items, (context, newValue) => {
this.populate(newValue);
}) : o.items;
this._populate();
this.element.scroll((e)=> {
this.element.scroll((e) => {
o.scrollTop = this.element.scrollTop();
this._calculateBlocksToRender();
});
BI.ResizeDetector.addResizeListener(this, ()=> {
BI.ResizeDetector.addResizeListener(this, () => {
if (this.element.is(":visible")) {
this._calculateBlocksToRender();
}
@ -76,13 +76,13 @@ export default class VirtualList extends Widget {
const minContentHeight = scrollTop + height + overscanHeight;
let index = (this.renderedIndex + 1) * blockSize, cnt = this.renderedIndex + 1;
let lastHeight;
const getElementHeight = ()=> {
const getElementHeight = () => {
return this.container.element.height() + this.topBlank.element.height() + this.bottomBlank.element.height();
}
lastHeight = getElementHeight();
while (lastHeight < minContentHeight && index < items.length) {
const itemsArr = items.slice(index, index + blockSize);
this.container.addItems(itemsArr.map((item, i)=> {
this.container.addItems(itemsArr.map((item, i) => {
return itemFormatter(item, index + i);
}), this);
const addedHeight = getElementHeight() - lastHeight;
@ -158,10 +158,10 @@ export default class VirtualList extends Widget {
this.container.element.append(lastFragment);
this.topBlank.setHeight(this.tree.sumTo(Math.max(-1, start - 1)) + "px");
this.bottomBlank.setHeight(this.tree.sumTo(this.renderedIndex) - this.tree.sumTo(Math.min(end, this.renderedIndex)) + "px");
BI.each(needMount, (i, child)=> {
BI.each(needMount, (i, child) => {
child && child._mount();
});
BI.each(needDestroyed, (i, child)=> {
BI.each(needDestroyed, (i, child) => {
child && child._destroy();
});
}
@ -180,7 +180,7 @@ export default class VirtualList extends Widget {
}
_clearChildren() {
BI.each(this.container._children, (i, cell)=> {
BI.each(this.container._children, (i, cell) => {
cell && cell._destroy();
});
this.container._children = {};

10
src/base/pager/pager.js

@ -23,7 +23,7 @@ export default class Pager extends Widget {
dynamicShowFirstLast: false, // 是否动态显示首页、尾页
dynamicShowPrevNext: false, // 是否动态显示上一页、下一页
pages: false, // 总页数
curr: function () {
curr: () => {
return 1;
}, // 初始化当前页
groups: 0, // 连续显示分页数
@ -34,7 +34,7 @@ export default class Pager extends Widget {
next: "下一页",
firstPage: 1,
lastPage: ()=> { // 在万不得已时才会调用这个函数获取最后一页的页码, 主要作用于setValue方法
lastPage: () => { // 在万不得已时才会调用这个函数获取最后一页的页码, 主要作用于setValue方法
return 1;
},
hasPrev: BI.emptyFn, // pages不可用时有效
@ -185,7 +185,7 @@ export default class Pager extends Widget {
this.button_group = BI.createWidget({
type: "bi.button_group",
element: this,
items: BI.map(view, (idx, v)=> {
items: BI.map(view, (idx, v) => {
v = BI.extend({
cls: "bi-list-item-select bi-border-radius",
height: 23,
@ -198,7 +198,7 @@ export default class Pager extends Widget {
behaviors,
layouts,
});
this.button_group.on(BI.Controller.EVENT_CHANGE, (type, value, obj, ...args)=> {
this.button_group.on(BI.Controller.EVENT_CHANGE, (type, value, obj, ...args) => {
// if (self._lock === true) {
// return;
// }
@ -230,7 +230,7 @@ export default class Pager extends Widget {
this._populate();
this.fireEvent(Pager.EVENT_CHANGE, obj);
}
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, value, obj, args]);
this.fireEvent.apply(this, [BI.Controller.EVENT_CHANGE, type, value, obj, ...args]);
});
this.fireEvent(Pager.EVENT_AFTER_POPULATE);
}

2
src/base/single/tip/tip.toast.js

@ -100,7 +100,7 @@ export default class Toast extends Tip {
items.push({
type: "bi.icon_button",
cls: "close-font toast-icon",
handler: ()=> {
handler: () => {
this.destroy();
},
height: textHeight,

2
src/base/single/tip/tip.tooltip.js

@ -51,7 +51,7 @@ export default class Tooltip extends Tip {
element: this,
hgap: this._const.hgap,
innerVgap: this._const.vgap,
items: BI.map(texts, function (i, text) {
items: BI.map(texts, (i, text) => {
return {
type: "bi.label",
textAlign: textAlign,

Loading…
Cancel
Save