325 lines
8.3 KiB
JavaScript
325 lines
8.3 KiB
JavaScript
|
|
"use strict";
|
||
|
|
cc._RF.push(module, 'f695beSj/JErabzvPcuZ0au', 'nx.view.mgr');
|
||
|
|
// Scripts/nx/view/nx.view.mgr.js
|
||
|
|
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
*
|
||
|
|
* Nx视图管理器
|
||
|
|
*
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* 2021.12.10
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
var NxObject = require("nx.object");
|
||
|
|
var nxViewManager = cc.Class({
|
||
|
|
"extends": NxObject,
|
||
|
|
name: "nxViewManager",
|
||
|
|
// 初始化
|
||
|
|
initialize: function initialize(_args) {
|
||
|
|
// USPER
|
||
|
|
if (!this._super(_args)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
this.models = {}; // 模型注册表
|
||
|
|
this.dicViews = {}; // 全视图字典
|
||
|
|
this.vRefCounter = 1000; // 多实例视图计数器
|
||
|
|
this.bindTargets = {}; // VBD表
|
||
|
|
|
||
|
|
// 全局服务开启
|
||
|
|
nx.views = this;
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
// 销毁
|
||
|
|
uninitialize: function uninitialize() {
|
||
|
|
nx.logger.debug("destroy all views: ");
|
||
|
|
for (var vid in this.dicViews) {
|
||
|
|
this.delView(vid);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 全局服务关闭
|
||
|
|
nx.views = null;
|
||
|
|
|
||
|
|
// VBD清理
|
||
|
|
this.cleanBindTargets();
|
||
|
|
|
||
|
|
// USPER
|
||
|
|
return this._super();
|
||
|
|
},
|
||
|
|
// 模型注册
|
||
|
|
regModel: function regModel(_key, _model, _single) {
|
||
|
|
var mod = this.queryModel(_key, false);
|
||
|
|
if (mod) {
|
||
|
|
nx.error("[视图]重复注册失败:%s!", _key);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 注册
|
||
|
|
this.models[_key] = {
|
||
|
|
name: _key,
|
||
|
|
model: nx.dt.objClone(_model),
|
||
|
|
single: _single ? true : false
|
||
|
|
};
|
||
|
|
nx.debug("[视图]注册成功:%s!", _key);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
// 模型注销
|
||
|
|
unregModel: function unregModel(_key) {
|
||
|
|
var mod = this.models[_key];
|
||
|
|
if (!mod) {
|
||
|
|
nx.error("[视图]注销失败,不存在注册信息:%s!", _key);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
delete this.models[_key];
|
||
|
|
nx.debug("[视图]注销成功:%s!", _key);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
// 查询
|
||
|
|
queryModel: function queryModel(_key, _warn) {
|
||
|
|
if (_warn === void 0) {
|
||
|
|
_warn = true;
|
||
|
|
}
|
||
|
|
var mod = this.models[_key];
|
||
|
|
if (mod) {
|
||
|
|
return mod;
|
||
|
|
}
|
||
|
|
if (_warn) {
|
||
|
|
nx.warn("[视图]查询模型失败:%s!", _key);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
},
|
||
|
|
// 获取合并模板结果
|
||
|
|
combineModel: function combineModel(_modName, _modEx) {
|
||
|
|
// 是否有效
|
||
|
|
var model = this.queryModel(_modName);
|
||
|
|
if (!model) {
|
||
|
|
return nx.dt.objClone(_modEx);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 追加
|
||
|
|
var ret = nx.dt.objClone(model);
|
||
|
|
if (nx.dt.arrGood(_modEx)) {
|
||
|
|
ret = ret.concat(_modEx);
|
||
|
|
}
|
||
|
|
return ret;
|
||
|
|
},
|
||
|
|
// 获取视图
|
||
|
|
queryView: function queryView(_vid, _warn) {
|
||
|
|
var v = this.dicViews[_vid];
|
||
|
|
if (!v && _warn != false) {
|
||
|
|
nx.warn("[视图]找到视图失败,无效的视图编号:", _vid);
|
||
|
|
}
|
||
|
|
return v;
|
||
|
|
},
|
||
|
|
// 创建视图
|
||
|
|
buildView: function buildView(_modName, _vmodel) {
|
||
|
|
// 如果_vmodel为空,则直接用_modName注册的模型进行实例化
|
||
|
|
// 否则用_vmodel作为模型直接创建
|
||
|
|
var model = _vmodel ? {
|
||
|
|
model: nx.dt.objClone(_vmodel)
|
||
|
|
} : this.queryModel(_modName);
|
||
|
|
if (!model || !model.model) {
|
||
|
|
nx.warn("[视图]模型不合法,创建失败:%s!", _modName);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建新视图
|
||
|
|
var v = nx.factory.create("nx.view");
|
||
|
|
if (!v) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 视图编号: 单例视图直接使用modName即可
|
||
|
|
if (this.isSingleView(_modName)) {
|
||
|
|
v.vid = _modName;
|
||
|
|
} else {
|
||
|
|
v.vid = "V" + ++this.vRefCounter;
|
||
|
|
}
|
||
|
|
this.dicViews[v.vid] = v;
|
||
|
|
v.reset(model.model);
|
||
|
|
|
||
|
|
// nx.debug( "[视图]创建成功:", _modName, v.vid );
|
||
|
|
|
||
|
|
return v.vid;
|
||
|
|
},
|
||
|
|
// 视图重置
|
||
|
|
resetView: function resetView(_vid, _vmodel, _clean) {
|
||
|
|
var v = this.queryView(_vid);
|
||
|
|
if (v) {
|
||
|
|
if (_clean) {
|
||
|
|
v.vclear();
|
||
|
|
}
|
||
|
|
return v.reset(_vmodel);
|
||
|
|
}
|
||
|
|
nx.error("[视图]重置视图失败,不存在!", _vid);
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
// 视图删除
|
||
|
|
delView: function delView(_vid) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]删除失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
nx.factory.remove(v);
|
||
|
|
delete this.dicViews[_vid];
|
||
|
|
nx.debug("[视图]删除成功:", _vid);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
// 监听绑定
|
||
|
|
bindView: function bindView(_vid, _bindername, _lst) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.error("[视图]监听失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return v.vbinds(_bindername, _lst);
|
||
|
|
},
|
||
|
|
// 监听解除
|
||
|
|
unbindView: function unbindView(_vid, _bindername) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]监听解除失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return v.vunbind(_bindername);
|
||
|
|
},
|
||
|
|
// 直接获取视图数据
|
||
|
|
getData: function getData(_vid, _vk, _def) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]获取属性失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return v.vget(_vk, _def);
|
||
|
|
},
|
||
|
|
// 直接获取视图数据
|
||
|
|
getOldData: function getOldData(_vid, _vk, _def) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]获取属性失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return v.vgetOld(_vk, _def);
|
||
|
|
},
|
||
|
|
// 直接设置视图数据
|
||
|
|
setData: function setData(_vid, _vk, _value, _notify) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]设置属性失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// console.log( "[视图]设置: %s %s", _vid, _vk );
|
||
|
|
return v.vset(_vk, _value, _notify);
|
||
|
|
},
|
||
|
|
// 直接批量设置视图数据
|
||
|
|
setDataBatch: function setDataBatch(_vid, _vpairs) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]批量设置失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
for (var k in _vpairs) {
|
||
|
|
var p = _vpairs[k];
|
||
|
|
v.vset(p[0], p[1], p[2]);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
// 是不是唯一视图
|
||
|
|
isSingleView: function isSingleView(_modName) {
|
||
|
|
var mod = this.queryModel(_modName);
|
||
|
|
if (mod && mod.single) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
// 视图对象化
|
||
|
|
toObject: function toObject(_vid) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]对象化失败,不存在:", _vid);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
var ret = {};
|
||
|
|
for (var key in v.props) {
|
||
|
|
ret[key] = v.props[key].now;
|
||
|
|
}
|
||
|
|
return ret;
|
||
|
|
},
|
||
|
|
// 视图对象化更新
|
||
|
|
fromObject: function fromObject(_vid, _obj) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]对象化更新失败,不存在:", _vid);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
var ret = {};
|
||
|
|
for (var key in _obj) {
|
||
|
|
v.vset(key, _obj[key]);
|
||
|
|
}
|
||
|
|
return ret;
|
||
|
|
},
|
||
|
|
// 打印视图
|
||
|
|
dumpView: function dumpView(_vid, _tform) {
|
||
|
|
var v = this.queryView(_vid, false);
|
||
|
|
if (!v) {
|
||
|
|
nx.warn("[视图]打印失败,不存在:", _vid);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (_tform != false) {
|
||
|
|
if (console && console.table) {
|
||
|
|
console.table(v.props, ["now", "old", "desc"]);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (console && console.log) {
|
||
|
|
console.log(v.props);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
nx.warn("[视图]打印失败,没有控制台!");
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
// ------------------------------------------------------------------------
|
||
|
|
// (自定义)VBD视图可绑定对象相关支持
|
||
|
|
// ------------------------------------------------------------------------
|
||
|
|
|
||
|
|
// 注册
|
||
|
|
regBindTarget: function regBindTarget(_key, _target) {
|
||
|
|
var target = this.bindTargets[_key];
|
||
|
|
if (target) {
|
||
|
|
nx.error("[视图]注册自定义视图对象重复:%s!", _key);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.bindTargets[_key] = _target;
|
||
|
|
nx.debug("[视图]注册自定义视图对象:%s", _key);
|
||
|
|
},
|
||
|
|
// 注销
|
||
|
|
unregBindTarget: function unregBindTarget(_key) {
|
||
|
|
var target = this.bindTargets[_key];
|
||
|
|
if (target) {
|
||
|
|
delete this.bindTargets[_key];
|
||
|
|
nx.debug("[视图]注销自定义视图对象:%s", _key);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 查找
|
||
|
|
queryBindTarget: function queryBindTarget(_key) {
|
||
|
|
var target = this.bindTargets[_key];
|
||
|
|
if (!target) {
|
||
|
|
nx.error("[视图]自定义视图对象查询失败:%s!", _key);
|
||
|
|
}
|
||
|
|
return target;
|
||
|
|
},
|
||
|
|
// 清理
|
||
|
|
cleanBindTargets: function cleanBindTargets() {
|
||
|
|
this.bindTargets = {};
|
||
|
|
nx.debug("[视图]注册自定义视图对象全清理!");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 模块导出
|
||
|
|
module.exports = nxViewManager;
|
||
|
|
|
||
|
|
cc._RF.pop();
|