1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 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 ********************************************************************************/
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
46 * Datetimecombo constructor
51 * @allowEmptyHM - if this param was set true , the hour and minute select field will has an empty option.
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) {
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();
66 this.fieldname = field;
67 //Get hours and minutes and adjust as necessary
71 parts = datetime.split(' ');
72 this.hrs = parseInt(parts[1].substring(0,2), 10);
73 this.mins = parseInt(parts[1].substring(3,5), 10);
76 //A safety scan to make sure hrs and minutes are formatted correctly
77 if (this.mins > 0 && this.mins < 15) {
79 } else if (this.mins > 15 && this.mins < 30) {
81 } else if (this.mins > 30 && this.mins < 45) {
83 } else if (this.mins > 45) {
89 this.timeformat = timeformat; //23:00 || 11:00
90 this.tabindex = tabindex == null || isNaN(tabindex) ? 1 : tabindex;
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);
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;
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.
116 Datetimecombo.prototype.jsscript = function(callback) {
117 //text = '\n<script language="javascript" type="text/html">';
118 text = '\nfunction update_' + this.fieldname + '(calendar) {';
120 text += '\nif(calendar != null) {';
121 text += '\ncalendar.onUpdateTime();';
122 text += '\ncalendar.onSetTime();';
123 text += '\ncalendar.hide();';
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;';
135 text += '\nif(trim(newdate) =="'+ this.timeseparator +'") newdate="";';
136 text += '\ndocument.getElementById("' + this.fieldname + '").value = newdate;';
137 text += '\n' + callback;
139 //text += '\n</script>';
145 * This function renders the HTML form elements for this widget
147 Datetimecombo.prototype.html = function(callback) {
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>';
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>';
161 text += '\n</select> ';
162 text += this.timeseparator;
163 text += '\n <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>';
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>';
173 if(this.hasMeridiem) {
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>';
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>';
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 += ' ' + SUGAR.language.get("app_strings", "LBL_LINK_NONE");
195 * This method handles events on the hour, minute and meridiem elements for the widget
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.
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;
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");
212 if(d.value == "") { // if date is not set wipe time settings
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;
224 var newdate = d.value + ' ' + h.value + this.timeseparator + m.value;
226 if(this.hasMeridiem) {
227 ampm = document.getElementById(this.fieldname + "_meridiem").value;
230 if(trim(newdate) == ""+this.timeseparator+""){
233 document.getElementById(this.fieldname).value = newdate;
234 //Check for onchange actions and fire them
236 SUGAR.util.callOnChangeListers(this.fieldname);
238 if(this.showCheckbox) {
239 flag = this.fieldname + '_flag';
240 date = this.fieldname + '_date';
241 hours = this.fieldname + '_hours';
242 mins = this.fieldname + '_minutes';
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;
251 document.getElementById(flag).value=0;
252 document.getElementById(date).disabled=false;
253 document.getElementById(hours).disabled=false;
254 document.getElementById(mins).disabled=false;