]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/calendar.js
Release 6.2.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / calendar.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 Calendar = function() {};
38
39 Calendar.getHighestZIndex = function (containerEl)
40 {
41    var highestIndex = 0;
42    var currentIndex = 0;
43    var els = Array();
44    
45    els = containerEl ? containerEl.getElementsByTagName('*') : document.getElementsByTagName('*');
46    
47    for(var i=0; i < els.length; i++)
48    {
49       currentIndex = YAHOO.util.Dom.getStyle(els[i], "zIndex");
50       if(!isNaN(currentIndex) && currentIndex > highestIndex)
51       { 
52          highestIndex = parseInt(currentIndex); 
53       }
54    }
55    
56    return (highestIndex == Number.MAX_VALUE) ? Number.MAX_VALUE : highestIndex+1;
57 };
58
59 Calendar.setup = function (params) {
60
61     YAHOO.util.Event.onDOMReady(function(){
62         
63         var Event = YAHOO.util.Event;
64         var Dom = YAHOO.util.Dom;
65         var dialog;
66         var calendar;
67         var showButton = params.button ? params.button : params.buttonObj;
68         var userDateFormat = params.ifFormat ? params.ifFormat : (params.daFormat ? params.daFormat : "m/d/Y");
69         var inputField = params.inputField ? params.inputField : params.inputFieldObj;
70         var dateFormat = userDateFormat.substr(0,10);
71         var date_field_delimiter = /([-.\\/])/.exec(dateFormat)[0];
72         dateFormat = dateFormat.replace(/[^a-zA-Z]/g,'');
73         
74         var monthPos = dateFormat.search(/m/);
75         var dayPos = dateFormat.search(/d/);
76         var yearPos = dateFormat.search(/Y/);         
77         
78         Event.on(Dom.get(showButton), "click", function() {
79
80             if (!dialog) {
81                                   
82                 dialog = new YAHOO.widget.SimpleDialog("container_" + showButton, {
83                     visible:false,
84                     context:[showButton, "tl", "bl"],
85                     buttons:[],
86                     draggable:false,
87                     close:true,
88                     zIndex: Calendar.getHighestZIndex(document.body)
89                 });
90                 
91                 dialog.setHeader(SUGAR.language.get('app_strings', 'LBL_MASSUPDATE_DATE'));
92                 var dialogBody = '<p class="callnav_today"><a href="#"  id="callnav_today">' + SUGAR.language.get('app_strings', 'LBL_EMAIL_DATE_TODAY') + '</a></p><div id="' + showButton + '_div"></div>';
93                 dialog.setBody(dialogBody);
94                 dialog.render(document.body);
95
96                 //Since the cal div name is dynamic we need to add a custom class to override some default yui css styles
97                 Dom.addClass("container_" + showButton, "cal_panel");
98                 
99                 //Clear the date selection if the user clicks on today.
100                 Event.addListener("callnav_today", "click", function(){ 
101                     calendar.clear();
102                     var now = new Date();
103                     //Reset the input field value
104                     Dom.get(inputField).value = formatSelectedDate(now);
105                     //Highlight the cell
106                     var cellIndex = calendar.getCellIndex(now);
107                     if(cellIndex > -1 )
108                     {
109                         var cell = calendar.cells[cellIndex];
110                         Dom.addClass(cell, calendar.Style.CSS_CELL_SELECTED);
111                     }
112                 });
113                 
114                 dialog.showEvent.subscribe(function() {
115                     if (YAHOO.env.ua.ie) {
116                         // Since we're hiding the table using yui-overlay-hidden, we 
117                         // want to let the dialog know that the content size has changed, when
118                         // shown
119                         dialog.fireEvent("changeContent");
120                     }
121                 });
122                 
123                 // Hide Calendar if we click anywhere in the document other than the calendar
124                 Event.on(document, "click", function(e) {
125                         
126                     if(!dialog)
127                     {
128                        return;  
129                     }                   
130                         
131                     var el = Event.getTarget(e);                   
132                     var dialogEl = dialog.element;
133                     if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != Dom.get(showButton) && !Dom.isAncestor(Dom.get(showButton), el)) {
134                         dialog.hide();
135                         calendar = null;
136                         dialog = null;
137                     }
138                 });                
139             }
140
141             // Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
142             if (!calendar) {
143             
144                 var navConfig = {
145                     strings : {
146                         month: SUGAR.language.get('app_strings', 'LBL_CHOOSE_MONTH'),
147                         year: SUGAR.language.get('app_strings', 'LBL_ENTER_YEAR'),
148                         submit: SUGAR.language.get('app_strings', 'LBL_EMAIL_OK'),
149                         cancel: SUGAR.language.get('app_strings', 'LBL_CANCEL_BUTTON_LABEL'),
150                         invalidYear: SUGAR.language.get('app_strings', 'LBL_ENTER_VALID_YEAR')
151                     },
152                     monthFormat: YAHOO.widget.Calendar.SHORT,
153                     initialFocus: "year"
154                 };                      
155                 
156                 calendar = new YAHOO.widget.Calendar(showButton + '_div', {
157                     iframe:false,
158                     hide_blank_weeks:true,
159                     navigator:navConfig
160                 });
161                 
162                 calendar.cfg.setProperty('DATE_FIELD_DELIMITER', date_field_delimiter);
163                 calendar.cfg.setProperty('MDY_DAY_POSITION', dayPos+1);
164                 calendar.cfg.setProperty('MDY_MONTH_POSITION', monthPos+1);
165                 calendar.cfg.setProperty('MDY_YEAR_POSITION', yearPos+1);
166                 
167                 //Configure the month and days label with localization support where defined
168                 if(typeof SUGAR.language.languages['app_list_strings'] != 'undefined' && SUGAR.language.languages['app_list_strings']['dom_cal_month_long'] != 'undefined')
169                 {
170                         if(SUGAR.language.languages['app_list_strings']['dom_cal_month_long'].length == 13)
171                         {
172                            SUGAR.language.languages['app_list_strings']['dom_cal_month_long'].shift();
173                         }
174                         calendar.cfg.setProperty('MONTHS_LONG', SUGAR.language.languages['app_list_strings']['dom_cal_month_long']);
175                 }
176                 
177                 if(typeof SUGAR.language.languages['app_list_strings'] != 'undefined'  && typeof SUGAR.language.languages['app_list_strings']['dom_cal_day_short'] != 'undefined')
178                 {
179                         if(SUGAR.language.languages['app_list_strings']['dom_cal_day_short'].length == 8)
180                         {
181                            SUGAR.language.languages['app_list_strings']['dom_cal_day_short'].shift();
182                         }                       
183                         calendar.cfg.setProperty('WEEKDAYS_SHORT', SUGAR.language.languages['app_list_strings']['dom_cal_day_short']);
184                 }
185
186                 var formatSelectedDate = function(selDate)
187                 {
188                     var monthVal = selDate.getMonth() + 1; //Add one for month value
189                     if(monthVal < 10)
190                     {
191                         monthVal = '0' + monthVal;
192                     }
193
194                     var dateVal = selDate.getDate();
195
196                     if(dateVal < 10)
197                     {
198                         dateVal = '0' + dateVal;
199                     }
200
201                     var yearVal = selDate.getFullYear();
202
203                     selDate = '';
204                     if(monthPos == 0)
205                     {
206                         selDate = monthVal;
207                     }
208                     else if(dayPos == 0)
209                     {
210                         selDate = dateVal;
211                     }
212                     else
213                     {
214                         selDate = yearVal;
215                     }
216
217                     if(monthPos == 1)
218                     {
219                         selDate += date_field_delimiter + monthVal;
220                     }
221                     else if(dayPos == 1)
222                     {
223                         selDate += date_field_delimiter + dateVal;
224                     }
225                     else
226                     {
227                         selDate += date_field_delimiter + yearVal;
228                     }
229
230                     if(monthPos == 2)
231                     {
232                         selDate += date_field_delimiter + monthVal;
233                     }
234                     else if(dayPos == 2)
235                     {
236                         selDate += date_field_delimiter + dateVal;
237                     }
238                     else
239                     {
240                         selDate += date_field_delimiter + yearVal;
241                     }
242
243                     return selDate;
244                 };
245                 
246                 calendar.selectEvent.subscribe(function() {
247                     var input = Dom.get(inputField);
248                                         if (calendar.getSelectedDates().length > 0) {
249
250                         input.value = formatSelectedDate(calendar.getSelectedDates()[0]);
251                         
252                         if(params.comboObject)
253                         {
254                            params.comboObject.update();
255                         }
256                     } else {
257                         input.value = "";
258                     }
259
260                     dialog.hide();
261                                         //Fire any on-change events for this input field
262                                         SUGAR.util.callOnChangeListers(input);
263                 });
264
265                 calendar.renderEvent.subscribe(function() {
266                     // Tell Dialog it's contents have changed, which allows 
267                     // container to redraw the underlay (for IE6/Safari2)
268                     dialog.fireEvent("changeContent");
269                 });
270                
271             }
272             
273             var seldate = calendar.getSelectedDates();
274             if (Dom.get(inputField).value.length > 0) {
275                 val = new Date(Dom.get(inputField).value);
276                 if(!isNaN(val.getTime()))
277                 {
278                         calendar.cfg.setProperty("selected", Dom.get(inputField).value);
279                         seldate = Dom.get(inputField).value.split(date_field_delimiter);        
280                         calendar.cfg.setProperty("pagedate", seldate[monthPos] + calendar.cfg.getProperty("DATE_FIELD_DELIMITER") + seldate[yearPos]);
281                     }
282             } else if (seldate.length > 0) {
283                 // Set the pagedate to show the selected date if it exists
284                 calendar.cfg.setProperty("selected", seldate[0]);
285                 var month = seldate[0].getMonth() + 1;
286                 var year = seldate[0].getFullYear();
287                 calendar.cfg.setProperty("pagedate", month + calendar.cfg.getProperty("DATE_FIELD_DELIMITER") + year);          
288             }      
289
290             calendar.render();
291             dialog.show();
292         });
293     }); 
294 };