summaryrefslogtreecommitdiff
path: root/js/PrefLabelTree.js
blob: e5eb595e5f82dabf41e6665bc3a1ea0becab4999 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* global lib,dijit */
define(["dojo/_base/declare", "dojo/dom-construct", "lib/CheckBoxTree", "dijit/form/DropDownButton"], function (declare, domConstruct) {

	return declare("fox.PrefLabelTree", lib.CheckBoxTree, {
		setNameById: function (id, name) {
			const item = this.model.store._itemsByIdentity['LABEL:' + id];

			if (item)
				this.model.store.setValue(item, 'name', name);

		},
		_createTreeNode: function(args) {
			const tnode = this.inherited(arguments);

			const fg_color = this.model.store.getValue(args.item, 'fg_color');
			const bg_color = this.model.store.getValue(args.item, 'bg_color');
			const type = this.model.store.getValue(args.item, 'type');
			const bare_id = this.model.store.getValue(args.item, 'bare_id');

			if (type == 'label') {
				const span = dojo.doc.createElement('span');
				span.innerHTML = 'α';
				span.className = 'labelColorIndicator';
				span.id = 'LICID-' + bare_id;

				span.setStyle({
					color: fg_color,
					backgroundColor: bg_color});

				tnode._labelIconNode = span;

				domConstruct.place(tnode._labelIconNode, tnode.labelNode, 'before');
			}

			return tnode;
		},
		getIconClass: function (item, opened) {
			return (!item || this.model.mayHaveChildren(item)) ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "invisible";
		},
		getSelectedLabels: function() {
			const tree = this;
			const items = tree.model.getCheckedItems();
			const rv = [];

			items.each(function(item) {
				rv.push(tree.model.store.getValue(item, 'bare_id'));
			});

			return rv;
		},
		resetColors: function() {
			const labels = this.getSelectedLabels();

			if (labels.length > 0) {
				if (confirm(__("Reset selected labels to default colors?"))) {

					const query = {
						op: "pref-labels", method: "colorreset",
						ids: labels.toString()
					};

					xhrPost("backend.php", query, () => {
						updateLabelList();
					});
				}

			} else {
				alert(__("No labels are selected."));
			}
		},
		removeSelected: function() {
			const sel_rows = this.getSelectedLabels();

			if (sel_rows.length > 0) {
				if (confirm(__("Remove selected labels?"))) {
					notify_progress("Removing selected labels...");

					const query = {
						op: "pref-labels", method: "remove",
						ids: sel_rows.toString()
					};

					xhrPost("backend.php", query, () => {
						updateLabelList();
					});
				}
			} else {
				alert(__("No labels are selected."));
			}

			return false;
		}
});

});