source: sandbox/expresso2.2/contactcenter/js/cc_tree.js @ 1662

Revision 1662, 7.9 KB checked in by amuller, 14 years ago (diff)

Ticket #490 - Criação dos códigos de unificação ajax

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