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