source: branches/2.0/contactcenter/js/cc_tree.js @ 3179

Revision 3179, 8.1 KB checked in by niltonneto, 14 years ago (diff)

Ticket #1167 - Corrigido problema ao clicar em organização vazia.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1  /***************************************************************************\
2  * eGroupWare - Contacts Center                                              *
3  * http://www.egroupware.org                                                 *
4  * Written by:                                                               *
5  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
6  *  sponsored by Thyamad - http://www.thyamad.com                            *
7  * ------------------------------------------------------------------------- *
8  *  This program is free software; you can redistribute it and/or modify it  *
9  *  under the terms of the GNU General Public License as published by the    *
10  *  Free Software Foundation; either version 2 of the License, or (at your   *
11  *  option) any later version.                                               *
12  \***************************************************************************/
13
14        /*
15         * ContactCenter API - Tree Management Class
16         */
17
18        if (!dFTree)
19        {
20                throw('dFTree lib must be loaded!');
21        }
22       
23        function ccCatalogTree(params)
24        {
25                if (!params || !params['name'] || !params['id_destination'])
26                {
27                        throw('Must specify the tree name!');
28                }
29
30                var _tree = this;
31               
32                /* This is the property that holds the Tree Object */
33                this.name = params['name'];
34                this.tree = new dFTree({'name': params['name']+'_tree'});
35                this.actualLevel = null;
36                this.treeAvailable = false;
37                this.afterSetCatalog = params['afterSetCatalog'];
38                this.catalog_perms = -1;
39                this.Connector = params['connector'];
40
41                /* Build the Inicial Tree */
42                //this._getActualLevel();
43                setTimeout(function(){ _tree._updateTree('0', true);}, 100);
44                this.tree.draw(Element(params['id_destination']));
45        }
46
47        ccCatalogTree.prototype.setCatalog = function(catalog) {
48                var _this = this;
49                var handler = function (responseText)
50                {
51                        Element('cc_debug').innerHTML = responseText;
52                        var data = unserialize(responseText);
53
54                        if (!data)
55                        {
56                                showMessage(Element('cc_msg_err_contacting_server').value);
57                                return;
58                        }
59                       
60                        if (data['status'] != 'ok')
61                        {
62                                showMessage(data['msg']);
63                                _this._getActualLevel(true);
64                                return;
65                        }
66
67                        _this.catalog_perms = parseInt(data['perms']);
68                       
69                        if(_this.catalog_perms == 0){
70                                if(CC_visual == 'cards')
71                                        drawCards(0);
72                                else if(CC_visual == 'table')
73                                        drawTable(0);
74                                setPages(0,0);
75                        }
76                        else if (_this.afterSetCatalog)
77                        {
78                                typeof(_this.afterSetCatalog) == 'function' ? _this.afterSetCatalog() : eval(_this.afterSetCatalog);
79                        }
80                };
81
82                Connector.newRequest(this.name+'catalog', CC_url+'set_catalog&catalog='+catalog, 'GET', handler);
83        }
84       
85        ccCatalogTree.prototype.setCatalogSearch = function (catalog) {
86       
87                var _this = this;
88                var handler = function (responseText)
89                {
90                        Element('cc_debug').innerHTML = responseText;
91                        var data = unserialize(responseText);
92
93                        if (!data)
94                        {
95                                showMessage(Element('cc_msg_err_contacting_server').value);
96                                return;
97                        }
98                       
99                        if (data['status'] != 'ok')
100                        {
101                                showMessage(data['msg']);
102                                _this._getActualLevel(true);
103                                return;
104                        }
105
106                        _this.catalog_perms = parseInt(data['perms']);
107
108                        this.afterSetCatalog = ccEmailWin.search.go();
109                       
110                };
111
112                Connector.newRequest(this.name+'catalog', CC_url+'set_catalog&catalog='+catalog, 'GET', handler);
113        }
114               
115       
116        ccCatalogTree.prototype.select = function(level, search)
117        {
118                if (!search) {
119                        this.tree.openTo(level);
120                        this.tree.getNodeById(level)._select();
121               
122                        if (level != this.actualLevel)
123                        {
124                                this.setCatalog(level);
125                                return;
126                        }
127                } else {
128                        this.tree.openTo(level);
129                        this.tree.getNodeById(level)._select();
130                        this.setCatalogSearch(level);
131                        return;
132                }
133        }
134
135        /*************************************************************************\
136         *                    Methods For Internal Use                           *
137        \*************************************************************************/
138       
139        ccCatalogTree.prototype._waitForTree = function(level, rlevel)
140        {
141                if (this.treeAvailable)
142                {
143                        this.setCatalog(level);
144                        return;
145                }
146
147                if (rlevel >= 100)
148                {
149                        return;
150                }
151               
152                setTimeout(this.name+'._waitForTree(\''+level+'\', '+rlevel+')', 100);
153        }
154
155        ccCatalogTree.prototype._getActualLevel = function(set)
156        {
157                var _this = this;
158                this.treeAvailable = false;
159
160                var handler = function (responseText)
161                {
162                        Element('cc_debug').innerHTML = responseText;
163                        var data = unserialize(responseText);
164
165                        if (!data)
166                        {
167                                showMessage(Element('cc_msg_err_contacting_server').value);
168                                return;
169                        }
170
171                        if (data['status'] != 'ok')
172                        {
173                                showMessage(data['msg']);
174                                return;
175                        }
176
177                        _this.actualLevel = data['data'];
178
179                        if (set)
180                        {
181                        //      _this.select(data['data']);
182                                _this.tree.openTo(_this.actualLevel);
183                                _this.tree.getNodeById(_this.actualLevel)._select();
184                                _this.setCatalog(_this.actualLevel);
185                                _this.expandTree();
186
187                        }
188                };
189               
190                Connector.newRequest(this.name+'actual', CC_url+'get_actual_catalog', 'GET', handler);
191        }
192
193        ccCatalogTree.prototype.expandTree = function() {
194                for (i=0; i < this.tree._aNodes.length; i++)
195                        this._expandSubTree(this.tree._aNodes[i]);
196        }
197
198        ccCatalogTree.prototype._expandSubTree = function(node) {
199                if ( node._children == '' ) {
200                        return;
201                }
202                if (node._children != '') {
203                        for (i = 0; i <= node._children.length; i++) {
204                                if ( node._io == false )
205                                        node.changeState();
206                                this._expandSubTree(node._children[i]);
207                        }
208                }
209        }
210
211
212        ccCatalogTree.prototype._updateTree = function(level, open)
213        {
214                var _this = this;
215                this.treeAvailable = false;
216
217                var handler = function (responseText)
218                {
219                        var data = unserialize(responseText);
220                        var treeData;
221
222                        if (!data)
223                        {
224                                showMessage(Element('cc_msg_err_contacting_server').value);
225                                return;
226                        }
227
228                        if (data['status'] != 'ok')
229                        {
230                                showMessage(data['msg']);
231                                return;
232                        }
233
234                        treeData = data['data'];
235                       
236                        var timeout = 10;
237                        var limit = 0;
238                        for (var i in treeData)
239                        {
240                                if (i == 'length')
241                                {
242                                        continue;
243                                }
244
245                                switch (treeData[i]['type'])
246                                {
247                                        case 'unknown':
248                                                setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '"+_this.name+"._updateTree(\\'"+treeData[i]['id']+"\\')', onClick: '"+_this.name+"._updateTree(\\'"+treeData[i]['id']+"\\');"+_this.name+"._waitForTree(\\'"+treeData[i]['id']+"\\',0)'}), '"+treeData[i]['pid']+"');", timeout);
249                                                break;
250
251                                        case 'catalog_group':
252                                                //setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '', onClick: '"+_this.name+".tree.getNodeById(\\'"+treeData[i]['id']+"\\').open();"+_this.name+"._getActualLevel(true);'}), '"+treeData[i]['pid']+"');", timeout);
253                                                setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '', onClick: '"+_this.name+".setCatalog(\\'"+treeData[i]['id']+"\\');'}), '"+treeData[i]['pid']+"');", timeout);
254                                                break;
255
256                                        case 'catalog':
257                                                setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '', onClick: '"+_this.name+".setCatalog(\\'"+treeData[i]['id']+"\\');'}), '"+treeData[i]['pid']+"');", timeout);
258                                                break;
259
260                                        case 'mixed_catalog_group':
261                                                setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '', onClick: '"+_this.name+".setCatalog(\\'"+treeData[i]['id']+"\\');'}), '"+treeData[i]['pid']+"');", timeout);
262                                                break;
263
264                                        case 'empty':
265                                                setTimeout(_this.name+".tree.add(new dNode({id: '"+treeData[i]['id']+"', caption: '"+treeData[i]['caption']+"', onFirstOpen: '', onClick: '"+_this.name+".setCatalog(\\'"+treeData[i]['id']+"\\');'}), '"+treeData[i]['pid']+"');", timeout);
266                                }
267
268                                timeout += 5;
269                        }
270                       
271                        _this.treeAvailable = true;
272
273                        if (open)
274                        {
275                                setTimeout(_this.name+"._getActualLevel(true)", timeout+10);
276                                //setTimeout(_this.name+".tree.openTo("+_this.name+".actualLevel);", timeout+100);
277                                //setTimeout(_this.name+".tree.getNodeById("+_this.name+".actualLevel)._select();", timeout+100);
278                        }
279                };
280                Connector.newRequest(this.name+'update', CC_url+'get_catalog_tree&level='+level, 'GET', handler);
281        }
Note: See TracBrowser for help on using the repository browser.