summaryrefslogtreecommitdiff
path: root/plugins/share/share.js
blob: 0ae4b2031bf9331a6c07cbc2bde47510b2175dd4 (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
/* global Plugins, App, Notify, fox, xhr, __ */

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);

					xhr.json("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);

								document.querySelector('.share-icon-' + id)?.classList.add('is-shared');

								Notify.close();

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

			},
			unshare: function () {
				if (confirm(__("Remove sharing for this article?"))) {
					xhr.post("backend.php", App.getPhArgs("share", "unshare", {id: id}), (reply) => {
						Notify.info(reply);
						document.querySelector('.share-icon-' + id)?.classList.remove('is-shared');
						dialog.hide();
					});
				}

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

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

			xhr.post("backend.php", App.getPhArgs("share", "shareDialog", {id: id}), (reply) => {
				dialog.attr('content', reply)
				document.querySelector('.share-icon-' + id)?.classList.add('is-shared');
			});
		});

		dialog.show();
	}
}