09c0cf46-1b3f-48fc-a57a-a7abe1e10388.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. cc._RF.push(module, '09c0c9GGz9I/KV6p6vh4QOI', 'ListItem');
  3. // common-plugin/Scripts/ListItem.ts
  4. "use strict";
  5. var __extends = (this && this.__extends) || (function () {
  6. var extendStatics = function (d, b) {
  7. extendStatics = Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  9. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  10. return extendStatics(d, b);
  11. };
  12. return function (d, b) {
  13. extendStatics(d, b);
  14. function __() { this.constructor = d; }
  15. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  16. };
  17. })();
  18. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. /******************************************
  26. * @author kL <klk0@qq.com>
  27. * @date 2019/6/6
  28. * @doc 列表Item组件.
  29. * 说明:
  30. * 1、此组件须配合List组件使用。(配套的配套的..)
  31. * @end
  32. ******************************************/
  33. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property, disallowMultiple = _a.disallowMultiple, menu = _a.menu, executionOrder = _a.executionOrder;
  34. var SelectedType;
  35. (function (SelectedType) {
  36. SelectedType[SelectedType["NONE"] = 0] = "NONE";
  37. SelectedType[SelectedType["TOGGLE"] = 1] = "TOGGLE";
  38. SelectedType[SelectedType["SWITCH"] = 2] = "SWITCH";
  39. })(SelectedType || (SelectedType = {}));
  40. var ListItem = /** @class */ (function (_super) {
  41. __extends(ListItem, _super);
  42. function ListItem() {
  43. var _this = _super !== null && _super.apply(this, arguments) || this;
  44. //图标
  45. _this.icon = null;
  46. //标题
  47. _this.title = null;
  48. //选择模式
  49. _this.selectedMode = SelectedType.NONE;
  50. //被选标志
  51. _this.selectedFlag = null;
  52. //被选择的SpriteFrame
  53. _this.selectedSpriteFrame = null;
  54. //未被选择的SpriteFrame
  55. _this._unselectedSpriteFrame = null;
  56. //自适应尺寸
  57. _this.adaptiveSize = false;
  58. //选择
  59. _this._selected = false;
  60. //是否已经注册过事件
  61. _this._eventReg = false;
  62. return _this;
  63. }
  64. Object.defineProperty(ListItem.prototype, "selected", {
  65. get: function () {
  66. return this._selected;
  67. },
  68. set: function (val) {
  69. this._selected = val;
  70. if (!this.selectedFlag)
  71. return;
  72. switch (this.selectedMode) {
  73. case SelectedType.TOGGLE:
  74. this.selectedFlag.active = val;
  75. break;
  76. case SelectedType.SWITCH:
  77. var sp = this.selectedFlag.getComponent(cc.Sprite);
  78. if (sp)
  79. sp.spriteFrame = val ? this.selectedSpriteFrame : this._unselectedSpriteFrame;
  80. break;
  81. }
  82. },
  83. enumerable: false,
  84. configurable: true
  85. });
  86. Object.defineProperty(ListItem.prototype, "btnCom", {
  87. get: function () {
  88. if (!this._btnCom)
  89. this._btnCom = this.node.getComponent(cc.Button);
  90. return this._btnCom;
  91. },
  92. enumerable: false,
  93. configurable: true
  94. });
  95. ListItem.prototype.onLoad = function () {
  96. // //没有按钮组件的话,selectedFlag无效
  97. // if (!this.btnCom)
  98. // this.selectedMode == SelectedType.NONE;
  99. //有选择模式时,保存相应的东西
  100. if (this.selectedMode == SelectedType.SWITCH) {
  101. var com = this.selectedFlag.getComponent(cc.Sprite);
  102. this._unselectedSpriteFrame = com.spriteFrame;
  103. }
  104. };
  105. ListItem.prototype.onDestroy = function () {
  106. this.node.off(cc.Node.EventType.SIZE_CHANGED, this._onSizeChange, this);
  107. };
  108. ListItem.prototype._registerEvent = function () {
  109. if (!this._eventReg) {
  110. if (this.btnCom && this.list.selectedMode > 0) {
  111. this.btnCom.clickEvents.unshift(this.createEvt(this, 'onClickThis'));
  112. }
  113. if (this.adaptiveSize) {
  114. this.node.on(cc.Node.EventType.SIZE_CHANGED, this._onSizeChange, this);
  115. }
  116. this._eventReg = true;
  117. }
  118. };
  119. ListItem.prototype._onSizeChange = function () {
  120. this.list._onItemAdaptive(this.node);
  121. };
  122. /**
  123. * 创建事件
  124. * @param {cc.Component} component 组件脚本
  125. * @param {string} handlerName 触发函数名称
  126. * @param {cc.Node} node 组件所在node(不传的情况下取component.node)
  127. * @returns cc.Component.EventHandler
  128. */
  129. ListItem.prototype.createEvt = function (component, handlerName, node) {
  130. if (node === void 0) { node = null; }
  131. if (!component.isValid)
  132. return; //有些异步加载的,节点以及销毁了。
  133. component['comName'] = component['comName'] || component.name.match(/\<(.*?)\>/g).pop().replace(/\<|>/g, '');
  134. var evt = new cc.Component.EventHandler();
  135. evt.target = node || component.node;
  136. evt.component = component['comName'];
  137. evt.handler = handlerName;
  138. return evt;
  139. };
  140. ListItem.prototype.showAni = function (aniType, callFunc, del) {
  141. var _this = this;
  142. var acts;
  143. switch (aniType) {
  144. case 0: //向上消失
  145. acts = [
  146. cc.scaleTo(.2, .7),
  147. cc.moveBy(.3, 0, this.node.height * 2),
  148. ];
  149. break;
  150. case 1: //向右消失
  151. acts = [
  152. cc.scaleTo(.2, .7),
  153. cc.moveBy(.3, this.node.width * 2, 0),
  154. ];
  155. break;
  156. case 2: //向下消失
  157. acts = [
  158. cc.scaleTo(.2, .7),
  159. cc.moveBy(.3, 0, this.node.height * -2),
  160. ];
  161. break;
  162. case 3: //向左消失
  163. acts = [
  164. cc.scaleTo(.2, .7),
  165. cc.moveBy(.3, this.node.width * -2, 0),
  166. ];
  167. break;
  168. default: //默认:缩小消失
  169. acts = [
  170. cc.scaleTo(.3, .1),
  171. ];
  172. break;
  173. }
  174. if (callFunc || del) {
  175. acts.push(cc.callFunc(function () {
  176. if (del) {
  177. _this.list._delSingleItem(_this.node);
  178. for (var n = _this.list.displayData.length - 1; n >= 0; n--) {
  179. if (_this.list.displayData[n].id == _this.listId) {
  180. _this.list.displayData.splice(n, 1);
  181. break;
  182. }
  183. }
  184. }
  185. callFunc();
  186. }));
  187. }
  188. this.node.runAction(cc.sequence(acts));
  189. };
  190. ListItem.prototype.onClickThis = function () {
  191. this.list.selectedId = this.listId;
  192. };
  193. __decorate([
  194. property({ type: cc.Sprite, tooltip: CC_DEV && '图标' })
  195. ], ListItem.prototype, "icon", void 0);
  196. __decorate([
  197. property({ type: cc.Node, tooltip: CC_DEV && '标题' })
  198. ], ListItem.prototype, "title", void 0);
  199. __decorate([
  200. property({
  201. type: cc.Enum(SelectedType),
  202. tooltip: CC_DEV && '选择模式'
  203. })
  204. ], ListItem.prototype, "selectedMode", void 0);
  205. __decorate([
  206. property({
  207. type: cc.Node, tooltip: CC_DEV && '被选标志',
  208. visible: function () { return this.selectedMode > SelectedType.NONE; }
  209. })
  210. ], ListItem.prototype, "selectedFlag", void 0);
  211. __decorate([
  212. property({
  213. type: cc.SpriteFrame, tooltip: CC_DEV && '被选择的SpriteFrame',
  214. visible: function () { return this.selectedMode == SelectedType.SWITCH; }
  215. })
  216. ], ListItem.prototype, "selectedSpriteFrame", void 0);
  217. __decorate([
  218. property({
  219. tooltip: CC_DEV && '自适应尺寸(宽或高)',
  220. })
  221. ], ListItem.prototype, "adaptiveSize", void 0);
  222. ListItem = __decorate([
  223. ccclass,
  224. disallowMultiple(),
  225. menu('自定义组件/List Item'),
  226. executionOrder(-5001) //先于List
  227. ], ListItem);
  228. return ListItem;
  229. }(cc.Component));
  230. exports.default = ListItem;
  231. cc._RF.pop();