You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

script-custom_elements.js 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Detail: 仿照 DOMTokenList 功能实现的自定义 CustomTokenList
  3. */
  4. class CustomTokenList extends Array {
  5. //Reference: https://stackoverflow.com/questions/49349195/using-splice-method-in-subclass-of-array-in-javascript/56181665#56181665 让splice能够用
  6. static get [Symbol.species]() { return Array; }
  7. static #illegalTokenRegRex = /\s/;
  8. #node = null;
  9. #attributeName = null;
  10. #refreshAttribute = true;
  11. #observer = null;
  12. #observerOptions = {
  13. attributeFilter: [],
  14. attributeOldValue: true,
  15. subtree: true
  16. };
  17. get #attribute() { //获取绑定的参数
  18. return this.#node.getAttributeNode(this.#attributeName);
  19. }
  20. set #attribute(node) { //直接设定绑定的参数AttrNode
  21. if (node instanceof Attr) {
  22. this.#node = node.ownerElement;
  23. this.#attributeName = node.nodeName;
  24. this.#observerOptions.attributeFilter = [this.#attributeName];
  25. } else {
  26. throw new TypeError(`${CustomTokenList.name}.#attribute: Argument 1 is not an Attr.\n参数 1 不是 Attr。`);
  27. }
  28. }
  29. constructor(node, attributeName){ //传入 HTMLElement 和需要绑定的 参数名称
  30. super();
  31. if (node instanceof Attr) {
  32. this.#attribute = node;
  33. } else if (node instanceof HTMLElement) {
  34. this.#node = node;
  35. this.#attributeName = attributeName.toString().toLowerCase(); //attributeName 只支持小写
  36. this.#observerOptions.attributeFilter = [this.#attributeName];
  37. } else {
  38. throw new TypeError(`${CustomTokenList.name}.constructor: Argument 1 is not an Attr or HTMLElement.\n参数 1 不是 Attr 或 HTMLElement。`);
  39. }
  40. const _this = this;
  41. this.#observer = new MutationObserver(function(mutationList) {
  42. for (const mutation of mutationList) {
  43. if (mutation.type == 'attributes' && mutation.attributeName == _this.#attributeName) {
  44. //因为可能是删除属性,所以i需要重新取得属性
  45. _this.#attribute = _this.#node.getAttributeNode(_this.#attributeName);
  46. //清空旧值
  47. _this.length = 0;
  48. if (_this.#attribute) {
  49. _this.#refreshAttribute = false; //外部属性变化时,添加内容不再循环进行属性的更新
  50. _this.add(...new Set(_this.#attribute.nodeValue.split(/\s+/g)));
  51. _this.#refreshAttribute = true;
  52. }
  53. }
  54. }
  55. });
  56. this.#observer.observe(this.#node, this.#observerOptions);
  57. }
  58. static #InvalidCharacterError(functionName) {
  59. return new DOMException(`${CustomTokenList.name}.${functionName}:The token can not contain whitespace.\bToken 不允许包含空格。`, "InvalidCharacterError");
  60. }
  61. add(...tokens){
  62. //全部强制转换为字符串
  63. tokens = tokens.map(token=>token.toString());
  64. //如果任何 token 里存在空格,就直接抛出错误
  65. if (tokens.some(token=>CustomTokenList.#illegalTokenRegRex.test(token)))
  66. throw CustomTokenList.#InvalidCharacterError('add');
  67. tokens.forEach(token => {
  68. if (!this.includes(token)) this.push(token);
  69. });
  70. if (this.#refreshAttribute) {
  71. this.#observer.disconnect(); //解除绑定
  72. if (!this.#attribute) {
  73. this.#node.setAttributeNode(document.createAttribute(this.#attributeName));
  74. }
  75. this.#attribute.value = this.value;
  76. this.#observer.observe(this.#node, this.#observerOptions); //恢复绑定
  77. }
  78. return;
  79. }
  80. remove(...tokens){
  81. //全部强制转换为字符串
  82. tokens = tokens.map(token=>token.toString());
  83. //如果任何 token 里存在空格,就直接抛出错误
  84. if (tokens.some(token=>CustomTokenList.#illegalTokenRegRex.test(token)))
  85. throw CustomTokenList.#InvalidCharacterError('remove');
  86. tokens.forEach(token => {
  87. const index = this.indexOf(token);
  88. if (index>=0) this.valueOf().splice(index,1);
  89. });
  90. if (this.#refreshAttribute) {
  91. this.#observer.disconnect(); //解除绑定
  92. this.#attribute.value = this.value;
  93. this.#observer.observe(this.#node, this.#observerOptions); //恢复绑定
  94. }
  95. return;
  96. }
  97. contains(token){
  98. return this.includes(token.toString());
  99. }
  100. toggle(token, force){
  101. if (CustomTokenList.#illegalTokenRegRex.test(token))
  102. throw CustomTokenList.#InvalidCharacterError('toggle');
  103. if (force !== undefined) {
  104. if(Boolean(force)) {
  105. this.add(token);
  106. return true;
  107. }else{
  108. this.remove(token);
  109. return false;
  110. }
  111. } else {
  112. if (this.contains(token)) {
  113. this.remove(token);
  114. return false;
  115. } else {
  116. this.add(token);
  117. return true;
  118. }
  119. }
  120. }
  121. get value() {
  122. return this.join(' ');
  123. }
  124. set value(string) {
  125. this.length = 0;
  126. //因为add里面已经解除绑定了,所以这里不需要解除绑定
  127. this.add(...new Set(string.split(/\s+/g)));
  128. }
  129. replace(oldToken, newToken){
  130. oldToken = oldToken.toString();
  131. newToken = newToken.toString();
  132. if (CustomTokenList.#illegalTokenRegRex.test(oldToken) || CustomTokenList.#illegalTokenRegRex.test(newToken))
  133. throw CustomTokenList.#InvalidCharacterError('replace');
  134. const index = this.indexOf(oldToken);
  135. if (index>=0) {
  136. this.splice(index,1, this.contains(newToken) ? undefined : newToken);
  137. if (this.#refreshAttribute) {
  138. this.#observer.disconnect(); //解除绑定
  139. this.#attribute.value = this.value;
  140. this.#observer.observe(this.#node, this.#observerOptions); //恢复绑定
  141. }
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147. item(index) {
  148. return this[index];
  149. }
  150. };
  151. const svgNS = "http://www.w3.org/2000/svg"; //svg用的命名空间
  152. // Create a class for the element
  153. class CardAvatar extends HTMLElement {
  154. // Specify observed attributes so that
  155. // attributeChangedCallback will work
  156. static get observedAttributes() {
  157. return ['iid'];
  158. }
  159. #iid = 0;
  160. get iid() {
  161. return this.#iid;
  162. }
  163. /**
  164. * @param {string | number} x
  165. */
  166. set iid(x) {
  167. //this.#iid = x; //在属性改变的内容里已经写入了
  168. this.setAttribute('iid', x);
  169. }
  170. //#member = new Member();
  171. /**
  172. * @param {Member} m
  173. */
  174. /*get member() {
  175. return this.#member;
  176. }*/
  177. /**
  178. * @param {Member} m
  179. */
  180. /*set member(m) {
  181. this.#member = m;
  182. console.log("设定新的Member",m);
  183. this.setAttribute('id', m.id);
  184. }*/
  185. constructor() {
  186. // Always call super first in constructor
  187. super();
  188. // Create a shadow root
  189. const shadow = this.attachShadow({mode: 'open'});
  190. // Create some CSS to apply to the shadow dom
  191. const linkElem = shadow.appendChild(document.createElement('link'));
  192. linkElem.setAttribute('rel', 'stylesheet');
  193. linkElem.setAttribute('href', 'css/card-avatar.css');
  194. // Create spans
  195. const wrapper = shadow.appendChild(document.createElement('a'));
  196. wrapper.className = 'wrapper';
  197. wrapper.target = '_blank';
  198. const dAttr1 = wrapper.appendChild(document.createElement('div'));
  199. dAttr1.className = 'attribute attribute-main';
  200. const dAttr2 = wrapper.appendChild(document.createElement('div'));
  201. dAttr2.className = 'attribute attribute-sub';
  202. const dLeftTop = wrapper.appendChild(document.createElement('div'));
  203. dLeftTop.className = "flex-box flex-left-top";
  204. const dLeftBottom = wrapper.appendChild(document.createElement('div'));
  205. dLeftBottom.className = "flex-box flex-left-bottom";
  206. const dRightTop = wrapper.appendChild(document.createElement('div'));
  207. dRightTop.className = "flex-box flex-right-top";
  208. const dRightBottom = wrapper.appendChild(document.createElement('div'));
  209. dRightBottom.className = "flex-box flex-right-bottom";
  210. const dId = dLeftBottom.appendChild(document.createElement('div'));
  211. dId.className = 'card-id';
  212. const dLevel = dLeftBottom.appendChild(document.createElement('div'));
  213. dLevel.className = 'level';
  214. dLevel.textContent = "110";
  215. const dEnhancement = dLeftTop.appendChild(document.createElement('div'));
  216. dEnhancement.className = 'enhancement';
  217. dEnhancement.textContent = "297";
  218. const dActiveSkillCD = dRightBottom.appendChild(document.createElement('div'));
  219. dActiveSkillCD.className = 'active-skill-cd';
  220. dActiveSkillCD.textContent = "99";
  221. }
  222. connectedCallback() {
  223. console.log('自定义标签添加到页面');
  224. this.update();
  225. }
  226. attributeChangedCallback(name, oldValue, newValue) {
  227. console.log('自定义标签属性改变', name, oldValue, newValue);
  228. //if (name == 'id') this.#id = parseInt(this.getAttribute('id'));
  229. if (name == 'iid') this.#iid = parseInt(newValue);
  230. this.update();
  231. }
  232. update() {
  233. //得到怪物ID
  234. const id = this.#iid || 0;
  235. this.iid = id;
  236. const card = Cards[id] || Cards[0];
  237. const dataSource = this.getAttribute('source') || currentDataSource.code;
  238. const shadow = this.shadowRoot;
  239. const wrapper = shadow.querySelector('.wrapper');
  240. wrapper.href = currentLanguage.guideURL(id, card.name);
  241. wrapper.style.backgroundImage = `url(images/cards_${dataSource.toLowerCase()}/CARDS_${Math.ceil(id / 100).toString().padStart(3,"0")}.PNG)`;
  242. const idxInPage = (id - 1) % 100; //获取当前页面内的序号
  243. wrapper.setAttribute("data-cards-pic-x", idxInPage % 10); //添加X方向序号
  244. wrapper.setAttribute("data-cards-pic-y", Math.floor(idxInPage / 10)); //添加Y方向序号
  245. const dAttr1 = wrapper.querySelector('.attribute-main');
  246. dAttr1.setAttribute('data-attr', card.attrs[0]);
  247. const dAttr2 = wrapper.querySelector('.attribute-sub');
  248. dAttr2.setAttribute('data-attr', card.attrs[1]);
  249. const dId = wrapper.querySelector('.card-id');
  250. dId.setAttribute('data-id', id);
  251. }
  252. }
  253. // Define the new element
  254. customElements.define('card-avatar', CardAvatar);
  255. class PadIcon extends HTMLElement {
  256. // Specify observed attributes so that
  257. // attributeChangedCallback will work
  258. static get observedAttributes() {
  259. return [
  260. 'type', //图标类型
  261. 'number', //编号或数字,必须是数字
  262. 'lang', //英语、中文特殊图标的设定
  263. 'icon-type',//子图标类型
  264. //'icon-value',//子图标的值
  265. 'flags', //各种选项开关,类似className
  266. ];
  267. }
  268. static ELEMENT_TYPE_AWOKEN = 'awoken';
  269. static ELEMENT_TYPE_TYPE = 'type';
  270. static ELEMENT_TYPE_AWOKEN_COUNT = 'awoken-count';
  271. #svg = document.createElementNS(svgNS,'svg');
  272. #flags = null;
  273. get flagsList() { return this.#flags; }
  274. #number = 0;
  275. #type = PadIcon.ELEMENT_TYPE_AWOKEN;
  276. #iconType = null;
  277. get number() { return this.#number; }
  278. set number(x) {
  279. const number = Number(x);
  280. if (Number.isNaN(number)) throw new Error('传入的 number 不是数字!');
  281. this.setAttribute('number', number);
  282. this.#number = number;
  283. }
  284. get type() { return this.#type; }
  285. set type(x) {
  286. if (x == null) {
  287. this.removeAttribute('type');
  288. } else {
  289. this.setAttribute('type', x);
  290. }
  291. this.#type = x;
  292. }
  293. get iconType() { this.#iconType; }
  294. set iconType(x) {
  295. if (x == null) {
  296. this.removeAttribute('icon-type');
  297. } else {
  298. this.setAttribute('icon-type', x);
  299. }
  300. this.#iconType = x;
  301. }
  302. constructor() {
  303. // Always call super first in constructor
  304. super();
  305. this.#flags = new CustomTokenList(this, "flags");
  306. // Create a shadow root
  307. const shadow = this.attachShadow({mode: 'open'});
  308. // Create some CSS to apply to the shadow dom
  309. const linkElem = shadow.appendChild(document.createElement('link'));
  310. linkElem.setAttribute('rel', 'stylesheet');
  311. linkElem.setAttribute('href', 'css/svg-icon.css');
  312. const svg = this.#svg;
  313. const use = document.createElementNS(svgNS, 'use');
  314. svg.appendChild(use);
  315. shadow.appendChild(svg);
  316. }
  317. connectedCallback() { //自定义标签添加到页面
  318. this.update();
  319. }
  320. attributeChangedCallback(name, oldValue, newValue) { //自定义标签属性改变
  321. const svg = this.shadowRoot.querySelector('svg');
  322. if (name == 'type') {
  323. this.#type = newValue;
  324. //如果更换类型,删除use以外的其他node
  325. const use = svg.querySelector('use');
  326. [...svg.childNodes].forEach(child=>{if(child!=use) child.remove();});
  327. }
  328. if (name == 'number') {
  329. const number = Number(newValue);
  330. this.#number = Number.isNaN(number) ? 0 : number;
  331. }
  332. if (name == 'icon-type') this.#iconType = newValue;
  333. //将这些属性全都复制到svg上去
  334. if (newValue == null) {
  335. svg.removeAttribute(name);
  336. } else {
  337. svg.setAttribute(name, newValue);
  338. }
  339. this.update();
  340. }
  341. update() {
  342. const number = this.#number;
  343. const lang = this.getAttribute('lang') || currentLanguage.i18n;
  344. //const shadow = this.shadowRoot;
  345. const svg = this.#svg;
  346. //svg.setAttribute("type", this.#type);
  347. //this.#iconType ? svg.setAttribute("icon-type", this.#iconType) : svg.removeAttribute("icon-type");
  348. const use = svg.querySelector(':scope>use');
  349. svg.setAttribute("viewBox", "0 0 32 32");
  350. switch (this.#type) {
  351. case PadIcon.ELEMENT_TYPE_AWOKEN: {
  352. if (/^(?:en|ko)/.test(lang) && [40,46,47,48].includes(number)) number += '-en'; //英文不一样的觉醒
  353. if (/^(?:zh)/.test(lang) && [46,47].includes(number)) number += '-zh'; //中文不一样的觉醒
  354. use.href.baseVal = `images/icon-awoken.svg#awoken-${number}`;
  355. break;
  356. }
  357. case PadIcon.ELEMENT_TYPE_TYPE: {
  358. if (/^(?:en|ko)/.test(lang) && [9,12].includes(number)) number += '-en'; //英文不一样的类型
  359. use.href.baseVal = `images/icon-type.svg#type-${number}`;
  360. break;
  361. }
  362. case PadIcon.ELEMENT_TYPE_AWOKEN_COUNT: {
  363. const full = this.flagsList.contains("full");
  364. const weapon = this.flagsList.contains("weapon");
  365. const canAssist = this.flagsList.contains("can-assist");
  366. //svg.setAttribute("viewBox", "0 0 34 38");
  367. svg.viewBox.baseVal.width = 34;
  368. svg.viewBox.baseVal.height = 38;
  369. if (full && weapon && canAssist) {
  370. use.href.baseVal = `images/icon-awoken.svg#awoken-49`;
  371. svg.querySelector('text')?.remove();
  372. break;
  373. }
  374. use.href.baseVal = `images/icon-awoken-count.svg#awoken-count-bg`;
  375. const text = svg.querySelector('text') || svg.appendChild(document.createElementNS(svgNS, 'text'));
  376. text.textContent = full ? '★' : number;
  377. //text.setAttribute("x", "50%");
  378. const lengthX = svg.createSVGLength();
  379. lengthX.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 50);
  380. text.x.baseVal.initialize(lengthX);
  381. //text.setAttribute("y", "47%");
  382. const lengthY = svg.createSVGLength();
  383. lengthY.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 47);
  384. text.y.baseVal.initialize(lengthY);
  385. text.classList.add("number");
  386. text.textLength.baseVal.newValueSpecifiedUnits(
  387. full ? SVGLength.SVG_LENGTHTYPE_EMS : SVGLength.SVG_LENGTHTYPE_PERCENTAGE,
  388. full ? 0.9 : 100);
  389. text.lengthAdjust.baseVal = full ? SVGTextElement.LENGTHADJUST_SPACINGANDGLYPHS : SVGTextElement.LENGTHADJUST_SPACING;
  390. break;
  391. }
  392. case 'latent':
  393. case 'badge':
  394. case 'attr':
  395. case 'orb':
  396. case 'common':
  397. case 'skill':
  398. default:
  399. break;
  400. }
  401. }
  402. }
  403. // Define the new element
  404. customElements.define('pad-icon', PadIcon);

智龙迷城队伍图制作工具