-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat-link.html
263 lines (217 loc) · 6.97 KB
/
at-link.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<link rel="import" href="../tangere/tangere.html">
<link rel="import" href="../at-core-style-classes/at-core-style-classes.html">
<dom-module id="at-link">
<template>
<style include="at-core-style-classes"></style>
<style>
:host {
display: inline;
box-sizing: border-box;
pointer-events: auto; /* ensure that links work even when vlist is disabled */
}
[disabled] {
pointer-events: none;
}
.active {
background-color: var(--at-active-background-color);
}
#link {
text-decoration: none;
}
.a {
color: red;
}
.action, .action:visited {
color: var(--at-action-color) !important;
cursor: pointer;
}
.action:hover {
color: var(--at-action-color-hover) !important;
cursor: pointer;
}
</style>
<a id="link" href="{{href}}" xref="{{xref}}" on-click="_ignoreEvent" on-tap="_handleLink" on-contextmenu="_handleLink" draggable="false">
<slot class="action"></slot>
</a>
</template>
</dom-module>
<script>
Polymer({
is: 'at-link',
properties: {
href: {
type: String,
value: ""
},
xref: {
type: String,
value: ""
},
signal: {
type: String,
value: ""
},
label: {
type: String,
value: "",
title: "label link for activity tracking"
},
signalData: {
type: String,
value: ''
}
},
_ignoreEvent: function (e) {
e.preventDefault();
e.stopPropagation();
return false;
},
attached: function () {
// copy "outer" className to "inner" link
this._setClassList(this.$.link, this.className);
},
_handleLink: function (e) {
// allow childs to set a different target url, if not provided use default url
var href = this.href, el = this, xref = this.xref || this.href;
if (Tangere.session.embedded) href = xref;
if (!href && e.url) {
href = e.url;
el = e
};
if (!href && e.currentTarget && e.currentTarget.href) {
href = e.currentTarget.href;
el = e.currentTarget
};
if (!href && e.currentTarget.attributes && e.currentTarget.attributes.url) {
href = e.currentTarget.attributes.url.value;
el = e.currentTarget
};
if (!href && e.detail && e.detail.sourceEvent && e.detail.sourceEvent.currentTarget && e.detail.sourceEvent.currentTarget.href) {
href = e.detail.sourceEvent.currentTarget.href;
el = e.detail.sourceEvent.currentTarget
};
if (!href && !this.signal) console.log("handLink Url missing");
if (href.indexOf("#?") == 0) {
// hrefs starting with #? just change parameters
href = this._updateHrefParameters(href.substring(2));
} else if (href.indexOf("#") == 0) {
var elName = href.substring(1, (href + "?").indexOf("?"));
// expand relative name with name prefix
if (elName.indexOf("/") < 0) {
var elNameOrg = elName;
var resolve = { namePrefix: "" };
this.fire("resolve-name-prefix", resolve);
var prefix = resolve.namePrefix;
if (elName == "default.view") elName = "";
elName = elName.replace("default.", "");
elName = prefix + "/" + prefix + (elName ? ("-" + elName.replace(".", "-")) : "");
href = href.replace("#" + elNameOrg, "#" + elName);
}
}
// try to get link tracking context
var trackingContext = { track: null };
this.fire("resolve-tracking-context", trackingContext, { bubbles: true });
if (trackingContext.track != null) {
if (this.label) trackingContext.track.label = this.label;
trackingContext.track.url = href;
Polymer.signal("log-event", trackingContext.track);
};
// rightclick - just update href, browser might open url in new tab
if (e && e.type == "contextmenu") {
el.href = href;
return;
}
// highlight current element
if (el) {
Polymer.dom(el).classList.add("active");
setTimeout(function () {
Polymer.dom(el).classList.remove("active");
}.bind(this), 300);
}
// when running in IFrame and we also have a direct link to the external url open it
if (el && el.getAttribute("xref")) {
// running in embeded mode
if (window.parentIFrame != null) {
href = el.getAttribute("xref");
window.parentIFrame.sendMessage({
cmd: "navigate-to",
url: href
});
return;
}
// chrome extension -> open in new tab
if (window.chrome && chrome.runtime && !!chrome.runtime.id) {
href = el.getAttribute("xref");
window.open(href, '_blank');
return;
}
}
// if signal= is provided we just send a signal
if (!!this.signal) {
el.href = "#";
var arg = { data: this.signalData, sender: this };
Polymer.signal(this.signal, arg);
return false;
}
// convert absolute link to app to relative
if (href.indexOf(window.location.origin + window.location.pathname + "#") == 0) {
href = href.substring(href.indexOf("#"));
}
if (href) {
if (href.indexOf("#") == 0) {
Polymer.signal("busy-start-async");
Polymer.signal("navigate-to", href);
} else {
window.open(href, '_blank');
}
}
if (e) {
e.preventDefault();
e.stopPropagation();
}
return false;
},
_updateHrefParameters: function (params) {
var hash = window.location.hash;
var args = {};
var app = "", href = "";
if (hash.indexOf("#!") == 0) {
hash = hash.substr(2);
} else if (hash.indexOf("#") == 0) {
hash = hash.substr(1);
}
var hashes = hash.split('?');
app = hashes[0];
// extract parameters from current hash
if (hashes.length > 1) {
var hashes = hashes[1].split('&');
for (var i = 0; i < hashes.length; i++) {
var hash = hashes[i].split('=');
if (hash.length > 1) {
args[hash[0]] = decodeURIComponent(hash[1]);
} else {
args[hash[0]] = "";
}
}
}
// merge new parameters
hashes = params.split('&');
for (var i = 0; i < hashes.length; i++) {
var hash = hashes[i].split('=');
if (hash.length > 1) {
args[hash[0]] = hash[1];
} else {
args[hash[0]] = "";
}
}
// build new hash
var argn = Object.getOwnPropertyNames(args).sort();
for (var i = 0; i < argn.length; i++) {
var argv = args[argn[i]];
if (!!argv) href += "&" + argn[i] + "=" + (encodeURI(argv).split("%2F").join("/"));
}
href = "#!" + app + (!!href ? "?" + href.substring(1) : "");
return href;
}
});
</script>