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.

359 lines
12 KiB

7 years ago
;(function () {
function initWatch (vm, watch) {
7 years ago
vm._watchers || (vm._watchers = []);
for (var key in watch) {
7 years ago
var handler = watch[key];
7 years ago
if (BI.isArray(handler)) {
for (var i = 0; i < handler.length; i++) {
7 years ago
vm._watchers.push(createWatcher(vm, key, handler[i]));
7 years ago
}
} else {
7 years ago
vm._watchers.push(createWatcher(vm, key, handler));
7 years ago
}
}
4 years ago
BI.each(vm.$watchDelayCallbacks, function (i, watchDelayCallback) {
var innerWatch = watchDelayCallback[0];
var innerHandler = watchDelayCallback[1];
if (BI.isKey(innerWatch)) {
var key = innerWatch;
innerWatch = {};
innerWatch[key] = innerHandler;
}
for (var key in innerWatch) {
var handler = innerWatch[key];
if (BI.isArray(handler)) {
for (var i = 0; i < handler.length; i++) {
vm._watchers.push(createWatcher(vm, key, handler[i]));
}
} else {
vm._watchers.push(createWatcher(vm, key, handler));
}
}
});
7 years ago
}
4 years ago
function createWatcher (vm, keyOrFn, cb, options) {
if (BI.isPlainObject(cb)) {
options = cb;
cb = cb.handler;
}
options = options || {};
return Fix.watch(vm.model, keyOrFn, _.bind(cb, vm), BI.extend(options, {
store: vm.store
}));
7 years ago
}
7 years ago
var target = null;
7 years ago
var targetStack = [];
7 years ago
function pushTarget (_target) {
7 years ago
if (target) targetStack.push(target);
7 years ago
Fix.Model.target = target = _target;
7 years ago
}
function popTarget () {
4 years ago
Fix.Model.target = target = targetStack.pop();
7 years ago
}
var oldWatch = Fix.watch;
Fix.watch = function (model, expOrFn, cb, options) {
if (BI.isPlainObject(cb)) {
7 years ago
options = cb;
cb = cb.handler;
}
7 years ago
if (typeof cb === "string") {
cb = model[cb];
}
return oldWatch.call(this, model, expOrFn, function () {
7 years ago
options && options.store && pushTarget(options.store);
try {
var res = cb.apply(this, arguments);
} catch (e) {
console.error(e);
}
7 years ago
options && options.store && popTarget();
return res;
}, options);
7 years ago
};
function findStore (widget) {
7 years ago
if (target != null) {
return target;
}
4 years ago
widget = widget || BI.Widget.context;
7 years ago
var p = widget;
7 years ago
while (p) {
7 years ago
if (p instanceof Fix.Model || p.store || p.__cacheStore) {
7 years ago
break;
}
7 years ago
p = p._parent || (p.options && p.options.element);
7 years ago
}
if (p) {
7 years ago
if (p instanceof Fix.Model) {
return widget.__cacheStore = p;
}
7 years ago
widget.__cacheStore = p.store || p.__cacheStore;
7 years ago
return p.__cacheStore || p.store;
7 years ago
}
}
4 years ago
// var _create = BI.createWidget;
// BI.createWidget = function (item, options, context) {
// var pushed = false;
// if (BI.isWidget(options)) {
// pushContext(options);
// pushed = true;
// } else if (context != null) {
// pushContext(context);
// pushed = true;
// }
// var result = _create.apply(this, arguments);
// // try {
// // var result = _create.apply(this, arguments);
// // } catch (e) {
// // console.error(e);
// // }
// pushed && popContext();
// return result;
// };
7 years ago
_.each(["populate", "addItems", "prependItems"], function (name) {
var old = BI.Loader.prototype[name];
BI.Loader.prototype[name] = function () {
4 years ago
BI.Widget.pushContext(this);
try {
var result = old.apply(this, arguments);
} catch (e) {
console.error(e);
}
4 years ago
BI.Widget.popContext();
return result;
};
});
function createStore () {
7 years ago
var needPop = false;
6 years ago
if (_global.Fix && this._store) {
4 years ago
var store = findStore(this.options.context || this._parent || this.options.element);
7 years ago
if (store) {
pushTarget(store);
needPop = true;
}
7 years ago
this.store = this._store();
7 years ago
this.store && (this.store._widget = this);
7 years ago
needPop && popTarget();
needPop = false;
7 years ago
pushTarget(this.store);
7 years ago
if (this.store instanceof Fix.Model) {
7 years ago
this.model = this.store.model;
} else {
this.model = this.store;
}
7 years ago
needPop = true;
7 years ago
}
return needPop;
}
var _init = BI.Widget.prototype._init;
BI.Widget.prototype._init = function () {
var self = this;
var needPop = createStore.call(this);
try {
_init.apply(this, arguments);
} catch (e) {
console.error(e);
}
7 years ago
needPop && popTarget();
7 years ago
};
7 years ago
var _render = BI.Widget.prototype._render;
7 years ago
BI.Widget.prototype._render = function () {
7 years ago
var needPop = false;
6 years ago
if (_global.Fix && this._store) {
7 years ago
needPop = true;
pushTarget(this.store);
7 years ago
initWatch(this, this.watch);
}
_render.apply(this, arguments);
// try {
// _render.apply(this, arguments);
// } catch (e) {
// console.error(e);
// }
7 years ago
needPop && popTarget();
7 years ago
};
BI.Widget.prototype._initElement = function () {
var self = this;
var render = BI.isFunction(this.options.render) ? this.options.render : this.render;
var els;
4 years ago
if (this.options.updateMode === "auto" && this._store) {
// 自动更新模式
var childComponents = {};
var rendered = false;
this._watchers.push(Fix.watch(this.model, function () {
if (rendered) {
var newEls = render && render.call(this);
BI.each(childComponents, function (i, childComponent) {
var nextProps = BI.get([newEls], childComponent.path);
if (nextProps) {
4 years ago
var shouldUpdate = childComponent.component.shouldUpdate && childComponent.component.shouldUpdate(nextProps);
childComponent.component._update(nextProps, shouldUpdate);
childComponent.props = BI.extend(childComponent.props, nextProps);
}
});
} else {
els = render && render.call(this);
function traverse (parent, path) {
BI.each(parent, function (i, child) {
var childPath = path.concat(i);
if (BI.isArray(child)) {
traverse(child, childPath);
} else if (BI.isPlainObject(child)) {
if (child.type) {
child.__ref = function (_ref) {
if (_ref) {
var comp = childComponents[this.getName()] = {};
comp.component = _ref;
comp.props = child;
comp.path = childPath;
} else {
delete childComponents[this.getName()];
}
};
}
traverse(child, childPath);
}
});
}
traverse([els], []);
rendered = true;
}
}));
} else {
els = render && render.call(this);
}
if (BI.isPlainObject(els)) {
els = [els];
}
if (BI.isArray(els)) {
BI.each(els, function (i, el) {
if (el) {
BI._lazyCreateWidget(el, {
element: self
});
}
});
}
// if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount();
};
7 years ago
var unMount = BI.Widget.prototype.__d;
BI.Widget.prototype.__d = function () {
try {
unMount.apply(this, arguments);
} catch (e) {
console.error(e);
}
this.store && BI.isFunction(this.store.destroy) && this.store.destroy();
7 years ago
BI.each(this._watchers, function (i, unwatches) {
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches];
BI.each(unwatches, function (j, unwatch) {
unwatch();
7 years ago
});
7 years ago
});
this._watchers && (this._watchers = []);
7 years ago
if (this.store) {
this.store._parent && (this.store._parent = null);
7 years ago
this.store._widget && (this.store._widget = null);
7 years ago
this.store = null;
}
7 years ago
delete this.__cacheStore;
7 years ago
};
7 years ago
7 years ago
_.each(["_mount"], function (name) {
var old = BI.Widget.prototype[name];
old && (BI.Widget.prototype[name] = function () {
7 years ago
this.store && pushTarget(this.store);
try {
var res = old.apply(this, arguments);
} catch (e) {
console.error(e);
}
7 years ago
this.store && popTarget();
7 years ago
return res;
});
7 years ago
});
6 years ago
if (BI.isIE9Below && BI.isIE9Below()) {
7 years ago
_.each(["each", "map", "reduce", "reduceRight", "find", "filter", "reject", "every", "all", "some", "any", "max", "min",
"sortBy", "groupBy", "indexBy", "countBy", "partition",
"keys", "allKeys", "values", "pairs", "invert",
"mapObject", "findKey", "pick", "omit", "tap"], function (name) {
var old = BI[name];
BI[name] = function (obj, fn, context) {
7 years ago
return typeof fn === "function" ? old(obj, function (key, value) {
if (!(key in Fix.$$skipArray)) {
return fn.apply(this, arguments);
}
}, context) : old.apply(this, arguments);
7 years ago
};
});
7 years ago
BI.isEmpty = function (ob) {
if (BI.isPlainObject(ob) && ob.__ob__) {
return BI.keys(ob).length === 0;
}
return _.isEmpty(ob);
};
BI.keys = function (ob) {
var keys = _.keys(ob);
var nKeys = [];
for (var i = 0; i < keys.length; i++) {
if (!(keys[i] in Fix.$$skipArray)) {
nKeys.push(keys[i]);
}
}
return nKeys;
};
BI.values = function (ob) {
var keys = BI.keys(obj);
var length = keys.length;
var values = [];
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
};
6 years ago
BI.extend = function () {
var args = Array.prototype.slice.call(arguments);
if (args.length < 1) {
return {};
}
var object = args[0];
var i = 1;
while (i < args.length) {
BI.each(args[i], function (key, v) {
object[key] = v;
});
i++;
}
return object;
};
7 years ago
BI.size = function (ob) {
if (BI.isPlainObject(ob) && ob.__ob__) {
return BI.keys(ob).length;
}
return _.size(ob);
};
BI.isEmptyObject = function (ob) {
return BI.size(ob) === 0;
};
7 years ago
BI.deepClone = function (ob) {
return Fix.toJSON(ob);
};
7 years ago
}
}());