public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager v4 07/10] ui: add form/Tag
Date: Wed, 23 Mar 2022 11:34:42 +0100	[thread overview]
Message-ID: <20220323103445.2075649-10-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220323103445.2075649-1-d.csapak@proxmox.com>

displays a single tag, with the ability to edit inline on click. This brings
up a list of globally available tags for simple selection.

Also has a mode for adding a new Tag.

This has a 'updateCallback' which is called when the value changes
so that the parent component can update the config, and a
'layoutCallback' which will be called on input, so that the parent
component can update the layout when the content changes.
This is necessary since we circumvent the extjs logic for updating.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 www/manager6/Makefile    |   1 +
 www/manager6/form/Tag.js | 207 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 208 insertions(+)
 create mode 100644 www/manager6/form/Tag.js

diff --git a/www/manager6/Makefile b/www/manager6/Makefile
index 225dffba..45862e71 100644
--- a/www/manager6/Makefile
+++ b/www/manager6/Makefile
@@ -74,6 +74,7 @@ JSSRC= 							\
 	form/ViewSelector.js				\
 	form/iScsiProviderSelector.js			\
 	form/TagColorGrid.js				\
+	form/Tag.js					\
 	grid/BackupView.js				\
 	grid/FirewallAliases.js				\
 	grid/FirewallOptions.js				\
