awselect.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. jQuery Awselect
  3. Developed by: Prev Wong
  4. Documentation: https://prevwong.github.io/awesome-select/
  5. Github: https://github.com/prevwong/awesome-select/
  6. **/
  7. var mobile_width = 800;
  8. HTMLSelectElement.prototype.__defineSetter__('awselect', function (awselect) {this.__awselect = awselect});
  9. HTMLSelectElement.prototype.awselect = {
  10. html_padding_right: '0px',
  11. animates: [],
  12. constructor: function() {
  13. this.awselect_section = true;
  14. this.awselect.build.call(this);
  15. this.awselect_section.addEventListener('aw:animate', function () {
  16. this.animate();
  17. });
  18. this.awselect_section.addEventListener('aw:deanimate', function () {
  19. this.deanimate();
  20. });
  21. this.awselect_section.addEventListener("change", function () {
  22. let select = this.select;
  23. let text = '';
  24. let selectedOptions = [...select.selectedOptions];
  25. let selectedOptionLastIndex = selectedOptions.length - 1;
  26. selectedOptions.forEach(function (item, index) {
  27. text += item.text + (selectedOptionLastIndex !== index ? ',' : '');
  28. });
  29. let value = document.createElement('span');
  30. value.classList.add('value');
  31. value.innerHTML = text;
  32. let content = this.querySelector(":scope > .frame > .content");
  33. content.firstChild.classList.contains('value') ? content.firstChild.replaceWith(value) : content.prepend(value);
  34. select.dispatchEvent(new Event("change"));
  35. setTimeout(function () {
  36. this.deanimate();
  37. }.bind(this), 100);
  38. });
  39. },
  40. animate: function () {
  41. if (this.classList.contains('animating')) {
  42. return;
  43. }
  44. this.classList.add('animating');
  45. let timeout = 100;
  46. let all_awselect = [...document.getElementsByClassName("awselect animate")];
  47. all_awselect.length > 0 && HTMLSelectElement.prototype.awselect.deanimate_all() && this.deanimate() && (timeout = 600);
  48. let immersive = this.select.immersive;
  49. if (document.documentElement.clientWidth < mobile_width || immersive) {
  50. this.immersive_animate();
  51. timeout += 200
  52. }
  53. setTimeout(function() {
  54. let expand_frame = this.expand_frame;
  55. let expand_background = this.expand_background;
  56. expand_background.style.transform = '';
  57. (document.documentElement.clientWidth < mobile_width || immersive) &&
  58. (this.style.top = parseInt(this.style.top) - expand_frame.clientHeight + 'px');
  59. this.classList.add("placeholder_animate");
  60. setTimeout(function() {
  61. setTimeout(function(){
  62. if (expand_frame.clientHeight == 200) {
  63. expand_frame.addClass("overflow");
  64. }
  65. }.bind(this), 200);
  66. this.classList.add('placeholder_animate2', 'animate', 'animate2');
  67. this.classList.remove("animating");
  68. HTMLSelectElement.prototype.awselect.animates.push(this);
  69. }.bind(this), 100);
  70. }.bind(this), timeout);
  71. },
  72. immersive_animate: function () {
  73. this.immersive_background && this.immersive_background.remove();
  74. if (window.innerWidth - document.documentElement.clientWidth > 0) {
  75. HTMLSelectElement.prototype.awselect.html_padding_right = `${ window.innerWidth - document.documentElement.clientWidth }px`;
  76. document.documentElement.style.paddingRight = `calc(${ getComputedStyle(document.documentElement).paddingRight } + ${ window.innerWidth - document.documentElement.clientWidth }px)`;
  77. document.documentElement.classList.add('immersive_awselect');
  78. }
  79. document.body.classList.add('immersive_awselect');
  80. let immersive_background = document.createElement('div');
  81. immersive_background.classList.add('awselect_immersive_background');
  82. this.immersive_background = immersive_background;
  83. document.body.prepend(immersive_background);
  84. setTimeout(function(){
  85. this.classList.add('animate');
  86. }.bind(immersive_background), 100)
  87. let immersive_width = this.clientWidth + 'px';
  88. let immersive_left = this.offsetLeft + 'px';
  89. let immersive_top = this.offsetTop - window.scrollY + 'px';
  90. this.immersive_width = immersive_width;
  91. this.immersive_left = immersive_left;
  92. this.immersive_top = immersive_top;
  93. this.classList.add('transition_paused');
  94. Object.assign(this.style,{
  95. "width" : immersive_width,
  96. "z-index": "9999",
  97. });
  98. setTimeout(function() {
  99. let immersive_shadow = document.createElement('div');
  100. Object.assign(immersive_shadow.style,{
  101. "width" : immersive_width,
  102. "height" : this.clientHeight + 'px',
  103. });
  104. immersive_shadow.classList.add('immersive_shadow');
  105. this.insertAdjacentElement('afterend', immersive_shadow);
  106. this.immersive_shadow = immersive_shadow;
  107. Object.assign(this.style,{
  108. position: 'fixed',
  109. top: immersive_top,
  110. left: immersive_left,
  111. });
  112. this.classList.remove('transition_paused')
  113. setTimeout(function() {
  114. this.style.width = (window.innerWidth < mobile_width ? window.innerWidth - 40 : window.innerWidth / 2) + 'px';
  115. Object.assign(this.style,{
  116. top: (window.innerHeight / 2 + this.clientHeight / 2) + 'px',
  117. left: '50%',
  118. transform: 'translateX(-50%) translateY(-50%)',
  119. });
  120. setTimeout(function() {
  121. this.animate();
  122. }.bind(this), 100)
  123. }.bind(this), 100)
  124. }.bind(this), 50)
  125. },
  126. deanimate: function () {
  127. setTimeout(function() {
  128. }, 300);
  129. this.classList.remove('animate2');
  130. this.expand_frame.classList.remove('overflow');
  131. this.classList.remove('animate', 'placeholder_animate2');
  132. HTMLSelectElement.prototype.awselect.animates = HTMLSelectElement.prototype.awselect.animates.filter(function (index) { return index !== this;}.bind(this));
  133. setTimeout(function() {
  134. let immersive_shadow = this.immersive_shadow;
  135. !!immersive_shadow && setTimeout(function(){
  136. Object.assign(this.style,{
  137. "width" : this.immersive_width,
  138. "left" : this.immersive_left,
  139. "transform": "translateX(0) translateY(0)",
  140. "top" : this.immersive_top
  141. })
  142. this.immersive_background.classList.remove('animate');
  143. setTimeout(function(){
  144. immersive_shadow.remove();
  145. this.immersive_shadow = undefined;
  146. if (document.documentElement.matches('.immersive_awselect')) {
  147. document.documentElement.style.paddingRight = `calc(${ getComputedStyle(document.documentElement).paddingRight } - ${ HTMLSelectElement.prototype.awselect.html_padding_right })`;
  148. document.documentElement.classList.remove('immersive_awselect');
  149. document.body.classList.remove('immersive_awselect');
  150. }
  151. setTimeout(function(){
  152. this.immersive_background.classList.remove('animate');
  153. this.immersive_background.remove();
  154. this.immersive_background = undefined;
  155. }.bind(this), 200);
  156. this.removeAttribute('style');
  157. }.bind(this), 300)
  158. }.bind(this), 100);
  159. this.classList.remove('placeholder_animate');
  160. }.bind(this), 100);
  161. },
  162. deanimate_all: function () {
  163. this.animates.forEach(function (awselect) {
  164. awselect.deanimate();
  165. });
  166. },
  167. build: function () {
  168. let id = this.id;
  169. let options = this.querySelectorAll(`:scope > option`);
  170. let classes = `${ this.className } awselect`;
  171. let options_html = '<ul class="awselect-options">';
  172. let awselect_icon = '<svg version="1.1" class="chevron_thin_down" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20" xml:space="preserve"><path d="M17.418,6.109c0.272-0.268,0.709-0.268,0.979,0c0.27,0.268,0.271,0.701,0,0.969l-7.908,7.83c-0.27,0.268-0.707,0.268-0.979,0l-7.908-7.83c-0.27-0.268-0.27-0.701,0-0.969c0.271-0.268,0.709-0.268,0.979,0L10,13.25L17.418,6.109z"/></svg>';
  173. let selected = false;
  174. options.forEach(function(item) {
  175. let text = item.text;
  176. item.selected && (selected = text);
  177. options_html += `<li><a>${ text }</a></li>`;
  178. });
  179. options_html += '</ul>';
  180. if (!id) {
  181. let xpath = '';
  182. let item = this;
  183. let slash = '';
  184. [...Array(5).keys()].some(function (index) {
  185. let tagName = item.tagName;
  186. let id = item.id;
  187. let siblingIndex = [...item.parentElement.querySelectorAll(`:scope > ${tagName}`)].indexOf(item);
  188. xpath = (tagName === 'BODY' ? '//' : '') + (!!id ? `id("${id}")` : `${tagName}[${siblingIndex + 1}]`) + slash + xpath;
  189. slash = '/';
  190. item = item.parentElement;
  191. return !!id || tagName === 'BODY';
  192. });
  193. id = '$$$$-$$$$-$$$$-$$$$'.replace(/[$]/g, function(c, p) {
  194. str = (new TextEncoder).encode(xpath).join('z');
  195. return str[p % str.length];
  196. });
  197. this.id = id;
  198. }
  199. this.immersive = this.attributes.immersive?.value === 'true';
  200. let awselect_section = document.createElement('div');
  201. awselect_section.id = 'awselect_' + id;
  202. awselect_section.select = this;
  203. awselect_section.classList.value = classes;
  204. let frame = document.createElement('div');
  205. frame.classList.value = 'frame';
  206. !!selected && (frame.innerHTML += `<span class="value">${ selected }</span>`);
  207. !!this.attributes?.placeholder?.value
  208. && (frame.innerHTML += `<span class="placeholder">${ this.attributes.placeholder.value }</span>`);
  209. frame.innerHTML += `<i class="icon">${ awselect_icon }</i>`;
  210. frame.innerHTML = `<div class="content">${ frame.innerHTML }</div>`;
  211. awselect_section.append(frame);
  212. let expand_background = document.createElement('div');
  213. expand_background.classList.value = 'expand_background';
  214. awselect_section.append(expand_background);
  215. let expand_frame = document.createElement('div');
  216. expand_frame.classList.value = 'expand_frame';
  217. expand_frame.style.display = 'block';
  218. expand_frame.innerHTML = options_html;
  219. awselect_section.append(expand_frame);
  220. this.insertAdjacentElement('afterend', awselect_section);
  221. this.style.display = 'none';
  222. this.awselect_section = awselect_section;
  223. awselect_section.expand_background = expand_background;
  224. awselect_section.expand_frame = expand_frame;
  225. awselect_section.frame = frame;
  226. expand_background.style.height = `calc(100% + ${ expand_frame.clientHeight }px)`;
  227. expand_frame.removeAttribute('style');
  228. this.awselect_section.animate = this.awselect.animate;
  229. this.awselect_section.deanimate = this.awselect.deanimate;
  230. this.awselect_section.immersive_animate = this.awselect.immersive_animate;
  231. }
  232. };
  233. HTMLSelectElement.prototype.__defineGetter__('awselect', function () {
  234. return (!!this.awselect_section || !(this instanceof HTMLSelectElement)) ? this.__awselect :
  235. (!HTMLSelectElement.prototype.awselect_document_event &&
  236. (document.addEventListener('click', function (event) {
  237. let option = event.target.closest('.awselect ul li a');
  238. let awselect = event.target.closest('.awselect');
  239. if (!!option) {
  240. awselect.select.value = awselect.select.children[[...option.closest('li').parentElement.children].indexOf(option.closest('li'))].value;
  241. awselect.dispatchEvent(new Event("change"));
  242. } else {
  243. !!awselect && awselect.dispatchEvent(new CustomEvent(awselect.classList.contains('animate') ? 'aw:deanimate' : 'aw:animate', {}));
  244. }
  245. }) ||
  246. (new MutationObserver(function () {HTMLSelectElement.prototype.awselect.animates = [...document.getElementsByClassName("awselect animate")]})).observe(document.body, { childList: true, subtree: true }) ||
  247. document.addEventListener("mouseup", function (event) {
  248. !event.target.closest('.awselect') && HTMLSelectElement.prototype.awselect.deanimate_all();
  249. }) || (HTMLSelectElement.prototype.awselect_document_event = true)), this.__awselect.constructor);
  250. });