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) {
padding: .2em;
}
> a.sug-sel {
background-color: #242424;
}
}
}
.tag-container {

@ -140,12 +140,21 @@ taginputs.forEach(function (tinput) {
// })
// }
let vals = suggest_input.value.split(' ')
console.log(vals)
// console.log(vals)
vals.forEach(tag => {
if (tag == '') return
add_selected(tag)
});
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
let drop = suggest_dropdown
@ -163,9 +172,7 @@ taginputs.forEach(function (tinput) {
let sugtag = create_suggestion_tag(el.content)
sugtag.addEventListener('click', (event) => {
add_selected(event.target.dataset.tagname)
sug_input.value = ''
render_suggestions([])
select_suggestion(event.target)
event.preventDefault()
})
@ -195,15 +202,58 @@ taginputs.forEach(function (tinput) {
// fill suggestions
// console.log(data)
render_suggestions(data)
update_sug_selection('0')
})
}, 500)
})
// search_input.addEventListener('keypress', (event) => {
// if (event.keyCode == 13) {
// // event.preventDefault()
// }
// })
let sug_selected = 0
function update_sug_selection(move) {
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) => {
// if (event.explicitOriginalTarget != null) {
// classlist = event.explicitOriginalTarget.parentNode.classList

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

@ -1,6 +1,6 @@
from wtforms import Form
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
@ -65,7 +65,7 @@ def validate_file(form, field):
class UploadForm(CSRFForm):
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)
rating = RadioField('Rating',
choices=[(e.name, e.name.capitalize()) for e in RATING],
@ -153,7 +153,7 @@ class PostForm(EditForm):
status = SelectField('Status',
choices=[(e.name, e.name.capitalize()) for e in POST_STATUS],
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()
approve = SubmitField('Approve')

File diff suppressed because one or more lines are too long

@ -197,12 +197,21 @@ taginputs.forEach(function (tinput) {
// })
// }
let vals = suggest_input.value.split(' ')
console.log(vals)
// console.log(vals)
vals.forEach(tag => {
if (tag == '') return
add_selected(tag)
});
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
let drop = suggest_dropdown
@ -220,9 +229,7 @@ taginputs.forEach(function (tinput) {
let sugtag = create_suggestion_tag(el.content)
sugtag.addEventListener('click', (event) => {
add_selected(event.target.dataset.tagname)
sug_input.value = ''
render_suggestions([])
select_suggestion(event.target)
event.preventDefault()
})
@ -252,15 +259,58 @@ taginputs.forEach(function (tinput) {
// fill suggestions
// console.log(data)
render_suggestions(data)
update_sug_selection('0')
})
}, 500)
})
// search_input.addEventListener('keypress', (event) => {
// if (event.keyCode == 13) {
// // event.preventDefault()
// }
// })
let sug_selected = 0
function update_sug_selection(move) {
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) => {
// if (event.explicitOriginalTarget != null) {
// classlist = event.explicitOriginalTarget.parentNode.classList

Loading…
Cancel
Save