/* --- description: A non-obtrusive image dropdown menu that extends and replaces a standard HTML Select control. license: MIT-style authors: - Lorenzo Stanco requires: - core/1.4.1: '*' provides: [FancySelect] ... */ var FancySelect = new Class({ Implements: [Options, Events], options: { showText: true, showImages: true, className: 'fancy-select', className2: null, id:false, offset: { x: 0, y: 0 }, autoHide: true, autoScrollWindow: false, animateFade: true, animateSlide: true, fx: { 'duration': 'short' } }, initialize: function(element, options) { this.setOptions(options); /*if (!Fx.Slide)*/ this.options.animateSlide = false; // Need review this.element = document.id(element); this.element.store('fancyselect_object', this); this._create(); this.attach(); // Auto-scroll when FancySelect is out of viewport if (this.options.autoScrollWindow) this.addEvent('show', function() { var windowScroll = window.getScroll(); var overflow = this.ul.getPosition().y + this.ul.getSize().y - window.getSize().y - windowScroll.y; if (overflow > 0) window.scrollTo(windowScroll.x, windowScroll.y + overflow + 10); }); // Auto-hide the dropdown menu when user clicks outside if (this.options.autoHide) document.addEvent('click', function(e) { if (!this.shown) return; var target = document.id(e.target); var parents = target.getParents().include(target); if (!parents.contains(this.ul) && !parents.contains(this.div)) this.hide(); }.bind(this)); return this; }, attach: function() { this.element.setStyle('display', 'none'); this.select(this.element.get('value')); // Select current item if (Browser.ie) window.addEvent('load', function() { this.select(this.element.get('value')); }.bind(this)); // IE refresh fix this.ul.fade('hide').inject(document.id(document.body)); this.div.inject(this.element, 'after'); this.attached = true; this.fireEvent('attach'); return this; }, detach: function() { if (this.ul) this.ul.dispose(); if (this.div) this.div.dispose(); this.element.setStyle('display', ''); this.attached = false; this.fireEvent('detach'); return this; }, select: function(value) { this.element.set('value', value); // Update hidden