ttrss/js/PrefLabelTree.js

232 lines
6.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable prefer-rest-params */
2021-02-19 09:28:14 +01:00
/* global __, define, lib, dijit, dojo, xhr, Notify, fox, App */
define(["dojo/_base/declare", "dojo/dom-construct", "lib/CheckBoxTree", "dijit/form/DropDownButton"], function (declare, domConstruct) {
2010-11-18 18:04:57 +01:00
return declare("fox.PrefLabelTree", lib.CheckBoxTree, {
setNameById: function (id, name) {
const item = this.model.store._itemsByIdentity['LABEL:' + id];
2010-11-18 18:04:57 +01:00
if (item)
this.model.store.setValue(item, 'name', name);
2010-11-18 18:04:57 +01:00
},
_createTreeNode: function(args) {
const tnode = this.inherited(arguments);
2010-11-18 18:04:57 +01:00
2021-02-12 16:38:26 +01:00
//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');
2021-02-12 16:38:26 +01:00
//const bare_id = this.model.store.getValue(args.item, 'bare_id');
2010-11-18 18:04:57 +01:00
if (type == 'label') {
2018-12-06 06:26:52 +01:00
const label = dojo.doc.createElement('i');
//const fg_color = args.item.fg_color[0];
const bg_color = String(args.item.bg_color);
2010-11-18 18:04:57 +01:00
2018-12-06 06:26:52 +01:00
label.className = "material-icons icon-label";
label.id = 'icon-label-' + String(args.item.bare_id);
2018-12-06 06:26:52 +01:00
label.innerHTML = "label";
label.setStyle({
color: bg_color,
});
2010-11-18 18:04:57 +01:00
2018-12-06 06:26:52 +01:00
domConstruct.place(label, tnode.iconNode, 'before');
2010-11-18 18:04:57 +01:00
2018-12-06 06:26:52 +01:00
//tnode._labelIconNode = span;
//domConstruct.place(tnode._labelIconNode, tnode.labelNode, 'before');
}
2010-11-18 18:04:57 +01:00
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;
},
2018-12-02 14:29:00 +01:00
reload: function() {
2023-10-25 11:55:09 +02:00
xhr.post("backend.php", { op: "Pref_Labels" }, (reply) => {
dijit.byId('labelsTab').attr('content', reply);
2018-12-02 18:56:30 +01:00
Notify.close();
2018-12-02 14:29:00 +01:00
});
},
editLabel: function(id) {
2023-10-25 11:55:09 +02:00
xhr.json("backend.php", {op: "Pref_Labels", method: "edit", id: id}, (reply) => {
2021-02-13 11:17:34 +01:00
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"),
2021-02-13 11:17:34 +01:00
setLabelColor: function (id, fg, bg) {
let kind = '';
let color = '';
2021-02-13 11:17:34 +01:00
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}`);
2021-02-13 11:17:34 +01:00
if (e) {
if (bg) e.style.color = bg;
}
const query = {
2023-10-25 11:55:09 +02:00
op: "Pref_Labels", method: "colorset", kind: kind,
2021-02-13 11:17:34 +01:00
ids: id, fg: fg, bg: bg, color: color
};
2021-02-19 09:28:14 +01:00
xhr.post("backend.php", query, () => {
const tree = dijit.byId("filterTree");
if (tree) tree.reload(); // maybe there's labels in there
});
2021-02-12 16:38:26 +01:00
2021-02-13 11:17:34 +01:00
},
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();
2021-02-19 09:28:14 +01:00
xhr.post("backend.php", this.attr('value'), () => {
2021-02-13 11:17:34 +01:00
const tree = dijit.byId("filterTree");
if (tree) tree.reload(); // maybe there's labels in there
});
}
},
content: `
<form onsubmit='return false'>
<section>
2021-03-05 13:16:41 +01:00
<input style='font-size : 16px; width : 550px; color : ${fg_color}; background : ${bg_color}; transition : background 0.1s linear'
2021-02-13 11:17:34 +01:00
id='labelEdit_caption'
2021-03-05 13:16:41 +01:00
placeholder="${__("Caption")}"
2021-02-13 11:17:34 +01:00
name='caption'
dojoType='dijit.form.ValidationTextBox'
required='true'
value="${App.escapeHtml(reply.caption)}">
</section>
${App.FormFields.hidden_tag('id', id)}
2023-10-25 11:55:09 +02:00
${App.FormFields.hidden_tag('op', 'Pref_Labels')}
${App.FormFields.hidden_tag('method', 'save')}
2021-02-13 11:17:34 +01:00
${App.FormFields.hidden_tag('fg_color', fg_color, {}, 'labelEdit_fgColor')}
${App.FormFields.hidden_tag('bg_color', bg_color, {}, 'labelEdit_bgColor')}
2021-02-13 11:17:34 +01:00
<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()'>
2021-03-05 13:16:41 +01:00
${App.FormFields.icon("save")}
2021-02-13 11:17:34 +01:00
${__('Save')}
</button>
<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>
${__('Cancel')}
</button>
</footer>
</form>
`
});
2021-02-12 16:38:26 +01:00
2021-02-13 11:17:34 +01:00
dialog.show();
2021-02-13 11:17:34 +01:00
});
},
resetColors: function() {
const labels = this.getSelectedLabels();
if (labels.length > 0) {
if (confirm(__("Reset selected labels to default colors?"))) {
const query = {
2023-10-25 11:55:09 +02:00
op: "Pref_Labels", method: "colorreset",
ids: labels.toString()
};
2021-02-19 09:28:14 +01:00
xhr.post("backend.php", query, () => {
2018-12-02 14:29:00 +01:00
this.reload();
});
}
} else {
2018-12-02 14:30:32 +01:00
alert(__("No labels selected."));
}
},
removeSelected: function() {
const sel_rows = this.getSelectedLabels();
if (sel_rows.length > 0) {
if (confirm(__("Remove selected labels?"))) {
2018-12-02 18:56:30 +01:00
Notify.progress("Removing selected labels...");
const query = {
2023-10-25 11:55:09 +02:00
op: "Pref_Labels", method: "remove",
ids: sel_rows.toString()
};
2021-02-19 09:28:14 +01:00
xhr.post("backend.php", query, () => {
2018-12-02 14:29:00 +01:00
this.reload();
});
}
} else {
2018-12-02 14:30:32 +01:00
alert(__("No labels selected."));
}
return false;
}
});
2010-11-18 18:04:57 +01:00
});