Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / AlbumTree.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">Imgorg.AlbumTree = Ext.extend(Ext.tree.TreePanel,{
9     initComponent: function() {
10         Ext.apply(this,{
11             loader: new Ext.ux.tree.DirectTreeLoader({
12                 api: Imgorg.ss.Albums
13             }),
14             root: new Ext.tree.TreeNode({
15                 text:'dummynode',
16                 expanded: true,
17                 leaf: false,
18                 cls:'album-node'
19             }),
20             rootVisible: false,
21             clearOnLoad: false,
22             autoScroll: true,
23             containerScroll: true,
24             enableDrop: true,
25             dropConfig: {
26                 ddGroup: 'organizerDD',
27                 notifyDrop: this.onNotifyDrop.createDelegate(this)
28             }
29         });
30         Imgorg.AlbumTree.superclass.initComponent.call(this);
31         this.loader.load(this.root);
32         
33         this.editor = new Ext.tree.TreeEditor(this, {
34             allowBlank: false,
35             blankText: 'A name is required',
36             selectOnFocus: true
37         });
38         this.editor.on('complete', this.onEditComplete, this);
39         this.on('contextmenu', this.onContextMenu, this);
40     },
41     
42     onContextMenu: function(node, e) {
43         e.stopEvent();
44         if(!this.contMenu) {
45             this.contMenu = new Ext.menu.Menu({
46                 items: [{
47                     text: 'Remove',
48                     handler: function() {
49                         var node = this.currentNode;
50                         node.unselect();
51                         Ext.fly(node.ui.elNode).ghost('l', {
52                             callback: function() {
53                                 Imgorg.ss.Albums.remove({
54                                     album: node.id
55                                 });
56                                 node.remove();
57                             }, scope: node, duration: 0.4
58                         });
59                     },
60                     scope: this
61                 }]
62             });
63         }
64         this.currentNode = node;
65         this.contMenu.showAt(e.getXY());
66     },
67     
68     onNotifyDrop: function(src, e, data) {
69         var nodes = data.nodes;
70         var nodeIds = [];
71         for (var i = 0;i < nodes.length;i++) {
72             nodeIds.push(nodes[i].id);
73         }
74         this.addToAlbum(nodeIds, this.dropZone.dragOverData.target.attributes.id);
75         return true; // cancell repair anim
76     },
77     
78     addToAlbum: function(nodes, album) {
79         Imgorg.ss.Images.addToAlbum({
80             images: nodes, 
81             album: album
82         });
83     },
84     
85     addAlbum: function() {
86         var root = this.root;
87         var node = root.appendChild(new Ext.tree.TreeNode({
88             text:'Album',
89             cls:'album-node',
90             allowDrag:false
91         }));
92         this.getSelectionModel().select(node);
93         var ge = this.editor;
94         setTimeout(function(){
95             ge.editNode = node;
96             ge.startEdit(node.ui.textNode);
97         }, 10);
98     },
99     
100     onEditComplete: function(editor, newVal, oldVal) {
101         var n = editor.editNode;
102         Imgorg.ss.Albums.addOrUpdate({node: n.id, text: newVal, id: n.attributes.id});
103     }
104 });
105 Ext.reg('img-albumtree', Imgorg.AlbumTree);
106
107 Ext.ns('Ext.ux.tree');
108 Ext.ux.tree.DirectTreeLoader = Ext.extend(Ext.tree.TreeLoader,{
109     baseAttrs: {
110         cls:'album-node'
111     },
112     
113     load : function(node, callback){
114         this.requestData(node, callback);
115     },
116     
117     requestData : function(node, callback){
118         if(this.fireEvent("beforeload", this, node, callback) !== false){
119             this.api.loadtree({
120                 node: node.id
121             }, this.processResponse.createDelegate(this, [node, callback], true));
122         }else{
123             // if the load is cancelled, make sure we notify
124             // the node that we are done
125             if(typeof callback == "function"){
126                 callback();
127             }
128         }
129     },
130     
131     processResponse : function(res, trans, node, callback){
132         try {
133             node.beginUpdate();
134             for(var i = 0, len = res.length; i < len; i++){
135                 var n = this.createNode(res[i]);
136                 if(n){
137                     node.appendChild(n);
138                 }
139             }
140             node.endUpdate();
141             if(typeof callback == "function"){
142                 callback(this, node);
143             }
144         }catch(e){
145             this.handleFailure(res);
146         }
147     }
148 });</pre>    \r
149 </body>\r
150 </html>