ed3113dbf53362650c026a17dc168e1b2fc2f88a.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, director, log, JsonOb, ViewModel, VMManager, _crd, VM_EMIT_HEAD, DEBUG_SHOW_PATH, VM;
  4. /** 通过 . 路径设置值 */
  5. function setValueFromPath(obj, path, value, tag = '') {
  6. let props = path.split('.');
  7. for (let i = 0; i < props.length; i++) {
  8. const propName = props[i];
  9. if (propName in obj === false) {
  10. console.error('[' + propName + '] not find in ' + tag + '.' + path);
  11. break;
  12. }
  13. if (i == props.length - 1) {
  14. obj[propName] = value;
  15. } else {
  16. obj = obj[propName];
  17. }
  18. }
  19. }
  20. /** 通过 . 路径 获取值 */
  21. function getValueFromPath(obj, path, def, tag = '') {
  22. let props = path.split('.');
  23. for (let i = 0; i < props.length; i++) {
  24. const propName = props[i];
  25. if (propName in obj === false) {
  26. console.error('[' + propName + '] not find in ' + tag + '.' + path);
  27. return def;
  28. }
  29. obj = obj[propName];
  30. }
  31. if (obj === null || typeof obj === "undefined") obj = def; //如果g == null 则返回一个默认值
  32. return obj;
  33. }
  34. /**
  35. * ModelViewer 类
  36. */
  37. function _reportPossibleCrUseOfJsonOb(extras) {
  38. _reporterNs.report("JsonOb", "./JsonOb", _context.meta, extras);
  39. }
  40. return {
  41. setters: [function (_unresolved_) {
  42. _reporterNs = _unresolved_;
  43. }, function (_cc) {
  44. _cclegacy = _cc.cclegacy;
  45. __checkObsolete__ = _cc.__checkObsolete__;
  46. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  47. director = _cc.director;
  48. log = _cc.log;
  49. }, function (_unresolved_2) {
  50. JsonOb = _unresolved_2.JsonOb;
  51. }],
  52. execute: function () {
  53. _crd = true;
  54. _cclegacy._RF.push({}, "54f75k4X+RP0qaXOzrfZysL", "ViewModel", undefined);
  55. __checkObsolete__(['director', 'log']);
  56. VM_EMIT_HEAD = 'VC:';
  57. DEBUG_SHOW_PATH = false;
  58. ViewModel = class ViewModel {
  59. constructor(data, tag) {
  60. this.$data = void 0;
  61. // 索引值用的标签
  62. this._tag = null;
  63. /** 激活状态, 将会通过 director.emit 发送值变动的信号, 适合需要屏蔽的情况 */
  64. this.active = true;
  65. /** 是否激活根路径回调通知, 不激活的情况下 只能监听末端路径值来判断是否变化 */
  66. this.emitToRootPath = false;
  67. new (_crd && JsonOb === void 0 ? (_reportPossibleCrUseOfJsonOb({
  68. error: Error()
  69. }), JsonOb) : JsonOb)(data, this._callback.bind(this));
  70. this.$data = data;
  71. this._tag = tag;
  72. }
  73. // 回调函数 请注意 回调的 path 数组是 引用类型,禁止修改
  74. _callback(n, o, path) {
  75. if (this.active == true) {
  76. let name = VM_EMIT_HEAD + this._tag + '.' + path.join('.');
  77. if (DEBUG_SHOW_PATH) log('>>', n, o, path);
  78. director.emit(name, n, o, [this._tag].concat(path)); // 通知末端路径
  79. if (this.emitToRootPath) director.emit(VM_EMIT_HEAD + this._tag, n, o, path); // 通知主路径
  80. if (path.length >= 2) {
  81. for (let i = 0; i < path.length - 1; i++) {
  82. const e = path[i]; //log('中端路径');
  83. }
  84. }
  85. }
  86. } // 通过路径设置数据的方法
  87. setValue(path, value) {
  88. setValueFromPath(this.$data, path, value, this._tag);
  89. } // 获取路径的值
  90. getValue(path, def) {
  91. return getValueFromPath(this.$data, path, def, this._tag);
  92. }
  93. };
  94. /**
  95. * VM 对象管理器(工厂)
  96. */
  97. VMManager = class VMManager {
  98. constructor() {
  99. this._mvs = new Map();
  100. this.setObjValue = setValueFromPath;
  101. this.getObjValue = getValueFromPath;
  102. }
  103. /**
  104. * 绑定一个数据,并且可以由VM所管理(绑定的数据只能是值类型)
  105. * @param data 需要绑定的数据
  106. * @param tag 对应该数据的标签(用于识别为哪个VM,不允许重复)
  107. * @param activeRootObject 激活主路径通知,可能会有性能影响,一般不使用
  108. */
  109. add(data, tag = 'global', activeRootObject = false) {
  110. let vm = new ViewModel(data, tag);
  111. let has = this._mvs.get(tag);
  112. if (tag.includes('.')) {
  113. console.error('cant write . in tag:', tag);
  114. return;
  115. }
  116. if (has) {
  117. console.error('already set VM tag:' + tag);
  118. return;
  119. }
  120. vm.emitToRootPath = activeRootObject;
  121. this._mvs.set(tag, vm);
  122. }
  123. /**
  124. * 移除并且销毁 VM 对象
  125. * @param tag
  126. */
  127. remove(tag) {
  128. this._mvs.delete(tag);
  129. }
  130. /**
  131. * 获取绑定的数据
  132. * @param tag 数据tag
  133. */
  134. get(tag) {
  135. let res = this._mvs.get(tag);
  136. return res;
  137. }
  138. /**
  139. * 通过全局路径,而不是 VM 对象来 设置值
  140. * @param path - 全局取值路径
  141. * @param value - 需要增加的值
  142. */
  143. addValue(path, value) {
  144. path = path.trim(); //防止空格,自动剔除
  145. let rs = path.split('.');
  146. if (rs.length < 2) {
  147. console.error('Cant find path:' + path);
  148. }
  149. ;
  150. let vm = this.get(rs[0]);
  151. if (!vm) {
  152. console.error('Cant Set VM:' + rs[0]);
  153. return;
  154. }
  155. ;
  156. let resPath = rs.slice(1).join('.');
  157. vm.setValue(resPath, vm.getValue(resPath) + value);
  158. }
  159. /**
  160. * 通过全局路径,而不是 VM 对象来 获取值
  161. * @param path - 全局取值路径
  162. * @param def - 如果取不到值的返回的默认值
  163. */
  164. getValue(path, def) {
  165. path = path.trim(); // 防止空格,自动剔除
  166. let rs = path.split('.');
  167. if (rs.length < 2) {
  168. console.error('Get Value Cant find path:' + path);
  169. return;
  170. }
  171. ;
  172. let vm = this.get(rs[0]);
  173. if (!vm) {
  174. console.error('Cant Get VM:' + rs[0]);
  175. return;
  176. }
  177. ;
  178. return vm.getValue(rs.slice(1).join('.'), def);
  179. }
  180. /**
  181. * 通过全局路径,而不是 VM 对象来 设置值
  182. * @param path - 全局取值路径
  183. * @param value - 需要设置的值
  184. */
  185. setValue(path, value) {
  186. path = path.trim(); // 防止空格,自动剔除
  187. let rs = path.split('.');
  188. if (rs.length < 2) {
  189. console.error('Set Value Cant find path:' + path);
  190. return;
  191. }
  192. ;
  193. let vm = this.get(rs[0]);
  194. if (!vm) {
  195. console.error('Cant Set VM:' + rs[0]);
  196. return;
  197. }
  198. ;
  199. vm.setValue(rs.slice(1).join('.'), value);
  200. }
  201. /** 等同于 director.on */
  202. bindPath(path, callback, target, useCapture) {
  203. path = path.trim(); // 防止空格,自动剔除
  204. if (path == '') {
  205. console.error(target.node.name, '节点绑定的路径为空');
  206. return;
  207. }
  208. if (path.split('.')[0] === '*') {
  209. console.error(path, '路径不合法,可能错误覆盖了 VMParent 的onLoad 方法, 或者父节点并未挂载 VMParent 相关的组件脚本');
  210. return;
  211. } // @ts-ignore
  212. director.on(VM_EMIT_HEAD + path, callback, target, useCapture);
  213. }
  214. /** 等同于 director.off */
  215. unbindPath(path, callback, target) {
  216. path = path.trim(); //防止空格,自动剔除
  217. if (path.split('.')[0] === '*') {
  218. console.error(path, '路径不合法,可能错误覆盖了 VMParent 的onLoad 方法, 或者父节点并未挂载 VMParent 相关的组件脚本');
  219. return;
  220. } // @ts-ignore
  221. director.off(VM_EMIT_HEAD + path, callback, target);
  222. }
  223. /** 冻结所有标签的 VM,视图将不会受到任何信息 */
  224. inactive() {
  225. this._mvs.forEach(mv => {
  226. mv.active = false;
  227. });
  228. }
  229. /** 激活所有标签的 VM*/
  230. active() {
  231. this._mvs.forEach(mv => {
  232. mv.active = false;
  233. });
  234. }
  235. }; // 整数、小数、时间、缩写
  236. /**
  237. * VM管理对象,使用文档:
  238. * https://github.com/wsssheep/cocos_creator_mvvm_tools/blob/master/docs/ViewModelScript.md
  239. */
  240. _export("VM", VM = new VMManager());
  241. _cclegacy._RF.pop();
  242. _crd = false;
  243. }
  244. };
  245. });
  246. //# sourceMappingURL=ed3113dbf53362650c026a17dc168e1b2fc2f88a.js.map