Merge pull request #1303 from umap-project/icons-quick-changes

Enhance icon selector form
This commit is contained in:
Yohan Boniface 2023-09-08 16:01:25 +02:00 committed by GitHub
commit aca473692a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 41 deletions

View file

@ -185,6 +185,9 @@ select[multiple="multiple"] {
.dark .button:hover, .dark input[type="submit"]:hover { .dark .button:hover, .dark input[type="submit"]:hover {
background-color: #2e3436; background-color: #2e3436;
} }
.dark a {
color: #eeeeec;
}
.help-text, .helptext { .help-text, .helptext {
display: block; display: block;
padding: 7px 7px; padding: 7px 7px;
@ -396,6 +399,7 @@ input.switch:checked ~ label:after {
padding-top: 5px; padding-top: 5px;
margin-top: 5px; margin-top: 5px;
} }
.umap-field-iconUrl .action-button,
.inheritable .define, .inheritable .define,
.inheritable .undefine { .inheritable .undefine {
float: right; float: right;
@ -498,13 +502,6 @@ i.info {
margin-top: -8px; margin-top: -8px;
padding: 0 5px; padding: 0 5px;
} }
.umap-form-iconfield {
position: relative;
overflow: hidden;
padding-bottom: 5px;
padding-top: 5px;
line-height: 30px;
}
.umap-icon-list, .umap-pictogram-list { .umap-icon-list, .umap-pictogram-list {
clear: both; clear: both;
} }

View file

@ -518,31 +518,25 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
}, },
build: function () { build: function () {
this.options.helpText = this.builder.map.help.formatIconSymbol
L.FormBuilder.BlurInput.prototype.build.call(this) L.FormBuilder.BlurInput.prototype.build.call(this)
this.parentContainer = L.DomUtil.create( // Try to guess if the icon content has been customized, and if yes
'div', // directly display the field
'umap-form-iconfield', this.input.type = this.value() && !this.value().startsWith('/') ? 'text' : 'hidden'
this.parentNode
)
this.buttonsContainer = L.DomUtil.create('div', '', this.parentContainer)
this.pictogramsContainer = L.DomUtil.create(
'div',
'umap-pictogram-list',
this.parentContainer
)
this.input.type = 'hidden'
this.input.placeholder = L._('Symbol or url') this.input.placeholder = L._('Symbol or url')
this.buttonsContainer = L.DomUtil.create('div', '')
this.pictogramsContainer = L.DomUtil.create('div', 'umap-pictogram-list')
L.DomUtil.before(this.input, this.buttonsContainer)
L.DomUtil.before(this.input, this.pictogramsContainer)
this.udpatePreview() this.udpatePreview()
this.on('define', this.fetchIconList) this.on('define', this.fetchIconList)
}, },
isUrl: function () { isUrl: function () {
return this.value().indexOf('/') !== -1 return this.value() && this.value().indexOf('/') !== -1
}, },
udpatePreview: function () { udpatePreview: function () {
if (L.Util.hasVar(this.value())) { if (!L.Util.hasVar(this.value())) {
// Do not try to render URL with variables // Do not try to render URL with variables
if (this.isUrl()) { if (this.isUrl()) {
const img = L.DomUtil.create( const img = L.DomUtil.create(
@ -562,8 +556,8 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
L.DomEvent.on(el, 'click', this.fetchIconList, this) L.DomEvent.on(el, 'click', this.fetchIconList, this)
} }
} }
this.button = L.DomUtil.create('a', '', this.buttonsContainer) this.button = L.DomUtil.create('a', 'button action-button', this.buttonsContainer)
this.button.textContent = this.value() ? L._('Change symbol') : L._('Add symbol') this.button.textContent = this.value() ? L._('Change') : L._('Add')
this.button.href = '#' this.button.href = '#'
L.DomEvent.on(this.button, 'click', L.DomEvent.stop).on( L.DomEvent.on(this.button, 'click', L.DomEvent.stop).on(
this.button, this.button,
@ -581,7 +575,7 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
img = L.DomUtil.create('img', '', container) img = L.DomUtil.create('img', '', container)
img.src = value img.src = value
if (pictogram.name && pictogram.attribution) { if (pictogram.name && pictogram.attribution) {
img.title = `${pictogram.name} — © ${pictogram.attribution}` container.title = `${pictogram.name} — © ${pictogram.attribution}`
} }
L.DomEvent.on( L.DomEvent.on(
container, container,
@ -591,8 +585,6 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
this.sync() this.sync()
this.unselectAll(this.pictogramsContainer) this.unselectAll(this.pictogramsContainer)
L.DomUtil.addClass(container, 'selected') L.DomUtil.addClass(container, 'selected')
this.pictogramsContainer.innerHTML = ''
this.udpatePreview()
}, },
this this
) )
@ -606,19 +598,30 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
this.udpatePreview() this.udpatePreview()
}, },
search: function (e) {
const icons = [...this.parentNode.querySelectorAll('.umap-icon-choice')],
search = this.searchInput.value.toLowerCase()
icons.forEach((el) => {
if (el.title.toLowerCase().indexOf(search) != -1) el.style.display = 'block'
else el.style.display = 'none'
})
},
buildIconList: function (data) { buildIconList: function (data) {
this.pictogramsContainer.innerHTML = '' this.searchInput = L.DomUtil.create('input', '', this.pictogramsContainer)
this.buttonsContainer.innerHTML = '' this.searchInput.type = 'search'
this.searchInput.placeholder = L._('Search')
L.DomEvent.on(this.searchInput, 'input', this.search, this)
for (const idx in data.pictogram_list) { for (const idx in data.pictogram_list) {
this.addIconPreview(data.pictogram_list[idx]) this.addIconPreview(data.pictogram_list[idx])
} }
const cancelButton = L.DomUtil.create('a', '', this.pictogramsContainer) const closeButton = L.DomUtil.create('a', 'button action-button', this.pictogramsContainer)
cancelButton.textContent = L._('Cancel') closeButton.textContent = L._('Close')
cancelButton.href = '#' closeButton.href = '#'
cancelButton.style.display = 'block' closeButton.style.display = 'block'
cancelButton.style.clear = 'both' closeButton.style.clear = 'both'
L.DomEvent.on(cancelButton, 'click', L.DomEvent.stop).on( L.DomEvent.on(closeButton, 'click', L.DomEvent.stop).on(
cancelButton, closeButton,
'click', 'click',
function (e) { function (e) {
this.pictogramsContainer.innerHTML = '' this.pictogramsContainer.innerHTML = ''
@ -627,7 +630,7 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
this this
) )
const customButton = L.DomUtil.create('a', '', this.pictogramsContainer) const customButton = L.DomUtil.create('a', '', this.pictogramsContainer)
customButton.textContent = L._('Set symbol') customButton.textContent = L._('Toggle direct input (advanced)')
customButton.href = '#' customButton.href = '#'
customButton.style.display = 'block' customButton.style.display = 'block'
customButton.style.clear = 'both' customButton.style.clear = 'both'
@ -636,14 +639,16 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
customButton, customButton,
'click', 'click',
function (e) { function (e) {
this.input.type = 'text' this.input.type = this.input.type === 'text' ? 'hidden' : 'text'
this.pictogramsContainer.innerHTML = ''
}, },
this this
) )
}, },
fetchIconList: function (e) { fetchIconList: function (e) {
// Clean parent element before calling ajax, to prevent blinking
this.pictogramsContainer.innerHTML = ''
this.buttonsContainer.innerHTML = ''
this.builder.map.get(this.builder.map.options.urls.pictogram_list_json, { this.builder.map.get(this.builder.map.options.urls.pictogram_list_json, {
callback: this.buildIconList, callback: this.buildIconList,
context: this, context: this,
@ -694,7 +699,7 @@ L.FormBuilder.FacetSearch = L.FormBuilder.Element.extend({
}, },
buildLabel: function () { buildLabel: function () {
this.label = L.DomUtil.add('h5', '', this.parentNode, this.options.label); this.label = L.DomUtil.add('h5', '', this.parentNode, this.options.label)
}, },
buildLi: function (value) { buildLi: function (value) {
@ -711,7 +716,7 @@ L.FormBuilder.FacetSearch = L.FormBuilder.Element.extend({
}, },
toJS: function () { toJS: function () {
return [...this.ul.querySelectorAll('input:checked')].map(i => i.dataset.value) return [...this.ul.querySelectorAll('input:checked')].map((i) => i.dataset.value)
}, },
}) })