]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/Meetings/duration_dependency.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / modules / Meetings / duration_dependency.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36  
37 // Class DurationDependency binds start and end datetimecombo fields with duration
38
39 function DurationDependency(start_field,end_field,duration_field,format){
40
41         this.duration = 0;
42         this.start_field = start_field;
43         this.end_field = end_field;
44         this.duration_field = duration_field;
45         this.format = format;
46         this.lock_end_listener = false;
47
48         var format_parts = this.format.split(" ");
49         this.date_format = format_parts[0];
50         this.time_format = format_parts[1];
51         if(format_parts.length == 3)
52                 this.time_format += "" + this.time_format[2];
53         this.date_delimiter = /([-.\\/])/.exec(this.date_format)[0];
54         this.time_delimiter = /([.:])/.exec(this.time_format)[0];
55         this.has_meridiem = /p/i.test(this.format);
56         
57         var date_format_cleaned = this.date_format.replace(/%/g,"").replace(new RegExp(this.date_delimiter, 'g'),"");
58         this.month_pos = date_format_cleaned.search(/m/);
59         this.day_pos = date_format_cleaned.search(/d/);
60         this.year_pos = date_format_cleaned.search(/Y/);        
61         
62         if(document.getElementById(end_field).value != "")
63                 this.calculate_duration();
64         else
65                 this.change_duration(); 
66         
67         this.update_duration_fields();
68         this.set_duration_handler();
69         
70         
71         var self = this;                
72         YAHOO.util.Event.addListener(start_field, "change", function(){
73                 self.change_start();            
74         });     
75         YAHOO.util.Event.addListener(end_field, "change", function(){
76                 if(self.lock_end_listener)
77                         return;
78                 self.calculate_duration();
79                 self.update_duration_fields();
80                 self.set_duration_handler();                            
81         });
82         if(duration_field != null){
83                 YAHOO.util.Event.addListener(duration_field, "change", function(){
84                         self.change_duration();
85                         self.update_duration_fields();  
86                         SugarWidgetScheduler.update_time();     
87                 });
88         }
89 }
90
91 DurationDependency.prototype.calculate_duration = function(){
92         try{
93                 var start = this.parse_date(document.getElementById(this.start_field).value);
94                 var end = this.parse_date(document.getElementById(this.end_field).value);       
95                 this.duration = (end.getTime() - start.getTime()) / 1000;
96         }catch (e){
97                 this.duration = 0;
98         }
99 }
100
101 DurationDependency.prototype.change_start = function(){
102         this.lock_end_listener = true;
103                                 
104         var start = this.parse_date(document.getElementById(this.start_field).value);   
105         var end = new Date(start.getTime() + this.duration * 1000);     
106         this.set_date(end,this.end_field);
107                         
108         var self = this;
109         setTimeout(function(){
110                 self.lock_end_listener = false;
111         },300); 
112 }
113
114 DurationDependency.prototype.change_duration = function(){      
115         this.lock_end_listener = true;  
116         
117         this.duration = document.getElementById(this.duration_field).value;     
118         var start = this.parse_date(document.getElementById(this.start_field).value);   
119         var end = new Date(start.getTime() + this.duration * 1000);
120         this.set_date(end,this.end_field);
121                 
122         var self = this;
123         setTimeout(function(){
124                 self.lock_end_listener = false;
125         },300); 
126 }
127
128 DurationDependency.prototype.update_duration_fields = function(){
129         var minutes = this.duration / 60;
130         
131         var hours_elm = document.getElementById(this.duration_field + "_hours");
132         var minutes_elm = document.getElementById(this.duration_field + "_minutes");
133         if(!hours_elm){
134                 hours_elm = document.createElement("input");
135                 hours_elm.setAttribute("type","hidden");
136                 hours_elm.name = this.duration_field + "_hours";
137                 hours_elm.id = hours_elm.name;
138                 document.getElementById(this.end_field).parentNode.appendChild(hours_elm);
139         }
140         if(!minutes_elm){
141                 minutes_elm = document.createElement("input");
142                 minutes_elm.setAttribute("type","hidden");
143                 minutes_elm.name = this.duration_field + "_minutes";
144                 minutes_elm.id = minutes_elm.name;
145                 document.getElementById(this.end_field).parentNode.appendChild(minutes_elm);
146         }
147         
148         hours_elm.value = parseInt(minutes / 60);
149         minutes_elm.value = parseInt(minutes % 60);
150 }
151
152 DurationDependency.prototype.get_duration_text = function(){
153         var minutes = this.duration / 60;
154         
155
156         var d = parseInt(minutes / 60 / 24);    
157         var h = parseInt((minutes / 60) % 24);
158         var m = parseInt(minutes % 60); 
159         d = format_part(d,SUGAR.language.get('app_strings', (d > 1)?'LBL_DURATION_DAYS':'LBL_DURATION_DAY'));
160         h = format_part(h,SUGAR.language.get('app_strings', (h > 1)?'LBL_DURATION_HOURS':'LBL_DURATION_HOUR'));
161         m = format_part(m,SUGAR.language.get('app_strings', (m > 1)?'LBL_DURATION_MINUTES':'LBL_DURATION_MINUTE'));     
162                 
163         function format_part(v,s){      
164                 if(v == 0)
165                         v = "";         
166                 else{
167                         v = v.toString();
168                         v = v + " " + s + " ";
169                 }
170                 return v;
171         }       
172                                 
173         return d + h + m;       
174 }
175
176 DurationDependency.prototype.set_duration_handler = function(){
177         if(this.duration_field == null)
178                 return;
179         var dur_elm = document.getElementById(this.duration_field);
180         
181         if(dur_elm){    
182                 if(this.duration >= 0){
183                         this.add_custom_duration(dur_elm);
184                 }       
185                 dur_elm.value = "";
186                 dur_elm.value = this.duration;
187         }
188 }
189
190 DurationDependency.prototype.add_custom_duration = function(dur_elm){
191                         for(var i = 0; i < dur_elm.length; i++){
192                                 if(dur_elm.options[i].className == 'custom'){
193                                         var el = dur_elm.options[i];
194                                         el.parentNode.removeChild(el);
195                                 }               
196                         }
197                 
198                         var option_exists = false;
199                         var pos_index = 0;
200                         var pos_found = false;
201                         for(var i = 0; i < dur_elm.length; i++){
202                                 var v = dur_elm.options[i].value;
203                                 if(v == this.duration){
204                                         var option_exists = true;
205                                         break;
206                                 }
207                                 if(!pos_found && v > this.duration){
208                                         pos_index = i;
209                                         pos_found = true;
210                                 }
211                                 if(!pos_found && i == (dur_elm.length - 1)){
212                                         pos_index = i + 1;
213                                         pos_found = true;
214                                 }                       
215                         }
216                 
217         
218                         if(!option_exists){
219                                 var option = document.createElement('option');
220                                 option.value = this.duration;
221                                 option.className = 'custom';
222                                 option.innerHTML = this.get_duration_text();
223                                 var ref = dur_elm.options[pos_index];
224                                 if(pos_index == dur_elm.length){
225                                         dur_elm.appendChild(option);
226                                 }else{
227                                         dur_elm.insertBefore(option, ref);
228                                 }
229                         }
230 }
231
232 DurationDependency.prototype.parse_date = function(d){
233         
234         var date_parts = d.split(" ");
235         var date_str = date_parts[0];
236         var time_str = date_parts[1];
237         if(date_parts.length == 3)
238                 time_str += date_parts[2];
239
240         var date_arr = date_str.split(this.date_delimiter);     
241         var year = date_arr[this.year_pos];
242         var month = date_arr[this.month_pos];
243         var day = date_arr[this.day_pos];       
244         var hour = time_str.substr(0,2);
245         var minute = time_str.substr(3,2);
246         if(this.has_meridiem){
247                 var meridiem = "am";
248                 if(/pm/i.test(time_str))
249                         meridiem = "pm";
250                 hour = hour % 12 + (meridiem === "am" ? 0 : 12);                                
251         }       
252                 
253         var date = new Date(year,month - 1,day,hour,minute);
254         
255         return date;
256 }
257
258 DurationDependency.prototype.set_date = function(date,field){
259         try{
260                 var year = date.getFullYear();
261                 var month = date.getMonth() + 1;
262                 var day = date.getDate();
263                 var hour = date.getHours();
264                 var minute = date.getMinutes();
265         }catch (e){
266                 return false;
267         }
268
269         if(this.has_meridiem){
270                         var meridiem = "am";
271                         if(hour * 60 + minute >= 12 * 60)
272                                 meridiem = "pm";                        
273                         if(hour == 0)
274                                 hour = 12;
275                         if(hour > 12)
276                                 hour = hour - 12;
277         }
278         year = pad(year);
279         month = pad(month);
280         day = pad(day);
281         hour = pad(hour);
282         minute = pad(minute);
283         
284         function pad(s){
285                 return s < 10 ? "0" + s : s;
286         }
287         
288         var date = "";
289         for(var i = 0; i < 3; i++){
290                 if(i > 0)
291                         date += this.date_delimiter;
292                 if(i == this.year_pos)
293                         date += year;
294                 if(i == this.month_pos)
295                         date += month;
296                 if(i == this.day_pos)
297                         date += day;
298         }       
299
300         document.getElementById(field + "_date").value = date;  
301         document.getElementById(field + "_hours").value = hour;
302         document.getElementById(field + "_minutes").value = minute;
303         if(this.has_meridiem){
304                 var nodes = document.getElementById(field + "_meridiem").childNodes;
305                 for(var i = 0; i < nodes.length; i++){
306                         if(nodes[i].value == "AM"){
307                                 meridiem = meridiem.toUpperCase();
308                                 break;
309                         }
310                 }       
311                 document.getElementById(field + "_meridiem").value = meridiem;
312         }
313         
314         eval("combo_" + field + ".update()");   
315 }