1
1
Fork 0

Better tag input selection,

post source url validator
dev
Jan Kužílek 5 years ago
parent ba1ea2d267
commit dbfa73a6ac

@ -214,6 +214,10 @@ label {
&:not(:empty) { &:not(:empty) {
padding: .2em; padding: .2em;
} }
> a.sug-sel {
background-color: #242424;
}
} }
} }
.tag-container { .tag-container {

@ -140,13 +140,22 @@ taginputs.forEach(function (tinput) {
// }) // })
// } // }
let vals = suggest_input.value.split(' ') let vals = suggest_input.value.split(' ')
console.log(vals) // console.log(vals)
vals.forEach(tag => { vals.forEach(tag => {
if (tag == '') return if (tag == '') return
add_selected(tag) add_selected(tag)
}); });
suggest_input.removeAttribute('value') // Disable prefill by browser suggest_input.removeAttribute('value') // Disable prefill by browser
function select_suggestion(tagelement) {
add_selected(tagelement.dataset.tagname)
suggest_input.value = ''
render_suggestions([])
// yeeeeeeeet
setTimeout(() => suggest_input.focus(), 2)
}
function render_suggestions(data) { // 3 references function render_suggestions(data) { // 3 references
let drop = suggest_dropdown let drop = suggest_dropdown
let sel = selected let sel = selected
@ -163,9 +172,7 @@ taginputs.forEach(function (tinput) {
let sugtag = create_suggestion_tag(el.content) let sugtag = create_suggestion_tag(el.content)
sugtag.addEventListener('click', (event) => { sugtag.addEventListener('click', (event) => {
add_selected(event.target.dataset.tagname) select_suggestion(event.target)
sug_input.value = ''
render_suggestions([])
event.preventDefault() event.preventDefault()
}) })
@ -195,15 +202,58 @@ taginputs.forEach(function (tinput) {
// fill suggestions // fill suggestions
// console.log(data) // console.log(data)
render_suggestions(data) render_suggestions(data)
update_sug_selection('0')
}) })
}, 500) }, 500)
}) })
// search_input.addEventListener('keypress', (event) => { let sug_selected = 0
// if (event.keyCode == 13) { function update_sug_selection(move) {
// // event.preventDefault() let suggs = suggest_dropdown.querySelectorAll('a.tagsuggestion')
// } let last_selected = sug_selected
// })
let subs = move.substring(0, 1)
if (subs == '+') {
sug_selected += parseInt(move.substring(1))
} else if (subs == '-') {
sug_selected -= parseInt(move.substring(1))
} else {
sug_selected = parseInt(move)
}
sug_selected = sug_selected%suggs.length
if (sug_selected < 0) sug_selected += suggs.length
try {
suggs[last_selected].classList.remove('sug-sel')
} catch (error) {
// console.log(error)
}
try {
suggs[sug_selected].classList.add('sug-sel')
} catch (error) {
// console.log(error)
}
}
suggest_input.addEventListener('keydown', (event) => {
// console.log(event)
if (event.keyCode == 13) {
if (suggest_input.value) {
select_suggestion(suggest_dropdown.querySelector('a.tagsuggestion.sug-sel'))
event.preventDefault()
}
}
if (event.keyCode == 40 || (event.keyCode == 9 && !event.shiftKey)) {
update_sug_selection('+1')
event.preventDefault()
}
if (event.keyCode == 38 || (event.keyCode == 9 && event.shiftKey)) {
update_sug_selection('-1')
event.preventDefault()
}
})
// search_input.addEventListener('blur', (event) => { // search_input.addEventListener('blur', (event) => {
// if (event.explicitOriginalTarget != null) { // if (event.explicitOriginalTarget != null) {
// classlist = event.explicitOriginalTarget.parentNode.classList // classlist = event.explicitOriginalTarget.parentNode.classList

@ -80,7 +80,7 @@ def modify_user():
flash('New {} has been created.'.format(str(el))) flash('New {} has been created.'.format(str(el)))
else: else:
el = User.query.filter_by(id=form.id.data).first() el = User.query.get(form.id.data)
if form.delete.data: if form.delete.data:
if el.is_current: if el.is_current:
@ -117,7 +117,7 @@ def modify_post():
if form.create.data: if form.create.data:
pass pass
else: else:
el = Post.query.filter_by(id=form.id.data).first() el = Post.query.get(form.id.data)
if not current_user.is_moderator and not (el.author.is_current if el.author is not None else None): if not current_user.is_moderator and not (el.author.is_current if el.author is not None else None):
flash("You don't have sufficient rights to do this.") flash("You don't have sufficient rights to do this.")
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
@ -152,10 +152,10 @@ def modify_post():
flash('Approved post {}'.format(str(el))) flash('Approved post {}'.format(str(el)))
# redirect(url_for('post.post_show', id=el.id)) # redirect(url_for('post.post_show', id=el.id))
flasherrors(form)
if form.referer.data == 'post_show': if form.referer.data == 'post_show':
return redirect(url_for('post.post_show', id=el.id)) return redirect(url_for('post.post_show', id=form.id.data))
flasherrors(form)
return redirect(url_for('.manage_posts')) return redirect(url_for('.manage_posts'))
# Example perfect create/edit/delete form endpoint # Example perfect create/edit/delete form endpoint
@ -176,7 +176,7 @@ def modify_tag():
db.session.commit() db.session.commit()
flash('New {} has been created.'.format(str(el))) flash('New {} has been created.'.format(str(el)))
else: else:
el = Tag.query.filter_by(id=form.id.data).first() el = Tag.query.get(form.id.data)
if form.delete.data: if form.delete.data:
db.session.delete(el) db.session.delete(el)

