]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/Meetings/duration_dependency.js
Release 6.5.10
[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-2013 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 delimiter = (this.date_delimiter=="."?"\\"+this.date_delimiter:this.date_delimiter);
58         var date_format_cleaned = this.date_format.replace(/%/g,"").replace(new RegExp(delimiter, 'g'),"");
59         this.month_pos = date_format_cleaned.search(/m/);
60         this.day_pos = date_format_cleaned.search(/d/);
61         this.year_pos = date_format_cleaned.search(/Y/);
62
63     if (YAHOO.util.Selector.query('input#' + end_field)[0].value != "")
64     {
65         this.calculate_duration();
66     }
67     else
68     {
69         this.change_duration();
70     }
71
72         this.update_duration_fields();
73         this.set_duration_handler();
74         
75         
76         var self = this;
77     YAHOO.util.Event.addListener(YAHOO.util.Selector.query('input#' + start_field)[0], "change", function(){
78         self.change_start();
79     });
80     YAHOO.util.Event.addListener(YAHOO.util.Selector.query('input#' + end_field)[0], "change", function(){
81         if (self.lock_end_listener)
82         {
83             return;
84         }
85         self.calculate_duration();
86         self.update_duration_fields();
87         self.set_duration_handler();
88     });
89         if(duration_field != null){
90         YAHOO.util.Event.addListener(YAHOO.util.Selector.query('select#' + duration_field)[0], "change", function(){
91             self.change_duration();
92             self.update_duration_fields();
93             SugarWidgetScheduler.update_time();
94         });
95         }
96 }
97
98 DurationDependency.prototype.calculate_duration = function(){
99         try{
100         var start = this.parse_date(YAHOO.util.Selector.query('input#' + this.start_field)[0].value);
101         var end = this.parse_date(YAHOO.util.Selector.query('input#' + this.end_field)[0].value);
102                 this.duration = (end.getTime() - start.getTime()) / 1000;
103         }catch (e){
104                 this.duration = 0;
105         }
106 }
107
108 DurationDependency.prototype.change_start = function(){
109         this.lock_end_listener = true;
110                                 
111     var start = this.parse_date(YAHOO.util.Selector.query('input#' + this.start_field)[0].value);
112         var end = new Date(start.getTime() + this.duration * 1000);     
113         this.set_date(end,this.end_field);
114                         
115         var self = this;
116         setTimeout(function(){
117                 self.lock_end_listener = false;
118         },300); 
119 }
120
121 DurationDependency.prototype.change_duration = function(){      
122         this.lock_end_listener = true;
123
124     this.duration = YAHOO.util.Selector.query('select#' + this.duration_field)[0].value;
125     var start = this.parse_date(YAHOO.util.Selector.query('input#' + this.start_field)[0].value);
126     var end = new Date(start.getTime() + this.duration * 1000);
127         this.set_date(end,this.end_field);
128                 
129         var self = this;
130         setTimeout(function(){
131                 self.lock_end_listener = false;
132         },300); 
133 }
134
135 DurationDependency.prototype.update_duration_fields = function(){
136         var minutes = this.duration / 60;
137
138     var hours_elm = YAHOO.util.Selector.query('input#' + this.duration_field + "_hours")[0];
139     var minutes_elm = YAHOO.util.Selector.query('input#' + this.duration_field + "_minutes")[0];
140     if(!hours_elm){
141                 hours_elm = document.createElement("input");
142                 hours_elm.setAttribute("type","hidden");
143                 hours_elm.name = this.duration_field + "_hours";
144                 hours_elm.id = hours_elm.name;
145         YAHOO.util.Selector.query('input#' + this.end_field)[0].parentNode.appendChild(hours_elm);
146     }
147         if(!minutes_elm){
148                 minutes_elm = document.createElement("input");
149                 minutes_elm.setAttribute("type","hidden");
150                 minutes_elm.name = this.duration_field + "_minutes";
151                 minutes_elm.id = minutes_elm.name;
152         YAHOO.util.Selector.query('input#' + this.end_field)[0].parentNode.appendChild(minutes_elm);
153     }
154         
155         hours_elm.value = parseInt(minutes / 60);
156         minutes_elm.value = parseInt(minutes % 60);
157 }
158
159 DurationDependency.prototype.get_duration_text = function(){
160         var minutes = this.duration / 60;
161         
162
163         var d = parseInt(minutes / 60 / 24);    
164         var h = parseInt((minutes / 60) % 24);
165         var m = parseInt(minutes % 60); 
166         d = format_part(d,SUGAR.language.get('app_strings', (d > 1)?'LBL_DURATION_DAYS':'LBL_DURATION_DAY'));
167         h = format_part(h,SUGAR.language.get('app_strings', (h > 1)?'LBL_DURATION_HOURS':'LBL_DURATION_HOUR'));
168         m = format_part(m,SUGAR.language.get('app_strings', (m > 1)?'LBL_DURATION_MINUTES':'LBL_DURATION_MINUTE'));     
169                 
170         function format_part(v,s){      
171                 if(v == 0)
172                         v = "";         
173                 else{
174                         v = v.toString();
175                         v = v + " " + s + " ";
176                 }
177                 return v;
178         }       
179                                 
180         return d + h + m;       
181 }
182
183 DurationDependency.prototype.set_duration_handler = function(){
184         if(this.duration_field == null)
185                 return;
186     var dur_elm = YAHOO.util.Selector.query('select#' + this.duration_field)[0];
187
188     if(dur_elm){
189                 if(this.duration >= 0){
190                         this.add_custom_duration(dur_elm);
191                 }       
192                 dur_elm.value = "";
193                 dur_elm.value = this.duration;
194         }
195 }
196
197 DurationDependency.prototype.add_custom_duration = function(dur_elm){
198                         for(var i = 0; i < dur_elm.length; i++){
199                                 if(dur_elm.options[i].className == 'custom'){
200                                         var el = dur_elm.options[i];
201                                         el.parentNode.removeChild(el);
202                                 }               
203                         }
204                 
205                         var option_exists = false;
206                         var pos_index = 0;
207                         var pos_found = false;
208                         for(var i = 0; i < dur_elm.length; i++){
209                                 var v = dur_elm.options[i].value;
210                                 if(v == this.duration){
211                                         var option_exists = true;
212                                         break;
213                                 }
214                                 if(!pos_found && v > this.duration){
215                                         pos_index = i;
216                                         pos_found = true;
217                                 }
218                                 if(!pos_found && i == (dur_elm.length - 1)){
219                                         pos_index = i + 1;
220                                         pos_found = true;
221                                 }                       
222                         }
223                 
224         
225                         if(!option_exists){
226                                 var option = document.createElement('option');
227                                 option.value = this.duration;
228                                 option.className = 'custom';
229                                 option.innerHTML = this.get_duration_text();
230                                 var ref = dur_elm.options[pos_index];
231                                 if(pos_index == dur_elm.length){
232                                         dur_elm.appendChild(option);
233                                 }else{
234                                         dur_elm.insertBefore(option, ref);
235                                 }
236                         }
237 }
238
239 DurationDependency.prototype.parse_date = function(d){
240         
241         var date_parts = d.split(" ");
242         var date_str = date_parts[0];
243         var time_str = date_parts[1];
244         if(date_parts.length == 3)
245                 time_str += date_parts[2];
246
247         var date_arr = date_str.split(this.date_delimiter);     
248         var year = date_arr[this.year_pos];
249         var month = date_arr[this.month_pos];
250         var day = date_arr[this.day_pos];       
251         var hour = time_str.substr(0,2);
252         var minute = time_str.substr(3,2);
253         if(this.has_meridiem){
254                 var meridiem = "am";
255                 if(/pm/i.test(time_str))
256                         meridiem = "pm";
257                 hour = hour % 12 + (meridiem === "am" ? 0 : 12);                                
258         }       
259                 
260         var date = new Date(year,month - 1,day,hour,minute);
261         
262         return date;
263 }
264
265 DurationDependency.prototype.set_date = function(date,field){
266         try{
267                 var year = date.getFullYear();
268                 var month = date.getMonth() + 1;
269                 var day = date.getDate();
270                 var hour = date.getHours();
271                 var minute = date.getMinutes();
272         }catch (e){
273                 return false;
274         }
275
276         if(this.has_meridiem){
277                         var meridiem = "am";
278                         if(hour * 60 + minute >= 12 * 60)
279                                 meridiem = "pm";                        
280                         if(hour == 0)
281                                 hour = 12;
282                         if(hour > 12)
283                                 hour = hour - 12;
284         }
285         year = pad(year);
286         month = pad(month);
287         day = pad(day);
288         hour = pad(hour);
289         minute = pad(minute);
290         
291         function pad(s){
292                 return s < 10 ? "0" + s : s;
293         }
294         
295         var date = "";
296         for(var i = 0; i < 3; i++){
297                 if(i > 0)
298                         date += this.date_delimiter;
299                 if(i == this.year_pos)
300                         date += year;
301                 if(i == this.month_pos)
302                         date += month;
303                 if(i == this.day_pos)
304                         date += day;
305         }
306
307     YAHOO.util.Selector.query('input#' + field + "_date")[0].value = date;
308     YAHOO.util.Selector.query('select#' + field + "_hours")[0].value = hour;
309     YAHOO.util.Selector.query('select#' + field + "_minutes")[0].value = minute;
310     if(this.has_meridiem){
311         var nodes = YAHOO.util.Selector.query('select#' + field + "_meridiem")[0].childNodes;
312         for(var i = 0; i < nodes.length; i++){
313                         if(nodes[i].value == "AM"){
314                                 meridiem = meridiem.toUpperCase();
315                                 break;
316                         }
317                 }
318         YAHOO.util.Selector.query('select#' + field + "_meridiem")[0].value = meridiem;
319     }
320         
321         eval("combo_" + field + ".update()");   
322 }