summaryrefslogtreecommitdiff
path: root/plugins/share/share.js
blob: 3fc42d654f1e2419f31721c9cc699531cb54bad7 (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
/* global Plugins, xhrJson, Notify, fox, xhrPost, __ */

Plugins.Share = {
	shareArticle: function(id) {
		const query = "backend.php?op=pluginhandler&plugin=share&method=shareArticle&param=" + encodeURIComponent(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);

					const query = {op: "pluginhandler", plugin: "share", method: "newkey", id: id};

					xhrJson("backend.php", query, (reply) => {
						if (reply) {
							const new_link = reply.link;
							const e = $('gen_article_url');

							if (new_link) {

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

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

								new Effect.Highlight(e);

								const img = $("SHARE-IMG-" + id);
								img.addClassName("shared");

								Notify.close();

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

			},
			unshare: function () {
				if (confirm(__("Remove sharing for this article?"))) {

					const query = {op: "pluginhandler", plugin: "share", method: "unshare", id: id};

					xhrPost("backend.php", query, () => {
						try {
							const img = $("SHARE-IMG-" + id);

							if (img) {
								img.removeClassName("shared");
								img.up("div[id*=RROW]").removeClassName("shared");
							}

							dialog.hide();
						} catch (e) {
							console.error(e);
						}
					});
				}

			},
			href: query
		});

		dialog.show();

		const img = $("SHARE-IMG-" + id);
		img.addClassName("shared");
	}
}