]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/debian/missing-sources/framework/Web/Javascripts/source/tinymce-405/classes/ui/MenuButton.js
baculum: Add missing-sources directory in debian metadata structure
[bacula/bacula] / gui / baculum / debian / missing-sources / framework / Web / Javascripts / source / tinymce-405 / classes / ui / MenuButton.js
1 /**
2  * MenuButton.js
3  *
4  * Copyright, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /**
12  * Creates a new menu button.
13  *
14  * @-x-less MenuButton.less
15  * @class tinymce.ui.MenuButton
16  * @extends tinymce.ui.Button
17  */
18 define("tinymce/ui/MenuButton", [
19         "tinymce/ui/Button",
20         "tinymce/ui/Factory",
21         "tinymce/ui/MenuBar"
22 ], function(Button, Factory, MenuBar) {
23         "use strict";
24
25         // TODO: Maybe add as some global function
26         function isChildOf(node, parent) {
27                 while (node) {
28                         if (parent === node) {
29                                 return true;
30                         }
31
32                         node = node.parentNode;
33                 }
34
35                 return false;
36         }
37
38         var MenuButton = Button.extend({
39                 /**
40                  * Constructs a instance with the specified settings.
41                  *
42                  * @constructor
43                  * @param {Object} settings Name/value object with settings.
44                  */
45                 init: function(settings) {
46                         var self = this;
47
48                         self._renderOpen = true;
49                         self._super(settings);
50
51                         self.addClass('menubtn');
52
53                         if (settings.fixedWidth) {
54                                 self.addClass('fixed-width');
55                         }
56
57                         self.aria('haspopup', true);
58                         self.hasPopup = true;
59                 },
60
61                 /**
62                  * Shows the menu for the button.
63                  *
64                  * @method showMenu
65                  */
66                 showMenu: function() {
67                         var self = this, settings = self.settings, menu;
68
69                         if (self.menu && self.menu.visible()) {
70                                 return self.hideMenu();
71                         }
72
73                         if (!self.menu) {
74                                 menu = settings.menu || [];
75
76                                 // Is menu array then auto constuct menu control
77                                 if (menu.length) {
78                                         menu = {
79                                                 type: 'menu',
80                                                 items: menu
81                                         };
82                                 } else {
83                                         menu.type = menu.type || 'menu';
84                                 }
85
86                                 self.menu = Factory.create(menu).parent(self).renderTo(self.getContainerElm());
87                                 self.fire('createmenu');
88                                 self.menu.reflow();
89                                 self.menu.on('cancel', function(e) {
90                                         if (e.control === self.menu) {
91                                                 self.focus();
92                                         }
93                                 });
94
95                                 self.menu.on('show hide', function(e) {
96                                         if (e.control == self.menu) {
97                                                 self.activeMenu(e.type == 'show');
98                                         }
99                                 }).fire('show');
100
101                                 self.aria('expanded', true);
102                         }
103
104                         self.menu.show();
105                         self.menu.layoutRect({w: self.layoutRect().w});
106                         self.menu.moveRel(self.getEl(), ['bl-tl', 'tl-bl']);
107                 },
108
109                 /**
110                  * Hides the menu for the button.
111                  *
112                  * @method hideMenu
113                  */
114                 hideMenu: function() {
115                         var self = this;
116
117                         if (self.menu) {
118                                 self.menu.items().each(function(item) {
119                                         if (item.hideMenu) {
120                                                 item.hideMenu();
121                                         }
122                                 });
123
124                                 self.menu.hide();
125                                 self.aria('expanded', false);
126                         }
127                 },
128
129                 /**
130                  * Sets the active menu state.
131                  *
132                  * @private
133                  */
134                 activeMenu: function(state) {
135                         this.toggleClass('active', state);
136                 },
137
138                 /**
139                  * Renders the control as a HTML string.
140                  *
141                  * @method renderHtml
142                  * @return {String} HTML representing the control.
143                  */
144                 renderHtml: function() {
145                         var self = this, id = self._id, prefix = self.classPrefix;
146                         var icon = self.settings.icon ? prefix + 'ico ' + prefix + 'i-' + self.settings.icon : '';
147
148                         self.aria('role', self.parent() instanceof MenuBar ? 'menuitem' : 'button');
149
150                         return (
151                                 '<div id="' + id + '" class="' + self.classes() + '" tabindex="-1">' +
152                                         '<button id="' + id + '-open" role="presentation" type="button" tabindex="-1">' +
153                                                 (icon ? '<i class="' + icon + '"></i>' : '') +
154                                                 '<span>' + (self._text ? (icon ? ' ' : '') + self.encode(self._text) : '') + '</span>' +
155                                                 ' <i class="' + prefix + 'caret"></i>' +
156                                         '</button>' +
157                                 '</div>'
158                         );
159                 },
160
161                 /**
162                  * Gets invoked after the control has been rendered.
163                  *
164                  * @method postRender
165                  */
166                 postRender: function() {
167                         var self = this;
168
169                         self.on('click', function(e) {
170                                 if (e.control === self && isChildOf(e.target, self.getEl())) {
171                                         self.showMenu();
172
173                                         if (e.keyboard) {
174                                                 self.menu.items()[0].focus();
175                                         }
176                                 }
177                         });
178
179                         self.on('mouseenter', function(e) {
180                                 var overCtrl = e.control, parent = self.parent(), hasVisibleSiblingMenu;
181
182                                 if (overCtrl && parent && overCtrl instanceof MenuButton && overCtrl.parent() == parent) {
183                                         parent.items().filter('MenuButton').each(function(ctrl) {
184                                                 if (ctrl.hideMenu && ctrl != overCtrl) {
185                                                         if (ctrl.menu && ctrl.menu.visible()) {
186                                                                 hasVisibleSiblingMenu = true;
187                                                         }
188
189                                                         ctrl.hideMenu();
190                                                 }
191                                         });
192
193                                         if (hasVisibleSiblingMenu) {
194                                                 overCtrl.focus(); // Fix for: #5887
195                                                 overCtrl.showMenu();
196                                         }
197                                 }
198                         });
199
200                         return self._super();
201                 },
202
203                 /**
204                  * Sets/gets the current button text.
205                  *
206                  * @method text
207                  * @param {String} [text] New button text.
208                  * @return {String|tinymce.ui.MenuButton} Current text or current MenuButton instance.
209                  */
210                 text: function(text) {
211                         var self = this, i, children;
212
213                         if (self._rendered) {
214                                 children = self.getEl('open').getElementsByTagName('span');
215                                 for (i = 0; i < children.length; i++) {
216                                         children[i].innerHTML = self.encode(text);
217                                 }
218                         }
219
220                         return this._super(text);
221                 },
222
223                 /**
224                  * Removes the control and it's menus.
225                  *
226                  * @method remove
227                  */
228                 remove: function() {
229                         this._super();
230
231                         if (this.menu) {
232                                 this.menu.remove();
233                         }
234                 }
235         });
236
237         return MenuButton;
238 });