summaryrefslogtreecommitdiff
path: root/plugins/share/share.js
blob: 46b62ca5b65e25a805ae466ceeb3197726d1f032 (plain)
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
/* global dojo, Effect, Plugins, xhrJson, Notify, fox, xhrPost, __ */

Plugins.Share = {
	shareArticle: function(id) {
		const dialog = new fox.SingleUseDialog({
			id: "shareArticleDlg",
			title: __("Share article by URL"),
			newurl: function () {
				if (confirm(__("Generate new share URL for this article?"))) {

					Notify.progress("Trying to change URL...", true);

					xhrJson("backend.php", App.getPhArgs("share", "newkey", {id: id}), (reply) => {
						if (reply) {
							const new_link = reply.link;
							const target = dialog.domNode.querySelector(".target-url");

							if (new_link && target) {

								target.innerHTML = target.innerHTML.replace(/&key=.*$/,
									"&key=" + new_link);

								target.href = target.href.replace(/&key=.*$/,
									"&key=" + new_link);

								// eslint-disable-next-line no-new
								new Effect.Highlight(target);

								const icon = document.querySelector(".share-icon-" + id);

								if (icon)
									icon.addClassName("is-shared");

								Notify.close();

							} else {
								Notify.error("Could not change URL.");
							}
						}
					});
				}

			},
			unshare: function () {
				if (confirm(__("Remove sharing for this article?"))) {
					xhrPost("backend.php", App.getPhArgs("share", "unshare", {id: id}), (transport) => {
						Notify.info(transport.responseText);

						const icon = document.querySelector(".share-icon-" + id);

						if (icon)
							icon.removeClassName("is-shared");

						dialog.hide();
					});
				}

			},
			content: __("Loading, please wait...")
		});

		const tmph = dojo.connect(dialog, 'onShow', function () {
			dojo.disconnect(tmph);

			xhrPost("backend.php", App.getPhArgs("share", "shareDialog", {id: id}), (transport) => {
				dialog.attr('content', transport.responseText)

				const icon = document.querySelector(".share-icon-" + id);

				if (icon)
					icon.addClassName("is-shared");
			});
		});

		dialog.show();
	}
}