|
|
@ -1,12 +1,13 @@ |
|
|
|
!(function () { |
|
|
|
import BI from "./2.base"; |
|
|
|
|
|
|
|
|
|
|
|
function extend() { |
|
|
|
function extend() { |
|
|
|
var target = arguments[0] || {}, length = arguments.length, i = 1, options, name, src, copy; |
|
|
|
let target = arguments[0] || {}, length = arguments.length, i = 1, name, copy; |
|
|
|
for (; i < length; i++) { |
|
|
|
for (; i < length; i++) { |
|
|
|
// Only deal with non-null/undefined values
|
|
|
|
// Only deal with non-null/undefined values
|
|
|
|
if ((options = arguments[i]) != null) { |
|
|
|
const options = arguments[i]; |
|
|
|
|
|
|
|
if (options !== null) { |
|
|
|
// Extend the base object
|
|
|
|
// Extend the base object
|
|
|
|
for (name in options) { |
|
|
|
for (name in options) { |
|
|
|
src = target[name]; |
|
|
|
|
|
|
|
copy = options[name]; |
|
|
|
copy = options[name]; |
|
|
|
|
|
|
|
|
|
|
|
// Prevent never-ending loop
|
|
|
|
// Prevent never-ending loop
|
|
|
@ -14,91 +15,90 @@ |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (copy !== undefined) { |
|
|
|
if (copy !== undefined) { |
|
|
|
target[name] = copy; |
|
|
|
target[name] = copy; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return target; |
|
|
|
return target; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
export default class OB { |
|
|
|
* 客户端观察者,主要处理事件的添加、删除、执行等 |
|
|
|
// props = {};
|
|
|
|
* @class BI.OB |
|
|
|
|
|
|
|
* @abstract |
|
|
|
// init = null;
|
|
|
|
*/ |
|
|
|
|
|
|
|
var OB = function (config) { |
|
|
|
|
|
|
|
this._constructor(config); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
BI._.extend(OB.prototype, { |
|
|
|
|
|
|
|
props: {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
init: null, |
|
|
|
// destroyed = null;
|
|
|
|
|
|
|
|
|
|
|
|
destroyed: null, |
|
|
|
constructor(config) { |
|
|
|
|
|
|
|
this._constructor(config); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_constructor: function (config) { |
|
|
|
_constructor(config) { |
|
|
|
this._initProps(config); |
|
|
|
this._initProps(config); |
|
|
|
this._init(); |
|
|
|
this._init(); |
|
|
|
this._initRef(); |
|
|
|
this._initRef(); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_defaultConfig: function (config) { |
|
|
|
_defaultConfig(config) { |
|
|
|
return {}; |
|
|
|
return {}; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_initProps: function (config) { |
|
|
|
_initProps(config) { |
|
|
|
var props = this.props; |
|
|
|
let props = this.props; |
|
|
|
if (BI.isFunction(this.props)) { |
|
|
|
if (BI.isFunction(this.props)) { |
|
|
|
props = this.props(config); |
|
|
|
props = this.props(config); |
|
|
|
} |
|
|
|
} |
|
|
|
var defaultProps = extend(this._defaultConfig(config), props); |
|
|
|
const defaultProps = extend(this._defaultConfig(config), props); |
|
|
|
var modifiedDefaultProps = (config && config.type && BI.OB.configFunctions[config.type + ".props"]) ? BI.reduce(BI.OB.configFunctions[config.type + ".props"], function (value, conf, index) { |
|
|
|
const modifiedDefaultProps = (config && config.type && OB.configFunctions[config.type + ".props"]) ? BI.reduce(OB.configFunctions[config.type + ".props"], function (value, conf, index) { |
|
|
|
return extend(conf, value.fn(defaultProps, config, value.opt)); |
|
|
|
return extend(conf, value.fn(defaultProps, config, value.opt)); |
|
|
|
}, {}) : null; |
|
|
|
}, {}) : null; |
|
|
|
this.options = extend(defaultProps, modifiedDefaultProps, config); |
|
|
|
this.options = extend(defaultProps, modifiedDefaultProps, config); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_init: function () { |
|
|
|
_init() { |
|
|
|
this._initListeners(); |
|
|
|
this._initListeners(); |
|
|
|
this.init && this.init(); |
|
|
|
this.init && this.init(); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_initListeners: function () { |
|
|
|
_initListeners() { |
|
|
|
var self = this; |
|
|
|
if (this.options.listeners !== null) { |
|
|
|
if (this.options.listeners != null) { |
|
|
|
BI._.each(this.options.listeners, (lis, eventName) => { |
|
|
|
BI._.each(this.options.listeners, function (lis, eventName) { |
|
|
|
|
|
|
|
if (BI._.isFunction(lis)) { |
|
|
|
if (BI._.isFunction(lis)) { |
|
|
|
self.on(eventName, lis); |
|
|
|
this.on(eventName, lis); |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (BI._.isArray(lis)) { |
|
|
|
if (BI._.isArray(lis)) { |
|
|
|
BI._.each(lis, function (l) { |
|
|
|
BI._.each(lis, (l) => { |
|
|
|
self.on(eventName, l); |
|
|
|
this.on(eventName, l); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
(lis.target ? lis.target : self)[lis.once ? "once" : "on"](lis.eventName, BI._.bind(lis.action, self)); |
|
|
|
(lis.target ? lis.target : this)[lis.once ? "once" : "on"](lis.eventName, BI._.bind(lis.action, this)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
delete this.options.listeners; |
|
|
|
delete this.options.listeners; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获得一个当前对象的引用
|
|
|
|
// 获得一个当前对象的引用
|
|
|
|
_initRef: function () { |
|
|
|
_initRef() { |
|
|
|
var o = this.options; |
|
|
|
const o = this.options; |
|
|
|
if (o.__ref) { |
|
|
|
if (o.__ref) { |
|
|
|
BI.isFunction(o.__ref) ? o.__ref.call(this, this) : o.__ref.current = this; |
|
|
|
BI.isFunction(o.__ref) ? o.__ref.call(this, this) : o.__ref.current = this; |
|
|
|
} |
|
|
|
} |
|
|
|
if (o.ref) { |
|
|
|
if (o.ref) { |
|
|
|
BI.isFunction(o.ref) ? o.ref.call(this, this) : o.ref.current = this; |
|
|
|
BI.isFunction(o.ref) ? o.ref.call(this, this) : o.ref.current = this; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 释放当前对象
|
|
|
|
// 释放当前对象
|
|
|
|
_purgeRef: function () { |
|
|
|
_purgeRef() { |
|
|
|
var o = this.options; |
|
|
|
const o = this.options; |
|
|
|
if (o.__ref) { |
|
|
|
if (o.__ref) { |
|
|
|
BI.isFunction(o.__ref) ? o.__ref.call(null, null) : o.__ref.current = null; |
|
|
|
BI.isFunction(o.__ref) ? o.__ref.call(null, null) : o.__ref.current = null; |
|
|
|
o.__ref = null; |
|
|
|
o.__ref = null; |
|
|
@ -107,80 +107,78 @@ |
|
|
|
BI.isFunction(o.ref) ? o.ref.call(null, null) : o.ref.current = null; |
|
|
|
BI.isFunction(o.ref) ? o.ref.call(null, null) : o.ref.current = null; |
|
|
|
o.ref = null; |
|
|
|
o.ref = null; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_getEvents: function () { |
|
|
|
_getEvents() { |
|
|
|
if (!BI._.isObject(this.events)) { |
|
|
|
if (!BI._.isObject(this.events)) { |
|
|
|
this.events = {}; |
|
|
|
this.events = {}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.events; |
|
|
|
return this.events; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 给观察者绑定一个事件 |
|
|
|
* 给观察者绑定一个事件 |
|
|
|
* @param {String} eventName 事件的名字 |
|
|
|
* @param {String} eventName 事件的名字 |
|
|
|
* @param {Function} fn 事件对应的执行函数 |
|
|
|
* @param {Function} fn 事件对应的执行函数 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
on: function (eventName, fn) { |
|
|
|
on(eventName, fn) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
eventName = eventName.toLowerCase(); |
|
|
|
eventName = eventName.toLowerCase(); |
|
|
|
var fns = this._getEvents()[eventName]; |
|
|
|
let fns = this._getEvents()[eventName]; |
|
|
|
if (!BI._.isArray(fns)) { |
|
|
|
if (!BI._.isArray(fns)) { |
|
|
|
fns = []; |
|
|
|
fns = []; |
|
|
|
this._getEvents()[eventName] = fns; |
|
|
|
this._getEvents()[eventName] = fns; |
|
|
|
} |
|
|
|
} |
|
|
|
fns.push(fn); |
|
|
|
fns.push(fn); |
|
|
|
|
|
|
|
|
|
|
|
return function () { |
|
|
|
return () => this.un(eventName, fn); |
|
|
|
self.un(eventName, fn); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 给观察者绑定一个只执行一次的事件 |
|
|
|
* 给观察者绑定一个只执行一次的事件 |
|
|
|
* @param {String} eventName 事件的名字 |
|
|
|
* @param {String} eventName 事件的名字 |
|
|
|
* @param {Function} fn 事件对应的执行函数 |
|
|
|
* @param {Function} fn 事件对应的执行函数 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
once: function (eventName, fn) { |
|
|
|
once(eventName, fn) { |
|
|
|
var proxy = function () { |
|
|
|
const proxy = () => { |
|
|
|
fn.apply(this, arguments); |
|
|
|
fn.apply(this, arguments); |
|
|
|
this.un(eventName, proxy); |
|
|
|
this.un(eventName, proxy); |
|
|
|
}; |
|
|
|
}; |
|
|
|
this.on(eventName, proxy); |
|
|
|
this.on(eventName, proxy); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 解除观察者绑定的指定事件 |
|
|
|
* 解除观察者绑定的指定事件 |
|
|
|
* @param {String} eventName 要解除绑定事件的名字 |
|
|
|
* @param {String} eventName 要解除绑定事件的名字 |
|
|
|
* @param {Function} fn 事件对应的执行函数,该参数是可选的,没有该参数时,将解除绑定所有同名字的事件 |
|
|
|
* @param {Function} fn 事件对应的执行函数,该参数是可选的,没有该参数时,将解除绑定所有同名字的事件 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
un: function (eventName, fn) { |
|
|
|
un(eventName, fn) { |
|
|
|
eventName = eventName.toLowerCase(); |
|
|
|
eventName = eventName.toLowerCase(); |
|
|
|
|
|
|
|
|
|
|
|
/* alex:如果fn是null,就是把eventName上面所有方法都un掉*/ |
|
|
|
/* alex:如果fn是null,就是把eventName上面所有方法都un掉*/ |
|
|
|
if (fn == null) { |
|
|
|
if (fn === null) { |
|
|
|
delete this._getEvents()[eventName]; |
|
|
|
delete this._getEvents()[eventName]; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
var fns = this._getEvents()[eventName]; |
|
|
|
const fns = this._getEvents()[eventName]; |
|
|
|
if (BI._.isArray(fns)) { |
|
|
|
if (BI._.isArray(fns)) { |
|
|
|
var newFns = []; |
|
|
|
const newFns = []; |
|
|
|
BI._.each(fns, function (ifn) { |
|
|
|
BI._.each(fns, function (ifn) { |
|
|
|
if (ifn != fn) { |
|
|
|
if (ifn !== fn) { |
|
|
|
newFns.push(ifn); |
|
|
|
newFns.push(ifn); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
this._getEvents()[eventName] = newFns; |
|
|
|
this._getEvents()[eventName] = newFns; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 清除观察者的所有事件绑定 |
|
|
|
* 清除观察者的所有事件绑定 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
purgeListeners: function () { |
|
|
|
purgeListeners() { |
|
|
|
/* alex:清空events*/ |
|
|
|
/* alex:清空events*/ |
|
|
|
this.events = {}; |
|
|
|
this.events = {}; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 触发绑定过的事件 |
|
|
|
* 触发绑定过的事件 |
|
|
@ -188,33 +186,36 @@ |
|
|
|
* @param {String} eventName 要触发的事件的名字 |
|
|
|
* @param {String} eventName 要触发的事件的名字 |
|
|
|
* @returns {Boolean} 如果事件函数返回false,则返回false并中断其他同名事件的执行,否则执行所有的同名事件并返回true |
|
|
|
* @returns {Boolean} 如果事件函数返回false,则返回false并中断其他同名事件的执行,否则执行所有的同名事件并返回true |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fireEvent: function () { |
|
|
|
fireEvent() { |
|
|
|
var eventName = arguments[0].toLowerCase(); |
|
|
|
const eventName = arguments[0].toLowerCase(); |
|
|
|
var fns = this._getEvents()[eventName]; |
|
|
|
const fns = this._getEvents()[eventName]; |
|
|
|
if (BI.isArray(fns)) { |
|
|
|
if (BI.isArray(fns)) { |
|
|
|
if (BI.isArguments(arguments[1])) { |
|
|
|
if (BI.isArguments(arguments[1])) { |
|
|
|
for (var i = 0; i < fns.length; i++) { |
|
|
|
for (let i = 0; i < fns.length; i++) { |
|
|
|
if (fns[i].apply(this, arguments[1]) === false) { |
|
|
|
if (fns[i].apply(this, arguments[1]) === false) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
var args = Array.prototype.slice.call(arguments, 1); |
|
|
|
const args = Array.prototype.slice.call(arguments, 1); |
|
|
|
for (var i = 0; i < fns.length; i++) { |
|
|
|
for (let i = 0; i < fns.length; i++) { |
|
|
|
if (fns[i].apply(this, args) === false) { |
|
|
|
if (fns[i].apply(this, args) === false) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
destroy: function () { |
|
|
|
destroy() { |
|
|
|
this.destroyed && this.destroyed(); |
|
|
|
this.destroyed && this.destroyed(); |
|
|
|
this._purgeRef(); |
|
|
|
this._purgeRef(); |
|
|
|
this.purgeListeners(); |
|
|
|
this.purgeListeners(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
BI.OB = BI.OB || OB; |
|
|
|
|
|
|
|
})(); |
|
|
|
// BI.OB = BI.OB || OB;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BI.extend(BI, { OB }); |
|
|
|