/
shareComponent.js
199 lines (167 loc) · 4.59 KB
/
shareComponent.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import angular from 'angular';
import gmfPermalinkShareService, {URL_MAX_LEN, URL_PATH_MAX_LEN} from 'gmf/permalink/ShareService.js';
import ngeoStatemanagerLocation from 'ngeo/statemanager/Location.js';
import {getUid as olUtilGetUid} from 'ol/util.js';
/**
* @type {!angular.IModule}
* @hidden
*/
const module = angular.module('gmfPermalinkShareComponent', [
gmfPermalinkShareService.name,
ngeoStatemanagerLocation.name,
]);
module.run(/* @ngInject */ ($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('gmf/permalink/shareComponent', require('./shareComponent.html'));
});
module.value('gmfPermalinkShareTemplateUrl',
/**
* @param {!angular.IAttributes} $attrs Attributes.
* @return {string} The template url.
*/
($attrs) => {
const templateUrl = $attrs['gmfPermalinkShareTemplateUrl'];
return templateUrl !== undefined ? templateUrl :
'gmf/permalink/shareComponent';
});
/**
* @param {!angular.IAttributes} $attrs Attributes.
* @param {!function(!angular.IAttributes): string} gmfPermalinkShareTemplateUrl Template function.
* @return {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function gmfPermalinkShareTemplateUrl($attrs, gmfPermalinkShareTemplateUrl) {
return gmfPermalinkShareTemplateUrl($attrs);
}
/**
* Component to display a shortened permalink and share it by email Example:
*
* <gmf-share
* gmf-share-email="true">
* </gmf-share>
*
* @htmlAttribute {boolean} gmf-share-email Enable emailing capability.
* @type {!angular.IComponentOptions}
*/
const permalinkShareComponent = {
bindings: {
'enableEmail': '<gmfShareEmail'
},
controller: 'GmfShareController',
templateUrl: gmfPermalinkShareTemplateUrl
};
module.component('gmfShare', permalinkShareComponent);
/**
* @private
* @hidden
*/
class ShareComponentController {
/**
* The controller for the share component
* @param {angular.IScope} $scope Scope.
* @param {import("ngeo/statemanager/Location.js").StatemanagerLocation} ngeoLocation ngeo Location service.
* @param {import("gmf/permalink/ShareService.js").PermalinkShareService} gmfShareService service for
* sharing map.
* @param {angular.IQService} $q Angular q service
* @param {angular.IAttributes} $attrs Attributes.
* @constructor
* @ngInject
* @ngdoc controller
* @ngname GmfShareController
*/
constructor($scope, ngeoLocation, gmfShareService, $q, $attrs) {
/**
* @type {string}
*/
this.uid = olUtilGetUid(this);
/**
* @type {angular.IScope}
* @private
*/
this.$scope_ = $scope;
/**
* @type {import("gmf/permalink/ShareService.js").PermalinkShareService}
* @private
*/
this.gmfShareService_ = gmfShareService;
/**
* @type {angular.IQService}
* @private
*/
this.$q_ = $q;
/**
* @type {import("ngeo/statemanager/Location.js").StatemanagerLocation}
* @private
*/
this.ngeoLocation_ = ngeoLocation;
/**
* @type {boolean}
*/
this.enableEmail;
/**
* @type {string}
*/
this.shortLink;
/**
* @type {string}
*/
this.email;
/**
* @type {string}
*/
this.message;
/**
* @type {string}
* @private
*/
this.permalink_ = this.ngeoLocation_.getUriString();
/**
* @type {boolean}
*/
this.showLengthWarning = this.permalink_.length > URL_MAX_LEN ||
ngeoLocation.getPath().length > URL_PATH_MAX_LEN;
/**
* @type {boolean}
*/
this.successfullySent = false;
/**
* @type {boolean}
*/
this.errorOnsend = false;
/**
* @type {boolean}
*/
this.errorOnGetShortUrl = false;
this.getShortUrl();
}
/**
* Get the short version of the permalink if the email is not provided
*/
getShortUrl() {
this.$q_.when(this.gmfShareService_.getShortUrl(this.permalink_))
.then((resp) => {
this.shortLink = /** @type {angular.IHttpResponse} */(resp).data.short_url;
this.errorOnGetShortUrl = false;
}, (resp) => {
this.shortLink = this.permalink_;
this.errorOnGetShortUrl = true;
});
}
/**
* Send the short version of the permalink if the email is provided
*/
sendShortUrl() {
if (this.$scope_['gmfShareForm'].$valid) {
this.$q_.when(this.gmfShareService_.sendShortUrl(this.permalink_, this.email, this.message))
.then((resp) => {
this.successfullySent = true;
}, (resp) => {
this.errorOnsend = true;
});
}
}
}
module.controller('GmfShareController', ShareComponentController);
export default module;