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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* MenuButton.js
*
* Copyright 2009, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://tinymce.moxiecode.com/license
* Contributing: http://tinymce.moxiecode.com/contributing
*/
(function(tinymce) {
var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each;
/**
* This class is used to create a UI button. A button is basically a link
* that is styled to look like a button or icon.
*
* @class tinymce.ui.MenuButton
* @extends tinymce.ui.Control
* @example
* // Creates a new plugin class and a custom menu button
* tinymce.create('tinymce.plugins.ExamplePlugin', {
* createControl: function(n, cm) {
* switch (n) {
* case 'mymenubutton':
* var c = cm.createSplitButton('mysplitbutton', {
* title : 'My menu button',
* image : 'some.gif'
* });
*
* c.onRenderMenu.add(function(c, m) {
* m.add({title : 'Some title', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
*
* m.add({title : 'Some item 1', onclick : function() {
* alert('Some item 1 was clicked.');
* }});
*
* m.add({title : 'Some item 2', onclick : function() {
* alert('Some item 2 was clicked.');
* }});
* });
*
* // Return the new menubutton instance
* return c;
* }
*
* return null;
* }
* });
*/
tinymce.create('tinymce.ui.MenuButton:tinymce.ui.Button', {
/**
* Constructs a new split button control instance.
*
* @constructor
* @method MenuButton
* @param {String} id Control id for the split button.
* @param {Object} s Optional name/value settings object.
* @param {Editor} ed Optional the editor instance this button is for.
*/
MenuButton : function(id, s, ed) {
this.parent(id, s, ed);
/**
* Fires when the menu is rendered.
*
* @event onRenderMenu
*/
this.onRenderMenu = new tinymce.util.Dispatcher(this);
s.menu_container = s.menu_container || DOM.doc.body;
},
/**
* Shows the menu.
*
* @method showMenu
*/
showMenu : function() {
var t = this, p1, p2, e = DOM.get(t.id), m;
if (t.isDisabled())
return;
if (!t.isMenuRendered) {
t.renderMenu();
t.isMenuRendered = true;
}
if (t.isMenuVisible)
return t.hideMenu();
p1 = DOM.getPos(t.settings.menu_container);
p2 = DOM.getPos(e);
m = t.menu;
m.settings.offset_x = p2.x;
m.settings.offset_y = p2.y;
m.settings.vp_offset_x = p2.x;
m.settings.vp_offset_y = p2.y;
m.settings.keyboard_focus = t._focused;
m.showMenu(0, e.clientHeight);
Event.add(DOM.doc, 'mousedown', t.hideMenu, t);
t.setState('Selected', 1);
t.isMenuVisible = 1;
},
/**
* Renders the menu to the DOM.
*
* @method renderMenu
*/
renderMenu : function() {
var t = this, m;
m = t.settings.control_manager.createDropMenu(t.id + '_menu', {
menu_line : 1,
'class' : this.classPrefix + 'Menu',
icons : t.settings.icons
});
m.onHideMenu.add(function() {
t.hideMenu();
t.focus();
});
t.onRenderMenu.dispatch(t, m);
t.menu = m;
},
/**
* Hides the menu. The optional event parameter is used to check where the event occurred so it
* doesn't close them menu if it was a event inside the menu.
*
* @method hideMenu
* @param {Event} e Optional event object.
*/
hideMenu : function(e) {
var t = this;
// Prevent double toogles by canceling the mouse click event to the button
if (e && e.type == "mousedown" && DOM.getParent(e.target, function(e) {return e.id === t.id || e.id === t.id + '_open';}))
return;
if (!e || !DOM.getParent(e.target, '.mceMenu')) {
t.setState('Selected', 0);
Event.remove(DOM.doc, 'mousedown', t.hideMenu, t);
if (t.menu)
t.menu.hideMenu();
}
t.isMenuVisible = 0;
},
/**
* Post render handler. This function will be called after the UI has been
* rendered so that events can be added.
*
* @method postRender
*/
postRender : function() {
var t = this, s = t.settings;
Event.add(t.id, 'click', function() {
if (!t.isDisabled()) {
if (s.onclick)
s.onclick(t.value);
t.showMenu();
}
});
}
});
})(tinymce);