summaryrefslogtreecommitdiffstats
path: root/comm/suite/chatzilla/js/lib/menu-manager.js
blob: 6fd06868336a3dc001df0d2cccc5ad6194a1988c (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
function MenuManager(commandManager, menuSpecs, contextFunction, commandStr)
{
    var menuManager = this;

    this.commandManager = commandManager;
    this.menuSpecs = menuSpecs;
    this.contextFunction = contextFunction;
    this.commandStr = commandStr;
    this.repeatId = 0;
    this.cxStore = new Object();

    this.onPopupShowing =
        function mmgr_onshow(event) { return menuManager.showPopup(event); };
    this.onPopupHiding =
        function mmgr_onhide(event) { return menuManager.hidePopup(event); };
    this.onMenuCommand =
        function mmgr_oncmd(event) { return menuManager.menuCommand(event); };

    /* The code using us may override these with functions which will be called
     * after all our internal processing is done. Both are called with the
     * arguments 'event' (DOM), 'cx' (JS), 'popup' (DOM).
     */
    this.onCallbackPopupShowing = null;
    this.onCallbackPopupHiding = null;
}

MenuManager.prototype.appendMenuItems =
function mmgr_append(menuId, items)
{
    for (var i = 0; i < items.length; ++i)
        this.menuSpecs[menuId].items.push(items[i]);
}

MenuManager.prototype.createContextMenus =
function mmgr_initcxs (document)
{
    for (var id in this.menuSpecs)
    {
        if (id.indexOf("context:") == 0)
            this.createContextMenu(document, id);
    }
}

MenuManager.prototype.createContextMenu =
function mmgr_initcx (document, id)
{
    if (!document.getElementById(id))
    {
        if (!ASSERT(id in this.menuSpecs, "unknown context menu " + id))
            return;

        var dp = document.getElementById("dynamic-popups");
        var popup = this.appendPopupMenu (dp, null, id, id);
        var items = this.menuSpecs[id].items;
        this.createMenuItems (popup, null, items);

        if (!("uiElements" in this.menuSpecs[id]))
            this.menuSpecs[id].uiElements = [popup];
        else if (!arrayContains(this.menuSpecs[id].uiElements, popup))
            this.menuSpecs[id].uiElements.push(popup);
    }
}


MenuManager.prototype.createMenus =
function mmgr_createtb(document, menuid)
{
    var menu = document.getElementById(menuid);
    for (var id in this.menuSpecs)
    {
        var domID;
        if ("domID" in this.menuSpecs[id])
            domID = this.menuSpecs[id].domID;
        else
            domID = id;

        if (id.indexOf(menuid + ":") == 0)
            this.createMenu(menu, null, id, domID);
    }
}

MenuManager.prototype.createMainToolbar =
function mmgr_createtb(document, id)
{
    var toolbar = document.getElementById(id);
    var spec = this.menuSpecs[id];
    for (var i in spec.items)
    {
        this.appendToolbarItem (toolbar, null, spec.items[i]);
    }

    toolbar.className = "toolbar-primary chromeclass-toolbar";
}

MenuManager.prototype.updateMenus =
function mmgr_updatemenus(document, menus)
{
    // Cope with one string (update just the one menu)...
    if (isinstance(menus, String))
    {
        menus = [menus];
    }
    // Or nothing/nonsense (update everything).
    else if ((typeof menus != "object") || !isinstance(menus, Array))
    {
        menus = [];
        for (var k in this.menuSpecs)
        {
            if ((/^(mainmenu|context)/).test(k))
                menus.push(k);
        }
    }

    var menuBar = document.getElementById("mainmenu");

    // Loop through this array and update everything we need to.
    for (var i = 0; i < menus.length; i++)
    {
        var id = menus[i];
        if (!(id in this.menuSpecs))
            continue;
        var menu = this.menuSpecs[id];
        var domID;
        if ("domID" in this.menuSpecs[id])
            domID = this.menuSpecs[id].domID;
        else
            domID = id;

        // Context menus need to be deleted in order to be regenerated...
        if ((/^context/).test(id))
        {
            var cxMenuNode;
            if ((cxMenuNode = document.getElementById(id)))
                cxMenuNode.parentNode.removeChild(cxMenuNode);
            this.createContextMenu(document, id);
        }
        else if ((/^mainmenu/).test(id) &&
                 !("uiElements" in this.menuSpecs[id]))
        {
            this.createMenu(menuBar, null, id, domID);
            continue;
        }
        else if ((/^(mainmenu|popup)/).test(id) &&
                 ("uiElements" in this.menuSpecs[id]))
        {
            for (var j = 0; j < menu.uiElements.length; j++)
            {
                var node = menu.uiElements[j];
                domID = node.parentNode.id;
                // Clear the menu node.
                while (node.lastChild)
                    node.removeChild(node.lastChild);

                this.createMenu(node.parentNode.parentNode,
                                node.parentNode.nextSibling,
                                id, domID);
            }
        }
        
        
    }
}


/**
 * Internal use only.
 *
 * Registers event handlers on a given menu.
 */
MenuManager.prototype.hookPopup =
function mmgr_hookpop (node)
{
    node.addEventListener ("popupshowing", this.onPopupShowing, false);
    node.addEventListener ("popuphiding",  this.onPopupHiding, false);
}

/**
 * Internal use only.
 *
 * |showPopup| is called from the "onpopupshowing" event of menus managed
 * by the CommandManager. If a command is disabled, represents a command
 * that cannot be "satisfied" by the current command context |cx|, or has an
 * "enabledif" attribute that eval()s to false, then the menuitem is disabled.
 * In addition "checkedif" and "visibleif" attributes are eval()d and
 * acted upon accordingly.
 */
MenuManager.prototype.showPopup =
function mmgr_showpop (event)
{
    /* returns true if the command context has the properties required to
     * execute the command associated with |menuitem|.
     */
    function satisfied()
    {
        if (menuitem.hasAttribute("isSeparator") ||
            !menuitem.hasAttribute("commandname"))
        {
            return true;
        }

        if (menuitem.hasAttribute("repeatfor"))
            return false;

        if (!("menuManager" in cx))
        {
            dd ("no menuManager in cx");
            return false;
        }

        var name = menuitem.getAttribute("commandname");
        var commandManager = cx.menuManager.commandManager;
        var commands = commandManager.commands;

        if (!ASSERT (name in commands,
                     "menu contains unknown command '" + name + "'"))
        {
            return false;
        }

        var rv = commandManager.isCommandSatisfied(cx, commands[name]);
        delete cx.parseError;
        return rv;
    };

    /* Convenience function for "enabledif", etc, attributes. */
    function has (prop)
    {
        return (prop in cx);
    };

    /* evals the attribute named |attr| on the node |node|. */
    function evalIfAttribute (node, attr)
    {
        var ex;
        var expr = node.getAttribute(attr);
        if (!expr)
            return true;

        expr = expr.replace (/\Wand\W/gi, " && ");
        expr = expr.replace (/\Wor\W/gi, " || ");

        try
        {
            return eval("(" + expr + ")");
        }
        catch (ex)
        {
            dd ("caught exception evaling '" + node.getAttribute("id") + "'.'" +
                attr + "': '" + expr + "'\n" + ex);
        }
        return true;
    };

    /* evals the attribute named |attr| on the node |node|. */
    function evalAttribute(node, attr)
    {
        var ex;
        var expr = node.getAttribute(attr);
        if (!expr)
            return null;

        try
        {
            return eval(expr);
        }
        catch (ex)
        {
            dd ("caught exception evaling '" + node.getAttribute("id") + "'.'" +
                attr + "': '" + expr + "'\n" + ex);
        }
        return null;
    };

    var cx;
    var popup = event.originalTarget;
    var menuName = popup.getAttribute("menuName");

    /* If the host provided a |contextFunction|, use it now.  Remember the
     * return result as this.cx for use if something from this menu is actually
     * dispatched.  */
    if (typeof this.contextFunction == "function")
    {
        cx = this.cx = this.contextFunction(menuName, event);
    }
    else
    {
        cx = this.cx = { menuManager: this, originalEvent: event };
    }

    // Keep the context around by menu name. Removed in hidePopup.
    this.cxStore[menuName] = cx;

    var menuitem = popup.firstChild;
    do
    {
        if (!menuitem.hasAttribute("repeatfor"))
            continue;

        // Remove auto-generated items (located prior to real item).
        while (menuitem.previousSibling &&
               menuitem.previousSibling.hasAttribute("repeatgenerated"))
        {
            menuitem.parentNode.removeChild(menuitem.previousSibling);
        }

        if (!("repeatList" in cx))
            cx.repeatList = new Object();

        /* Get the array of new items to add by evaluating "repeatfor" with
         * "cx" in scope. Usually will return an already-calculated Array
         * either from "cx" or somewhere in the object model.
         */
        var ary = evalAttribute(menuitem, "repeatfor");

        if ((typeof ary != "object") || !isinstance(ary, Array))
            ary = [];

        /* The item itself should only be shown if there's no items in the
         * array - this base item is always disabled.
         */
        if (ary.length > 0)
            menuitem.setAttribute("hidden", "true");
        else
            menuitem.removeAttribute("hidden");

        // Save the array in the context object.
        cx.repeatList[menuitem.getAttribute("repeatid")] = ary;

        /* Get the maximum number of items we're allowed to show from |ary| by
         * evaluating "repeatlimit" with "cx" in scope. This could be a fixed
         * limit or dynamically calculated (e.g. from prefs).
         */
        var limit = evalAttribute(menuitem, "repeatlimit");
        // Make sure we've got a number at all...
        if (typeof limit != "number")
            limit = ary.length;
        // ...and make sure it's no higher than |ary.length|.
        limit = Math.min(ary.length, limit);

        var cmd = menuitem.getAttribute("commandname");
        var props = { repeatgenerated: true, repeatindex: -1,
                      repeatid: menuitem.getAttribute("repeatid"),
                      repeatmap: menuitem.getAttribute("repeatmap") };

        /* Clone non-repeat attributes. All attributes except those starting
         * with 'repeat', and those matching 'hidden' or 'disabled' are saved
         * to |props|, which is then supplied to |appendMenuItem| later.
         */
        for (var i = 0; i < menuitem.attributes.length; i++)
        {
            var name = menuitem.attributes[i].nodeName;
            if (!name.match(/^(repeat|(hidden|disabled)$)/))
                props[name] = menuitem.getAttribute(name);
        }

        var lastGroup = "";
        for (i = 0; i < limit; i++)
        {
            /* Check for groupings. For each item we add, if "repeatgroup" gives
             * a different value, we insert a separator.
             */
            if (menuitem.getAttribute("repeatgroup"))
            {
                cx.index = i;
                ary = cx.repeatList[menuitem.getAttribute("repeatid")];
                var item = ary[i];
                /* Apply any updates to "cx" for this item by evaluating
                 * "repeatmap" with "cx" and "item" in scope. This may just
                 * copy some attributes from "item" to "cx" or it may do more.
                 */
                evalAttribute(menuitem, "repeatmap");
                /* Get the item's group by evaluating "repeatgroup" with "cx"
                 * and "item" in scope. Usually will return an appropriate
                 * property from "item".
                 */
                var group = evalAttribute(menuitem, "repeatgroup");

                if ((i > 0) && (lastGroup != group))
                    this.appendMenuSeparator(popup, menuitem, props);

                lastGroup = group;
            }

            props.repeatindex = i;
            this.appendMenuItem(popup, menuitem, cmd, props);
        }
    } while ((menuitem = menuitem.nextSibling));

    menuitem = popup.firstChild;
    do
    {
        if (menuitem.hasAttribute("repeatgenerated") &&
            menuitem.hasAttribute("repeatmap"))
        {
            cx.index = menuitem.getAttribute("repeatindex");
            ary = cx.repeatList[menuitem.getAttribute("repeatid")];
            var item = ary[cx.index];
            /* Apply any updates to "cx" for this item by evaluating
             * "repeatmap" with "cx" and "item" in scope. This may just
             * copy some attributes from "item" to "cx" or it may do more.
             */
            evalAttribute(menuitem, "repeatmap");
        }

        /* should it be visible? */
        if (menuitem.hasAttribute("visibleif"))
        {
            if (evalIfAttribute(menuitem, "visibleif"))
                menuitem.removeAttribute ("hidden");
            else
            {
                menuitem.setAttribute ("hidden", "true");
                continue;
            }
        }

        /* it's visible, maybe it has a dynamic label? */
        if (menuitem.hasAttribute("format"))
        {
            var label = replaceVars(menuitem.getAttribute("format"), cx);
            if (label.indexOf("\$") != -1)
                label = menuitem.getAttribute("backupLabel");
            menuitem.setAttribute("label", label);
        }

        /* ok, it's visible, maybe it should be disabled? */
        if (satisfied())
        {
            if (menuitem.hasAttribute("enabledif"))
            {
                if (evalIfAttribute(menuitem, "enabledif"))
                    menuitem.removeAttribute ("disabled");
                else
                    menuitem.setAttribute ("disabled", "true");
            }
            else
                menuitem.removeAttribute ("disabled");
        }
        else
        {
            menuitem.setAttribute ("disabled", "true");
        }

        /* should it have a check? */
        if (menuitem.hasAttribute("checkedif"))
        {
            if (evalIfAttribute(menuitem, "checkedif"))
                menuitem.setAttribute ("checked", "true");
            else
                menuitem.removeAttribute ("checked");
        }
    } while ((menuitem = menuitem.nextSibling));

    if (typeof this.onCallbackPopupShowing == "function")
        this.onCallbackPopupShowing(event, cx, popup);

    return true;
}

/**
 * Internal use only.
 *
 * |hidePopup| is called from the "onpopuphiding" event of menus
 * managed by the CommandManager.  Clean up this.cxStore, but
 * not this.cx because that messes up nested menus.
 */
MenuManager.prototype.hidePopup =
function mmgr_hidepop(event)
{
    var popup = event.originalTarget;
    var menuName = popup.getAttribute("menuName");

    if (typeof this.onCallbackPopupHiding == "function")
        this.onCallbackPopupHiding(event, this.cxStore[menuName], popup);

    delete this.cxStore[menuName];

    return true;
}

MenuManager.prototype.menuCommand =
function mmgr_menucmd(event)
{
    /* evals the attribute named |attr| on the node |node|. */
    function evalAttribute(node, attr)
    {
        var ex;
        var expr = node.getAttribute(attr);
        if (!expr)
            return null;

        try
        {
            return eval(expr);
        }
        catch (ex)
        {
            dd ("caught exception evaling '" + node.getAttribute("id") + "'.'" +
                attr + "': '" + expr + "'\n" + ex);
        }
        return null;
    };

    var menuitem = event.originalTarget;
    var cx = this.cx;
    /* We need to re-run the repeat-map if the user has selected a special
     * repeat-generated menu item, so that the context object is correct.
     */
    if (menuitem.hasAttribute("repeatgenerated") &&
        menuitem.hasAttribute("repeatmap"))
    {
        cx.index = menuitem.getAttribute("repeatindex");
        var ary = cx.repeatList[menuitem.getAttribute("repeatid")];
        var item = ary[cx.index];
        /* Apply any updates to "cx" for this item by evaluating
         * "repeatmap" with "cx" and "item" in scope. This may just
         * copy some attributes from "item" to "cx" or it may do more.
         */
        evalAttribute(menuitem, "repeatmap");
    }

    eval(this.commandStr);
};


/**
 * Appends a sub-menu to an existing menu.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param domId       ID of the sub-menu to add.
 * @param label       Text to use for this sub-menu.
 * @param accesskey   Accesskey to use for the sub-menu.
 * @param attribs     Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendSubMenu =
function mmgr_addsmenu(parentNode, beforeNode, menuName, domId, label,
                       accesskey, attribs)
{
    var document = parentNode.ownerDocument;

    /* sometimes the menu is already there, for overlay purposes. */
    var menu = document.getElementById(domId);

    if (!menu)
    {
        menu = document.createElement ("menu");
        menu.setAttribute ("id", domId);
    }

    var menupopup = menu.firstChild;

    if (!menupopup)
    {
        menupopup = document.createElement ("menupopup");
        menupopup.setAttribute ("id", domId + "-popup");
        menu.appendChild(menupopup);
        menupopup = menu.firstChild;
    }

    menupopup.setAttribute ("menuName", menuName);

    menu.setAttribute("accesskey", accesskey);
    label = label.replace("&", "");
    menu.setAttribute ("label", label);
    menu.setAttribute ("isSeparator", true);

    // Only attach the menu if it's not there already. This can't be in the
    // if (!menu) block because the updateMenus code clears toplevel menus,
    // orphaning the submenus, to (parts of?) which we keep handles in the
    // uiElements array. See the updateMenus code.
    if (!menu.parentNode)
        parentNode.insertBefore(menu, beforeNode);

    if (typeof attribs == "object")
    {
        for (var p in attribs)
            menu.setAttribute (p, attribs[p]);
    }

    this.hookPopup (menupopup);

    return menupopup;
}

