awselect.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. this.classList.add('expand');
  56. this.classList.add("placeholder_animate");
  57. setTimeout(function() {
  58. setTimeout(function(){
  59. if (expand_frame.clientHeight == 200) {
  60. expand_frame.addClass("overflow");
  61. }
  62. }.bind(this), 200);
  63. this.classList.add('placeholder_animate2', 'animate', 'animate2');
  64. this.classList.remove("animating");
  65. HTMLSelectElement.prototype.awselect.animates.push(this);
  66. }.bind(this), 100);
  67. }.bind(this), timeout);
  68. },
  69. immersive_animate: function () {
  70. this.immersive_background && this.immersive_background.remove();
  71. if (window.innerWidth - document.documentElement.clientWidth > 0) {
  72. HTMLSelectElement.prototype.awselect.html_padding_right = `${ window.innerWidth - document.documentElement.clientWidth }px`;
  73. document.documentElement.style.paddingRight = `calc(${ getComputedStyle(document.documentElement).paddingRight } + ${ window.innerWidth - document.documentElement.clientWidth }px)`;
  74. document.documentElement.classList.add('immersive_awselect');
  75. document.body.classList.add('immersive_awselect');
  76. }
  77. let immersive_background = document.createElement('div');
  78. immersive_background.classList.add('awselect_immersive_background');
  79. this.immersive_background = immersive_background;
  80. document.body.prepend(immersive_background);
  81. setTimeout(function(){
  82. this.classList.add('animate');
  83. }.bind(immersive_background), 100)
  84. let immersive_width = this.clientWidth + 'px';
  85. let immersive_left = this.offsetLeft + 'px';
  86. let immersive_top = this.offsetTop - window.scrollY + 'px';
  87. this.immersive_width = immersive_width;
  88. this.immersive_left = immersive_left;
  89. this.immersive_top = immersive_top;
  90. this.classList.add('transition_paused');
  91. Object.assign(this.style,{
  92. "width" : immersive_width,
  93. "z-index": "9999",
  94. });
  95. setTimeout(function() {
  96. let immersive_shadow = document.createElement('div');
  97. Object.assign(immersive_shadow.style,{
  98. "width" : immersive_width,
  99. "height" : this.clientHeight + 'px',
  100. });
  101. immersive_shadow.classList.add('immersive_shadow');
  102. this.insertAdjacentElement('afterend', immersive_shadow);
  103. this.immersive_shadow = immersive_shadow;
  104. Object.assign(this.style,{
  105. position: 'fixed',
  106. top: immersive_top,
  107. left: immersive_left,
  108. });
  109. this.classList.remove('transition_paused')
  110. setTimeout(function() {
  111. this.style.width = (window.innerWidth < mobile_width ? window.innerWidth - 40 : window.innerWidth / 2) + 'px';
  112. Object.assign(this.style,{
  113. top: (window.innerHeight / 2 + this.clientHeight / 2 - this.expand_frame.clientHeight) + 'px',
  114. left: '50%',
  115. transform: 'translateX(-50%) translateY(-50%)',
  116. });
  117. setTimeout(function() {
  118. this.animate();
  119. }.bind(this), 100)
  120. }.bind(this), 100)
  121. }.bind(this), 50)
  122. },
  123. deanimate: function () {
  124. setTimeout(function() {
  125. }, 300);
  126. this.classList.remove('animate2');
  127. this.expand_frame.classList.remove('overflow');
  128. this.classList.remove('animate', 'placeholder_animate2', 'expand');
  129. HTMLSelectElement.prototype.awselect.animates = HTMLSelectElement.prototype.awselect.animates.filter(function (index) { return index !== this;}.bind(this));
  130. setTimeout(function() {
  131. let immersive_shadow = this.immersive_shadow;
  132. !!immersive_shadow && setTimeout(function(){
  133. Object.assign(this.style,{
  134. "width" : this.immersive_width,
  135. "left" : this.immersive_left,
  136. "transform": "translateX(0) translateY(0)",
  137. "top" : this.immersive_top
  138. })
  139. this.immersive_background.classList.remove('animate');
  140. setTimeout(function(){
  141. immersive_shadow.remove();
  142. this.immersive_shadow = undefined;
  143. if (document.body.matches('.immersive_awselect')) {
  144. document.documentElement.style.paddingRight = `calc(${ getComputedStyle(document.documentElement).paddingRight } - ${ HTMLSelectElement.prototype.awselect.html_padding_right })`;
  145. document.documentElement.classList.remove('immersive_awselect');
  146. document.body.classList.remove('immersive_awselect');
  147. }
  148. setTimeout(function(){
  149. this.immersive_background.classList.remove('animate');
  150. this.immersive_background.remove();
  151. this.immersive_background = undefined;
  152. }.bind(this), 200);
  153. this.removeAttribute('style');
  154. }.bind(this), 300)
  155. }.bind(this), 100);
  156. this.classList.remove('placeholder_animate');
  157. }.bind(this), 100);
  158. },
  159. deanimate_all: function () {
  160. this.animates.forEach(function (awselect) {
  161. awselect.deanimate();
  162. });
  163. },
  164. build: function () {
  165. let id = this.id;
  166. let options = this.querySelectorAll(`:scope > option`);
  167. let classes = `${ this.className } awselect`;
  168. let options_html = '<ul class="awselect-options">';
  169. 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>';
  170. let selected = false;
  171. options.forEach(function(item) {
  172. let text = item.text;
  173. item.selected && (selected = text);
  174. options_html += `<li><a>${ text }</a></li>`;
  175. });
  176. options_html += '</ul>';
  177. if (!id) {
  178. let xpath = '';
  179. let item = this;
  180. let slash = '';
  181. [...Array(5).keys()].some(function (index) {
  182. let tagName = item.tagName;
  183. let id = item.id;
  184. let siblingIndex = [...item.parentElement.querySelectorAll(`:scope > ${tagName}`)].indexOf(item);
  185. xpath = (tagName === 'BODY' ? '//' : '') + (!!id ? `id("${id}")` : `${tagName}[${siblingIndex + 1}]`) + slash + xpath;
  186. slash = '/';
  187. item = item.parentElement;
  188. return !!id || tagName === 'BODY';
  189. });
  190. id = '$$$$-$$$$-$$$$-$$$$'.replace(/[$]/g, function(c, p) {
  191. str = (new TextEncoder).encode(xpath).join('z');
  192. return str[p % str.length];
  193. });
  194. this.id = id;
  195. }
  196. this.immersive = this.attributes.immersive?.value === 'true';
  197. let awselect_section = document.createElement('div');
  198. awselect_section.id = 'awselect_' + id;
  199. awselect_section.select = this;
  200. awselect_section.classList.value = classes;
  201. let frame = document.createElement('div');
  202. frame.classList.value = 'frame';
  203. !!selected && (frame.innerHTML += `<span class="value">${ selected }</span>`);
  204. !!this.attributes?.placeholder?.value
  205. && (frame.innerHTML += `<span class="placeholder">${ this.attributes.placeholder.value }</span>`);
  206. frame.innerHTML += `<i class="icon">${ awselect_icon }</i>`;
  207. frame.innerHTML = `<div class="content">${ frame.innerHTML }</div>`;
  208. awselect_section.append(frame);
  209. let expand_background = document.createElement('div');
  210. expand_background.classList.value = 'expand_background';
  211. awselect_section.append(expand_background);
  212. let expand_frame = document.createElement('div');
  213. expand_frame.classList.value = 'expand_frame';
  214. expand_frame.style.display = 'block';
  215. expand_frame.innerHTML = options_html;
  216. awselect_section.append(expand_frame);
  217. this.insertAdjacentElement('afterend', awselect_section);
  218. this.style.display = 'none';
  219. this.awselect_section = awselect_section;
  220. awselect_section.expand_background = expand_background;
  221. awselect_section.expand_frame = expand_frame;
  222. awselect_section.frame = frame;
  223. expand_background.style.height = `calc(100% + ${ expand_frame.clientHeight }px)`;
  224. expand_frame.removeAttribute('style');
  225. this.awselect_section.animate = this.awselect.animate;
  226. this.awselect_section.deanimate = this.awselect.deanimate;
  227. this.awselect_section.immersive_animate = this.awselect.immersive_animate;
  228. }
  229. };
  230. HTMLSelectElement.prototype.__defineGetter__('awselect', function () {
  231. return (!!this.awselect_section || !(this instanceof HTMLSelectElement)) ? this.__awselect :
  232. (!HTMLSelectElement.prototype.awselect_document_event &&
  233. (document.addEventListener('click', function (event) {
  234. let option = event.target.closest('.awselect ul li a');
  235. let awselect = event.target.closest('.awselect');
  236. if (!!option) {
  237. awselect.select.value = awselect.select.children[[...option.closest('li').parentElement.children].indexOf(option.closest('li'))].value;
  238. awselect.dispatchEvent(new Event("change"));
  239. } else {
  240. !!awselect && awselect.dispatchEvent(new CustomEvent(awselect.classList.contains('animate') ? 'aw:deanimate' : 'aw:animate', {}));
  241. }
  242. }) ||
  243. (new MutationObserver(function () {HTMLSelectElement.prototype.awselect.animates = [...document.getElementsByClassName("awselect animate")]})).observe(document.body, { childList: true, subtree: true }) ||
  244. document.addEventListener("mouseup", function (event) {
  245. !event.target.closest('.awselect') && HTMLSelectElement.prototype.awselect.deanimate_all();
  246. }) || (HTMLSelectElement.prototype.awselect_document_event = true)), this.__awselect.constructor);
  247. });