source: branches/1.2/contactcenter/js/cc_tree.js @ 285

Revision 285, 7.9 KB checked in by brunocosta, 16 years ago (diff)

Correção dos problemas gerados no commit anterior, a funcionalidade citada no ticket #199 foram temporariamente desativadas.

  • 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.afterSetCatalog)
70                        {
71                                typeof(_this.afterSetCatalog) == 'function' ? _this.afterSetCatalog() : eval(_this.afterSetCatalog);
72                        }
73                };
74
75                Connector.newRequest(this.name+'catalog', CC_url+'set_catalog&catalog='+catalog, 'GET', handler);
76        }
77       
78        ccCatalogTree.prototype.setCatalogSearch = function (catalog) {
79       
80                var _this = this;
81                var handler = function (responseText)
82                {
83                        Element('cc_debug').innerHTML = responseText;
84                        var data = unserialize(responseText);
85
86                        if (!data)
87                        {
88                                showMessage(Element('cc_msg_err_contacting_server').value);
89                                return;
90                        }
91                       
92                        if (data['status'] != 'ok')
93                        {
94                                showMessage(data['msg']);
95                                _this._getActualLevel(true);
96                                return;
97                        }
98
99                        _this.catalog_perms = parseInt(data['perms']);
100
101                        this.afterSetCatalog = ccEmailWin.search.go();
102                       
103                };
104
105                Connector.newRequest(this.name+'catalog', CC_url+'set_catalog&catalog='+catalog, 'GET', handler);
106        }
107               
108       
109        ccCatalogTree.prototype.select = function(level, search)
110        {
111                if (!search) {
112                        this.tree.openTo(level);
113                        this.tree.getNodeById(level)._select();
114               
115                        if (level != this.actualLevel)
116                        {
117                                this.setCatalog(level);
118                                return;
119                        }
120                } else {
121                        this.tree.openTo(level);
122                        this.tree.getNodeById(level)._select();
123                        this.setCatalogSearch(level);
124                        return;
125                }
126        }
127
128        /*************************************************************************\
129         *                    Methods For Internal Use                           *
130        \*************************************************************************/
131       
132        ccCatalogTree.prototype._waitForTree = function(level, rlevel)
133        {
134                if (this.treeAvailable)
135                {
136                        this.setCatalog(level);
137                        return;
138                }
139
140                if (rlevel >= 100)
141                {
142                        return;
143                }
144               
145                setTimeout(this.name+'._waitForTree(\''+level+'\', '+rlevel+')', 100);
146        }
147
148        ccCatalogTree.prototype._getActualLevel = function(set)
149        {
150                var _this = this;
151                this.treeAvailable = false;
152
153                var handler = function (responseText)
154                {
155                        Element('cc_debug').innerHTML = responseText;
156                        var data = unserialize(responseText);
157
158                        if (!data)
159                        {
160                                showMessage(Element('cc_msg_err_contacting_server').value);
161                                return;
162                        }
163
164                        if (data['status'] != 'ok')
165                        {
166                                showMessage(data['msg']);
167                                return;
168                        }
169
170                        _this.actualLevel = data['data'];
171
172                        if (set)
173                        {
174                        //      _this.select(data['data']);
175                                _this.tree.openTo(_this.actualLevel);
176                                _this.tree.getNodeById(_this.actualLevel)._select();
177                                _this.setCatalog(_this.actualLevel);
178                                _this.expandTree();
179
180                        }
181                };
182               
183                Connector.newRequest(this.name+'actual', CC_url+'get_actual_catalog', 'GET', handler);
184        }
185
186        ccCatalogTree.prototype.expandTree = function() {
187                for (i=0; i < this.tree._aNodes.length; i++)
188                        this._expandSubTree(this.tree._aNodes[i]);
189        }
190
191        ccCatalogTree.prototype._expandSubTree = function(node) {
192                if ( node._children == '' ) {
193                        return;
194                }
195                if (node._children != '') {
196                        for (i = 0; i <= node._children.length; i++) {
197                                if ( node._io == false )
198                                        node.changeState();
199                                this._expandSubTree(node._children[i]);
200                        }
201                }
202        }
203
204
205        ccCatalogTree.prototype._updateTree = function(level, open)
206        {
207                var _this = this;
208                this.treeAvailable = false;
209
210                var handler = function (responseText)
211                {
212                        var data = unserialize(responseText);
213                        var treeData;
214
215                        if (!data)
216                        {
217                                showMessage(Element('cc_msg_err_contacting_server').value);
218                                return;
219                        }
220
221                        if (data['status'] != 'ok')
222                        {
223                                showMessage(data['msg']);
224                                return;
225                        }
226
227                        treeData = data['data'];
228                       
229                        var timeout = 10;
230                        var limit = 0;
231                        for (var i in treeData)
232                        {
233                                if (i == 'length')
234                                {
235                                        continue;
236                                }
237
238                                switch (treeData[i]['type'])
239                                {
240                                        case 'unknown':
241                                                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);
242                                                break;
243
244                                        case 'catalog_group':
245                                                //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);
246                                                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);
247                                                break;
248
249                                        case 'catalog':
250                                                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);
251                                                break;
252
253                                        case 'mixed_catalog_group':
254                                                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);
255                                                break;
256
257                                        case 'empty':
258                                                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);
259                                }
260
261                                timeout += 5;
262                        }
263                       
264                        _this.treeAvailable = true;
265
266                        if (open)
267                        {
268                                setTimeout(_this.name+"._getActualLevel(true)", timeout+10);
269                                //setTimeout(_this.name+".tree.openTo("+_this.name+".actualLevel);", timeout+100);
270                                //setTimeout(_this.name+".tree.getNodeById("+_this.name+".actualLevel)._select();", timeout+100);
271                        }
272                };
273                Connector.newRequest(this.name+'update', CC_url+'get_catalog_tree&level='+level, 'GET', handler);
274        }
Note: See TracBrowser for help on using the repository browser.