@ -1,6 +1,6 @@
from wtforms import Form from wtforms import Form
from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField, TextAreaField, HiddenField, SelectField from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField, TextAreaField, HiddenField, SelectField
from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, AnyOf, optional, StopValidation from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, AnyOf, optional, StopValidation, URL
from werkzeug.utils import cached_property from werkzeug.utils import cached_property
@ -65,7 +65,7 @@ def validate_file(form, field):
class UploadForm(CSRFForm): class UploadForm(CSRFForm):
post_img = FileField('Image', validators=[validate_file], render_kw={'required':''}) post_img = FileField('Image', validators=[validate_file], render_kw={'required':''})
sauce = StringField('Sauce', validators=[DataRequired()], render_kw=dict(placeholder='Source URL', autocomplete='off')) sauce = StringField('Sauce', validators=[DataRequired(), URL()], render_kw=dict(placeholder='Source URL', autocomplete='off'))
tags = StringField('Tags', validators=[DataRequired()], render_kw=dict(placeholder='Tags', autocomplete='off')) # CUSTOM VALIDATOR (also for Post edits) tags = StringField('Tags', validators=[DataRequired()], render_kw=dict(placeholder='Tags', autocomplete='off')) # CUSTOM VALIDATOR (also for Post edits)
rating = RadioField('Rating', rating = RadioField('Rating',
choices=[(e.name, e.name.capitalize()) for e in RATING], choices=[(e.name, e.name.capitalize()) for e in RATING],
@ -153,7 +153,7 @@ class PostForm(EditForm):
status = SelectField('Status', status = SelectField('Status',
choices=[(e.name, e.name.capitalize()) for e in POST_STATUS], choices=[(e.name, e.name.capitalize()) for e in POST_STATUS],
validators=[optional()]) validators=[optional()])
source = StringField('Source', render_kw=dict(placeholder='Source URL', autocomplete='off')) source = StringField('Source', validators=[optional(), URL()], render_kw=dict(placeholder='Source URL', autocomplete='off'))
referer = HiddenField() referer = HiddenField()
approve = SubmitField('Approve') approve = SubmitField('Approve')

File diff suppressed because one or more lines are too long

@ -197,13 +197,22 @@ taginputs.forEach(function (tinput) {
// }) // })
// } // }
let vals = suggest_input.value.split(' ') let vals = suggest_input.value.split(' ')
console.log(vals) // console.log(vals)
vals.forEach(tag => { vals.forEach(tag => {
if (tag == '') return if (tag == '') return
add_selected(tag) add_selected(tag)
}); });
suggest_input.removeAttribute('value') // Disable prefill by browser suggest_input.removeAttribute('value') // Disable prefill by browser
function select_suggestion(tagelement) {
add_selected(tagelement.dataset.tagname)
suggest_input.value = ''
render_suggestions([])
// yeeeeeeeet
setTimeout(() => suggest_input.focus(), 2)
}
function render_suggestions(data) { // 3 references function render_suggestions(data) { // 3 references
let drop = suggest_dropdown let drop = suggest_dropdown
let sel = selected let sel = selected
@ -220,9 +229,7 @@ taginputs.forEach(function (tinput) {
let sugtag = create_suggestion_tag(el.content) let sugtag = create_suggestion_tag(el.content)
sugtag.addEventListener('click', (event) => { sugtag.addEventListener('click', (event) => {
add_selected(event.target.dataset.tagname) select_suggestion(event.target)
sug_input.value = ''
render_suggestions([])
event.preventDefault() event.preventDefault()
}) })
@ -252,15 +259,58 @@ taginputs.forEach(function (tinput) {
// fill suggestions // fill suggestions
// console.log(data) // console.log(data)
render_suggestions(data) render_suggestions(data)
update_sug_selection('0')
}) })
}, 500) }, 500)
}) })
// search_input.addEventListener('keypress', (event) => { let sug_selected = 0
// if (event.keyCode == 13) { function update_sug_selection(move) {
// // event.preventDefault() let suggs = suggest_dropdown.querySelectorAll('a.tagsuggestion')
// } let last_selected = sug_selected
// })
let subs = move.substring(0, 1)
if (subs == '+') {
sug_selected += parseInt(move.substring(1))
} else if (subs == '-') {
sug_selected -= parseInt(move.substring(1))
} else {
sug_selected = parseInt(move)
}
sug_selected = sug_selected%suggs.length
if (sug_selected < 0) sug_selected += suggs.length
try {
suggs[last_selected].classList.remove('sug-sel')
} catch (error) {
// console.log(error)
}
try {
suggs[sug_selected].classList.add('sug-sel')
} catch (error) {
// console.log(error)
}
}
suggest_input.addEventListener('keydown', (event) => {
// console.log(event)
if (event.keyCode == 13) {
if (suggest_input.value) {
select_suggestion(suggest_dropdown.querySelector('a.tagsuggestion.sug-sel'))
event.preventDefault()
}
}
if (event.keyCode == 40 || (event.keyCode == 9 && !event.shiftKey)) {
update_sug_selection('+1')
event.preventDefault()
}
if (event.keyCode == 38 || (event.keyCode == 9 && event.shiftKey)) {
update_sug_selection('-1')
event.preventDefault()
}
})
// search_input.addEventListener('blur', (event) => { // search_input.addEventListener('blur', (event) => {
// if (event.explicitOriginalTarget != null) { // if (event.explicitOriginalTarget != null) {
// classlist = event.explicitOriginalTarget.parentNode.classList // classlist = event.explicitOriginalTarget.parentNode.classList

Loading…
Cancel
Save