summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/widgets/Menu.js
blob: df53a2aeb89114b56337d0deb8d7923592dfe17f (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*  Authors:
 *    Petr Vobornik <pvoborni@redhat.com>
 *
 * Copyright (C) 2012 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define(['dojo/_base/declare',
        'dojo/_base/array',
        'dojo/_base/lang',
        'dojo/dom',
        'dojo/dom-construct',
        'dojo/dom-prop',
        'dojo/dom-class',
        'dojo/dom-style',
        'dojo/dom-attr',
        'dojo/query',
        'dojo/Evented',
        'dojo/on',
        '../jquery',
        '../ipa'], function(declare, array, lang, dom, construct, prop, dom_class,
                            dom_style, attr, query, Evented, on, $, IPA) {

    return declare([Evented], {
        /**
         * Creates UI for freeipa.navigation.menu. Provides an event when
         * a menu items is selected.
         *
         * @class widgets.Menu
         */

        /**
         * @event item-select(menu_item)
         */


        /**
         * Object store of menu items
         * @protected
         * @property {navigation.Menu}
         */
        menu: null,

        /**
         * dom_node of this widget. FIXME: move to superclass (none yet)
         * @property {HTMLElement}
         */
        dom_node: null,

        /**
         * Turns off update on data change
         * @property {boolean}
         */
        ignore_changes: false,

        /**
         * Css class for nodes containing a submenu of certain level_class
         * @property {string}
         */
        level_class: 'menu-level',

        /**
         * Renders widget's elements
         */
        render: function() {
            if (this.dom_node) {
                construct.empty(this.dom_node);
            }

            this.dom_node = this._render_children(null, null, this.dom_node, null, 1);

            return this.dom_node;
        },

        /**
         * Render submenu container of given level
         *
         * @protected
         * @param {Number} level submenu level
         */
        _render_level_container: function(level, parent) {

            var lvl_class = this._get_lvl_class(level);
            var type_cls = 'nav';
            if (level === 1) {
                type_cls = 'nav navbar-nav navbar-primary persistent-secondary';
            } else if (level === 2) {
                type_cls = 'nav navbar-nav navbar-persistent';
            } else {
                type_cls = 'dropdown-menu';
            }

            var cont = construct.create('ul', {
                'class': type_cls + ' ' + lvl_class
            });

            return cont;
        },

        /**
         * Render item and submenu to container
         */
        _render_item: function(menu_item, container, level) {

            var self = this;
            var click_handler = function(event) {
                if (event.defaultPrevented) return;
                self.item_clicked(menu_item, event);
                event.preventDefault();
            };

            var item_node = construct.create('li', {
                'data-name': menu_item.name,
                click: click_handler
            });
            var a_node = construct.create('a', {}, item_node);

            var children = this._get_children(menu_item);
            if (level > 1 && children.total > 0) {
                dom_class.add(item_node, 'dropdown-submenu');
                dom_class.add(a_node, 'dropdown-toggle');
                prop.set(item_node, 'onclick', undefined);
                attr.set(a_node, 'data-toggle', 'dropdown');
                attr.set(a_node, 'data-target', '#');
                $(a_node).dropdown();
            }

            this._update_item(menu_item, item_node);

             // create submenu
            this._render_children(menu_item, children, null, item_node, level + 1);

            construct.place(item_node, container);
        },

        /**
         * Render children of menu_item or top level items if menu_item is
         * null.
         *
         * @protected
         * @param {navigation.MenuItem|null} menu_item
         * @param {Object|null} children query result
         * @param {HTMLElement|null} item_container  container for children
         * @param {HTMLElement|null} container container for item_container
         * @param {number} level
         */
        _render_children: function(menu_item, children, item_container, container, level) {

            if (children === null) {
                children = this._get_children(menu_item);
            }

            if (!item_container) {
                item_container = this._render_level_container(level, container);
            }

            if (children.total > 0) {
                array.forEach(children, function(menu_item) {
                    this._render_item(menu_item, item_container, level);
                }, this);
            }

            if (container) {
                construct.place(item_container, container);
                // use jQuery resize to make use of window.resize throttling
                $(window).bind('resize', lang.hitch(this, function() {
                    this._adjust_size(container, item_container, level);
                }));
            }
            return item_container;
        },

        _get_children: function(menu_item) {
            var name = menu_item ? menu_item.name : null;
            var children = this.menu.items.query({ parent: name, hidden: false },
                                 { sort: [{attribute:'position'}]});
            return children;
        },

        _get_lvl_class: function(level) {
            return this.level_class + '-' + level;
        },

        /**
         * Updates content of li_node associated with menu_item base on
         * menu_item's state.
         *
         * @protected
         * @param {navigation.MenuItem|string} menu_item
         * @param {HTMLElement} [li_node]
         */
        _update_item: function(menu_item, li_node) {

            if (typeof menu_item === 'string') {
                menu_item = this.menu.items.get(menu_item);
            }

            if (!li_node) {
                li_node = query('li[data-name=\''+menu_item.name+'\']')[0];

                // Quit for non-existing nodes.
                // FIXME: maybe change to exception
                if (!li_node) return;
            }

            dom_class.toggle(li_node, 'disabled', !!menu_item.disabled);
            dom_class.toggle(li_node, 'active', menu_item.selected);
            dom_style.set(li_node, {
                display: menu_item.hidden ? 'none': 'default'
            });

            var a_node = query('a', li_node)[0];

            prop.set(a_node, 'href', '#' + menu_item.name);
            prop.set(a_node, 'textContent', menu_item.label);
            prop.set(a_node, 'title', menu_item.title || menu_item.label);
        },

        /**
         * Displays only supplied menu items.
         * @param {navigation.MenuItem[]} menu_items Items to show
         */
        select: function(menu_items) {

            // hide all except top level
            var exception = this._get_lvl_class(1);
            query('div.submenu', this.dom_node).forEach(function(submenu_node) {

                if (dom_class.contains(submenu_node, exception)) return;

                dom_style.set(submenu_node, {
                    display: 'none'
                });
            }, this);

            // show and update selected
            array.forEach(menu_items, function(item) {
                this._update_item(item);
            }, this);

            // to force adjusting of item sizes
            $(window).trigger('resize');
        },

        /**
         * Handles changes in this.menu object.
         *
         * @protected
         * @param {navigation.MenuItem} object
         * @param {number} removedFrom
         * @param {number} insertedInto
         */
        _items_changed: function(object, removedFrom, insertedInto) {

            if (this.ignore_changes) return;

            if (removedFrom === -1 && insertedInto === -1) {
                this._update_item(object);
            } else {
                // on add or removal, replace whole menu
                this.render();
                this.select(this.menu.selected);
            }
        },

        /**
         * Sets this.menu and starts to watch its changes
         * @param {navigation.Menu} menu
         */
        set_menu: function(menu) {
            this.menu = menu;
            //get all items
            var q = menu.items.query();
            q.observe(lang.hitch(this, this._items_changed), true);
            on(this.menu, 'selected', lang.hitch(this, function(event) {
                this.select(event.new_selection);
            }));
        },

        /**
         * Internal handler for clicking on menu item.
         * Raises item-select event.
         * @protected
         * @param {navigation.MenuItem} menu_items
         */
        _item_clicked: function(menu_item) {
            this.emit('item-select', menu_item);
        },

        /**
         * Handles click on menu item.
         *
         * Intended for overriding.
         *
         * @param {navigation.MenuItem} menu_item
         * @param {Event} event
         */
        item_clicked: function(menu_item/*, event*/) {
            this._item_clicked(menu_item);
        },

        /**
         * Adjust parent size according to child size
         * @param  {HTMLElement} parent parent menu item container
         * @param  {HTMLElement} child child menu item container
         * @param  {number} level level of the child menu item
         */
        _adjust_size: function(parent, child, level) {

            if (level !== 2) return;

            var child_height = dom_style.get(child, 'height');
            var absolute = dom_style.get(child, 'position') === 'absolute';
            if (child_height && absolute) {
                dom_style.set(parent, 'marginBottom', child_height+'px');
            }
        }
    });
});