]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/ui/Menu.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / ui / Menu.js
1 /**
2  * Menu.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function(tinymce) {
12         var is = tinymce.is, DOM = tinymce.DOM, each = tinymce.each, walk = tinymce.walk;
13
14         /**
15          * This class is base class for all menu types like DropMenus etc. This class should not
16          * be instantiated directly other menu controls should inherit from this one.
17          *
18          * @class tinymce.ui.Menu
19          * @extends tinymce.ui.MenuItem
20          */
21         tinymce.create('tinymce.ui.Menu:tinymce.ui.MenuItem', {
22                 /**
23                  * Constructs a new button control instance.
24                  *
25                  * @constructor
26                  * @method Menu
27                  * @param {String} id Button control id for the button.
28                  * @param {Object} s Optional name/value settings object.
29                  */
30                 Menu : function(id, s) {
31                         var t = this;
32
33                         t.parent(id, s);
34                         t.items = {};
35                         t.collapsed = false;
36                         t.menuCount = 0;
37                         t.onAddItem = new tinymce.util.Dispatcher(this);
38                 },
39
40                 /**
41                  * Expands the menu, this will show them menu and all menu items.
42                  *
43                  * @method expand
44                  * @param {Boolean} d Optional deep state. If this is set to true all children will be expanded as well.
45                  */
46                 expand : function(d) {
47                         var t = this;
48
49                         if (d) {
50                                 walk(t, function(o) {
51                                         if (o.expand)
52                                                 o.expand();
53                                 }, 'items', t);
54                         }
55
56                         t.collapsed = false;
57                 },
58
59                 /**
60                  * Collapses the menu, this will hide the menu and all menu items.
61                  *
62                  * @method collapse
63                  * @param {Boolean} d Optional deep state. If this is set to true all children will be collapsed as well.
64                  */
65                 collapse : function(d) {
66                         var t = this;
67
68                         if (d) {
69                                 walk(t, function(o) {
70                                         if (o.collapse)
71                                                 o.collapse();
72                                 }, 'items', t);
73                         }
74
75                         t.collapsed = true;
76                 },
77
78                 /**
79                  * Returns true/false if the menu has been collapsed or not.
80                  *
81                  * @method isCollapsed
82                  * @return {Boolean} True/false state if the menu has been collapsed or not.
83                  */
84                 isCollapsed : function() {
85                         return this.collapsed;
86                 },
87
88                 /**
89                  * Adds a new menu, menu item or sub classes of them to the drop menu.
90                  *
91                  * @method add
92                  * @param {tinymce.ui.Control} o Menu or menu item to add to the drop menu.
93                  * @return {tinymce.ui.Control} Same as the input control, the menu or menu item.
94                  */
95                 add : function(o) {
96                         if (!o.settings)
97                                 o = new tinymce.ui.MenuItem(o.id || DOM.uniqueId(), o);
98
99                         this.onAddItem.dispatch(this, o);
100
101                         return this.items[o.id] = o;
102                 },
103
104                 /**
105                  * Adds a menu separator between the menu items.
106                  *
107                  * @method addSeparator
108                  * @return {tinymce.ui.MenuItem} Menu item instance for the separator.
109                  */
110                 addSeparator : function() {
111                         return this.add({separator : true});
112                 },
113
114                 /**
115                  * Adds a sub menu to the menu.
116                  *
117                  * @method addMenu
118                  * @param {Object} o Menu control or a object with settings to be created into an control.
119                  * @return {tinymce.ui.Menu} Menu control instance passed in or created.
120                  */
121                 addMenu : function(o) {
122                         if (!o.collapse)
123                                 o = this.createMenu(o);
124
125                         this.menuCount++;
126
127                         return this.add(o);
128                 },
129
130                 /**
131                  * Returns true/false if the menu has sub menus or not.
132                  *
133                  * @method hasMenus
134                  * @return {Boolean} True/false state if the menu has sub menues or not.
135                  */
136                 hasMenus : function() {
137                         return this.menuCount !== 0;
138                 },
139
140                 /**
141                  * Removes a specific sub menu or menu item from the menu.
142                  *
143                  * @method remove
144                  * @param {tinymce.ui.Control} o Menu item or menu to remove from menu.
145                  * @return {tinymce.ui.Control} Control instance or null if it wasn't found.
146                  */
147                 remove : function(o) {
148                         delete this.items[o.id];
149                 },
150
151                 /**
152                  * Removes all menu items and sub menu items from the menu.
153                  *
154                  * @method removeAll
155                  */
156                 removeAll : function() {
157                         var t = this;
158
159                         walk(t, function(o) {
160                                 if (o.removeAll)
161                                         o.removeAll();
162                                 else
163                                         o.remove();
164
165                                 o.destroy();
166                         }, 'items', t);
167
168                         t.items = {};
169                 },
170
171                 /**
172                  * Created a new sub menu for the menu control.
173                  *
174                  * @method createMenu
175                  * @param {Object} s Optional name/value settings object.
176                  * @return {tinymce.ui.Menu} New drop menu instance.
177                  */
178                 createMenu : function(o) {
179                         var m = new tinymce.ui.Menu(o.id || DOM.uniqueId(), o);
180
181                         m.onAddItem.add(this.onAddItem.dispatch, this.onAddItem);
182
183                         return m;
184                 }
185         });
186 })(tinymce);