Browse Source

无JIRA任务,invisible为true的时候不create子组件

es6
Dailer 6 years ago
parent
commit
7fae9a9aa6
  1. 29
      dist/fix/fix.compact.js
  2. 873
      src/core/widget.js

29
dist/fix/fix.compact.js vendored

@ -1,5 +1,5 @@
;(function () { ;(function () {
function initWatch (vm, watch) { function initWatch(vm, watch) {
vm._watchers || (vm._watchers = []); vm._watchers || (vm._watchers = []);
for (var key in watch) { for (var key in watch) {
var handler = watch[key]; var handler = watch[key];
@ -13,7 +13,7 @@
} }
} }
function createWatcher (vm, keyOrFn, handler) { function createWatcher(vm, keyOrFn, handler) {
return Fix.watch(vm.model, keyOrFn, _.bind(handler, vm), { return Fix.watch(vm.model, keyOrFn, _.bind(handler, vm), {
store: vm.store store: vm.store
}); });
@ -22,24 +22,24 @@
var target = null; var target = null;
var targetStack = []; var targetStack = [];
function pushTarget (_target) { function pushTarget(_target) {
if (target) targetStack.push(target); if (target) targetStack.push(target);
Fix.Model.target = target = _target; Fix.Model.target = target = _target;
} }
function popTarget () { function popTarget() {
Fix.Model.target = target = targetStack.pop(); Fix.Model.target = target = targetStack.pop();
} }
var context = null; var context = null;
var contextStack = []; var contextStack = [];
function pushContext (_context) { function pushContext(_context) {
if (context) contextStack.push(context); if (context) contextStack.push(context);
Fix.Model.context = context = _context; Fix.Model.context = context = _context;
} }
function popContext () { function popContext() {
Fix.Model.context = context = contextStack.pop(); Fix.Model.context = context = contextStack.pop();
} }
@ -60,7 +60,7 @@
}, options); }, options);
}; };
function findStore (widget) { function findStore(widget) {
if (target != null) { if (target != null) {
return target; return target;
} }
@ -106,12 +106,11 @@
}; };
}); });
var _init = BI.Widget.prototype._init; function createStore() {
BI.Widget.prototype._init = function () {
var self = this;
var needPop = false; var needPop = false;
if (window.Fix && this._store) { if (!this._storeCreated && window.Fix && this._store && this.isVisible()) {
var store = findStore(this.options.context || this.options.element); var store = findStore(this.options.context || this.options.element);
this._storeCreated = true;
if (store) { if (store) {
pushTarget(store); pushTarget(store);
needPop = true; needPop = true;
@ -128,6 +127,13 @@
} }
needPop = true; needPop = true;
} }
return needPop;
}
var _init = BI.Widget.prototype._init;
BI.Widget.prototype._init = function () {
var self = this;
var needPop = createStore.call(this);
_init.apply(this, arguments); _init.apply(this, arguments);
needPop && popTarget(); needPop && popTarget();
}; };
@ -166,6 +172,7 @@
_.each(["_mount"], function (name) { _.each(["_mount"], function (name) {
var old = BI.Widget.prototype[name]; var old = BI.Widget.prototype[name];
old && (BI.Widget.prototype[name] = function () { old && (BI.Widget.prototype[name] = function () {
createStore.call(this);
this.store && pushTarget(this.store); this.store && pushTarget(this.store);
var res = old.apply(this, arguments); var res = old.apply(this, arguments);
this.store && popTarget(); this.store && popTarget();

873
src/core/widget.js

@ -5,466 +5,495 @@
* *
* @cfg {JSON} options 配置属性 * @cfg {JSON} options 配置属性
*/ */
BI.Widget = BI.inherit(BI.OB, {
_defaultConfig: function () { !(function () {
return BI.extend(BI.Widget.superclass._defaultConfig.apply(this), { var lazy = (typeof document !== 'undefined' &&
root: false, typeof document.documentMode === 'number') ||
tagName: "div", (typeof navigator !== 'undefined' &&
attributes: null, typeof navigator.userAgent === 'string' &&
data: null, /\bEdge\/\d/.test(navigator.userAgent));
BI.Widget = BI.inherit(BI.OB, {
tag: null, _defaultConfig: function () {
disabled: false, return BI.extend(BI.Widget.superclass._defaultConfig.apply(this), {
invisible: false, root: false,
invalid: false, tagName: "div",
baseCls: "", attributes: null,
extraCls: "", data: null,
cls: ""
}); tag: null,
}, disabled: false,
invisible: false,
beforeInit: null, invalid: false,
baseCls: "",
// 生命周期函数 extraCls: "",
beforeCreate: null, cls: ""
});
created: null, },
render: null, beforeInit: null,
beforeMount: null, // 生命周期函数
beforeCreate: null,
mounted: null,
created: null,
shouldUpdate: null,
render: null,
update: function () {
}, beforeMount: null,
beforeDestroy: null, mounted: null,
destroyed: null, shouldUpdate: null,
_init: function () { update: function () {
BI.Widget.superclass._init.apply(this, arguments); },
this._initRoot();
this._initElementWidth(); beforeDestroy: null,
this._initElementHeight();
this._initVisual(); destroyed: null,
this._initState();
if (this.beforeInit) { _init: function () {
this.__asking = true; BI.Widget.superclass._init.apply(this, arguments);
this.beforeInit(BI.bind(this._render, this)); this._initRoot();
if (this.__asking === true) { this._initElementWidth();
this.__async = true; this._initElementHeight();
this._initVisual();
this._initState();
if (this.isVisible()) {
if (this.beforeInit) {
this.__asking = true;
this.beforeInit(BI.bind(this._render, this));
if (this.__asking === true) {
this.__async = true;
}
} else {
this._render();
}
this.rendered = true
} }
} else { if (this._isRoot) {
this._render(); this._mount()
} }
}, },
_render: function () { _render: function () {
this.__asking = false; this.__asking = false;
this.beforeCreate && this.beforeCreate(); this.beforeCreate && this.beforeCreate();
this._initElement(); this._initElement();
this._initEffects(); this._initEffects();
this.created && this.created(); this.created && this.created();
}, },
/** /**
* 初始化根节点 * 初始化根节点
* @private * @private
*/ */
_initRoot: function () { _initRoot: function () {
var o = this.options; var o = this.options;
this.widgetName = o.widgetName || BI.uniqueId("widget"); this.widgetName = o.widgetName || BI.uniqueId("widget");
this._isRoot = o.root; this._isRoot = o.root;
if (BI.isWidget(o.element)) { if (BI.isWidget(o.element)) {
if (o.element instanceof BI.Widget) { if (o.element instanceof BI.Widget) {
this._parent = o.element; this._parent = o.element;
this._parent.addWidget(this.widgetName, this); this._parent.addWidget(this.widgetName, this);
} else { } else {
this._isRoot = true;
}
this.element = this.options.element.element;
} else if (o.element) {
// if (o.root !== true) {
// throw new Error("root is a required property");
// }
this.element = $(o.element);
this._isRoot = true; this._isRoot = true;
} else {
this.element = $(document.createElement(o.tagName));
} }
this.element = this.options.element.element; this.element._isWidget = true;
} else if (o.element) { if (o.baseCls || o.extraCls || o.cls) {
// if (o.root !== true) { this.element.addClass((o.baseCls || "") + " " + (o.extraCls || "") + " " + (o.cls || ""));
// throw new Error("root is a required property"); }
// } if (o.attributes) {
this.element = $(o.element); this.element.attr(o.attributes);
this._isRoot = true; }
} else { if (o.data) {
this.element = $(document.createElement(o.tagName)); this.element.data(o.data);
} }
this.element._isWidget = true; this._children = {};
if (o.baseCls || o.extraCls || o.cls) { },
this.element.addClass((o.baseCls || "") + " " + (o.extraCls || "") + " " + (o.cls || ""));
}
if (o.attributes) {
this.element.attr(o.attributes);
}
if (o.data) {
this.element.data(o.data);
}
this._children = {};
},
_initElementWidth: function () {
var o = this.options;
if (BI.isWidthOrHeight(o.width)) {
this.element.css("width", o.width);
}
},
_initElementHeight: function () { _initElementWidth: function () {
var o = this.options; var o = this.options;
if (BI.isWidthOrHeight(o.height)) { if (BI.isWidthOrHeight(o.width)) {
this.element.css("height", o.height); this.element.css("width", o.width);
} }
}, },
_initVisual: function () { _initElementHeight: function () {
var o = this.options; var o = this.options;
if (o.invisible) { if (BI.isWidthOrHeight(o.height)) {
// 用display属性做显示和隐藏,否则jquery会在显示时将display设为block会覆盖掉display:flex属性 this.element.css("height", o.height);
this.element.css("display", "none"); }
} },
},
_initEffects: function () { _initVisual: function () {
var o = this.options; var o = this.options;
if (o.disabled || o.invalid) { if (o.invisible) {
if (this.options.disabled) { // 用display属性做显示和隐藏,否则jquery会在显示时将display设为block会覆盖掉display:flex属性
this.setEnable(false); this.element.css("display", "none");
} }
if (this.options.invalid) { },
this.setValid(false);
_initEffects: function () {
var o = this.options;
if (o.disabled || o.invalid) {
if (this.options.disabled) {
this.setEnable(false);
}
if (this.options.invalid) {
this.setValid(false);
}
} }
} },
},
_initState: function () { _initState: function () {
this._isMounted = false; this._isMounted = false;
}, },
_initElement: function () { _initElement: function () {
var self = this; var self = this;
var els = this.render && this.render(); var els = this.render && this.render();
if (BI.isPlainObject(els)) { if (BI.isPlainObject(els)) {
els = [els]; els = [els];
} }
if (BI.isArray(els)) { if (BI.isArray(els)) {
BI.each(els, function (i, el) { BI.each(els, function (i, el) {
BI.createWidget(el, { BI.createWidget(el, {
element: self element: self
});
}); });
}
// if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount();
// }
},
_setParent: function (parent) {
this._parent = parent;
},
_mount: function () {
var self = this;
var isMounted = this._isMounted;
if (this._isMounting || isMounted || !this.isVisible() || this.__asking === true) {
return;
}
if (this._isRoot === true) {
isMounted = true;
} else if (this._parent && this._parent._isMounted === true) {
isMounted = true;
}
if (!isMounted) {
return;
}
this._isMounting = true
if (!this.rendered) {
if (this.beforeInit) {
this.__asking = true;
this.beforeInit(BI.bind(this._render, this));
if (this.__asking === true) {
this.__async = true;
}
} else {
this._render();
}
}
this.beforeMount && this.beforeMount();
this._isMounted = true;
lazy && this._mountChildren && this._mountChildren();
BI.each(this._children, function (i, widget) {
!self.isEnabled() && widget._setEnable(false);
!self.isValid() && widget._setValid(false);
widget._mount && widget._mount();
}); });
} !lazy && this._mountChildren && this._mountChildren();
// if (this._isRoot === true || !(this instanceof BI.Layout)) { this.mounted && this.mounted();
this._mount(); this._isMounting = false
// } },
},
_mountChildren: null,
_setParent: function (parent) {
this._parent = parent; isMounted: function () {
}, return this._isMounted;
},
_mount: function () {
var self = this; setWidth: function (w) {
var isMounted = this._isMounted; this.options.width = w;
if (isMounted || !this.isVisible() || this.__asking === true) { this._initElementWidth();
return; },
}
if (this._isRoot === true) { setHeight: function (h) {
isMounted = true; this.options.height = h;
} else if (this._parent && this._parent._isMounted === true) { this._initElementHeight();
isMounted = true; },
}
if (!isMounted) { _setEnable: function (enable) {
return; if (enable === true) {
} this.options.disabled = false;
this.beforeMount && this.beforeMount(); } else if (enable === false) {
this._isMounted = true; this.options.disabled = true;
this._mountChildren && this._mountChildren(); }
BI.each(this._children, function (i, widget) { // 递归将所有子组件使能
!self.isEnabled() && widget._setEnable(false); BI.each(this._children, function (i, child) {
!self.isValid() && widget._setValid(false); !child._manualSetEnable && child._setEnable && child._setEnable(enable);
widget._mount && widget._mount(); });
}); },
this.mounted && this.mounted();
},
_mountChildren: null,
isMounted: function () {
return this._isMounted;
},
setWidth: function (w) {
this.options.width = w;
this._initElementWidth();
},
setHeight: function (h) {
this.options.height = h;
this._initElementHeight();
},
_setEnable: function (enable) {
if (enable === true) {
this.options.disabled = false;
} else if (enable === false) {
this.options.disabled = true;
}
// 递归将所有子组件使能
BI.each(this._children, function (i, child) {
!child._manualSetEnable && child._setEnable && child._setEnable(enable);
});
},
_setValid: function (valid) {
if (valid === true) {
this.options.invalid = false;
} else if (valid === false) {
this.options.invalid = true;
}
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
!child._manualSetValid && child._setValid && child._setValid(valid);
});
},
_setVisible: function (visible) {
if (visible === true) {
this.options.invisible = false;
} else if (visible === false) {
this.options.invisible = true;
}
},
setEnable: function (enable) {
this._manualSetEnable = true;
this._setEnable(enable);
if (enable === true) {
this.element.removeClass("base-disabled disabled");
} else if (enable === false) {
this.element.addClass("base-disabled disabled");
}
},
setVisible: function (visible) { _setValid: function (valid) {
this._setVisible(visible); if (valid === true) {
if (visible === true) { this.options.invalid = false;
// 用this.element.show()会把display属性改成block } else if (valid === false) {
this.element.css("display", ""); this.options.invalid = true;
this._mount();
} else if (visible === false) {
this.element.css("display", "none");
}
this.fireEvent(BI.Events.VIEW, visible);
},
setValid: function (valid) {
this._manualSetValid = true;
this._setValid(valid);
if (valid === true) {
this.element.removeClass("base-invalid invalid");
} else if (valid === false) {
this.element.addClass("base-invalid invalid");
}
},
doBehavior: function () {
var args = arguments;
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
child.doBehavior && child.doBehavior.apply(child, args);
});
},
getWidth: function () {
return this.options.width;
},
getHeight: function () {
return this.options.height;
},
isValid: function () {
return !this.options.invalid;
},
addWidget: function (name, widget) {
var self = this;
if (name instanceof BI.Widget) {
widget = name;
name = widget.getName();
}
if (BI.isKey(name)) {
name = name + "";
}
name = name || widget.getName() || BI.uniqueId("widget");
if (this._children[name]) {
throw new Error("name has already been existed");
}
widget._setParent && widget._setParent(this);
widget.on(BI.Events.DESTROY, function () {
BI.remove(self._children, this);
});
return (this._children[name] = widget);
},
getWidgetByName: function (name) {
if (!BI.isKey(name) || name === this.getName()) {
return this;
}
name = name + "";
var widget = void 0, other = {};
BI.any(this._children, function (i, wi) {
if (i === name) {
widget = wi;
return true;
} }
other[i] = wi; // 递归将所有子组件使有效
}); BI.each(this._children, function (i, child) {
if (!widget) { !child._manualSetValid && child._setValid && child._setValid(valid);
BI.any(other, function (i, wi) {
return (widget = wi.getWidgetByName(i));
}); });
} },
return widget;
}, _setVisible: function (visible) {
if (visible === true) {
removeWidget: function (nameOrWidget) { this.options.invisible = false;
var self = this; } else if (visible === false) {
if (BI.isWidget(nameOrWidget)) { this.options.invisible = true;
BI.remove(this._children, nameOrWidget); }
} else { },
delete this._children[nameOrWidget];
} setEnable: function (enable) {
}, this._manualSetEnable = true;
this._setEnable(enable);
if (enable === true) {
this.element.removeClass("base-disabled disabled");
} else if (enable === false) {
this.element.addClass("base-disabled disabled");
}
},
setVisible: function (visible) {
this._setVisible(visible);
if (visible === true) {
// 用this.element.show()会把display属性改成block
this.element.css("display", "");
this._mount();
} else if (visible === false) {
this.element.css("display", "none");
}
this.fireEvent(BI.Events.VIEW, visible);
},
setValid: function (valid) {
this._manualSetValid = true;
this._setValid(valid);
if (valid === true) {
this.element.removeClass("base-invalid invalid");
} else if (valid === false) {
this.element.addClass("base-invalid invalid");
}
},
hasWidget: function (name) { doBehavior: function () {
return this._children[name] != null; var args = arguments;
}, // 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
child.doBehavior && child.doBehavior.apply(child, args);
});
},
getName: function () { getWidth: function () {
return this.widgetName; return this.options.width;
}, },
setTag: function (tag) { getHeight: function () {
this.options.tag = tag; return this.options.height;
}, },
getTag: function () { isValid: function () {
return this.options.tag; return !this.options.invalid;
}, },
attr: function (key, value) { addWidget: function (name, widget) {
var self = this; var self = this;
if (BI.isPlainObject(key)) { if (name instanceof BI.Widget) {
BI.each(key, function (k, v) { widget = name;
self.attr(k, v); name = widget.getName();
}
if (BI.isKey(name)) {
name = name + "";
}
name = name || widget.getName() || BI.uniqueId("widget");
if (this._children[name]) {
throw new Error("name has already been existed");
}
widget._setParent && widget._setParent(this);
widget.on(BI.Events.DESTROY, function () {
BI.remove(self._children, this);
}); });
return; return (this._children[name] = widget);
} },
if (BI.isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
},
getText: function () { getWidgetByName: function (name) {
if (!BI.isKey(name) || name === this.getName()) {
return this;
}
name = name + "";
var widget = void 0, other = {};
BI.any(this._children, function (i, wi) {
if (i === name) {
widget = wi;
return true;
}
other[i] = wi;
});
if (!widget) {
BI.any(other, function (i, wi) {
return (widget = wi.getWidgetByName(i));
});
}
return widget;
},
}, removeWidget: function (nameOrWidget) {
var self = this;
if (BI.isWidget(nameOrWidget)) {
BI.remove(this._children, nameOrWidget);
} else {
delete this._children[nameOrWidget];
}
},
setText: function (text) { hasWidget: function (name) {
return this._children[name] != null;
},
}, getName: function () {
return this.widgetName;
},
getValue: function () { setTag: function (tag) {
this.options.tag = tag;
},
}, getTag: function () {
return this.options.tag;
},
setValue: function (value) { attr: function (key, value) {
var self = this;
if (BI.isPlainObject(key)) {
BI.each(key, function (k, v) {
self.attr(k, v);
});
return;
}
if (BI.isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
},
getText: function () {
},
setText: function (text) {
},
}, getValue: function () {
isEnabled: function () { },
return !this.options.disabled;
},
isVisible: function () { setValue: function (value) {
return !this.options.invisible;
},
disable: function () { },
this.setEnable(false);
},
enable: function () { isEnabled: function () {
this.setEnable(true); return !this.options.disabled;
}, },
valid: function () { isVisible: function () {
this.setValid(true); return !this.options.invisible;
}, },
invalid: function () { disable: function () {
this.setValid(false); this.setEnable(false);
}, },
invisible: function () { enable: function () {
this.setVisible(false); this.setEnable(true);
}, },
visible: function () { valid: function () {
this.setVisible(true); this.setValid(true);
}, },
__d: function () { invalid: function () {
this.beforeDestroy && this.beforeDestroy(); this.setValid(false);
BI.each(this._children, function (i, widget) { },
widget && widget._unMount && widget._unMount();
});
this._children = {};
this._parent = null;
this._isMounted = false;
this.destroyed && this.destroyed();
},
_unMount: function () { invisible: function () {
this.__d(); this.setVisible(false);
this.fireEvent(BI.Events.UNMOUNT); },
this.purgeListeners();
},
isolate: function () { visible: function () {
if (this._parent) { this.setVisible(true);
this._parent.removeWidget(this); },
__d: function () {
this.beforeDestroy && this.beforeDestroy();
BI.each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
this._parent = null;
this._isMounted = false;
this.destroyed && this.destroyed();
},
_unMount: function () {
this.__d();
this.fireEvent(BI.Events.UNMOUNT);
this.purgeListeners();
},
isolate: function () {
if (this._parent) {
this._parent.removeWidget(this);
}
BI.DOM.hang([this]);
},
empty: function () {
BI.each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
this.element.empty();
},
_destroy: function () {
this.__d();
this.element.destroy();
this.purgeListeners();
},
destroy: function () {
this.__d();
this.element.destroy();
this.fireEvent(BI.Events.DESTROY);
this._purgeRef();
this.purgeListeners();
} }
BI.DOM.hang([this]); });
}, })();
empty: function () {
BI.each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
this.element.empty();
},
_destroy: function () {
this.__d();
this.element.destroy();
this.purgeListeners();
},
destroy: function () {
this.__d();
this.element.destroy();
this.fireEvent(BI.Events.DESTROY);
this._purgeRef();
this.purgeListeners();
}
});
Loading…
Cancel
Save