summaryrefslogtreecommitdiff
path: root/plugins/note/note.js
blob: ea173bba615b156424251232badd0d732f492c7c (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
/* global require, Plugins, PluginHost, xhr, App, Notify, fox, __ */

require(['dojo/_base/kernel', 'dojo/ready'], function  (dojo, ready) {
	ready(function() {

		Plugins.Note = {
			set_click_handler: function() {
				document.querySelectorAll(".article-note[data-note-for]").forEach((note) => {
					note.onclick = function() {
						Plugins.Note.edit(this.getAttribute('data-note-for'));
					}
				});
			},
			edit: function(id) {
				const dialog = new fox.SingleUseDialog({
					title: __("Edit article note"),
					execute: function () {
						if (this.validate()) {
							Notify.progress("Saving article note...", true);

							xhr.json("backend.php", this.attr('value'), (reply) => {
								Notify.close();
								dialog.hide();

								if (reply) {
									App.findAll(`div[data-note-for="${reply.id}"]`).forEach((elem) => {
										elem.querySelector(".body").innerHTML = reply.note;

										if (reply.note)
											elem.show();
										else
											elem.hide();
									});
								}
							});
						}
					},
					content: __("Loading, please wait...")
				});

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

					xhr.post("backend.php", App.getPhArgs("note", "edit", {id: id}), (reply) => {
						dialog.attr('content', reply);
					});
				});

				dialog.show();
			}
		};

		PluginHost.register(PluginHost.HOOK_HEADLINE_RENDERED, function() {
			Plugins.Note.set_click_handler();
			return true;
		});
	});
});