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