]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarFields/Fields/Datetimecombo/Datetimecombo.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / SugarFields / Fields / Datetimecombo / Datetimecombo.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  * Datetimecombo.js
38  * This is a javascript object that handles rendering the non-date portions
39  * of a datetime field.  There are some assumptions that are made so this
40  * class serves more as a utility rather than a building block.  It is used
41  * primarily to handle the datetime field shown for Call, Meetings and Tasks
42  * edit views.
43  */
44  
45 /*
46  * Datetimecombo constructor
47  * @param datetime 
48  * @param fieldname
49  * @param timeformat
50  * @param tabindex
51  * @allowEmptyHM - if this param was set true , the hour and minute select field will has an empty option.
52  */
53 function Datetimecombo (datetime, field, timeformat, tabindex, showCheckbox, checked, allowEmptyHM) {
54     this.datetime = datetime;
55     this.allowEmptyHM = allowEmptyHM;
56     if(typeof this.datetime == "undefined" || datetime == '' || trim(datetime).length < 10) {
57        this.datetime = '';
58        var d = new Date();
59        var month = d.getMonth();
60        var date = d.getDate();
61        var year = d.getYear();
62        var hours = d.getHours();
63        var minutes = d.getMinutes(); 
64     }
65
66     this.fieldname = field;
67     //Get hours and minutes and adjust as necessary
68     
69     if(datetime != '')
70     {
71         parts = datetime.split(' ');
72         this.hrs = parseInt(parts[1].substring(0,2), 10);
73         this.mins = parseInt(parts[1].substring(3,5), 10);      
74     }
75     
76     this.timeformat = timeformat;  //23:00 || 11:00
77     this.tabindex = tabindex == null || isNaN(tabindex) ? 1 : tabindex;
78     
79     // Calculate other derived values
80     this.timeseparator = this.timeformat.substring(2,3);
81     this.has12Hours = /^11/.test(this.timeformat);
82     this.hasMeridiem = /am|pm/i.test(this.timeformat);
83         if(this.hasMeridiem) {
84            this.pm = /pm/.test(this.timeformat);
85     }
86     this.meridiem = this.hasMeridiem ? trim(this.datetime.substring(16)) : '';
87     this.datetime = this.datetime.substr(0,10);
88     this.showCheckbox = showCheckbox;
89     this.checked = parseInt(checked);
90     document.getElementById(this.fieldname + '_date').value = this.datetime;
91
92     //A safety scan to make sure hrs and minutes are formatted correctly
93         if (this.mins > 0 && this.mins < 15) {
94                 this.mins = 15;
95         } else if (this.mins > 15 && this.mins < 30) {
96                 this.mins = 30;
97         } else if (this.mins > 30 && this.mins < 45) {
98                 this.mins = 45;
99         } else if (this.mins > 45) {
100                 this.hrs += 1;
101                 this.mins = 0;
102                 if(this.hasMeridiem && this.hrs == 12) {
103                         if(this.meridiem == "pm" || this.meridiem == "am") {
104                                 if(this.meridiem == "pm") {
105                                         this.meridiem = "am";
106                                 } else {
107                                         this.meridiem = "pm";
108                                 }
109                         } else {
110                                 if(this.meridiem == "PM") {
111                                         this.meridiem = "AM";
112                                 } else {
113                                         this.meridiem = "PM";
114                                 }
115                         }
116                 }
117                 if (this.hasMeridiem && this.hrs > 12) {
118                         this.hrs = this.hrs - 12;
119                 }
120         } //if-else
121
122 }
123
124 /**
125  * jsscript
126  * This function renders the javascript portion to handle updates
127  * on the calendar widget.  We have to do this because browsers like Mozilla
128  * have errors when attempting to handle events inside a class function.
129  * This is the reason why this code that is generated is not placed within
130  * the update function of the Datetimecombo class.  Instead, it is run
131  * using the eval() method when the widget is loaded.
132  */
133 Datetimecombo.prototype.jsscript = function(callback) {
134         //text = '\n<script language="javascript" type="text/html">';
135         text = '\nfunction update_' + this.fieldname + '(calendar) {';
136         /*
137         text += '\nif(calendar != null) {';
138         text += '\ncalendar.onUpdateTime();';
139         text += '\ncalendar.onSetTime();';
140         text += '\ncalendar.hide();';
141         text += '\n}'
142         */
143     text += '\nd = document.getElementById("' + this.fieldname + '_date").value;';
144     text += '\nh = document.getElementById("' + this.fieldname + '_hours").value;';
145     text += '\nm = document.getElementById("' + this.fieldname + '_minutes").value;';
146     text += '\nnewdate = d + " " + h + "' + this.timeseparator + '" + m;';
147     if(this.hasMeridiem) {
148        text += '\nif(typeof document.getElementById("' + this.fieldname + '_meridiem") != "undefined") {';
149        text += '\n   newdate += document.getElementById("' + this.fieldname + '_meridiem").value;';
150        text += '\n}';
151     }
152     text += '\nif(trim(newdate) =="'+ this.timeseparator +'") newdate="";';
153     text += '\ndocument.getElementById("' + this.fieldname + '").value = newdate;';
154     text += '\n' + callback;
155     text += '\n}';
156     //text += '\n</script>';
157     return text;
158 }
159
160 /**
161  * html
162  * This function renders the HTML form elements for this widget
163  */
164 Datetimecombo.prototype.html = function(callback) {
165         
166         //Now render the items
167         var text = '<span style="position:relative; top:6px;"><select class="datetimecombo_time" size="1" id="' + this.fieldname + '_hours" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
168         var h1 = this.has12Hours ? 1 : 0;
169         var h2 = this.has12Hours ? 12 : 23;
170         if(this.allowEmptyHM){
171                  text += '<option></option>';
172         }
173         for(i=h1; i <= h2; i++) {
174             val = i < 10 ? "0" + i : i;
175             text += '<option value="' + val + '" ' + (i == this.hrs ? "SELECTED" : "") +  '>' + val + '</option>';
176         }
177         
178         text += '\n</select>&nbsp;';
179         text += this.timeseparator;
180         text += '\n&nbsp;<select class="datetimecombo_time" size="1" id="' + this.fieldname + '_minutes" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
181         if(this.allowEmptyHM){
182                 text += '\n<option></option>';
183         }
184         text += '\n<option value="00" ' + (this.mins == 0 ? "SELECTED" : "") + '>00</option>';
185         text += '\n<option value="15" ' + (this.mins == 15 ? "SELECTED" : "") + '>15</option>';
186         text += '\n<option value="30" ' + (this.mins == 30 ? "SELECTED" : "") + '>30</option>';
187         text += '\n<option value="45" ' + (this.mins == 45 ? "SELECTED" : "") + '>45</option>';
188         text += '\n</select>';
189         
190         if(this.hasMeridiem) {
191                 text += '\n&nbsp;';
192                 text += '\n<select class="datetimecombo_time" size="1" id="' + this.fieldname + '_meridiem" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
193                 if(this.allowEmptyHM){
194                         text += '\n<option></option>';
195                 }
196                 text += '\n<option value="' + (this.pm ? "am" : "AM") + '" ' + (/am/i.test(this.meridiem) ? "SELECTED" : "") + '>' + (this.pm ? "am" : "AM") + '</option>';
197                 text += '\n<option value="' + (this.pm ? "pm" : "PM") + '" ' + (/pm/i.test(this.meridiem) ? "SELECTED" : "") + '>' + (this.pm ? "pm" : "PM") + '</option>';
198                 text += '\n</select>';
199         }
200         
201         if(this.showCheckbox) {
202             text += '\n<input style="visibility:hidden;" type="checkbox" name="' + this.fieldname + '_flag" id="' + this.fieldname + '_flag" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '" ' + (this.checked ? 'CHECKED' : '') + '>';
203             //text += '&nbsp;' + SUGAR.language.get("app_strings", "LBL_LINK_NONE");    
204         }
205
206     text += '</span>';
207         return text;
208 };
209
210
211 /**
212  * update
213  * This method handles events on the hour, minute and meridiem elements for the widget
214  * 
215  * XXX TODO 20100317 Frank Steegmans: The code in this module is violating so many best practices
216  * that it will need to get rewritten. Also note that it still stems from before the datetime unification.
217  */
218 Datetimecombo.prototype.update = function(updateListeners) {
219         //On initial load, we call update but we don't want to trigger listeners as the value hasn't really changed.
220         if (typeof (updateListeners) == "undefined")
221                 updateListeners = true;
222
223         // Bug 42025: hour/minute/second still required when start_date is non required
224         //                        Fixing this by just assigning default when they aren't required
225     var d = window.document.getElementById(this.fieldname + '_date');
226         var h = window.document.getElementById(this.fieldname + '_hours');
227         var m = window.document.getElementById(this.fieldname + '_minutes');
228         var mer = document.getElementById(this.fieldname + "_meridiem");
229         
230         if(d.value == "") { // if date is not set wipe time settings
231                 h.selectedIndex = 0;
232                 m.selectedIndex = 0;
233                 if(mer) mer.selectedIndex = 0;
234         } else { // if date is set and hours/minutes are not allowed empty, initialize them
235                 if(this.allowEmptyHM) {
236                         if(h.selectedIndex == 0) h.selectedIndex = 12;
237                         if(m.selectedIndex == 0) m.selectedIndex = 1;
238                         if(mer && (mer.selectedIndex == 0)) mer.selectedIndex = 1;
239                 }
240         }
241         
242         var newdate = d.value + ' ' + h.value + this.timeseparator  + m.value;
243
244         if(this.hasMeridiem) {
245            ampm = document.getElementById(this.fieldname + "_meridiem").value;
246            newdate += ampm;
247         }
248         if(trim(newdate) == ""+this.timeseparator+""){
249                 newdate = '';
250         }
251         document.getElementById(this.fieldname).value = newdate;
252         //Check for onchange actions and fire them
253         if(updateListeners)
254                 SUGAR.util.callOnChangeListers(this.fieldname);
255
256     if(this.showCheckbox) {     
257          flag = this.fieldname + '_flag';
258          date = this.fieldname + '_date';
259          hours = this.fieldname + '_hours';
260          mins = this.fieldname + '_minutes';
261          
262                  if(document.getElementById(flag).checked) {
263                         document.getElementById(flag).value=1;
264                         document.getElementById(this.fieldname).value = '';
265                         document.getElementById(date).disabled=true;
266                         document.getElementById(hours).disabled=true;
267                         document.getElementById(mins).disabled=true;
268                  } else {
269                         document.getElementById(flag).value=0;
270                         document.getElementById(date).disabled=false;
271                         document.getElementById(hours).disabled=false;
272                         document.getElementById(mins).disabled=false;            
273                  } 
274         }
275           
276 };