/**
 * Appends a popup to an existing popupset.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param id      ID of the popup to add.
 * @param label   Text to use for this popup.  Popup menus don't normally have
 *                labels, but we set a "label" attribute anyway, in case
 *                the host wants it for some reason.  Any "&" characters will
 *                be stripped.
 * @param attribs Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendPopupMenu =
function mmgr_addpmenu (parentNode, beforeNode, menuName, id, label, attribs)
{
    var document = parentNode.ownerDocument;
    var popup = document.createElement ("menupopup");
    popup.setAttribute ("id", id);
    if (label)
        popup.setAttribute ("label", label.replace("&", ""));
    if (typeof attribs == "object")
    {
        for (var p in attribs)
            popup.setAttribute (p, attribs[p]);
    }

    popup.setAttribute ("menuName", menuName);

    parentNode.insertBefore(popup, beforeNode);
    this.hookPopup (popup);

    return popup;
}

/**
 * Appends a menuitem to an existing menu or popup.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param command A reference to the CommandRecord this menu item will represent.
 * @param attribs Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendMenuItem =
function mmgr_addmenu (parentNode, beforeNode, commandName, attribs)
{
    var menuManager = this;

    var document = parentNode.ownerDocument;
    if (commandName == "-")
        return this.appendMenuSeparator(parentNode, beforeNode, attribs);

    var parentId = parentNode.getAttribute("id");

    if (!ASSERT(commandName in this.commandManager.commands,
                "unknown command " + commandName + " targeted for " +
                parentId))
    {
        return null;
    }

    var command = this.commandManager.commands[commandName];
    var menuitem = document.createElement ("menuitem");
    menuitem.setAttribute ("id", parentId + ":" + commandName);
    menuitem.setAttribute ("commandname", command.name);
    // Add keys if this isn't a context menu:
    if (parentId.indexOf("context") != 0)
        menuitem.setAttribute("key", "key:" + command.name);
    menuitem.setAttribute("accesskey", command.accesskey);
    var label = command.label.replace("&", "");
    menuitem.setAttribute ("label", label);
    if (command.format)
    {
        menuitem.setAttribute("format", command.format);
        menuitem.setAttribute("backupLabel", label);
    }

    if ((typeof attribs == "object") && attribs)
    {
        for (var p in attribs)
            menuitem.setAttribute (p, attribs[p]);
        if ("repeatfor" in attribs)
            menuitem.setAttribute("repeatid", this.repeatId++);
    }

    command.uiElements.push(menuitem);
    parentNode.insertBefore (menuitem, beforeNode);
    /* It seems, bob only knows why, that this must be done AFTER the node is
     * added to the document.
     */
    menuitem.addEventListener("command", this.onMenuCommand, false);

    return menuitem;
}

