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