diff --git a/www/manager6/form/Tag.js b/www/manager6/form/Tag.js
new file mode 100644
index 00000000..5c308f51
--- /dev/null
+++ b/www/manager6/form/Tag.js
@@ -0,0 +1,207 @@
+Ext.define('Proxmox.Tag', {
+    extend: 'Ext.Component',
+    alias: 'widget.pmxTag',
+
+    // if set to true, displays 'Add Tag' and a plus symbol
+    addTag: false,
+
+    // callback to update the config
+    updateCallback: Ext.emptyFn,
+
+    // callback to update the layout in the containing element
+    // this is necessary since we circumvent extjs layout with 'contentEditable'
+    layoutCallback: Ext.emptyFn,
+
+    inEdit: false,
+    style: {
+	'padding-right': '1px',
+	'white-space': 'nowrap',
+    },
+
+    icons: {
+	addTag: 'plus',
+	normal: 'times',
+	edit: 'check',
+    },
+
+    // we need to do this in mousedown, because that triggers before
+    // focusleave (which triggers before click)
+    onMouseDown: function(event) {
+	let me = this;
+	if (event.target.tagName === "I" && (!me.addTag || me.inEdit)) {
+	    if (!me.inEdit) {
+		me.setVisible(false);
+		me.setText('');
+	    }
+	    me.finishEdit();
+	}
+    },
+
+    onClick: function(event) {
+	let me = this;
+	if ((me.addTag || event.target.tagName === "SPAN") && !me.inEdit) {
+	    if (me.addTag) {
+		me.setText('');
+		me.setStyle('cursor');
+	    }
+	    me.inEdit = true;
+	    me.tagEl().contentEditable = true;
+	    me.iconEl().classList = `fa fa-fw fa-${me.icons.edit}-circle`;
+
+	    // select text in the element
+	    let range = document.createRange();
+	    range.selectNodeContents(me.tagEl());
+	    let sel = window.getSelection();
+	    sel.removeAllRanges();
+	    sel.addRange(range);
+
+	    me.showPicker();
+	}
+    },
+
+    showPicker: function() {
+	let me = this;
+	if (!me.picker) {
+	    me.picker = Ext.widget({
+		xtype: 'boundlist',
+		minWidth: 70,
+		scrollable: true,
+		floating: true,
+		hidden: true,
+		userCls: 'proxmox-tags-circle',
+		displayField: 'tag',
+		itemTpl: [
+		    '{[Proxmox.Utils.getTagElement(values.tag, PVE.Utils.getTagOverrides())]} {tag}',
+		],
+		store: [],
+		listeners: {
+		    select: function(picker, rec) {
+			me.setText(rec.data.tag);
+			me.finishEdit();
+		    },
+		},
+	    });
+	}
+	me.picker.getStore().clearFilter();
+	let taglist = PVE.Utils.getTagList().map(v => ({ tag: v }));
+	if (taglist.length < 1) {
+	    return;
+	}
+	me.picker.getStore().setData(taglist);
+	me.picker.showBy(me, 'tl-bl');
+	me.picker.setMaxHeight(200);
+    },
+
+    finishEdit: function(update = true) {
+	let me = this;
+	me.picker?.hide();
+
+	let tag = me.tagEl().innerHTML;
+	let mode;
+	if (me.addTag) {
+	    me.setText(me.tag);
+	    me.setStyle('cursor', 'pointer');
+	    mode = 'addTag';
+	} else {
+	    me.tag = tag;
+	    mode = 'normal';
+	}
+	me.iconEl().classList = `fa fa-fw fa-${me.icons[mode]}-circle`;
+	me.updateColor(me.tag);
+
+
+	if (update) {
+	    me.updateCallback(tag);
+	}
+	me.tagEl().contentEditable = false;
+	me.inEdit = false;
+    },
+
+    setText: function(text) {
+	let me = this;
+	me.tagEl().innerHTML = text;
+	me.layoutCallback();
+    },
+
+    cancelEdit: function(list, event) {
+	let me = this;
+	if (me.inEdit) {
+	    me.setText(me.tag);
+	    me.finishEdit(false);
+	}
+    },
+
+    onKeyPress: function(event) {
+	let me = this;
+	let key = event.browserEvent.key;
+	if (key === "Enter") {
+	    me.finishEdit();
+	} else if (!key.match(/^[a-z0-9+\-_.]$/i)) {
+	    event.browserEvent.preventDefault();
+	    event.browserEvent.stopPropagation();
+	}
+    },
+
+    onInput: function() {
+	let me = this;
+	let tag = me.tagEl().innerHTML;
+	me.layoutCallback();
+	me.picker.getStore().filter({
+	    property: 'tag',
+	    value: tag,
+	});
+    },
+
+    listeners: {
+	mousedown: 'onMouseDown',
+	click: 'onClick',
+	focusleave: 'cancelEdit',
+	keypress: 'onKeyPress',
+	input: 'onInput',
+	element: 'el',
+    },
+
+    updateColor: function(tag) {
+	let me = this;
+	if (me.addTag) {
+	    me.setUserCls(`proxmox-tag-dark`);
+	    me.setStyle('background-color');
+	    return;
+	}
+	let rgb = PVE.Utils.getTagOverrides()?.[tag] ?? Proxmox.Utils.stringToRGB(tag);
+	let color = Proxmox.Utils.rgbToCss(rgb);
+	let cls = Proxmox.Utils.rgbToLuminance(rgb) < 160 ? 'light' : 'dark';
+	me.setUserCls(`proxmox-tag-${cls}`);
+	me.setStyle('background-color', color);
+    },
+
+    tagEl: function() {
+	return this.el?.dom?.children?.[0];
+    },
+
+    iconEl: function() {
+	return this.el?.dom?.children?.[1];
+    },
+
+    initComponent: function() {
+	let me = this;
+	if (me.tag === undefined && !me.addTag) {
+	    throw "no tag given";
+	}
+
+	let mode = 'normal';
+	if (me.addTag) {
+	    me.tag = gettext('Add Tag');
+	    mode = 'addTag';
+	}
+
+	let icon = `<i style="cursor: pointer;" class="fa fa-fw fa-${me.icons[mode]}-circle"></i>`;
+	me.html = `<span>${me.tag}</span> ${icon}`;
+
+	me.callParent();
+	me.updateColor(me.tag);
+	if (me.addTag) {
+	    me.setStyle('cursor', 'pointer');
+	}
+    },
+});
-- 
2.30.2





  parent reply	other threads:[~2022-03-23 10:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 10:34 [pve-devel] [PATCH cluster/widget-toolkit/manager v4] add tags to ui Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH cluster v4 1/1] datacenter.cfg: add option for tag-tree-style and tag-colors Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH widget-toolkit v4 1/1] add tag related helpers Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 01/10] api: /cluster/resources: add tags to returned properties Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 02/10] api: /version: add 'tag-colors' and 'tag-tree-style' Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 03/10] ui: parse and save tag color overrides from /version Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 04/10] ui: tree/ResourceTree: collect tags on update Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 05/10] ui: add form/TagColorGrid Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 06/10] ui: dc/OptionView: add editors for tag settings Dominik Csapak
2022-03-23 10:34 ` Dominik Csapak [this message]
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 08/10] ui: {lxc, qemu}/Config: show Tags and make them editable Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 09/10] ui: tree/ResourceTree: show Tags in tree Dominik Csapak
2022-03-23 10:34 ` [pve-devel] [PATCH manager v4 10/10] ui: form/GlobalSearchField: display tags and allow to search for them Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220323103445.2075649-10-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal