summaryrefslogtreecommitdiff
path: root/js/PrefLabelTree.js
blob: 39e3f83153ae903464e1126e3a2f17b72fdac076 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* eslint-disable prefer-rest-params */
/* global __, define, lib, dijit, dojo, xhr, Notify, fox, App */

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 label = dojo.doc.createElement('i');
				//const fg_color = args.item.fg_color[0];
				const bg_color = String(args.item.bg_color);

				label.className = "material-icons icon-label";
				label.id = 'icon-label-' + String(args.item.bare_id);
				label.innerHTML = "label";
				label.setStyle({
					color: bg_color,
				});

				domConstruct.place(label, tnode.iconNode, 'before');

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

			return tnode;
		},
		getIconClass: function (item, opened) {
			// eslint-disable-next-line no-nested-ternary
			return (!item || this.model.mayHaveChildren(item)) ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "invisible";
		},
		getSelectedLabels: function() {
			const tree = this;
			const items = tree.model.getCheckedItems();
			const rv = [];

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

			return rv;
		},
		reload: function() {
			xhr.post("backend.php", { op: "pref-labels" }, (reply) => {
				dijit.byId('labelsTab').attr('content', reply);
				Notify.close();
			});
		},
		editLabel: function(id) {
			xhr.json("backend.php", {op: "pref-labels", method: "edit", id: id}, (reply) => {

				const fg_color = reply['fg_color'];
				const bg_color = reply['bg_color'] ? reply['bg_color'] : '#fff7d5';

				const dialog = new fox.SingleUseDialog({
					id: "labelEditDlg",
					title: __("Edit label"),
					setLabelColor: function (id, fg, bg) {

						let kind = '';
						let color = '';

						if (fg && bg) {
							kind = 'both';
						} else if (fg) {
							kind = 'fg';
							color = fg;
						} else if (bg) {
							kind = 'bg';
							color = bg;
						}

						const e = App.byId(`icon-label-${id}`);

						if (e) {
							if (bg) e.style.color = bg;
						}

						const query = {
							op: "pref-labels", method: "colorset", kind: kind,
							ids: id, fg: fg, bg: bg, color: color
						};

						xhr.post("backend.php", query, () => {
							const tree = dijit.byId("filterTree");
							if (tree) tree.reload(); // maybe there's labels in there
						});

					},
					execute: function () {
						if (this.validate()) {
							const caption = this.attr('value').caption;
							const fg_color = this.attr('value').fg_color;
							const bg_color = this.attr('value').bg_color;

							dijit.byId('labelTree').setNameById(id, caption);
							this.setLabelColor(id, fg_color, bg_color);
							this.hide();

							xhr.post("backend.php", this.attr('value'), () => {
								const tree = dijit.byId("filterTree");
								if (tree) tree.reload(); // maybe there's labels in there
							});
						}
					},
					content: `
						<form onsubmit='return false'>

						<section>
							<input style='font-size : 16px; width : 550px; color : ${fg_color}; background : ${bg_color}; transition : background 0.1s linear'
								id='labelEdit_caption'
								placeholder="${__("Caption")}"
								name='caption'
								dojoType='dijit.form.ValidationTextBox'
								required='true'
								value="${App.escapeHtml(reply.caption)}">
						</section>

						${App.FormFields.hidden_tag('id', id)}
						${App.FormFields.hidden_tag('op', 'pref-labels')}
						${App.FormFields.hidden_tag('method', 'save')}

						${App.FormFields.hidden_tag('fg_color', fg_color, {}, 'labelEdit_fgColor')}
						${App.FormFields.hidden_tag('bg_color', bg_color, {}, 'labelEdit_bgColor')}

						<section>
							<table width='100%'>
								<tr>
									<th>${__("Foreground:")}</th>
									<th>${__("Background:")}</th>
								</tr>
								<tr>
									<td class='text-center'>
										<div dojoType='dijit.ColorPalette'>
											<script type='dojo/method' event='onChange' args='fg_color'>
												dijit.byId('labelEdit_fgColor').attr('value', fg_color);
												dijit.byId('labelEdit_caption').domNode.setStyle({color: fg_color});
											</script>
										</div>
									</td>
									<td class='text-center'>
										<div dojoType='dijit.ColorPalette'>
											<script type='dojo/method' event='onChange' args='bg_color'>
												dijit.byId('labelEdit_bgColor').attr('value', bg_color);
												dijit.byId('labelEdit_caption').domNode.setStyle({backgroundColor: bg_color});
											</script>
										</div>
									</td>
								</tr>
							</table>
						</section>

						<footer>
							<button dojoType='dijit.form.Button' type='submit' class='alt-primary' onclick='App.dialogOf(this).execute()'>
								${App.FormFields.icon("save")}
								${__('Save')}
							</button>
							<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>
								${__('Cancel')}
							</button>
						</footer>

						</form>
					`
				});

				dialog.show();

			});
		},
		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()
					};

					xhr.post("backend.php", query, () => {
						this.reload();
					});
				}

			} else {
				alert(__("No labels 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()
					};

					xhr.post("backend.php", query, () => {
						this.reload();
					});
				}
			} else {
				alert(__("No labels selected."));
			}

			return false;
		}
});

});