source: trunk/contactcenter/js/ccQuickAdd-plugin.js @ 6908

Revision 6908, 5.7 KB checked in by gustavo, 12 years ago (diff)

Ticket #2986 - Inconsistencia no container(retangulo) de destinatarios

  • 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 - Quick Add Plugin
16         *
17         *
18         * USAGE INSTRUCTIONS
19         *
20         *  Just insert in your PHP code the following statement:
21         *
22         *  $ccContents = ExecMethod('contactcenter.ui_api.get_quick_add_plugin');
23         *
24         *  and $ccContents becomes a valid HTML that inserts everything that is needed
25         *  to use the JS Objects that Contact Center provides. Just remember that you
26         *  need to insert it before your JS code, so all objects are instantiated
27         *  correctly.
28         *
29         * Provides Objects:
30         *
31         *      ccQuickAdd
32         *
33         * Provides Classes:
34         *
35         *  dJSWin, dTabsManager, dFTree
36         *
37         */
38
39        function cQuickAdd ()
40        {
41                // Private
42                this._card;
43                this._button = new Array();
44                this._fields;
45
46                // Public
47                this.window;
48                this.afterSave;
49               
50                // Constructor
51                var wHeight = 0;
52                this._nFields = Element('ccQAnFields').value;
53                this._fields = new Array();
54
55                for (var i = 0; i < this._nFields; i++)
56                {
57                        this._fields[i] = Element('ccQuickAddI'+i);
58                }
59
60                //wHeight = (i+1)*25+5;
61                wHeight = Element('ccQAWinHeight').value;
62
63               
64                this.window = new dJSWin({
65                        id: 'ccQuickAddDOM',
66                        content_id: 'ccQuickAddContent',
67                        width: '262px',
68                        height: wHeight+'px',
69                        title_color: '#3978d6',
70                        bg_color: '#eee',
71                        title: Element('ccQATitle').value,
72                        title_text_color: 'white',
73                        button_x_img: Element('cc_phpgw_img_dir').value+'/winclose.gif',
74                        border: true });
75
76                this.window.draw();
77        }
78
79        /*!
80                @method associateAsButton
81                @abstract Associates the button functions with the spacified DOM Element
82                @author Raphael Derosso Pereira
83
84                @param div DOMElement The HTML DOM element that will "host" the
85                        plugin button.
86
87                @param func function The function that returns the data to be used
88                        to pre-populate the quickAdd fields. The return format MUST be
89                        an Array like:
90
91                                var return_data = new Array();
92                                return_data[0] = <value>;  // Value for the first field
93                                return_data[1] = <value>;  // Value for the second field
94                                ...
95               
96         */
97        cQuickAdd.prototype.associateAsButton = function (div, func)
98        {
99                var _this = this;
100               
101                if (func)
102                {
103                        div.onclick = function() { _this.window.open(); _this.setValues(func()); };
104                }
105                else
106                {
107                        div.onclick = function() { _this.window.open(); };
108                }
109        }
110
111        /*!
112
113                @method setValues
114                @abstract Set the contents of the QuickAdd window with the specified
115                        data
116                @author Raphael Derosso Pereira
117
118                @param data Array The data to be used
119
120        */
121        cQuickAdd.prototype.setValues = function (data)
122        {
123                for (var i in data)
124                {
125                        this._fields[i].value = data[i];
126                }
127
128                this.data = data;
129        }
130
131        /*!
132       
133                @method send
134                @abstract Sends data to server
135                @author Raphael Derosso Pereira
136
137        */
138        cQuickAdd.prototype.send = function ()
139        {
140                var _this = this;
141                var handler = function (responseText)
142                {
143                        Element('cc_debug').innerHTML = responseText;
144
145                        var data = unserialize(responseText);
146
147                        if (!data || typeof(data) != 'object')
148                        {
149                                showMessage(Element('cc_msg_err_contacting_server').value);
150                                return;
151                        }
152                        else if (data['status'] == 'alreadyExists')
153                        {
154                                showMessage(data['msg']);
155                                return;
156                        }
157                        else if (data['status'] != 'ok')
158                        {
159                                return;
160                        }
161
162                        _this.clear();
163                        _this.window.close();
164
165                        if (_this.afterSave)
166                        {
167                                switch (typeof(_this.afterSave))
168                                {
169                                        case 'function':
170                                                _this.afterSave();
171                                                break;
172
173                                        case 'string':
174                                                eval(_this.afterSave);
175                                                break;
176                                }
177                        }
178                }
179
180                var sdata = new Array();
181                var empty = true;
182               
183                for (var i in this._fields)
184                {
185                        sdata[i] = this._fields[i].value;
186                        if (sdata[i] != '')
187                        {
188                                empty = false;
189                        }
190                }
191
192                if(!$.trim(sdata[1])){
193                alert("O campo nome é obrigatório(a)!");
194                return false;
195            }
196               
197                if (empty) return false;
198
199                if(this._fields[1].value=='') {
200                        alert(Element('cc_msg_name_mandatory').value);
201                        return false;
202                }
203                //Utiliza expressão regular para validar email
204                if (this._fields[4].value != ''){
205                        var reEmail = /^[a-zA-Z0-9][_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{1,})*$/;
206                       
207                        if (sdata[4] !== "" && !reEmail.test(this._fields[4].value)) {
208                                alert("O endereço de email '" + this._fields[4].value + "' não é válido!\n" +
209                                "Por favor informe um endereço válido.");
210                                return false;
211                        }
212                }
213                sdata[4] = sdata[4].toLowerCase();
214                var sdata = 'add='+escape(serialize(sdata));
215
216                Connector.newRequest('cQuickAdd.Send', CC_url+'quick_add', 'POST', handler, sdata);
217        }
218
219        /*!
220
221                @method clear
222                @abstract Clear all Plugin Fields
223                @author Raphael Derosso Pereira
224
225        */
226        cQuickAdd.prototype.clear = function ()
227        {
228                for (var i in this._fields)
229                {
230                        this._fields[i].value = '';
231                }
232        }
233
234
235        /* Build the Object */
236        var ccQuickAdd;
237        var cQuickAdd_pre_load = document.body.onload;
238
239        if (is_ie)
240        {
241                document.body.onload = function (e)
242                {
243                        cQuickAdd_pre_load();
244                        ccQuickAdd = new cQuickAdd();
245                       
246                };
247        }
248        else
249        {
250                ccQuickAdd = new cQuickAdd();
251        }
Note: See TracBrowser for help on using the repository browser.