1 | define(["dojo", "dijit", "dojox", "dijit/Menu","dijit/MenuItem"], function(dojo, dijit, dojox){ |
---|
2 | dojo.experimental("dojox.widget.PlaceholderMenuItem"); |
---|
3 | |
---|
4 | dojo.declare("dojox.widget.PlaceholderMenuItem", dijit.MenuItem, { |
---|
5 | // summary: |
---|
6 | // A menu item that can be used as a placeholder. Set the label |
---|
7 | // of this item to a unique key and you can then use it to add new |
---|
8 | // items at that location. This item is not displayed. |
---|
9 | |
---|
10 | _replaced: false, |
---|
11 | _replacedWith: null, |
---|
12 | _isPlaceholder: true, |
---|
13 | |
---|
14 | postCreate: function(){ |
---|
15 | this.domNode.style.display = "none"; |
---|
16 | this._replacedWith = []; |
---|
17 | if(!this.label){ |
---|
18 | this.label = this.containerNode.innerHTML; |
---|
19 | } |
---|
20 | this.inherited(arguments); |
---|
21 | }, |
---|
22 | |
---|
23 | replace: function(/*dijit.MenuItem[]*/ menuItems){ |
---|
24 | // summary: |
---|
25 | // replaces this menu item with the given menuItems. The original |
---|
26 | // menu item is not actually removed from the menu - so if you want |
---|
27 | // it removed, you must do that explicitly. |
---|
28 | // returns: |
---|
29 | // true if the replace happened, false if not |
---|
30 | if(this._replaced){ return false; } |
---|
31 | |
---|
32 | var index = this.getIndexInParent(); |
---|
33 | if(index < 0){ return false; } |
---|
34 | |
---|
35 | var p = this.getParent(); |
---|
36 | |
---|
37 | dojo.forEach(menuItems, function(item){ |
---|
38 | p.addChild(item, index++); |
---|
39 | }); |
---|
40 | this._replacedWith = menuItems; |
---|
41 | |
---|
42 | this._replaced = true; |
---|
43 | return true; |
---|
44 | }, |
---|
45 | |
---|
46 | unReplace: function(/*Boolean?*/ destroy){ |
---|
47 | // summary: |
---|
48 | // Removes menu items added by calling replace(). It returns the |
---|
49 | // array of items that were actually removed (in case you want to |
---|
50 | // clean them up later) |
---|
51 | // destroy: |
---|
52 | // Also call destroy on any removed items. |
---|
53 | // returns: |
---|
54 | // The array of items that were actually removed |
---|
55 | |
---|
56 | if(!this._replaced){ return []; } |
---|
57 | |
---|
58 | var p = this.getParent(); |
---|
59 | if(!p){ return []; } |
---|
60 | |
---|
61 | var r = this._replacedWith; |
---|
62 | dojo.forEach(this._replacedWith, function(item){ |
---|
63 | p.removeChild(item); |
---|
64 | if(destroy){ |
---|
65 | item.destroyRecursive(); |
---|
66 | } |
---|
67 | }); |
---|
68 | this._replacedWith = []; |
---|
69 | this._replaced = false; |
---|
70 | |
---|
71 | return r; // dijit.MenuItem[] |
---|
72 | } |
---|
73 | }); |
---|
74 | |
---|
75 | // Se need to extend dijit.Menu so that we have a getPlaceholders function. |
---|
76 | dojo.extend(dijit.Menu, { |
---|
77 | getPlaceholders: function(/*String?*/ label){ |
---|
78 | // summary: |
---|
79 | // Returns an array of placeholders with the given label. There |
---|
80 | // can be multiples. |
---|
81 | // label: |
---|
82 | // Label to search for - if not specified, then all placeholders |
---|
83 | // are returned |
---|
84 | // returns: |
---|
85 | // An array of placeholders that match the given label |
---|
86 | var r = []; |
---|
87 | |
---|
88 | var children = this.getChildren(); |
---|
89 | dojo.forEach(children, function(child){ |
---|
90 | if(child._isPlaceholder && (!label || child.label == label)){ |
---|
91 | r.push(child); |
---|
92 | }else if(child._started && child.popup && child.popup.getPlaceholders){ |
---|
93 | r = r.concat(child.popup.getPlaceholders(label)); |
---|
94 | }else if(!child._started && child.dropDownContainer){ |
---|
95 | var node = dojo.query("[widgetId]", child.dropDownContainer)[0]; |
---|
96 | var menu = dijit.byNode(node); |
---|
97 | if(menu.getPlaceholders){ |
---|
98 | r = r.concat(menu.getPlaceholders(label)); |
---|
99 | } |
---|
100 | } |
---|
101 | }, this); |
---|
102 | return r; // dojox.widget.PlaceholderMenuItem[] |
---|
103 | } |
---|
104 | }); |
---|
105 | |
---|
106 | return dojox.widget.PlaceholderMenuItem; |
---|
107 | }); |
---|