forked from anandchakru-archives/angular-addtocalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtocalendar.js
135 lines (119 loc) · 4.6 KB
/
addtocalendar.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
/**
* angular-addtocalendar v1.1.5
* An AngularJS directive for adding an event to calendar apps.
*
* Controller and directive.
*/
'use strict';
angular
.module('jshor.angular-addtocalendar', [])
.config([
'$compileProvider',
function ($compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(http(s)?|data):/);
}
])
.controller('AddtocalendarCtrl', ['$scope', '$attrs',
function ($scope, $attrs) {
$scope.description = $scope.description || '';
/**
* Returns file contents for a .ics file.
* @return {String} ics calendar data
*/
function getIcsCalendar() {
return [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'CLASS:PUBLIC',
'DESCRIPTION:' + formatIcsText($scope.description, 62),
'DTSTART:' + $scope.startDate,
'DTEND:' + $scope.endDate,
'LOCATION:' + formatIcsText($scope.location, 64),
'SUMMARY:' + formatIcsText($scope.title, 66),
'TRANSP:TRANSPARENT',
'END:VEVENT',
'END:VCALENDAR'
].join('\n');
}
function getYahooCalendarUrl() {
var yahooCalendarUrl = 'http://calendar.yahoo.com/?v=60&view=d&type=20';
yahooCalendarUrl += '&title=' + encodeURIComponent($scope.title);
yahooCalendarUrl += '&st=' + encodeURIComponent($scope.startDate) + '&et=' + encodeURIComponent($scope.endDate);
yahooCalendarUrl += '&desc=' + encodeURIComponent($scope.description);
yahooCalendarUrl += '&in_loc=' + encodeURIComponent($scope.location);
return yahooCalendarUrl;
}
function getGoogleCalendarUrl() {
var googleCalendarUrl = 'https://www.google.com/calendar/render?action=TEMPLATE';
googleCalendarUrl += '&text=' + encodeURIComponent($scope.title);
googleCalendarUrl += '&dates=' + encodeURIComponent($scope.startDate) + '/' + encodeURIComponent($scope.endDate);
googleCalendarUrl += '&details=' + encodeURIComponent($scope.description);
googleCalendarUrl += '&location=' + encodeURIComponent($scope.location);
return googleCalendarUrl;
}
function getMicrosoftCalendarUrl() {
var microsoftCalendarUrl = 'http://calendar.live.com/calendar/calendar.aspx?rru=addevent';
microsoftCalendarUrl += '&summary=' + encodeURIComponent($scope.title);
microsoftCalendarUrl += '&dtstart=' + encodeURIComponent($scope.startDate) + '&dtend=' + encodeURIComponent($scope.endDate);
microsoftCalendarUrl += '&description=' + encodeURIComponent($scope.description);
microsoftCalendarUrl += '&location=' + encodeURIComponent($scope.location);
return microsoftCalendarUrl;
}
function buildUrl() {
$scope.calendarUrl = {
microsoft: getMicrosoftCalendarUrl(),
google: getGoogleCalendarUrl(),
yahoo: getYahooCalendarUrl(),
icalendar: getIcsCalendar(),
dlIcal: dlIcal
};
}
/**
* Downloads a .ics file for the given parameters.
* The name of the file will be the event title with alphanumeric chars
* having the extension `.ics`.
*/
function dlIcal() {
var fileName = $scope.title.replace(/[^\w ]+/g, '') + '.ics';
download(getIcsCalendar(), fileName, 'application/octet-stream');
}
// observe user-specified attributes
forEachAttr($attrs, function(key) {
$attrs.$observe(key, function() {
buildUrl();
});
});
buildUrl();
}
])
.directive('addtocalendar', function () {
return {
restrict: 'E',
scope: {
startDate: '@',
endDate: '@',
title: '@',
description: '@',
location: '@',
className: '@',
btnText: '@'
},
controller: 'AddtocalendarCtrl',
template: '\
<div class="btn-group" dropdown on-toggle="toggled(open)">\
<span\
ng-class="className || \'btn btn-sm btn-default dropdown-toggle\'"\
dropdown-toggle>\
{{btnText || \'Add to calendar\'}} <span class="caret"></span>\
</span>\
<ul class="dropdown-menu">\
<li><a ng-click="calendarUrl.dlIcal()">iCalendar</a></li>\
<li><a href="{{calendarUrl.google}}" target="_blank">Google Calendar</a></li>\
<li><a ng-click="calendarUrl.dlIcal()">Outlook</a></li>\
<li><a href="{{calendarUrl.yahoo}}" target="_blank">Yahoo! Calendar</a></li>\
<li><a href="{{calendarUrl.microsoft}}" target="_blank">Microsoft Calendar</a></li>\
</ul>\
</div>'
};
});