selectFx.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * selectFx.js v1.0.0
  3. * http://www.codrops.com
  4. *
  5. * Licensed under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Copyright 2014, Codrops
  9. * http://www.codrops.com
  10. */
  11. ;( function( window ) {
  12. 'use strict';
  13. /**
  14. * based on from https://github.com/inuyaksa/jquery.nicescroll/blob/master/jquery.nicescroll.js
  15. */
  16. function hasParent( e, p ) {
  17. if (!e) return false;
  18. var el = e.target||e.srcElement||e||false;
  19. while (el && el != p) {
  20. el = el.parentNode||false;
  21. }
  22. return (el!==false);
  23. };
  24. /**
  25. * extend obj function
  26. */
  27. function extend( a, b ) {
  28. for( var key in b ) {
  29. if( b.hasOwnProperty( key ) ) {
  30. a[key] = b[key];
  31. }
  32. }
  33. return a;
  34. }
  35. /**
  36. * SelectFx function
  37. */
  38. function SelectFx( el, options ) {
  39. this.el = el;
  40. this.options = extend( {}, this.options );
  41. extend( this.options, options );
  42. this._init();
  43. }
  44. /**
  45. * SelectFx options
  46. */
  47. SelectFx.prototype.options = {
  48. // if true all the links will open in a new tab.
  49. // if we want to be redirected when we click an option, we need to define a data-link attr on the option of the native select element
  50. newTab : true,
  51. // when opening the select element, the default placeholder (if any) is shown
  52. stickyPlaceholder : true,
  53. // callback when changing the value
  54. onChange : function( val ) { return false; }
  55. }
  56. /**
  57. * init function
  58. * initialize and cache some vars
  59. */
  60. SelectFx.prototype._init = function() {
  61. // check if we are using a placeholder for the native select box
  62. // we assume the placeholder is disabled and selected by default
  63. var selectedOpt = this.el.querySelector( 'option[selected]' );
  64. this.hasDefaultPlaceholder = selectedOpt && selectedOpt.disabled;
  65. // get selected option (either the first option with attr selected or just the first option)
  66. this.selectedOpt = selectedOpt || this.el.querySelector( 'option' );
  67. // create structure
  68. this._createSelectEl();
  69. // all options
  70. this.selOpts = [].slice.call( this.selEl.querySelectorAll( 'li[data-option]' ) );
  71. // total options
  72. this.selOptsCount = this.selOpts.length;
  73. // current index
  74. this.current = this.selOpts.indexOf( this.selEl.querySelector( 'li.cs-selected' ) ) || -1;
  75. // placeholder elem
  76. this.selPlaceholder = this.selEl.querySelector( 'span.cs-placeholder' );
  77. // init events
  78. this._initEvents();
  79. }
  80. /**
  81. * creates the structure for the select element
  82. */
  83. SelectFx.prototype._createSelectEl = function() {
  84. var self = this, options = '', createOptionHTML = function(el) {
  85. var optclass = '', classes = '', link = '';
  86. if( el.selectedOpt && !this.foundSelected && !this.hasDefaultPlaceholder ) {
  87. classes += 'cs-selected ';
  88. this.foundSelected = true;
  89. }
  90. // extra classes
  91. if( el.getAttribute( 'data-class' ) ) {
  92. classes += el.getAttribute( 'data-class' );
  93. }
  94. // link options
  95. if( el.getAttribute( 'data-link' ) ) {
  96. link = 'data-link=' + el.getAttribute( 'data-link' );
  97. }
  98. if( classes !== '' ) {
  99. optclass = 'class="' + classes + '" ';
  100. }
  101. return '<li ' + optclass + link + ' data-option data-value="' + el.value + '"><span>' + el.textContent + '</span></li>';
  102. };
  103. [].slice.call( this.el.children ).forEach( function(el) {
  104. if( el.disabled ) { return; }
  105. var tag = el.tagName.toLowerCase();
  106. if( tag === 'option' ) {
  107. options += createOptionHTML(el);
  108. }
  109. else if( tag === 'optgroup' ) {
  110. options += '<li class="cs-optgroup"><span>' + el.label + '</span><ul>';
  111. [].slice.call( el.children ).forEach( function(opt) {
  112. options += createOptionHTML(opt);
  113. } );
  114. options += '</ul></li>';
  115. }
  116. } );
  117. var opts_el = '<div class="cs-options"><ul>' + options + '</ul></div>';
  118. this.selEl = document.createElement( 'div' );
  119. this.selEl.className = this.el.className;
  120. this.selEl.tabIndex = this.el.tabIndex;
  121. this.selEl.innerHTML = '<span class="cs-placeholder">' + this.selectedOpt.textContent + '</span>' + opts_el;
  122. this.el.parentNode.appendChild( this.selEl );
  123. this.selEl.appendChild( this.el );
  124. }
  125. /**
  126. * initialize the events
  127. */
  128. SelectFx.prototype._initEvents = function() {
  129. var self = this;
  130. // open/close select
  131. this.selPlaceholder.addEventListener( 'click', function() {
  132. self._toggleSelect();
  133. } );
  134. // clicking the options
  135. this.selOpts.forEach( function(opt, idx) {
  136. opt.addEventListener( 'click', function() {
  137. self.current = idx;
  138. self._changeOption();
  139. // close select elem
  140. self._toggleSelect();
  141. } );
  142. } );
  143. // close the select element if the target it´s not the select element or one of its descendants..
  144. document.addEventListener( 'click', function(ev) {
  145. var target = ev.target;
  146. if( self._isOpen() && target !== self.selEl && !hasParent( target, self.selEl ) ) {
  147. self._toggleSelect();
  148. }
  149. } );
  150. // keyboard navigation events
  151. this.selEl.addEventListener( 'keydown', function( ev ) {
  152. var keyCode = ev.keyCode || ev.which;
  153. switch (keyCode) {
  154. // up key
  155. case 38:
  156. ev.preventDefault();
  157. self._navigateOpts('prev');
  158. break;
  159. // down key
  160. case 40:
  161. ev.preventDefault();
  162. self._navigateOpts('next');
  163. break;
  164. // space key
  165. case 32:
  166. ev.preventDefault();
  167. if( self._isOpen() && typeof self.preSelCurrent != 'undefined' && self.preSelCurrent !== -1 ) {
  168. self._changeOption();
  169. }
  170. self._toggleSelect();
  171. break;
  172. // enter key
  173. case 13:
  174. ev.preventDefault();
  175. if( self._isOpen() && typeof self.preSelCurrent != 'undefined' && self.preSelCurrent !== -1 ) {
  176. self._changeOption();
  177. self._toggleSelect();
  178. }
  179. break;
  180. // esc key
  181. case 27:
  182. ev.preventDefault();
  183. if( self._isOpen() ) {
  184. self._toggleSelect();
  185. }
  186. break;
  187. }
  188. } );
  189. }
  190. /**
  191. * navigate with up/dpwn keys
  192. */
  193. SelectFx.prototype._navigateOpts = function(dir) {
  194. if( !this._isOpen() ) {
  195. this._toggleSelect();
  196. }
  197. var tmpcurrent = typeof this.preSelCurrent != 'undefined' && this.preSelCurrent !== -1 ? this.preSelCurrent : this.current;
  198. if( dir === 'prev' && tmpcurrent > 0 || dir === 'next' && tmpcurrent < this.selOptsCount - 1 ) {
  199. // save pre selected current - if we click on option, or press enter, or press space this is going to be the index of the current option
  200. this.preSelCurrent = dir === 'next' ? tmpcurrent + 1 : tmpcurrent - 1;
  201. // remove focus class if any..
  202. this._removeFocus();
  203. // add class focus - track which option we are navigating
  204. classie.add( this.selOpts[this.preSelCurrent], 'cs-focus' );
  205. }
  206. }
  207. /**
  208. * open/close select
  209. * when opened show the default placeholder if any
  210. */
  211. SelectFx.prototype._toggleSelect = function() {
  212. // remove focus class if any..
  213. this._removeFocus();
  214. if( this._isOpen() ) {
  215. if( this.current !== -1 ) {
  216. // update placeholder text
  217. this.selPlaceholder.textContent = this.selOpts[ this.current ].textContent;
  218. }
  219. classie.remove( this.selEl, 'cs-active' );
  220. }
  221. else {
  222. if( this.hasDefaultPlaceholder && this.options.stickyPlaceholder ) {
  223. // everytime we open we wanna see the default placeholder text
  224. this.selPlaceholder.textContent = this.selectedOpt.textContent;
  225. }
  226. classie.add( this.selEl, 'cs-active' );
  227. }
  228. }
  229. /**
  230. * change option - the new value is set
  231. */
  232. SelectFx.prototype._changeOption = function() {
  233. // if pre selected current (if we navigate with the keyboard)...
  234. if( typeof this.preSelCurrent != 'undefined' && this.preSelCurrent !== -1 ) {
  235. this.current = this.preSelCurrent;
  236. this.preSelCurrent = -1;
  237. }
  238. // current option
  239. var opt = this.selOpts[ this.current ];
  240. // update current selected value
  241. this.selPlaceholder.textContent = opt.textContent;
  242. // change native select element´s value
  243. this.el.value = opt.getAttribute( 'data-value' );
  244. // remove class cs-selected from old selected option and add it to current selected option
  245. var oldOpt = this.selEl.querySelector( 'li.cs-selected' );
  246. if( oldOpt ) {
  247. classie.remove( oldOpt, 'cs-selected' );
  248. }
  249. classie.add( opt, 'cs-selected' );
  250. // if there´s a link defined
  251. if( opt.getAttribute( 'data-link' ) ) {
  252. // open in new tab?
  253. if( this.options.newTab ) {
  254. window.open( opt.getAttribute( 'data-link' ), '_blank' );
  255. }
  256. else {
  257. window.location = opt.getAttribute( 'data-link' );
  258. }
  259. }
  260. // callback
  261. this.options.onChange( this.el.value );
  262. }
  263. /**
  264. * returns true if select element is opened
  265. */
  266. SelectFx.prototype._isOpen = function(opt) {
  267. return classie.has( this.selEl, 'cs-active' );
  268. }
  269. /**
  270. * removes the focus class from the option
  271. */
  272. SelectFx.prototype._removeFocus = function(opt) {
  273. var focusEl = this.selEl.querySelector( 'li.cs-focus' )
  274. if( focusEl ) {
  275. classie.remove( focusEl, 'cs-focus' );
  276. }
  277. }
  278. /**
  279. * add to global namespace
  280. */
  281. window.SelectFx = SelectFx;
  282. } )( window );