Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
executable file
·195 lines (166 loc) · 5.79 KB
/
Copy pathutils.js
File metadata and controls
executable file
·195 lines (166 loc) · 5.79 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2010 and onwards Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Calendar-related utils, all under the utils namespace.
*
* @author manas@google.com (Manas Tungare)
*/
/**
* The namespace for calendar-related utils.
* @namespace
*/
var utils = {};
/**
* The maximum number of characters per field, to keep the resulting Google
* Calendar template URL below acceptable limits.
* @type {number}
* @const
* @private
*/
utils.MAX_CHARS_PER_FIELD_ = 300;
/**
* Processes a calendar event object by inferring missing fields,
* trimming lengths of fields that are too long, and adding computed fields.
* @param {CalendarEvent} event The event to process.
* @return {CalendarEvent} The processed event.
*/
utils.processEvent = function(event) {
if (!event.end) { // If there's no end time, infer one as best as we can.
var startMoment = moment(event.start);
if (startMoment.hours() === 0 && startMoment.minutes() === 0) {
// Assume it's an all-day event if hh=0 & mm=0.
event.end = startMoment.add('d', 1).valueOf();
} else {
// It's not an all-day event, so default to start + X hours, and the user
// can tweak it before adding to their calendar.
event.end = startMoment.add('h', 2).valueOf();
}
}
// Trim each field to a maximum acceptable length.
for (var field in event) {
if (event.hasOwnProperty(field) &&
event[field].length > utils.MAX_CHARS_PER_FIELD_) {
event[field] = event[field].replace(/[\s]+/gi, ' ').
substring(0, utils.MAX_CHARS_PER_FIELD_ - 2) + ' \u2026';
}
}
if (event.address) {
if (event.location) {
event.location = event.address + ' (' + event.location + ')';
} else {
event.location = event.address;
}
delete event.address;
}
// Create Calendar URL after all fields have been trimmed.
event.gcal_url = utils.getGCalUrl_(event);
return event;
};
/**
* Returns a link to a template URL that prefills a Google Calendar event
* template with the details of this event.
* @param {CalendarEvent} event The event to create the URL for.
* @return {string} Google Calendar event template link.
* @private
*/
utils.getGCalUrl_ = function(event) {
// Basic event information: Title, Start, End.
var link =
'https://www.google.com/calendar/event?action=TEMPLATE&trp=false&ctext=' +
encodeURIComponent(event.title);
// Dates could be optional.
if (event.start) {
// If the time is exactly midnight, this might be an all-day event, so skip
// the T000000 part.
link += '&dates=' +
moment(event.start).format('YYYYMMDDTHHmmss').replace('T000000', '');
// Even if start date is present, end date could be missing.
if (event.end) {
link += '/' +
moment(event.end).format('YYYYMMDDTHHmmss').replace('T000000', '');
}
}
// Location
if (event.location) {
link += '&location=' + encodeURIComponent(event.location);
}
// URL
if (event.url) {
link += '&sprop=' + encodeURIComponent(event.url) +
'&sprop=name:' + encodeURIComponent(event.title);
}
// Details
if (event.description || event.url) {
link += '&details=';
if (event.description) {
link += encodeURIComponent(event.description + '\n\n');
}
if (event.url) {
link += chrome.i18n.getMessage('read_more_at_original_url') +
encodeURIComponent(event.url);
}
}
return link;
};
/**
* Parse ISO 8601 date/time into a JavaScript date.
* @param {string|number} date ISO 8601 date as a string, or milliseconds since the epoch.
* @return {Moment} Parsed JavaScript date object.
*/
utils.fromIso8601 = function(date) {
// TODO(manas): Audit and fix this so that it's not used for dates expressed as milliseconds since the epoch.
if (!date) {
return null;
}
if (typeof date === 'string') {
date = date.replace('Z', '+00:00');
return moment(date, [
'YYYY-MM-DDTHH:mm:ssZZ', 'YYYY-MM-DDTHHmmssZZ', 'YYYYMMDDTHHmmssZZ',
'YYYY-MM-DDTHH:mm:ss', 'YYYY-MM-DDTHHmmss', 'YYYYMMDDTHHmmss',
'YYYY-MM-DDTHH:mmZZ', 'YYYY-MM-DDTHHmmZZ', 'YYYYMMDDTHHmmZZ',
'YYYY-MM-DDTHH:mm', 'YYYY-MM-DDTHHmm', 'YYYYMMDDTHHmm',
'YYYY-MM-DDTHH', 'YYYYMMDDTHH',
'YYYY-MM-DD', 'YYYYMMDD'
]);
} else {
return moment(date);
}
};
/**
* In a DOMElement, locate the first instance of the given selector. If
* such an instance is present, then return its text. Else return "".
* @param {Element} element The DOMElement to start looking under.
* @param {string} selector What to look for, as a CSS selector.
* @return {string} The text of the first child if found; "" otherwise.
*/
utils.getFirstFieldText = function(element, selector) {
var rawField = $(element).find(selector);
if (rawField && rawField.length > 0) {
return $(rawField[0]).text().trim();
}
return '';
};
/**
* Loads Google Analytics.
*/
utils.startAnalytics_ = function() {
window._gaq = window._gaq || [];
_gaq.push(['_setAccount', 'UA-21917188-3']);
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
};