]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/jsclass_base.js
Release 6.2.2
[Github/sugarcrm.git] / jssource / src_files / include / javascript / jsclass_base.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 // Class SugarClass
38 // superclass for all Sugar* sub-classes
39 //
40 ///////////////////////////////////////////////
41
42 function SugarClass() {
43     this.init();
44 }
45
46 SugarClass.prototype.init = function() {}
47
48 // create inheritance for a class
49 SugarClass.inherit = function(className,parentClassName) {
50   var str = className+".prototype = new "+parentClassName+"();";
51   str += className+".prototype.constructor = "+className+";";
52   str += className+".superclass = "+parentClassName+".prototype;";
53
54   try {
55     eval(str);
56   } catch (e) {}
57 }
58
59 //Root class of Sugar JS Application:
60
61 SugarClass.inherit("SugarContainer", "SugarClass");
62
63 function SugarContainer(root_div) {
64     GLOBAL_REGISTRY.container = this;
65     this.init(root_div);
66 }
67
68 SugarContainer.prototype.init = function(root_div) {
69     this.root_div = root_div;
70     SugarContainer.superclass.init.call(this);
71 }
72
73 SugarContainer.prototype.start = function(root_widget) {
74       this.root_widget = new root_widget();
75       this.root_widget.load(this.root_div);
76 }
77
78 var global_request_registry = new Object();
79 var req_count = 0;
80
81 //////////////////////////////////////////////////
82 // class: SugarDateTime 
83 // date and time utilities
84 //
85 //////////////////////////////////////////////////
86
87 SugarClass.inherit("SugarDateTime","SugarClass");
88
89 function SugarDateTime() {
90     this.init(root_div);
91 }
92
93 SugarDateTime.prototype.init = function(root_div) {
94     this.root_div = root_div;
95 }
96
97 // return the javascript Date object
98 // given the Sugar Meetings date_start/time_start or date_end/time_end
99 SugarDateTime.mysql2jsDateTime = function(mysql_date, mysql_time) {
100     var match = new RegExp(date_reg_format);
101     if (((result = match.exec(mysql_date))) == null) {
102         return null;
103     }
104
105     var match2 = new RegExp(time_reg_format);
106     
107     if ((result2 = match2.exec(mysql_time)) == null) {
108         result2 = [0,0,0,0];
109     }
110
111     var match3 = /^0(\d)/;
112
113     if ((result3 = match3.exec(result2[1])) != null) {
114         result2[1] = result3[1];
115     }
116
117     if (typeof (result2[3]) != 'undefined') {
118         if (result2[3] == 'pm' || result2[3] == 'PM') {
119             if (parseInt(result2[1]) != 12) {
120                 result2[1] = parseInt(result2[1]) + 12;
121             }
122         }
123         else if (result2[1] == 12) {
124             result2[1] = 0;
125         }
126     }
127
128     return new Date(result[date_reg_positions['Y']], result[date_reg_positions['m']] - 1, result[date_reg_positions['d']], result2[1], result2[2], 0, 0);
129 }
130 // make it a static func
131
132 // return the formatted day of the week of the date given a date object
133 SugarDateTime.prototype.getFormattedDate = function(date_obj) {
134     var returnDate = '';
135     var userDateFormat = GLOBAL_REGISTRY['current_user']['fields']['date_time_format']['date'];
136     var dow = GLOBAL_REGISTRY['calendar_strings']['dom_cal_weekdays_long'][date_obj.getDay()];
137     var month = date_obj.getMonth() + 1;
138     month = GLOBAL_REGISTRY['calendar_strings']['dom_cal_month_long'][month];
139
140     returnDate = dow;
141
142     for (i = 0; i < 5; i++) {
143         switch (userDateFormat.charAt(i)) {
144             case "Y":
145                 returnDate += " " + date_obj.getFullYear();
146                 break;
147             case "m":
148                 returnDate += " " + month;
149                 break;
150             case "d":
151                 returnDate += " " + date_obj.getDate();
152                 break;
153             default:
154             // cn: use locale's date separator? probably not.
155             //returnDate += " " + userDateFormat.charAt(i);
156         }
157     }
158
159     return returnDate;
160 }
161
162 SugarDateTime.getFormattedDate = SugarDateTime.prototype.getFormattedDate;
163
164 // return the formatted day of the week of the date given a date object
165 SugarDateTime.prototype.getFormattedDOW = function(date_obj) {
166     var hour = config.strings.mod_strings.Calendar.dow[date_obj.getDay()];
167 }
168 SugarDateTime.getFormattedDOW = SugarDateTime.prototype.getFormattedDOW;
169
170 // return the formatted hour of the date given a date object
171 SugarDateTime.getAMPM = function(date_obj) {
172     var hour = date_obj.getHour();
173     var am_pm = 'AM';
174     if (hour > 12) {
175         hour -= 12;
176         am_pm = 'PM';
177     }
178     else if (hour == 12) {
179         am_pm = 'PM';
180     }
181     else if (hour == 0) {
182         hour = 12;
183     }
184     return am_pm;
185 }
186 SugarDateTime.getFormattedHour = SugarDateTime.prototype.getFormattedHour;
187
188 //mod.SugarDateTime.getFormattedDate = publ.getFormattedDate;
189
190 // return the javascript Date object given a vCal UTC string
191 SugarDateTime.prototype.parseUTCDate = function(date_string) {
192     var match = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/;
193     if (((result = match.exec(date_string))) != null) {
194         var new_date = new Date(Date.UTC(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]) + time_offset));
195         return new_date;
196     }
197
198 }
199
200 SugarDateTime.parseUTCDate = SugarDateTime.prototype.parseUTCDate;
201
202 SugarDateTime.prototype.parseAdjustedDate = function(date_string, dst_start, dst_end, gmt_offset_secs) {
203
204     var match = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/;
205     dst_start_parse = match.exec(dst_start);
206     dst_end_parse = match.exec(dst_end);
207
208     if (dst_start_parse == null || dst_end_parse == null) {
209         var new_date = new Date(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]));
210         new_date = new Date(new_date.getTime() + gmt_offset_secs * 1000);
211     } else {
212         dst_start_obj = new Date(dst_start_parse[1], dst_start_parse[2] - 1, dst_start_parse[3], dst_start_parse[4], dst_start_parse[5], parseInt(dst_start_parse[6]));
213         dst_end_obj = new Date(dst_end_parse[1], dst_end_parse[2] - 1, dst_end_parse[3], dst_end_parse[4], dst_end_parse[5], parseInt(dst_end_parse[6]));
214
215         if (((result = match.exec(date_string))) != null) {
216             var new_date = new Date(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]));
217             var event_ts = new_date.getTime();
218             var dst_start_ts = dst_start_obj.getTime();
219             var dst_end_ts = dst_end_obj.getTime();
220
221             if (((event_ts >= dst_start_ts || event_ts < dst_end_ts) && dst_start_ts > dst_end_ts)
222                     || (event_ts >= dst_start_ts && event_ts < dst_end_ts)) {
223                 new_date = new Date(new_date.getTime() + 60 * 60 * 1000);
224             }
225
226             new_date = new Date(new_date.getTime() + gmt_offset_secs * 1000);
227
228         }
229     }
230     return new_date;
231 }
232
233 SugarDateTime.parseAdjustedDate = SugarDateTime.prototype.parseAdjustedDate;
234
235 // create a hash based on a date
236 SugarDateTime.prototype.getUTCHash = function(startdate) {
237     var month = (startdate.getUTCMonth() < 10) ? "0" + startdate.getUTCMonth() : "" + startdate.getUTCMonth();
238     var day = (startdate.getUTCDate() < 10) ? "0" + startdate.getUTCDate() : "" + startdate.getUTCDate();
239     var hours = (startdate.getUTCHours() < 10) ? "0" + startdate.getUTCHours() : "" + startdate.getUTCHours();
240     var minutes = (startdate.getUTCMinutes() < 10) ? "0" + startdate.getUTCMinutes() : "" + startdate.getUTCMinutes();
241     return startdate.getUTCFullYear() + month + day + hours + minutes;
242 }
243
244 SugarDateTime.getUTCHash = SugarDateTime.prototype.getUTCHash;
245