/**
 * Appends a menuseparator to an existing menu or popup.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param attribs Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendMenuSeparator =
function mmgr_addsep (parentNode, beforeNode, attribs)
{
    var document = parentNode.ownerDocument;
    var menuitem = document.createElement ("menuseparator");
    menuitem.setAttribute ("isSeparator", true);
    if (typeof attribs == "object")
    {
        for (var p in attribs)
            menuitem.setAttribute (p, attribs[p]);
    }
    parentNode.insertBefore (menuitem, beforeNode);

    return menuitem;
}

/**
 * Appends a toolbaritem to an existing box element.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param command A reference to the CommandRecord this toolbaritem will
 *                represent.
 * @param attribs Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendToolbarItem =
function mmgr_addtb (parentNode, beforeNode, commandName, attribs)
{
    if (commandName == "-")
        return this.appendToolbarSeparator(parentNode, beforeNode, attribs);

    var parentId = parentNode.getAttribute("id");

    if (!ASSERT(commandName in this.commandManager.commands,
                "unknown command " + commandName + " targeted for " +
                parentId))
    {
        return null;
    }

    var command = this.commandManager.commands[commandName];
    var document = parentNode.ownerDocument;
    var tbitem = document.createElement ("toolbarbutton");

    var id = parentNode.getAttribute("id") + ":" + commandName;
    tbitem.setAttribute ("id", id);
    tbitem.setAttribute ("class", "toolbarbutton-1");
    if (command.tip)
        tbitem.setAttribute ("tooltiptext", command.tip);
    tbitem.setAttribute ("label", command.label.replace("&", ""));
    tbitem.setAttribute ("oncommand",
                         "dispatch('" + commandName + "');");
    if (typeof attribs == "object")
    {
        for (var p in attribs)
            tbitem.setAttribute (p, attribs[p]);
    }

    command.uiElements.push(tbitem);
    parentNode.insertBefore (tbitem, beforeNode);

    return tbitem;
}

/**
 * Appends a toolbarseparator to an existing box.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param attribs Object containing CSS attributes to set on the element.
 */
