-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from michelegiorgi/development
Release 1.4.1
- Loading branch information
Showing
25 changed files
with
485 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Formality admin script | ||
|
||
import 'jquery'; | ||
import init from './admin/init' | ||
import main from './admin/main' | ||
import exports from './admin/export' | ||
|
||
jQuery(document).ready(() => { | ||
init() | ||
main() | ||
exports() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,43 @@ | ||
// Formality public scripts | ||
|
||
// Formality public script | ||
import 'jquery'; | ||
import init from './public/init'; | ||
|
||
jQuery(document).ready(() => init()); | ||
//core functions | ||
import loader from './public/core/loader' | ||
import uiux from './public/core/uiux' | ||
import nav from './public/core/nav' | ||
import validate from './public/core/validate' | ||
import submit from './public/core/submit' | ||
import conditional from './public/core/conditional' | ||
import embed from './public/core/embed' | ||
import hints from './public/core/hints' | ||
import hooks from './public/core/hooks' | ||
|
||
//fields functions | ||
import select from './public/fields/select' | ||
import switch1 from './public/fields/switch' | ||
import textarea from './public/fields/textarea' | ||
import number from './public/fields/number' | ||
import rating from './public/fields/rating' | ||
import multiple from './public/fields/multiple' | ||
import media from './public/fields/media' | ||
import upload from './public/fields/upload' | ||
|
||
jQuery(document).ready(() => { | ||
loader.init() | ||
uiux.init() | ||
submit.init() | ||
nav.init() | ||
validate.init() | ||
conditional.init() | ||
embed.init() | ||
hints.init() | ||
|
||
select.init() | ||
switch1.init() | ||
textarea.init() | ||
number.init() | ||
rating.init() | ||
multiple.init() | ||
media.init() | ||
upload.init() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
import { el, uid } from './helpers' | ||
import 'parsleyjs' | ||
const { __ } = wp.i18n | ||
let fieldOptions = { | ||
text: { | ||
multiple: false, | ||
}, | ||
message: { | ||
multiple: false, | ||
}, | ||
email: { | ||
multiple: false, | ||
rules: { | ||
email: __("This value should be a valid email", "formality"), | ||
} | ||
}, | ||
number: { | ||
multiple: false, | ||
rules: { | ||
number: __("This value should be a valid number", "formality"), | ||
min: /* translators: validation */ __("This value should be greater than or equal to %s", "formality"), | ||
max: /* translators: validation */ __("This value should be lower than or equal to %s", "formality"), | ||
} | ||
}, | ||
select: { | ||
multiple: false, | ||
}, | ||
multiple: { | ||
multiple: true, | ||
}, | ||
rating: { | ||
multiple: true, | ||
}, | ||
switch: { | ||
multiple: false, | ||
}, | ||
upload: { | ||
multiple: false, | ||
}, | ||
} | ||
|
||
export default { | ||
init() { | ||
//init validation | ||
$(el("form")).each(function() { | ||
uid($(this)) | ||
$(el("section", "uid")).each(function(index, section) { | ||
$(section).find(':input').attr('data-parsley-group', 'step-' + index) | ||
}) | ||
}) | ||
this.field_error() | ||
this.field_success() | ||
this.form_error() | ||
this.i18n() | ||
$('body').prepend('<button id="testvalidate">Test</button>'); | ||
let validate = this; | ||
$('#testvalidate').click(function(){ | ||
let form = document.querySelector(el("form")) | ||
validate.validateForm(form) | ||
}) | ||
}, | ||
checkstep(index, newindex) { | ||
//validate single step | ||
let valid = false | ||
let options = this.parsley_options() | ||
if(index > newindex) { | ||
valid = true | ||
} else { | ||
$(el("form", "uid")).parsley(options).whenValidate({ | ||
group: 'step-' + index, | ||
}).done(function() { | ||
valid = true | ||
$(el("nav_section", "uid")).eq(index).addClass(el("nav_section", false, "--validated")) | ||
}) | ||
} | ||
return valid | ||
}, | ||
form() { | ||
//validate standard form (1 step) | ||
let options = this.parsley_options() | ||
$(el("form", "uid")).parsley(options) | ||
}, | ||
parsley_options() { | ||
//create parsley options array | ||
let options = { | ||
classHandler: function (element) { | ||
return element.$element.closest(el("field")) | ||
}, | ||
errorClass: el("field_error", false), | ||
errorsContainer: function(element) { | ||
return element.$element.closest(el("input")).find(el("input", true, "__status")) | ||
}, | ||
successClass: el("field_success", false), | ||
errorsWrapper: '<ul class="'+el("input_errors", false)+'"></ul>', | ||
} | ||
return options | ||
}, | ||
form_error() { | ||
window.Parsley.on('form:error', function() { | ||
|
||
}) | ||
}, | ||
field_error() { | ||
//field error event | ||
window.Parsley.on('field:error', function() { | ||
const id = $(this.$element).attr("id") | ||
uid($(this.$element)) | ||
$(el("nav_legend", 'uid', ' li[data-name="' + id + '"]')).addClass("error") | ||
const index = $(el("nav_section", "uid")).index(el("nav_section", "uid", "--active")) | ||
$(el("nav_section", "uid")).eq(index).removeClass(el("nav_section", false, "--validated")) | ||
}) | ||
}, | ||
field_success() { | ||
//field success event | ||
window.Parsley.on('field:success', function() { | ||
const id = $(this.$element).attr("id") | ||
uid($(this.$element)) | ||
$(el("nav_legend", "uid", ' li[data-name="' + id + '"]')).removeClass("error") | ||
}) | ||
}, | ||
i18n() { | ||
window.Parsley.addMessages('en', { | ||
defaultMessage: __("This value seems to be invalid", "formality"), | ||
type: { | ||
email: __("This value should be a valid email", "formality"), | ||
url: __("This value should be a valid url", "formality"), | ||
number: __("This value should be a valid number", "formality"), | ||
integer: __("This value should be a valid integer", "formality"), | ||
digits: __("This value should be digits", "formality"), | ||
alphanum: __("This value should be alphanumeric", "formality"), | ||
}, | ||
required: __("This value is required", "formality"), | ||
pattern: __("This value seems to be invalid", "formality"), | ||
min: /* translators: validation */ __("This value should be greater than or equal to %s", "formality"), | ||
max: /* translators: validation */ __("This value should be lower than or equal to %s", "formality"), | ||
range: /* translators: validation */ __("This value should be between %s and %s", "formality"), | ||
minlength: /* translators: validation */ __("This value is too short. It should have %s characters or more", "formality"), | ||
maxlength: /* translators: validation */ __("This value is too long. It should have %s characters or fewer", "formality"), | ||
length: /* translators: validation */ __("This value length is invalid. It should be between %s and %s characters long", "formality"), | ||
check: /* translators: validation */ __("You must select between %s and %s choices", "formality"), | ||
}); | ||
window.Parsley.setLocale('en'); | ||
}, | ||
checkRule(input, rule) { | ||
let valid = false; | ||
switch(rule) { | ||
case 'required': | ||
if(NodeList.prototype.isPrototypeOf(input)){ | ||
Array.prototype.forEach.call(input, function(single, i){ if(single.checked) { valid = true; } }) | ||
} else { | ||
valid = input.value !== '' | ||
} | ||
break; | ||
case 'email': | ||
valid = input.value.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/); | ||
break; | ||
case 'checked': | ||
valid = input.checked; | ||
break; | ||
case 'notchecked': | ||
valid = !input.checked; | ||
break; | ||
} | ||
return valid; | ||
}, | ||
validateField(field) { | ||
let validate = this; | ||
const type = field.getAttribute('data-type') | ||
const required = field.classList.contains(el("field", false, "--required")) | ||
let rules = 'rules' in fieldOptions[type] ? Object.keys(fieldOptions[type]['rules']) : [] | ||
const multiple = fieldOptions[type]['multiple'] | ||
if(required) { rules.unshift('required') } | ||
const input = multiple ? field.querySelectorAll('input, select, textarea') : field.querySelector('input, select, textarea') | ||
const status = field.querySelector(el("input_status")) | ||
let valid = true; | ||
let error = ''; | ||
if(!rules.includes('required') && !multiple && !input.value) { | ||
//skip validation | ||
} else { | ||
Array.prototype.forEach.call(rules, function(rule){ | ||
if(valid && !validate.checkRule(input, rule)) { | ||
error = rule == 'required' ? __("This value is required", "formality") : fieldOptions[type]['rules'][rule]; | ||
valid = false; | ||
} | ||
}) | ||
} | ||
field.classList.toggle(el("field", false, "--error"), !valid); | ||
status.innerHTML = !error ? '' : ('<div class="' + el("input_errors", false) + '">' + error + '</div>') | ||
return valid; | ||
}, | ||
validateForm(form) { | ||
let validate = this; | ||
let errors = false; | ||
let fields = document.querySelectorAll(el("field")) | ||
let firsterror = false; | ||
Array.prototype.forEach.call(fields, function(field, i){ | ||
const error = !validate.validateField(field) | ||
if(!errors && error) { | ||
firsterror = field.querySelector('input, select, textarea') | ||
} | ||
}) | ||
if(firsterror) { firsterror.focus() } | ||
return !errors | ||
} | ||
} |
Oops, something went wrong.