]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarFields/Fields/Datetimecombo/Datetimecombo.js
Release 6.2.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-2011 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     //A safety scan to make sure hrs and minutes are formatted correctly
77         if (this.mins > 0 && this.mins < 15) {
78                 this.mins = 15;
79         } else if (this.mins > 15 && this.mins < 30) {
80                 this.mins = 30;
81         } else if (this.mins > 30 && this.mins < 45) {
82                 this.mins = 45;
83         } else if (this.mins > 45) {
84                 this.hrs += 1;
85                 this.mins = 0;
86         } //if-else
87
88
89     this.timeformat = timeformat;  //23:00 || 11:00
90     this.tabindex = tabindex == null || isNaN(tabindex) ? 1 : tabindex;
91     
92     // Calculate other derived values
93     this.timeseparator = this.timeformat.substring(2,3);
94     this.has12Hours = /^11/.test(this.timeformat);
95     this.hasMeridiem = /am|pm/i.test(this.timeformat);
96         if(this.hasMeridiem) {
97            this.pm = /pm/.test(this.timeformat);
98     }
99     this.meridiem = this.hasMeridiem ? trim(this.datetime.substring(16)) : '';
100     this.datetime = this.datetime.substr(0,10);
101     this.showCheckbox = showCheckbox;
102     this.checked = parseInt(checked);
103     document.getElementById(this.fieldname + '_date').value = this.datetime;
104
105 }
106
107 /**
108  * jsscript
109  * This function renders the javascript portion to handle updates
110  * on the calendar widget.  We have to do this because browsers like Mozilla
111  * have errors when attempting to handle events inside a class function.
112  * This is the reason why this code that is generated is not placed within
113  * the update function of the Datetimecombo class.  Instead, it is run
114  * using the eval() method when the widget is loaded.
115  */
116 Datetimecombo.prototype.jsscript = function(callback) {
117         //text = '\n<script language="javascript" type="text/html">';
118         text = '\nfunction update_' + this.fieldname + '(calendar) {';
119         /*
120         text += '\nif(calendar != null) {';
121         text += '\ncalendar.onUpdateTime();';
122         text += '\ncalendar.onSetTime();';
123         text += '\ncalendar.hide();';
124         text += '\n}'
125         */
126     text += '\nd = document.getElementById("' + this.fieldname + '_date").value;';
127     text += '\nh = document.getElementById("' + this.fieldname + '_hours").value;';
128     text += '\nm = document.getElementById("' + this.fieldname + '_minutes").value;';
129     text += '\nnewdate = d + " " + h + "' + this.timeseparator + '" + m;';
130     if(this.hasMeridiem) {
131        text += '\nif(typeof document.getElementById("' + this.fieldname + '_meridiem") != "undefined") {';
132        text += '\n   newdate += document.getElementById("' + this.fieldname + '_meridiem").value;';
133        text += '\n}';
134     }
135     text += '\nif(trim(newdate) =="'+ this.timeseparator +'") newdate="";';
136     text += '\ndocument.getElementById("' + this.fieldname + '").value = newdate;';
137     text += '\n' + callback;
138     text += '\n}';
139     //text += '\n</script>';
140     return text;
141 }
142
143 /**
144  * html
145  * This function renders the HTML form elements for this widget
146  */
147 Datetimecombo.prototype.html = function(callback) {
148         
149         //Now render the items
150         var text = '<select class="datetimecombo_time" size="1" id="' + this.fieldname + '_hours" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
151         var h1 = this.has12Hours ? 1 : 0;
152         var h2 = this.has12Hours ? 12 : 23;
153         if(this.allowEmptyHM){
154                  text += '<option></option>';
155         }
156         for(i=h1; i <= h2; i++) {
157             val = i < 10 ? "0" + i : i;
158             text += '<option value="' + val + '" ' + (i == this.hrs ? "SELECTED" : "") +  '>' + val + '</option>';
159         }
160         
161         text += '\n</select>&nbsp;';
162         text += this.timeseparator;
163         text += '\n&nbsp;<select class="datetimecombo_time" size="1" id="' + this.fieldname + '_minutes" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
164         if(this.allowEmptyHM){
165                 text += '\n<option></option>';
166         }
167         text += '\n<option value="00" ' + (this.mins == 0 ? "SELECTED" : "") + '>00</option>';
168         text += '\n<option value="15" ' + (this.mins == 15 ? "SELECTED" : "") + '>15</option>';
169         text += '\n<option value="30" ' + (this.mins == 30 ? "SELECTED" : "") + '>30</option>';
170         text += '\n<option value="45" ' + (this.mins == 45 ? "SELECTED" : "") + '>45</option>';
171         text += '\n</select>';
172         
173         if(this.hasMeridiem) {
174                 text += '\n&nbsp;';
175                 text += '\n<select class="datetimecombo_time" size="1" id="' + this.fieldname + '_meridiem" tabindex="' + this.tabindex + '" onchange="combo_' + this.fieldname + '.update(); ' + callback + '">';
176                 if(this.allowEmptyHM){
177                         text += '\n<option></option>';
178                 }
179                 text += '\n<option value="' + (this.pm ? "am" : "AM") + '" ' + (/am/i.test(this.meridiem) ? "SELECTED" : "") + '>' + (this.pm ? "am" : "AM") + '</option>';
180                 text += '\n<option value="' + (this.pm ? "pm" : "PM") + '" ' + (/pm/i.test(this.meridiem) ? "SELECTED" : "") + '>' + (this.pm ? "pm" : "PM") + '</option>';
181                 text += '\n</select>';
182         }
183         
184         if(this.showCheckbox) {
185             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' : '') + '>';
186             //text += '&nbsp;' + SUGAR.language.get("app_strings", "LBL_LINK_NONE");    
187         }
188
189         return text;
190 };
191
192
193 /**
194  * update
195  * This method handles events on the hour, minute and meridiem elements for the widget
196  * 
197  * XXX TODO 20100317 Frank Steegmans: The code in this module is violating so many best practices
198  * that it will need to get rewritten. Also note that it still stems from before the datetime unification.
199  */
200 Datetimecombo.prototype.update = function(updateListeners) {
201         //On initial load, we call update but we don't want to trigger listeners as the value hasn't really changed.
202         if (typeof (updateListeners) == "undefined")
203                 updateListeners = true;
204
205         // Bug 42025: hour/minute/second still required when start_date is non required
206         //                        Fixing this by just assigning default when they aren't required
207     var d = window.document.getElementById(this.fieldname + '_date');
208         var h = window.document.getElementById(this.fieldname + '_hours');
209         var m = window.document.getElementById(this.fieldname + '_minutes');
210         var mer = document.getElementById(this.fieldname + "_meridiem");
211         
212         if(d.value == "") { // if date is not set wipe time settings
213                 h.selectedIndex = 0;
214                 m.selectedIndex = 0;
215                 if(mer) mer.selectedIndex = 0;
216         } else { // if date is set and hours/minutes are not allowed empty, initialize them
217                 if(this.allowEmptyHM) {
218                         if(h.selectedIndex == 0) h.selectedIndex = 12;
219                         if(m.selectedIndex == 0) m.selectedIndex = 1;
220                         if(mer && (mer.selectedIndex == 0)) mer.selectedIndex = 1;
221                 }
222         }
223         
224         var newdate = d.value + ' ' + h.value + this.timeseparator  + m.value;
225
226         if(this.hasMeridiem) {
227            ampm = document.getElementById(this.fieldname + "_meridiem").value;
228            newdate += ampm;
229         }
230         if(trim(newdate) == ""+this.timeseparator+""){
231                 newdate = '';
232         }
233         document.getElementById(this.fieldname).value = newdate;
234         //Check for onchange actions and fire them
235         if(updateListeners)
236                 SUGAR.util.callOnChangeListers(this.fieldname);
237
238     if(this.showCheckbox) {     
239          flag = this.fieldname + '_flag';
240          date = this.fieldname + '_date';
241          hours = this.fieldname + '_hours';
242          mins = this.fieldname + '_minutes';
243          
244                  if(document.getElementById(flag).checked) {
245                         document.getElementById(flag).value=1;
246                         document.getElementById(this.fieldname).value = '';
247                         document.getElementById(date).disabled=true;
248                         document.getElementById(hours).disabled=true;
249                         document.getElementById(mins).disabled=true;
250                  } else {
251                         document.getElementById(flag).value=0;
252                         document.getElementById(date).disabled=false;
253                         document.getElementById(hours).disabled=false;
254                         document.getElementById(mins).disabled=false;            
255                  } 
256         }
257           
258 };