1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 // Class DurationDependency binds start and end datetimecombo fields with duration
39 function DurationDependency(start_field,end_field,duration_field,format){
42 this.start_field = start_field;
43 this.end_field = end_field;
44 this.duration_field = duration_field;
46 this.lock_end_listener = false;
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);
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/);
63 if (YAHOO.util.Selector.query('input#' + end_field)[0].value != "")
65 this.calculate_duration();
69 this.change_duration();
72 this.update_duration_fields();
73 this.set_duration_handler();
77 YAHOO.util.Event.addListener(YAHOO.util.Selector.query('input#' + start_field)[0], "change", function(){
80 YAHOO.util.Event.addListener(YAHOO.util.Selector.query('input#' + end_field)[0], "change", function(){
81 if (self.lock_end_listener)
85 self.calculate_duration();
86 self.update_duration_fields();
87 self.set_duration_handler();
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();
98 DurationDependency.prototype.calculate_duration = function(){
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;
108 DurationDependency.prototype.change_start = function(){
109 this.lock_end_listener = true;
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);
116 setTimeout(function(){
117 self.lock_end_listener = false;
121 DurationDependency.prototype.change_duration = function(){
122 this.lock_end_listener = true;
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);
130 setTimeout(function(){
131 self.lock_end_listener = false;
135 DurationDependency.prototype.update_duration_fields = function(){
136 var minutes = this.duration / 60;
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];
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);
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);
155 hours_elm.value = parseInt(minutes / 60);
156 minutes_elm.value = parseInt(minutes % 60);
159 DurationDependency.prototype.get_duration_text = function(){
160 var minutes = this.duration / 60;
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'));
170 function format_part(v,s){
175 v = v + " " + s + " ";
183 DurationDependency.prototype.set_duration_handler = function(){
184 if(this.duration_field == null)
186 var dur_elm = YAHOO.util.Selector.query('select#' + this.duration_field)[0];
189 if(this.duration >= 0){
190 this.add_custom_duration(dur_elm);
193 dur_elm.value = this.duration;
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);
205 var option_exists = false;
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;
214 if(!pos_found && v > this.duration){
218 if(!pos_found && i == (dur_elm.length - 1)){
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);
234 dur_elm.insertBefore(option, ref);
239 DurationDependency.prototype.parse_date = function(d){
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];
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){
255 if(/pm/i.test(time_str))
257 hour = hour % 12 + (meridiem === "am" ? 0 : 12);
260 var date = new Date(year,month - 1,day,hour,minute);
265 DurationDependency.prototype.set_date = function(date,field){
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();
276 if(this.has_meridiem){
278 if(hour * 60 + minute >= 12 * 60)
289 minute = pad(minute);
292 return s < 10 ? "0" + s : s;
296 for(var i = 0; i < 3; i++){
298 date += this.date_delimiter;
299 if(i == this.year_pos)
301 if(i == this.month_pos)
303 if(i == this.day_pos)
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();
318 YAHOO.util.Selector.query('select#' + field + "_meridiem")[0].value = meridiem;
321 eval("combo_" + field + ".update()");