awselect.js 13 KB

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