/
menu_item.js
97 lines (81 loc) · 2.63 KB
/
menu_item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ========================================================================
// SproutCore
// copyright 2006-2008 Sprout Systems, Inc.
// ========================================================================
require('views/view') ;
/** @class
This is a basic menu item for inclusion in a popup menu. This is a type
of button that will automatically close the popup menu when it is
pressed.
@extends SC.ButtonView
*/
SC.MenuItemView = SC.ButtonView.extend({
emptyElement: [
'<li class="button menu-item">',
'<a href="javascript:;">',
'<span class="sel">✓</span>',
'<span class="mixed">-</span>',
'<span class="inner">',
'<span class="label"></span>',
'</span>',
'<span class="shortcut"></span>',
'</a>',
'</li>'].join(''),
// this method returns the computed required width. This is potentially
// expensive, so don't call it often. It is intended to be used with the
// wrapper MenuView that will decide how wide to make the menu.
computedRequiredWidth: function() {
var ret = 0;
// first, get the left edge offset for the .inner span.
// we expect this to make room for any checkboxes that might appear on
// the left and required spacing on the right.
var el = this.$sel('.inner') ;
if (el) {
ret = el.offsetLeft ;
ret += parseInt(Element.getStyle(el, 'padding-left'),0) ;
ret += parseInt(Element.getStyle(el, 'padding-right'),0) ;
}
// next, add in the width of any img tag.
var img = Element.$sel(el,'img') ;
if (img) {
ret += Element.getDimensions(img).width ;
}
// next add in the width of any label. We assume this includes the width
// of the label text itself.
el = Element.$sel(el, '.label') ;
if (el) {
ret += Element.getDimensions(el).width ;
}
// finally, add the width of any shortcut. We assume this includes any
// padding required to go between the label and the shortcut.
el = this.$sel('.shortcut') ;
if (el) {
ret += Element.getDimensions(el).width ;
}
// that should do it...
return ret ;
},
mouseMoved: function(evt)
{
if (!this.get('isDefault')) this.get('parentNode').set('currentSelectedMenuItem', this);
},
mouseOut: function(evt)
{
this.set('isDefault', false);
this.setClassName('active', false);
},
mouseUp: function(evt)
{
sc_super();
this._closeParentMenu();
},
didTriggerAction: function()
{
this._closeParentMenu();
},
_closeParentMenu: function()
{
var menu = this.get('parentNode');
if (menu) menu.set('isVisible', false);
}
});