// vim: et:ts=4:sw=4:sts=4:ft=javascript 'use strict'; /*@ Ox.MenuButton Menu Button () -> Menu Button (options) -> Menu Button (options, self) -> Menu Button options Options object disabled If true, button is disabled id Element id items Menu items maxWidth Maximum menu width style Style ('rounded' or 'square') title Menu title tooltip Tooltip title, or function that returns one (e) -> Tooltip title e Mouse event type Type ('text' or 'image') width Width in px, or 'auto' @*/ Ox.MenuButton = function(options, self) { self = self || {}; var that = Ox.Element({ tooltip: options.tooltip || '' }, self) .defaults({ disabled: false, id: '', items: [], maxWidth: 0, style: 'rounded', title: '', type: 'text', width: 'auto' }) .options(options || {}) .addClass( 'OxSelect Ox' + Ox.toTitleCase(self.options.style) ) .css(self.options.width == 'auto' ? {} : { width: self.options.width - 2 + 'px' }) .bindEvent({ anyclick: showMenu }); if (self.options.type == 'text') { self.$title = $('
') .addClass('OxTitle') .css({width: self.options.width - 24 + 'px'}) .html(self.options.title) .appendTo(that); } self.$button = Ox.Button({ id: self.options.id + 'Button', style: 'symbol', title: self.options.type == 'text' || !self.options.title ? 'select' : self.options.title, type: 'image' }) .appendTo(that); self.$menu = Ox.Menu({ element: self.$title || self.$button, id: self.options.id + 'Menu', items: self.options.items, maxWidth: self.options.maxWidth, side: 'bottom', // FIXME: should be edge }) .bindEvent({ change: changeMenu, click: clickMenu, hide: hideMenu }); self.options.type == 'image' && self.$menu.addClass('OxRight'); function clickMenu(data) { that.triggerEvent('click', data); } function changeMenu(data) { that.triggerEvent('change', data); } function hideMenu(data) { that.loseFocus(); that.removeClass('OxSelected'); that.triggerEvent('hide'); } function showMenu() { that.gainFocus(); that.addClass('OxSelected'); self.options.tooltip && that.$tooltip.hide(); self.$menu.showMenu(); that.triggerEvent('show'); } self.setOption = function(key, value) { if (key == 'title') { if (self.options.type == 'text') { self.$title.html(value); } else { self.$button.options({title: value}); } } else if (key == 'width') { that.css({width: value - 2 + 'px'}); self.$title.css({width: self.options.width - 24 + 'px'}); } } /*@ checkItem @*/ that.checkItem = function(id) { self.$menu.checkItem(id); }; /*@ disableItem @*/ that.disableItem = function(id) { self.$menu.getItem(id).options({disabled: true}); }; /*@ enableItem @*/ that.enableItem = function(id) { self.$menu.getItem(id).options({disabled: false}); }; /*@ remove @*/ self.superRemove = that.remove; that.remove = function() { self.$menu.remove(); self.superRemove(); }; that.setItemTitle = function(id, title) { self.$menu.setItemTitle(id, title); return that; }; return that; };