// ========================================================================== // Project: SproutCore - JavaScript Application Framework // Copyright: ©2006-2009 Sprout Systems, Inc. and contributors. // Portions ©2008-2009 Apple Inc. All rights reserved. // License: Licensed under MIT license (see license.js) // ========================================================================== /** @class SelectView has a functionality similar to that of SelectField Clicking the SelectView button displays a menu pane with a list of items. The selected item will be displayed on the button. User has the option of enabling checkbox for the selected menu item. I AM CHANGING THIS AND BREAKING APIs, so I have renamed it (and put it in a more logical place) so that I don't break anyone else's current use of SelectButtonView... even though I have a suspiscion that no one uses it. @extends SC.ButtonView @version 1.0 @author Alex Iskander, Mohammed Ashik */ sc_require('views/button'); SC.SelectView = SC.ButtonView.extend( /** @scope SC.SelectView.prototype */ { /** An array of items that will be form the menu you want to show. @property @type {Array} */ items: [], /** Binding default for an array of items @property @default SC.Binding.multiple() */ itemsBindingDefault: SC.Binding.multiple(), /** If you set this to a non-null value, then the name shown for each menu item will be pulled from the object using the named property. if this is null, the collection items themselves will be used. @property @type {String} @default: null */ itemTitleKey: null, /** If you set this to a non-null value, then the value of this key will be used to sort the items. If this is not set, then itemTitleKey will be used. @property @type: {String} @default: null */ itemSortKey: null, /** Set this to a non-null value to use a key from the passed set of items as the value for the options popup. If you don't set this, then the items themselves will be used as the value. @property @type {String} @default null */ itemValueKey: null, /** Key used to extract icons from the items array */ itemIconKey: null, /** Key to use to identify separators. */ itemSeparatorKey: "separator", /** If true, the empty name will be localized. @property @type {Boolean} @default YES */ localize: YES, /** if true, it means that no sorting will occur, items will appear in the same order as in the array @property @type {Boolean} @default YES */ disableSort: YES, /** @property @default ['sc-select-button'] */ classNames: ['sc-select-view'], /** List of actual menu items, handed off to the menu view. @property @private @type:{Array} */ _itemList: [], /** Current selected menu item @property @private @default null */ _currentSelItem: null, /** Property to set the index of the selected menu item. This in turn is used to calculate the preferMatrix. @property @type {Number} @default null @private */ _itemIdx: null, /** Current Value of the selectButton @property @default null */ value: null , /** if this property is set to 'YES', a checbox is shown next to the selected menu item. @private @default YES */ showCheckbox: YES, /** Default value of the select button. This will be the first item from the menu item list. @private */ _defaultVal: null, /** Default title of the select button. This will be the title corresponding to the _defaultVal. @private */ _defaultTitle: null, /** Default icon of the select button. This will be the icon corresponding to the _defaultVal. @private */ _defaultIcon: null, /** @private The button theme will be popup */ theme: 'popup', /** Render method gets triggered when these properties change @property @type{SC.Array} */ displayProperties: ['icon', 'value','controlSize','items'], /** Prefer matrix to position the select button menu such that the selected item for the menu item will appear aligned to the the button. The value at the second index(0) changes based on the postion(index) of the menu item in the menu pane. @property @type {Array} @default null */ preferMatrix: null, /** Property to set the menu item height. This in turn is used for the calculation of prefMatrix. @property @type {Number} @default 20 */ CUSTOM_MENU_ITEM_HEIGHT: 20, /** Binds the button's selection state to the menu's visibility. @private */ isSelectedBinding: '*menu.isVisibleInWindow', /** If this property is set to 'YES', the menu pane will be positioned below the anchor. @private @default NO */ positionMenuBelow: NO, /** lastMenuWidth is the width of the last menu which was created from the items of this select button. @private */ lastMenuWidth: null, /** Example view used for menu items. */ exampleView: null, /** customView menu offset width */ customViewMenuOffsetWidth: 0, /** This is a property for enabling/disabling ellipsis @private @default YES */ needsEllipsis: YES, /** This property allows you at add extra padding to the height of the menu pane. @default 0 @property {Number} heightPadding for menu pane. */ menuPaneHeightPadding: 0, /** The amount of space to add to the calculated width of the menu item strings to determine the width of the menu pane. */ menuItemPadding: 35, /** Disable context menu. */ isContextMenuEnabled: NO, /** Left Alignment based on the size of the button @private */ leftAlign: function() { var val = 0, controlSize = this.get('controlSize') ; // what. the. heck? // no, I don't want to shift the menu to the left. Yet. if(controlSize === SC.SMALL_CONTROL_SIZE) val = -14 ; if(controlSize === SC.REGULAR_CONTROL_SIZE) val = -16 ; return val; }.property('controlSize'), /** override this method to implement your own sorting of the menu. By default, menu items are sorted using the value shown or the sortKey @param{SC.Array} objects the unsorted array of objects to display. @returns sorted array of objects */ sortObjects: function(objects) { if(!this.get('disableSort')){ var nameKey = this.get('itemSortKey') || this.get('itemTitleKey') ; objects = objects.sort(function(a,b) { if (nameKey) { a = a.get ? a.get(nameKey) : a[nameKey] ; b = b.get ? b.get(nameKey) : b[nameKey] ; } return (ab) ? 1 : 0) ; }) ; } return objects ; }, /** render method @private */ render: function(context,firstTime) { sc_super(); var layoutWidth, items, len, nameKey, iconKey, valueKey, separatorKey, showCheckbox, currentSelectedVal, shouldLocalize, isSeparator, itemList, isChecked, idx, name, icon, value, item; items = this.get('items') ; items = this.sortObjects(items) ; len = items.length ; //Get the namekey, iconKey and valueKey set by the user nameKey = this.get('itemTitleKey') ; iconKey = this.get('itemIconKey') ; valueKey = this.get('itemValueKey') ; separatorKey = this.get('itemSeparatorKey'); showCheckbox = this.get('showCheckbox') ; //get the current selected value currentSelectedVal = this.get('value') ; // get the localization flag. shouldLocalize = this.get('localize') ; //itemList array to set the menu items itemList = [] ; //to set the 'checkbox' property of menu items isChecked = YES ; //index for finding the first item in the list idx = 0 ; items.forEach(function(object) { if (object) { //Get the name value. If value key is not specified convert obj //to string name = nameKey ? (object.get ? object.get(nameKey) : object[nameKey]) : object.toString() ; // localize name if specified. name = shouldLocalize? name.loc() : name ; //Get the icon value icon = iconKey ? (object.get ? object.get(iconKey) : object[iconKey]) : null ; if (SC.none(object[iconKey])) icon = null ; // get the value using the valueKey or the object value = (valueKey) ? (object.get ? object.get(valueKey) : object[valueKey]) : object ; if (!SC.none(currentSelectedVal) && !SC.none(value)){ if( currentSelectedVal === value ) { this.set('title', name) ; this.set('icon', icon) ; } } //Check if the item is currentSelectedItem or not if(value === this.get('value')) { //set the _itemIdx - To change the prefMatrix accordingly. this.set('_itemIdx', idx) ; isChecked = !showCheckbox ? NO : YES ; } else { isChecked = NO ; } // get the separator isSeparator = separatorKey ? (object.get ? object.get(separatorKey) : object[separatorKey]) : NO; //Set the first item from the list as default selected item if (idx === 0) { this._defaultVal = value ; this._defaultTitle = name ; this._defaultIcon = icon ; } var item = SC.Object.create({ separator: isSeparator, title: name, icon: icon, value: value, isEnabled: YES, checkbox: isChecked, action: this.displaySelectedItem }) ; //Set the items in the itemList array itemList.push(item); } idx += 1 ; this.set('_itemList', itemList) ; }, this ) ; if(firstTime) { this.invokeLast(function() { var value = this.get('value') ; if(SC.none(value)) { this.set('value', this._defaultVal) ; this.set('title', this._defaultTitle) ; this.set('icon', this._defaultIcon) ; } }); } //Set the preference matrix for the menu pane this.changeSelectButtonPreferMatrix(this._itemIdx) ; }, /** Button action handler @private @param {DOMMouseEvent} evt mouseup event that triggered the action */ _action: function( evt ) { var buttonLabel, menuWidth, scrollWidth, lastMenuWidth, offsetWidth, items, elementOffsetWidth, largestMenuWidth, item, element, idx, currSel, itemList, menuControlSize, menuHeightPadding, customView, customMenuView, menu, itemsLength; buttonLabel = this.$('.sc-button-label')[0] ; // Get the length of the text on the button in pixels menuWidth = this.get('layer').offsetWidth ; scrollWidth = buttonLabel.scrollWidth ; lastMenuWidth = this.get('lastMenuWidth') ; if(scrollWidth) { // Get the original width of the label in the button offsetWidth = buttonLabel.offsetWidth ; if(scrollWidth && offsetWidth) { menuWidth = menuWidth + scrollWidth - offsetWidth ; } } if (!lastMenuWidth || (menuWidth > lastMenuWidth)) { lastMenuWidth = menuWidth ; } items = this.get('_itemList') ; var customViewClassName = this.get('customViewClassName') ; var customViewMenuOffsetWidth = this.get('customViewMenuOffsetWidth') ; var className = 'sc-view sc-pane sc-panel sc-palette sc-picker sc-menu select-button sc-scroll-view sc-menu-scroll-view sc-container-view menuContainer sc-button-view sc-menu-item sc-regular-size' ; className = customViewClassName ? (className + ' ' + customViewClassName) : className ; SC.prepareStringMeasurement("", className); for (idx = 0, itemsLength = items.length; idx < itemsLength; ++idx) { //getting the width of largest menu item item = items.objectAt(idx) ; elementOffsetWidth = SC.measureString(item.title).width; if (!largestMenuWidth || (elementOffsetWidth > largestMenuWidth)) { largestMenuWidth = elementOffsetWidth ; } } SC.teardownStringMeasurement(); lastMenuWidth = (largestMenuWidth + this.menuItemPadding > lastMenuWidth) ? largestMenuWidth + this.menuItemPadding : lastMenuWidth ; // Get the window size width and compare with the lastMenuWidth. // If it is greater than windows width then reduce the maxwidth by 25px // so that the ellipsis property is enabled by default var maxWidth = SC.RootResponder.responder.get('currentWindowSize').width; if(lastMenuWidth > maxWidth) { lastMenuWidth = (maxWidth - 25) ; } this.set('lastMenuWidth',lastMenuWidth) ; currSel = this.get('_currentSelItem') ; itemList = this.get('_itemList') ; menuControlSize = this.get('controlSize') ; menuHeightPadding = this.get('menuPaneHeightPadding') ; // get the user defined custom view customView = this.get('exampleView') ; customMenuView = customView ? customView : SC.MenuItemView ; menu = SC.MenuPane.create({ /** Class name - select-button-item */ classNames: ['select-button'], /** The menu items are set from the itemList property of SelectButton @property */ items: itemList, /** Example view which will be used to create the Menu Items @default SC.MenuItemView @type SC.View */ exampleView: customMenuView, /** This property enables all the items and makes them selectable. @property */ isEnabled: YES, menuHeightPadding: menuHeightPadding, preferType: SC.PICKER_MENU, itemHeightKey: 'height', layout: { width: lastMenuWidth }, controlSize: menuControlSize, itemWidth: lastMenuWidth, contentView: SC.View.extend({ }) }) ; // no menu to toggle... bail... if (!menu) return NO ; menu.popup(this, this.preferMatrix) ; menu.set('currentSelectedMenuItem', currSel) ; return YES ; }, /** Action method for the select button menu items */ displaySelectedItem: function() { var menuView, currSel, itemViews, title, val, itemIdx = 0, button, object, len, found = null, objTmp; //Get MenuPane, currentSelectedMenuItem & menuItemView // Get the main parent view to show the menus menuView = this.parentMenu() ; currSel = menuView.get('currentSelectedMenuItem') ; itemViews = menuView.menuItemViews ; // Fetch the index of the current selected item if (currSel && itemViews) { itemIdx = itemViews.indexOf(currSel) ; } // Get the select button View button = menuView.get('anchor') ; // set the value and title object = menuView.get('items') ; len = object.length ; while (!found && (--len >= 0)) { objTmp = object[len]; title = !SC.none(objTmp.title) ? objTmp.title: object.toString() ; val = !SC.none(objTmp.value) ? objTmp.value: title ; if (title === this.get('value') && (itemIdx === len)) { found = object ; button.set('value', val) ; button.set('title', title) ; } } // set the icon, currentSelectedItem and itemIdx button.set('icon', this.get('icon')).set('_currentSelItem', currSel). set('_itemIdx', itemIdx) ; }, /** Set the "top" attribute in the prefer matrix property which will position menu such that the selected item in the menu will be place aligned to the item on the button when menu is opened. */ changeSelectButtonPreferMatrix: function() { var preferMatrixAttributeTop = 0 , itemIdx = this.get('_itemIdx') , leftAlign = this.get('leftAlign'), defPreferMatrix, tempPreferMatrix ; if(this.get('positionMenuBelow')) { defPreferMatrix = [leftAlign, 4, 3] ; this.set('preferMatrix', defPreferMatrix) ; } else { if(itemIdx) { preferMatrixAttributeTop = itemIdx * this.CUSTOM_MENU_ITEM_HEIGHT ; } tempPreferMatrix = [leftAlign, -preferMatrixAttributeTop, 2] ; this.set('preferMatrix', tempPreferMatrix) ; } }, /** @private Holding down the button should display the menu pane. */ mouseDown: function(evt) { if (!this.get('isEnabled')) return YES ; // handled event, but do nothing this.set('isActive', YES); this._isMouseDown = YES; this.becomeFirstResponder() ; this._action() ; return YES ; }, /** @private Handle Key event - Down arrow key */ keyDown: function(event) { if ( this.interpretKeyEvents(event) ) { return YES; } else { arguments.callee.base.apply(this,arguments); } }, /** @private Pressing the Up or Down arrow key should display the menu pane */ interpretKeyEvents: function(event) { if (event) { if ((event.keyCode === 38 || event.keyCode === 40)) { this._action() ; } else if (event.keyCode === 27) { this.resignFirstResponder() ; } } return arguments.callee.base.apply(this,arguments); } }) ;