forked from bmoeskau/Extensible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensible.js
183 lines (166 loc) · 7.69 KB
/
Extensible.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @class Ext.ensible
* Extensible core utilities and functions.
* @singleton
*/
(function(){
Ext.ns('Ext.ensible.ux', 'Ext.ensible.sample', 'Ext.ensible.plugins', 'Ext.ensible.cal');
Ext.apply(Ext.ensible, {
/**
* The version of the framework
* @type String
*/
version : '1.0-rc2',
/**
* The version of the framework, broken out into its numeric parts. This returns an
* object that contains the following integer properties: major, minor and patch.
* @type Object
*/
versionDetails : {
major: 1,
minor: 0,
patch: 0
},
hasBorderRadius : !(Ext.isIE || Ext.isOpera),
log : function(s){
//console.log(s);
},
/**
* @class Ext.ensible.cal.Date
* @extends Object
* <p>Contains utility date functions used by the calendar components.</p>
* @singleton
*/
Date : {
/**
* Determines whether times used throughout all Extensible components should be displayed as
* 12 hour times with am/pm (default) or 24 hour / military format. Note that some locale files
* may override this value by default.
* @type Boolean
* @property use24HourTime
*/
use24HourTime : false,
/**
* Returns the time duration between two dates in the specified units. For finding the number
* of calendar days (ignoring time) between two dates use {@link Ext.ensible.Date.diffDays diffDays} instead.
* @param {Date} start The start date
* @param {Date} end The end date
* @param {String} unit (optional) The time unit to return. Valid values are 'ms' (milliseconds, the default), 's' (seconds),
* 'm' (minutes) or 'h' (hours).
* @return {Number} The time difference between the dates in the units specified by the unit param
*/
diff : function(start, end, unit){
var denom = 1,
diff = end.getTime() - start.getTime();
if(unit == 's'){
denom = 1000;
}
else if(unit == 'm'){
denom = 1000*60;
}
else if(unit == 'h'){
denom = 1000*60*60;
}
return Math.round(diff/denom);
},
/**
* Calculates the number of calendar days between two dates, ignoring time values.
* A time span that starts at 11pm (23:00) on Monday and ends at 1am (01:00) on Wednesday is
* only 26 total hours, but it spans 3 calendar days, so this function would return 3. For the
* exact time difference, use {@link Ext.ensible.Date.diff diff} instead.
* @param {Date} start The start date
* @param {Date} end The end date
* @return {Number} The number of calendar days difference between the dates
*/
diffDays : function(start, end){
day = 1000*60*60*24;
diff = end.clearTime(true).getTime() - start.clearTime(true).getTime();
return Math.ceil(diff/day);
},
/**
* Copies the time value from one date object into another without altering the target's
* date value. This function returns a new Date instance without modifying either original value.
* @param {Date} fromDt The original date from which to copy the time
* @param {Date} toDt The target date to copy the time to
* @return {Date} The new date/time value
*/
copyTime : function(fromDt, toDt){
var dt = toDt.clone();
dt.setHours(
fromDt.getHours(),
fromDt.getMinutes(),
fromDt.getSeconds(),
fromDt.getMilliseconds());
return dt;
},
/**
* Compares two dates and returns a value indicating how they relate to each other.
* @param {Date} dt1 The first date
* @param {Date} dt2 The second date
* @param {Boolean} precise (optional) If true, the milliseconds component is included in the comparison,
* else it is ignored (the default).
* @return {Number} The number of milliseconds difference between the two dates. If the dates are equal
* this will be 0. If the first date is earlier the return value will be positive, and if the second date
* is earlier the value will be negative.
*/
compare : function(dt1, dt2, precise){
var d1 = dt1, d2 = dt2;
if(precise !== true){
d1 = dt1.clone();
d1.setMilliseconds(0);
d2 = dt2.clone();
d2.setMilliseconds(0);
}
return d2.getTime() - d1.getTime();
},
// private helper fn
maxOrMin : function(max){
var dt = (max ? 0 : Number.MAX_VALUE), i = 0, args = arguments[1], ln = args.length;
for(; i < ln; i++){
dt = Math[max ? 'max' : 'min'](dt, args[i].getTime());
}
return new Date(dt);
},
/**
* Returns the maximum date value passed into the function. Any number of date
* objects can be passed as separate params.
* @param {Date} dt1 The first date
* @param {Date} dt2 The second date
* @param {Date} dtN (optional) The Nth date, etc.
* @return {Date} A new date instance with the latest date value that was passed to the function
*/
max : function(){
return this.maxOrMin.apply(this, [true, arguments]);
},
/**
* Returns the minimum date value passed into the function. Any number of date
* objects can be passed as separate params.
* @param {Date} dt1 The first date
* @param {Date} dt2 The second date
* @param {Date} dtN (optional) The Nth date, etc.
* @return {Date} A new date instance with the earliest date value that was passed to the function
*/
min : function(){
return this.maxOrMin.apply(this, [false, arguments]);
},
isInRange : function(dt, rangeStart, rangeEnd) {
return (dt >= rangeStart && dt <= rangeEnd);
},
/**
* Returns true if two date ranges overlap (either one starts or ends within the other, or one completely
* overlaps the start and end of the other), else false if they do not.
* @param {Date} start1 The start date of range 1
* @param {Date} end1 The end date of range 1
* @param {Date} start2 The start date of range 2
* @param {Date} end2 The end date of range 2
* @return {Booelan} True if the ranges overlap, else false
*/
rangesOverlap : function(start1, end1, start2, end2){
var startsInRange = (start1 >= start2 && start1 <= end2),
endsInRange = (end1 >= start2 && end1 <= end2),
spansRange = (start1 <= start2 && end1 >= end2);
return (startsInRange || endsInRange || spansRange);
}
}
});
})();