]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/duration_dependency.js
Release 6.5.0
[Github/sugarcrm.git] / 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 function DurationDependency(start_field,end_field,duration_field,format){this.duration=0;this.start_field=start_field;this.end_field=end_field;this.duration_field=duration_field;this.format=format;this.lock_end_listener=false;var format_parts=this.format.split(" ");this.date_format=format_parts[0];this.time_format=format_parts[1];if(format_parts.length==3)
36 this.time_format+=""+this.time_format[2];this.date_delimiter=/([-.\\/])/.exec(this.date_format)[0];this.time_delimiter=/([.:])/.exec(this.time_format)[0];this.has_meridiem=/p/i.test(this.format);var date_format_cleaned=this.date_format.replace(/%/g,"").replace(new RegExp(this.date_delimiter,'g'),"");this.month_pos=date_format_cleaned.search(/m/);this.day_pos=date_format_cleaned.search(/d/);this.year_pos=date_format_cleaned.search(/Y/);if(document.getElementById(end_field).value!="")
37 this.calculate_duration();else
38 this.change_duration();this.update_duration_fields();this.set_duration_handler();var self=this;YAHOO.util.Event.addListener(start_field,"change",function(){self.change_start();});YAHOO.util.Event.addListener(end_field,"change",function(){if(self.lock_end_listener)
39 return;self.calculate_duration();self.update_duration_fields();self.set_duration_handler();});if(duration_field!=null){YAHOO.util.Event.addListener(duration_field,"change",function(){self.change_duration();self.update_duration_fields();SugarWidgetScheduler.update_time();});}}
40 DurationDependency.prototype.calculate_duration=function(){try{var start=this.parse_date(document.getElementById(this.start_field).value);var end=this.parse_date(document.getElementById(this.end_field).value);this.duration=(end.getTime()-start.getTime())/ 1000;}catch(e){this.duration=0;}}
41 DurationDependency.prototype.change_start=function(){this.lock_end_listener=true;var start=this.parse_date(document.getElementById(this.start_field).value);var end=new Date(start.getTime()+this.duration*1000);this.set_date(end,this.end_field);var self=this;setTimeout(function(){self.lock_end_listener=false;},300);}
42 DurationDependency.prototype.change_duration=function(){this.lock_end_listener=true;this.duration=document.getElementById(this.duration_field).value;var start=this.parse_date(document.getElementById(this.start_field).value);var end=new Date(start.getTime()+this.duration*1000);this.set_date(end,this.end_field);var self=this;setTimeout(function(){self.lock_end_listener=false;},300);}
43 DurationDependency.prototype.update_duration_fields=function(){var minutes=this.duration / 60;var hours_elm=document.getElementById(this.duration_field+"_hours");var minutes_elm=document.getElementById(this.duration_field+"_minutes");if(!hours_elm){hours_elm=document.createElement("input");hours_elm.setAttribute("type","hidden");hours_elm.name=this.duration_field+"_hours";hours_elm.id=hours_elm.name;document.getElementById(this.end_field).parentNode.appendChild(hours_elm);}
44 if(!minutes_elm){minutes_elm=document.createElement("input");minutes_elm.setAttribute("type","hidden");minutes_elm.name=this.duration_field+"_minutes";minutes_elm.id=minutes_elm.name;document.getElementById(this.end_field).parentNode.appendChild(minutes_elm);}
45 hours_elm.value=parseInt(minutes / 60);minutes_elm.value=parseInt(minutes%60);}
46 DurationDependency.prototype.get_duration_text=function(){var minutes=this.duration / 60;var d=parseInt(minutes / 60 / 24);var h=parseInt((minutes / 60)%24);var m=parseInt(minutes%60);d=format_part(d,SUGAR.language.get('app_strings',(d>1)?'LBL_DURATION_DAYS':'LBL_DURATION_DAY'));h=format_part(h,SUGAR.language.get('app_strings',(h>1)?'LBL_DURATION_HOURS':'LBL_DURATION_HOUR'));m=format_part(m,SUGAR.language.get('app_strings',(m>1)?'LBL_DURATION_MINUTES':'LBL_DURATION_MINUTE'));function format_part(v,s){if(v==0)
47 v="";else{v=v.toString();v=v+" "+s+" ";}
48 return v;}
49 return d+h+m;}
50 DurationDependency.prototype.set_duration_handler=function(){if(this.duration_field==null)
51 return;var dur_elm=document.getElementById(this.duration_field);if(dur_elm){if(this.duration>=0){this.add_custom_duration(dur_elm);}
52 dur_elm.value="";dur_elm.value=this.duration;}}
53 DurationDependency.prototype.add_custom_duration=function(dur_elm){for(var i=0;i<dur_elm.length;i++){if(dur_elm.options[i].className=='custom'){var el=dur_elm.options[i];el.parentNode.removeChild(el);}}
54 var option_exists=false;var pos_index=0;var pos_found=false;for(var i=0;i<dur_elm.length;i++){var v=dur_elm.options[i].value;if(v==this.duration){var option_exists=true;break;}
55 if(!pos_found&&v>this.duration){pos_index=i;pos_found=true;}
56 if(!pos_found&&i==(dur_elm.length-1)){pos_index=i+1;pos_found=true;}}
57 if(!option_exists){var option=document.createElement('option');option.value=this.duration;option.className='custom';option.innerHTML=this.get_duration_text();var ref=dur_elm.options[pos_index];if(pos_index==dur_elm.length){dur_elm.appendChild(option);}else{dur_elm.insertBefore(option,ref);}}}
58 DurationDependency.prototype.parse_date=function(d){var date_parts=d.split(" ");var date_str=date_parts[0];var time_str=date_parts[1];if(date_parts.length==3)
59 time_str+=date_parts[2];var date_arr=date_str.split(this.date_delimiter);var year=date_arr[this.year_pos];var month=date_arr[this.month_pos];var day=date_arr[this.day_pos];var hour=time_str.substr(0,2);var minute=time_str.substr(3,2);if(this.has_meridiem){var meridiem="am";if(/pm/i.test(time_str))
60 meridiem="pm";hour=hour%12+(meridiem==="am"?0:12);}
61 var date=new Date(year,month-1,day,hour,minute);return date;}
62 DurationDependency.prototype.set_date=function(date,field){try{var year=date.getFullYear();var month=date.getMonth()+1;var day=date.getDate();var hour=date.getHours();var minute=date.getMinutes();}catch(e){return false;}
63 if(this.has_meridiem){var meridiem="am";if(hour*60+minute>=12*60)
64 meridiem="pm";if(hour==0)
65 hour=12;if(hour>12)
66 hour=hour-12;}
67 year=pad(year);month=pad(month);day=pad(day);hour=pad(hour);minute=pad(minute);function pad(s){return s<10?"0"+s:s;}
68 var date="";for(var i=0;i<3;i++){if(i>0)
69 date+=this.date_delimiter;if(i==this.year_pos)
70 date+=year;if(i==this.month_pos)
71 date+=month;if(i==this.day_pos)
72 date+=day;}
73 document.getElementById(field+"_date").value=date;document.getElementById(field+"_hours").value=hour;document.getElementById(field+"_minutes").value=minute;if(this.has_meridiem){var nodes=document.getElementById(field+"_meridiem").childNodes;for(var i=0;i<nodes.length;i++){if(nodes[i].value=="AM"){meridiem=meridiem.toUpperCase();break;}}
74 document.getElementById(field+"_meridiem").value=meridiem;}
75 eval("combo_"+field+".update()");}