Skip to content

Commit

Permalink
Merge pull request #36 from michelegiorgi/development
Browse files Browse the repository at this point in the history
Release 1.4.1
  • Loading branch information
michelegiorgi authored Sep 27, 2021
2 parents 6e13547 + 9d8e00d commit c73e7fb
Show file tree
Hide file tree
Showing 25 changed files with 485 additions and 412 deletions.
8 changes: 5 additions & 3 deletions admin/class-formality-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ public function admin_header() {
if(!empty( $update_plugins->response )) {
$updates = array_keys($update_plugins->response);
foreach ($updates as $update) { if($update == 'formality/formality.php') { $updated = false; }}
}
echo '<span class="formality-header-version'. ($updated ? ' updated' : '') .'">' . $this->version . '</span>';
?>
}?>
<span class="formality-header-version<?php echo $updated ? ' updated' : '';?>">
<?php echo $this->version; ?>
<?php if(!$updated) {?><i><?php _e('There is a new version of Formality available', 'formality'); ?></i><?php } ?>
</span>
<?php
if($pagenow == 'edit.php' && $typenow == 'formality_result' && isset($object)) {
if(property_exists($object, 'term_id')) {
Expand Down
4 changes: 2 additions & 2 deletions assets/scripts/admin.js
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()
});
1 change: 0 additions & 1 deletion assets/scripts/admin/export.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { __ } = wp.i18n


export default function() {

let exportToggle = document.querySelector('.formality-export-toggle')
Expand Down
File renamed without changes.
7 changes: 1 addition & 6 deletions assets/scripts/editor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Formality editor scripts
import { pageLoad } from './editor/utility/init.js';

import './editor/components/repeaterControl.js';

import { pageLoad } from './editor/utility/init.js';
import { formSidebar } from './editor/plugins/sidebar.js';

import { textBlock } from './editor/blocks/text.js';
import { textareaBlock } from './editor/blocks/textarea.js';
import { emailBlock } from './editor/blocks/email.js';
Expand All @@ -22,9 +19,7 @@ import { widgetBlock } from './editor/blocks/widget.js';
if(formality.editor=='formality') {

pageLoad()

formSidebar()

textBlock()
textareaBlock()
emailBlock()
Expand Down
45 changes: 41 additions & 4 deletions assets/scripts/public.js
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()
});
1 change: 1 addition & 0 deletions assets/scripts/public/core/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export let el = (name, parent = true, child = "") => {
field_success: "formality__field--success",
field_disabled: "formality__field--disabled",
input: "formality__input",
input_status: "formality__input__status",
input_errors: "formality__input__errors",
message: "formality__message",
media: "formality__media",
Expand Down
32 changes: 14 additions & 18 deletions assets/scripts/public/core/nav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { el, uid } from './helpers'
import validate from './validate'
import uiux from './uiux'
import emergence from '../vendor/emergence.formality'

export default {
init() {
Expand Down Expand Up @@ -127,21 +126,17 @@ export default {
})
},
conversational() {
let emergence_container = document.querySelector('.formality__main');
let emergence_current = 0;
if($("body").hasClass("body-formality")) {
emergence_container = window;
}
emergence.init({
selector: el("field", "uid"),
container: emergence_container,
offsetY: "50%",
callback: function(element, state) {
if (state === 'visible') {
const $el = $(element);
let emergence_active = $el.attr("id");
if(emergence_current!==emergence_active) {
emergence_current = emergence_active;
let container = $("body").hasClass("body-formality") ? null : document.querySelector('.formality__main');
let current = 0;

const sections = document.querySelectorAll(el("field", "uid"));
for (let i = 0; i < sections.length; i++) {
const observer = new IntersectionObserver((entry) => {
if (entry[0].isIntersecting) {
const $el = $(sections[i]);
let active = $el.attr("id");
if(current!==active) {
current = active;
const sended = $el.closest(el("form", true, "--sended")).length
const sectionid = $el.attr("id")
const $navlist = $(el("nav_list", "uid"))
Expand All @@ -156,8 +151,9 @@ export default {
}
}
}
},
});
},{ root: container, rootMargin: "-50% 0px" });
observer.observe(sections[i]);
}

$(el("button", "uid", "--mininext")).click(function(e){
let $element = $(el("field_focus")).find(":input")
Expand Down
205 changes: 205 additions & 0 deletions assets/scripts/public/core/validate2.js
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
}
}
Loading

0 comments on commit c73e7fb

Please sign in to comment.