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