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 14 kB

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

智龙迷城队伍图制作工具