From ae3b18cd19be0ca0be49bb7d3128e11924671aaa Mon Sep 17 00:00:00 2001 From: guy Date: Sun, 11 Oct 2020 21:01:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/shortcut.js | 18 +++++++++++------- src/core/widget.js | 2 +- src/core/wrapper/layout.js | 2 +- src/core/wrapper/layout/adapt/adapt.table.js | 4 ++-- .../wrapper/layout/adapt/float.horizontal.js | 8 ++++---- src/core/wrapper/layout/layout.border.js | 12 ++++++------ src/core/wrapper/layout/layout.card.js | 6 +++--- src/core/wrapper/layout/layout.division.js | 4 ++-- src/core/wrapper/layout/layout.grid.js | 8 ++++---- src/core/wrapper/layout/layout.horizontal.js | 4 ++-- src/core/wrapper/layout/layout.table.js | 4 ++-- src/core/wrapper/layout/layout.tape.js | 6 +++--- src/core/wrapper/layout/layout.td.js | 8 ++++---- src/core/wrapper/layout/layout.window.js | 4 ++-- .../wrapper/layout/middle/middle.center.js | 6 +++--- .../layout/middle/middle.float.center.js | 6 +++--- .../wrapper/layout/middle/middle.horizontal.js | 6 +++--- .../wrapper/layout/middle/middle.vertical.js | 6 +++--- 18 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/core/shortcut.js b/src/core/shortcut.js index 9bdddf158..6d45e91b3 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -11,7 +11,7 @@ }; // 根据配置属性生成widget - var createWidget = function (config) { + var createWidget = function (config, lazy) { var cls = kv[config.type]; if (!cls) { @@ -23,13 +23,13 @@ widget._initProps(config); widget._initRoot(); widget._initRef(); - // if (config.element || config.root) { - widget._lazyConstructor(); - // } + if (config.element || config.root || !lazy) { + widget._lazyConstructor(); + } return widget; }; - BI.createWidget = BI.createWidget || function (item, options, context) { + BI.createWidget = BI.createWidget || function (item, options, context, lazy) { // 先把准备环境准备好 BI.init(); var el, w; @@ -57,7 +57,7 @@ BI.Plugin.getObject(el.type, this); } }]); - return w.type === el.type ? createWidget(w) : BI.createWidget(BI.extend({}, item, {type: w.type}, options)); + return w.type === el.type ? createWidget(w, lazy) : BI.createWidget(BI.extend({}, item, {type: w.type}), options, context, lazy); } if (item.el && (item.el.type || options.type)) { el = BI.extend({}, options, item.el); @@ -68,7 +68,7 @@ BI.Plugin.getObject(el.type, this); } }]); - return w.type === el.type ? createWidget(w) : BI.createWidget(BI.extend({}, item, {type: w.type}, options)); + return w.type === el.type ? createWidget(w, lazy) : BI.createWidget(BI.extend({}, item, {type: w.type}), options, context, lazy); } if (BI.isWidget(item.el)) { return item.el; @@ -76,6 +76,10 @@ throw new Error("无法根据item创建组件"); }; + BI._lazyCreateWidget = BI._lazyCreateWidget || function (item, options, context) { + return BI.createWidget(item, options, context, true); + }; + BI.createElement = BI.createElement || function () { var widget = BI.createWidget.apply(this, arguments); return widget.element; diff --git a/src/core/widget.js b/src/core/widget.js index c56d5fd04..7b10f46e0 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -181,7 +181,7 @@ } if (BI.isArray(els)) { BI.each(els, function (i, el) { - BI.createWidget(el, { + BI._lazyCreateWidget(el, { element: self }); }); diff --git a/src/core/wrapper/layout.js b/src/core/wrapper/layout.js index e2b966d59..5b5b7970f 100644 --- a/src/core/wrapper/layout.js +++ b/src/core/wrapper/layout.js @@ -89,7 +89,7 @@ BI.Layout = BI.inherit(BI.Widget, { _addElement: function (i, item, context) { var self = this, w; if (!this.hasWidget(this._getChildName(i))) { - w = BI.createWidget(item, context); + w = BI._lazyCreateWidget(item, context); w.on(BI.Events.DESTROY, function () { BI.each(self._children, function (name, child) { if (child === w) { diff --git a/src/core/wrapper/layout/adapt/adapt.table.js b/src/core/wrapper/layout/adapt/adapt.table.js index 98e7bc259..c391a156b 100644 --- a/src/core/wrapper/layout/adapt/adapt.table.js +++ b/src/core/wrapper/layout/adapt/adapt.table.js @@ -37,9 +37,9 @@ BI.TableAdaptLayout = BI.inherit(BI.Layout, { var td; var width = o.columnSize[i] <= 1 ? (o.columnSize[i] * 100 + "%") : o.columnSize[i]; if (!this.hasWidget(this._getChildName(i))) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({position: "relative", top: "0", left: "0", margin: "0px auto"}); - td = BI.createWidget({ + td = BI._lazyCreateWidget({ type: "bi.default", width: width, items: [w] diff --git a/src/core/wrapper/layout/adapt/float.horizontal.js b/src/core/wrapper/layout/adapt/float.horizontal.js index 6e15f07fb..84f2cbaa0 100644 --- a/src/core/wrapper/layout/adapt/float.horizontal.js +++ b/src/core/wrapper/layout/adapt/float.horizontal.js @@ -33,7 +33,7 @@ BI.FloatHorizontalLayout = BI.inherit(BI.Layout, { delete self._children[i]; } }); - BI.createWidget({ + BI._lazyCreateWidget({ type: "bi.horizontal_auto", element: this, items: [this.left] @@ -42,7 +42,7 @@ BI.FloatHorizontalLayout = BI.inherit(BI.Layout, { _addElement: function (i, item) { var self = this, o = this.options; - this.left = BI.createWidget({ + this.left = BI._lazyCreateWidget({ type: "bi.vertical", items: [item], hgap: o.hgap, @@ -53,7 +53,7 @@ BI.FloatHorizontalLayout = BI.inherit(BI.Layout, { rgap: o.rgap }); - this.container = BI.createWidget({ + this.container = BI._lazyCreateWidget({ type: "bi.left", element: this, items: [this.left] @@ -67,4 +67,4 @@ BI.FloatHorizontalLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.horizontal_float", BI.FloatHorizontalLayout); \ No newline at end of file +BI.shortcut("bi.horizontal_float", BI.FloatHorizontalLayout); diff --git a/src/core/wrapper/layout/layout.border.js b/src/core/wrapper/layout/layout.border.js index f96c9e6f0..4f2028b43 100644 --- a/src/core/wrapper/layout/layout.border.js +++ b/src/core/wrapper/layout/layout.border.js @@ -36,7 +36,7 @@ BI.BorderLayout = BI.inherit(BI.Layout, { if (item != null) { if (item.el) { if (!this.hasWidget(this.getName() + "north")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); this.addWidget(this.getName() + "north", w); } this.getWidgetByName(this.getName() + "north").element.height(item.height) @@ -56,7 +56,7 @@ BI.BorderLayout = BI.inherit(BI.Layout, { if (item != null) { if (item.el) { if (!this.hasWidget(this.getName() + "south")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); this.addWidget(this.getName() + "south", w); } this.getWidgetByName(this.getName() + "south").element.height(item.height) @@ -76,7 +76,7 @@ BI.BorderLayout = BI.inherit(BI.Layout, { if (item != null) { if (item.el) { if (!this.hasWidget(this.getName() + "west")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); this.addWidget(this.getName() + "west", w); } this.getWidgetByName(this.getName() + "west").element.width(item.width) @@ -96,7 +96,7 @@ BI.BorderLayout = BI.inherit(BI.Layout, { if (item != null) { if (item.el) { if (!this.hasWidget(this.getName() + "east")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); this.addWidget(this.getName() + "east", w); } this.getWidgetByName(this.getName() + "east").element.width(item.width) @@ -115,7 +115,7 @@ BI.BorderLayout = BI.inherit(BI.Layout, { item = regions["center"]; if (item != null) { if (!this.hasWidget(this.getName() + "center")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); this.addWidget(this.getName() + "center", w); } this.getWidgetByName(this.getName() + "center").element @@ -132,4 +132,4 @@ BI.BorderLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.border", BI.BorderLayout); \ No newline at end of file +BI.shortcut("bi.border", BI.BorderLayout); diff --git a/src/core/wrapper/layout/layout.card.js b/src/core/wrapper/layout/layout.card.js index 0a9e9171f..ad266ca79 100644 --- a/src/core/wrapper/layout/layout.card.js +++ b/src/core/wrapper/layout/layout.card.js @@ -28,7 +28,7 @@ BI.CardLayout = BI.inherit(BI.Layout, { BI.each(items, function (i, item) { if (item) { if (!self.hasWidget(item.cardName)) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.on(BI.Events.DESTROY, function () { var index = BI.findIndex(o.items, function (i, tItem) { return tItem.cardName == item.cardName; @@ -98,7 +98,7 @@ BI.CardLayout = BI.inherit(BI.Layout, { if (this.isCardExisted(cardName)) { throw new Error("cardName is already exist"); } - var widget = BI.createWidget(cardItem, this); + var widget = BI._lazyCreateWidget(cardItem, this); widget.element.css({ position: "relative", top: "0", @@ -206,4 +206,4 @@ BI.CardLayout = BI.inherit(BI.Layout, { } } }); -BI.shortcut("bi.card", BI.CardLayout); \ No newline at end of file +BI.shortcut("bi.card", BI.CardLayout); diff --git a/src/core/wrapper/layout/layout.division.js b/src/core/wrapper/layout/layout.division.js index 253a0570a..59ca653d1 100644 --- a/src/core/wrapper/layout/layout.division.js +++ b/src/core/wrapper/layout/layout.division.js @@ -115,7 +115,7 @@ BI.DivisionLayout = BI.inherit(BI.Layout, { throw new Error("item be required"); } if (!this.hasWidget(this.getName() + i + "_" + j)) { - var w = BI.createWidget(map[i][j]); + var w = BI._lazyCreateWidget(map[i][j]); this.addWidget(this.getName() + i + "_" + j, w); } else { w = this.getWidgetByName(this.getName() + i + "_" + j); @@ -159,4 +159,4 @@ BI.DivisionLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.division", BI.DivisionLayout); \ No newline at end of file +BI.shortcut("bi.division", BI.DivisionLayout); diff --git a/src/core/wrapper/layout/layout.grid.js b/src/core/wrapper/layout/layout.grid.js index e00560cc2..125b15080 100644 --- a/src/core/wrapper/layout/layout.grid.js +++ b/src/core/wrapper/layout/layout.grid.js @@ -93,16 +93,16 @@ BI.GridLayout = BI.inherit(BI.Layout, { BI.each(items, function (i, item) { if (BI.isArray(item)) { BI.each(item, function (j, el) { - els[i][j] = BI.createWidget(el); + els[i][j] = BI._lazyCreateWidget(el); }); return; } - els[item.row][item.column] = BI.createWidget(item); + els[item.row][item.column] = BI._lazyCreateWidget(item); }); for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { if (!els[i][j]) { - els[i][j] = BI.createWidget({ + els[i][j] = BI._lazyCreateWidget({ type: "bi.layout" }); } @@ -127,4 +127,4 @@ BI.GridLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.grid", BI.GridLayout); \ No newline at end of file +BI.shortcut("bi.grid", BI.GridLayout); diff --git a/src/core/wrapper/layout/layout.horizontal.js b/src/core/wrapper/layout/layout.horizontal.js index 2fc56a69f..8008ef57d 100644 --- a/src/core/wrapper/layout/layout.horizontal.js +++ b/src/core/wrapper/layout/layout.horizontal.js @@ -41,9 +41,9 @@ BI.HorizontalLayout = BI.inherit(BI.Layout, { var td; var width = o.columnSize[i] <= 1 ? (o.columnSize[i] * 100 + "%") : o.columnSize[i]; if (!this.hasWidget(this._getChildName(i))) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({position: "relative", margin: "0px auto"}); - td = BI.createWidget({ + td = BI._lazyCreateWidget({ type: "bi.default", tagName: "td", attributes: { diff --git a/src/core/wrapper/layout/layout.table.js b/src/core/wrapper/layout/layout.table.js index 4967dce78..39e40ccd4 100644 --- a/src/core/wrapper/layout/layout.table.js +++ b/src/core/wrapper/layout/layout.table.js @@ -110,7 +110,7 @@ BI.TableLayout = BI.inherit(BI.Layout, { right: right <= 1 ? right * 100 + "%" : right }, arr[i])); } - var w = BI.createWidget({ + var w = BI._lazyCreateWidget({ type: "bi.absolute", height: BI.isArray(o.rowSize) ? o.rowSize[this.rows] : o.rowSize, items: abs @@ -146,4 +146,4 @@ BI.TableLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.table", BI.TableLayout); \ No newline at end of file +BI.shortcut("bi.table", BI.TableLayout); diff --git a/src/core/wrapper/layout/layout.tape.js b/src/core/wrapper/layout/layout.tape.js index 04481709d..4f4730ddd 100644 --- a/src/core/wrapper/layout/layout.tape.js +++ b/src/core/wrapper/layout/layout.tape.js @@ -47,7 +47,7 @@ BI.HTapeLayout = BI.inherit(BI.Layout, { items = BI.compact(items); BI.each(items, function (i, item) { if (!self.hasWidget(self.getName() + i + "")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); self.addWidget(self.getName() + i + "", w); } else { w = self.getWidgetByName(self.getName() + i + ""); @@ -160,7 +160,7 @@ BI.VTapeLayout = BI.inherit(BI.Layout, { items = BI.compact(items); BI.each(items, function (i, item) { if (!self.hasWidget(self.getName() + i + "")) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); self.addWidget(self.getName() + i + "", w); } else { w = self.getWidgetByName(self.getName() + i + ""); @@ -216,4 +216,4 @@ BI.VTapeLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.vtape", BI.VTapeLayout); \ No newline at end of file +BI.shortcut("bi.vtape", BI.VTapeLayout); diff --git a/src/core/wrapper/layout/layout.td.js b/src/core/wrapper/layout/layout.td.js index b452751b5..24add74d9 100644 --- a/src/core/wrapper/layout/layout.td.js +++ b/src/core/wrapper/layout/layout.td.js @@ -77,13 +77,13 @@ BI.TdLayout = BI.inherit(BI.Layout, { } } - var tr = BI.createWidget({ + var tr = BI._lazyCreateWidget({ type: "bi.default", tagName: "tr" }); for (var i = 0; i < arr.length; i++) { - var w = BI.createWidget(arr[i]); + var w = BI._lazyCreateWidget(arr[i]); w.element.css({position: "relative", top: "0", left: "0", margin: "0px auto"}); if (arr[i].lgap) { w.element.css({"margin-left": arr[i].lgap + "px"}); @@ -98,7 +98,7 @@ BI.TdLayout = BI.inherit(BI.Layout, { w.element.css({"margin-bottom": arr[i].bgap + "px"}); } first(w, this.rows++, i); - var td = BI.createWidget({ + var td = BI._lazyCreateWidget({ type: "bi.default", attributes: { width: o.columnSize[i] <= 1 ? (o.columnSize[i] * 100 + "%") : o.columnSize[i] @@ -143,4 +143,4 @@ BI.TdLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.td", BI.TdLayout); \ No newline at end of file +BI.shortcut("bi.td", BI.TdLayout); diff --git a/src/core/wrapper/layout/layout.window.js b/src/core/wrapper/layout/layout.window.js index 642e785fd..75f9069ed 100644 --- a/src/core/wrapper/layout/layout.window.js +++ b/src/core/wrapper/layout/layout.window.js @@ -95,7 +95,7 @@ BI.WindowLayout = BI.inherit(BI.Layout, { throw new Error("item be required"); } if (!this.hasWidget(this.getName() + i + "_" + j)) { - var w = BI.createWidget(o.items[i][j]); + var w = BI._lazyCreateWidget(o.items[i][j]); w.element.css({position: "absolute"}); this.addWidget(this.getName() + i + "_" + j, w); } @@ -188,4 +188,4 @@ BI.WindowLayout = BI.inherit(BI.Layout, { this._mount(); } }); -BI.shortcut("bi.window", BI.WindowLayout); \ No newline at end of file +BI.shortcut("bi.window", BI.WindowLayout); diff --git a/src/core/wrapper/layout/middle/middle.center.js b/src/core/wrapper/layout/middle/middle.center.js index 02d5053bc..6309e520d 100644 --- a/src/core/wrapper/layout/middle/middle.center.js +++ b/src/core/wrapper/layout/middle/middle.center.js @@ -24,7 +24,7 @@ BI.CenterLayout = BI.inherit(BI.Layout, { list.push({ column: i, row: 0, - el: BI.createWidget({ + el: BI._lazyCreateWidget({ type: "bi.default", cls: "center-element " + (i === 0 ? "first-element " : "") + (i === items.length - 1 ? "last-element" : "") }) @@ -32,7 +32,7 @@ BI.CenterLayout = BI.inherit(BI.Layout, { }); BI.each(items, function (i, item) { if (item) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({ position: "absolute", left: o.hgap + o.lgap, @@ -73,4 +73,4 @@ BI.CenterLayout = BI.inherit(BI.Layout, { this.wrapper.populate.apply(this.wrapper, arguments); } }); -BI.shortcut("bi.center", BI.CenterLayout); \ No newline at end of file +BI.shortcut("bi.center", BI.CenterLayout); diff --git a/src/core/wrapper/layout/middle/middle.float.center.js b/src/core/wrapper/layout/middle/middle.float.center.js index c8b9c0022..213272543 100644 --- a/src/core/wrapper/layout/middle/middle.float.center.js +++ b/src/core/wrapper/layout/middle/middle.float.center.js @@ -20,7 +20,7 @@ BI.FloatCenterLayout = BI.inherit(BI.Layout, { var self = this, o = this.options, items = o.items; var list = [], width = 100 / items.length; BI.each(items, function (i) { - var widget = BI.createWidget({ + var widget = BI._lazyCreateWidget({ type: "bi.default" }); widget.element.addClass("center-element " + (i === 0 ? "first-element " : "") + (i === items.length - 1 ? "last-element" : "")).css({ @@ -33,7 +33,7 @@ BI.FloatCenterLayout = BI.inherit(BI.Layout, { }); BI.each(items, function (i, item) { if (item) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({ position: "absolute", left: o.hgap + o.lgap, @@ -72,4 +72,4 @@ BI.FloatCenterLayout = BI.inherit(BI.Layout, { this.wrapper.populate.apply(this.wrapper, arguments); } }); -BI.shortcut("bi.float_center", BI.FloatCenterLayout); \ No newline at end of file +BI.shortcut("bi.float_center", BI.FloatCenterLayout); diff --git a/src/core/wrapper/layout/middle/middle.horizontal.js b/src/core/wrapper/layout/middle/middle.horizontal.js index 7c101b64e..e4389624e 100644 --- a/src/core/wrapper/layout/middle/middle.horizontal.js +++ b/src/core/wrapper/layout/middle/middle.horizontal.js @@ -23,7 +23,7 @@ BI.HorizontalCenterLayout = BI.inherit(BI.Layout, { list.push({ column: i, row: 0, - el: BI.createWidget({ + el: BI._lazyCreateWidget({ type: "bi.default", cls: "center-element " + (i === 0 ? "first-element " : "") + (i === items.length - 1 ? "last-element" : "") }) @@ -31,7 +31,7 @@ BI.HorizontalCenterLayout = BI.inherit(BI.Layout, { }); BI.each(items, function (i, item) { if (item) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({ position: "absolute", left: o.hgap + o.lgap, @@ -71,4 +71,4 @@ BI.HorizontalCenterLayout = BI.inherit(BI.Layout, { this.wrapper.populate.apply(this.wrapper, arguments); } }); -BI.shortcut("bi.horizontal_center", BI.HorizontalCenterLayout); \ No newline at end of file +BI.shortcut("bi.horizontal_center", BI.HorizontalCenterLayout); diff --git a/src/core/wrapper/layout/middle/middle.vertical.js b/src/core/wrapper/layout/middle/middle.vertical.js index f7a1082a7..0b61f2054 100644 --- a/src/core/wrapper/layout/middle/middle.vertical.js +++ b/src/core/wrapper/layout/middle/middle.vertical.js @@ -24,7 +24,7 @@ BI.VerticalCenterLayout = BI.inherit(BI.Layout, { list.push({ column: 0, row: i, - el: BI.createWidget({ + el: BI._lazyCreateWidget({ type: "bi.default", cls: "center-element " + (i === 0 ? "first-element " : "") + (i === items.length - 1 ? "last-element" : "") }) @@ -32,7 +32,7 @@ BI.VerticalCenterLayout = BI.inherit(BI.Layout, { }); BI.each(items, function (i, item) { if (item) { - var w = BI.createWidget(item); + var w = BI._lazyCreateWidget(item); w.element.css({ position: "absolute", left: o.hgap + o.lgap, @@ -72,4 +72,4 @@ BI.VerticalCenterLayout = BI.inherit(BI.Layout, { this.wrapper.populate.apply(this.wrapper, arguments); } }); -BI.shortcut("bi.vertical_center", BI.VerticalCenterLayout); \ No newline at end of file +BI.shortcut("bi.vertical_center", BI.VerticalCenterLayout);