source: branches/2.2/contactcenter/js/cc_tree.js @ 3282

Revision 3282, 8.3 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1251 - Commit principal com adição da melhoria no módulo

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