MenuManager.prototype.appendToolbarSeparator =
function mmgr_addmenu (parentNode, beforeNode, attribs)
{
    var document = parentNode.ownerDocument;
    var tbitem = document.createElement ("toolbarseparator");
    tbitem.setAttribute ("isSeparator", true);
    if (typeof attribs == "object")
    {
        for (var p in attribs)
            tbitem.setAttribute (p, attribs[p]);
    }
    parentNode.appendChild (tbitem);

    return tbitem;
}

/**
 * Creates menu DOM nodes from a menu specification.
 * @param parentNode  DOM Node to insert into
 * @param beforeNode  DOM Node already contained by parentNode, to insert before
 * @param menuSpec    array of menu items
 */
MenuManager.prototype.createMenu =
function mmgr_newmenu (parentNode, beforeNode, menuName, domId, attribs)
{
    if (typeof domId == "undefined")
        domId = menuName;

    if (!ASSERT(menuName in this.menuSpecs, "unknown menu name " + menuName))
        return null;

    var menuSpec = this.menuSpecs[menuName];
    if (!("accesskey" in menuSpec))
        menuSpec.accesskey = getAccessKey(menuSpec.label);

    var subMenu = this.appendSubMenu(parentNode, beforeNode, menuName, domId,
                                     menuSpec.label, menuSpec.accesskey,
                                     attribs);

    // Keep track where we're adding popup nodes derived from some menuSpec
    if (!("uiElements" in this.menuSpecs[menuName]))
        this.menuSpecs[menuName].uiElements = [subMenu];
    else if (!arrayContains(this.menuSpecs[menuName].uiElements, subMenu))
        this.menuSpecs[menuName].uiElements.push(subMenu);

    this.createMenuItems (subMenu, null, menuSpec.items);
    return subMenu;
}

MenuManager.prototype.createMenuItems =
function mmgr_newitems (parentNode, beforeNode, menuItems)
{
    function itemAttribs()
    {
        return (1 in menuItems[i]) ? menuItems[i][1] : null;
    };

    var parentId = parentNode.getAttribute("id");

    for (var i in menuItems)
    {
        var itemName = menuItems[i][0];
        if (itemName[0] == ">")
        {
            itemName = itemName.substr(1);
            if (!ASSERT(itemName in this.menuSpecs,
                        "unknown submenu " + itemName + " referenced in " +
                        parentId))
            {
                continue;
            }
            this.createMenu (parentNode, beforeNode, itemName,
                             parentId + ":" + itemName, itemAttribs());
        }
        else if (itemName in this.commandManager.commands)
        {
            this.appendMenuItem (parentNode, beforeNode, itemName,
                                 itemAttribs());
        }
        else if (itemName == "-")
        {
            this.appendMenuSeparator (parentNode, beforeNode, itemAttribs());
        }
        else
        {
            dd ("unknown command " + itemName + " referenced in " + parentId);
        }
    }
}