import from la bonne adresse and first refactoring for ouidade.com
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
root = root.FormFields = root.FormFields || {};
|
||||
|
||||
var ArrayField = function (el, form) {
|
||||
el = $(el);
|
||||
this.el = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']');
|
||||
|
||||
this.el.on('click', '[data-grav-array-action="add"]', this.add.bind(this));
|
||||
this.el.on('click', '[data-grav-array-action="rem"]', this.remove.bind(this));
|
||||
this.el.on('click', '[data-grav-array-action="addArrayItem"]', this.addArray.bind(this));
|
||||
this.el.on('click', '[data-grav-array-action="remArrayItem"]', this.removeArray.bind(this));
|
||||
this.el.on('keyup', '[data-grav-array-type="key"]', this.update.bind(this));
|
||||
this.el.on('keyup', '[data-grav-array-type="keyArray"]', this.updateArray.bind(this));
|
||||
this.el.on('keyup', '[data-grav-array-type="keyArraySubelement"]', this.updateArraySubelement.bind(this));
|
||||
};
|
||||
|
||||
ArrayField.getName = function () {
|
||||
return 'array';
|
||||
};
|
||||
|
||||
ArrayField.getTypes = function () {
|
||||
return [ 'array' ];
|
||||
};
|
||||
|
||||
ArrayField.prototype.valid = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
ArrayField.prototype.disabled = function() {
|
||||
return false;
|
||||
};
|
||||
|
||||
ArrayField.prototype.name = function(name) {
|
||||
if (name && !this.isValueOnly()) {
|
||||
this.el.data('grav-array-name', name);
|
||||
return name;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
return this.el.data('grav-array-name')
|
||||
};
|
||||
|
||||
ArrayField.prototype.isValueOnly = function() {
|
||||
return this.el.find('[data-grav-array-mode="value_only"]').length;
|
||||
};
|
||||
|
||||
ArrayField.prototype.value = function(val) {
|
||||
if (typeof val === 'object') {
|
||||
// Remove old
|
||||
this.el.find('[data-grav-array-type="row"]').remove();
|
||||
|
||||
var container = this.el.find('[data-grav-array-type="container"]');
|
||||
for (var key in val) { if (val.hasOwnProperty(key)) {
|
||||
container.append(this._getNewField(key, val[key]));
|
||||
}
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
var values = {};
|
||||
this.el.find('[data-grav-array-type="value"]').each(function () {
|
||||
var key = $(this).attr('name'),
|
||||
value = $(this).val();
|
||||
|
||||
values[key] = value;
|
||||
});
|
||||
|
||||
return values;
|
||||
};
|
||||
|
||||
ArrayField.prototype.reset = function() {
|
||||
this.value('');
|
||||
};
|
||||
|
||||
ArrayField.prototype.formValues = function() {
|
||||
var values = this.value(),
|
||||
name = this.name(),
|
||||
formValues = {};
|
||||
|
||||
for (var key in values) { if (values.hasOwnProperty(key)) {
|
||||
formValues[this.isValueOnly() ? key : name + '[' + key + ']'] = values[key];
|
||||
}
|
||||
}
|
||||
|
||||
return formValues;
|
||||
};
|
||||
|
||||
ArrayField.prototype.add = function(event) {
|
||||
$(this._getNewField()).insertAfter($(event.target).closest('[data-grav-array-type="row"]'));
|
||||
if (this.isValueOnly()) {
|
||||
this.refreshAll();
|
||||
}
|
||||
};
|
||||
|
||||
ArrayField.prototype.remove = function(event) {
|
||||
var row = $(event.target).closest('[data-grav-array-type="row"]');
|
||||
if (row.siblings().length == 0) {
|
||||
//on the last item we just clear its values
|
||||
row.find('input').val('');
|
||||
return;
|
||||
}
|
||||
row.remove();
|
||||
if (this.isValueOnly()) {
|
||||
this.refreshAll();
|
||||
}
|
||||
};
|
||||
|
||||
ArrayField.prototype.addArray = function(event) {
|
||||
$(this._getNewArrayField()).insertAfter($(event.target).closest('[data-grav-array-type="subrow"]'));
|
||||
};
|
||||
|
||||
ArrayField.prototype.removeArray = function(event) {
|
||||
if ($(event.target).closest('[data-grav-array-type="subrow"]').siblings().length == 0) {
|
||||
//disable for the last item
|
||||
return;
|
||||
}
|
||||
$(event.target).closest('[data-grav-array-type="subrow"]').remove();
|
||||
};
|
||||
|
||||
ArrayField.prototype.update = function(event) {
|
||||
var keyField = $(event.target),
|
||||
valueField = keyField.closest('[data-grav-array-type="row"]').find('[data-grav-array-type="value"]');
|
||||
|
||||
valueField.attr('name', this.getFieldName() + '[' + keyField.val() + ']');
|
||||
};
|
||||
|
||||
ArrayField.prototype.updateArray = function(event) {
|
||||
var keyField = $(event.target),
|
||||
row = keyField.closest('[data-grav-array-type="row"]'),
|
||||
valueFields = row.find('[data-grav-array-type="value"]'),
|
||||
keyArrayField = row.find('[data-grav-array-type="keyArray"]'),
|
||||
fieldName = this.getFieldName();
|
||||
|
||||
valueFields.each(function() {
|
||||
$(this).attr('name', fieldName + '[' + keyArrayField.val() + ']' + '[' + $(this).attr('subkey') + ']');
|
||||
});
|
||||
};
|
||||
|
||||
ArrayField.prototype.updateArraySubelement = function(event) {
|
||||
var keyField = $(event.target),
|
||||
row = keyField.closest('[data-grav-array-type="row"]'),
|
||||
subrow = keyField.closest('[data-grav-array-type="subrow"]'),
|
||||
valueFields = subrow.find('[data-grav-array-type="value"]'),
|
||||
keyArrayField = row.find('[data-grav-array-type="keyArray"]'),
|
||||
fieldName = this.getFieldName();
|
||||
|
||||
valueFields.each(function() {
|
||||
$(this).attr('name', fieldName + '[' + keyArrayField.val() + ']' + '[' + keyField.val() + ']');
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
ArrayField.prototype.refreshAll = function() {
|
||||
var that = this;
|
||||
this.el.find('[data-grav-array-type="value"]').each(function(index, element){
|
||||
$(element).attr('name', that.getFieldName() + '[' + index + ']');
|
||||
});
|
||||
};
|
||||
|
||||
ArrayField.prototype.getFieldName = function(element) {
|
||||
return this.el.data('grav-array-name');
|
||||
};
|
||||
|
||||
ArrayField.prototype._getNewField = function(key, value) {
|
||||
var name = this.name(),
|
||||
value_only = this.isValueOnly(),
|
||||
placeholder = {
|
||||
key: this.el.data('grav-array-keyname') || 'Key',
|
||||
val: this.el.data('grav-array-valuename') || 'Value'
|
||||
};
|
||||
|
||||
key = key || '';
|
||||
value = value || '';
|
||||
|
||||
var output;
|
||||
|
||||
if (value_only) {
|
||||
output = '<div class="form-row array-field-value_only" data-grav-array-type="row">' + "\n" +
|
||||
'<input data-grav-array-type="value" type="text" value="' + value + '" placeholder="' + placeholder.val + '" />' + "\n";
|
||||
} else {
|
||||
output = '<div class="form-row" data-grav-array-type="row">' + "\n" +
|
||||
'<input data-grav-array-type="key" type="text" value="' + key + '" placeholder="' + placeholder.key + '" />' + "\n" +
|
||||
'<input data-grav-array-type="value" type="text" name="' + key + '" value="' + value + '" placeholder="' + "\n" + placeholder.val + '" />';
|
||||
}
|
||||
|
||||
output += '<span data-grav-array-action="rem" class="fa fa-minus"></span>' + "\n" +
|
||||
'<span data-grav-array-action="add" class="fa fa-plus"></span>' + "\n" +
|
||||
'</div>';
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
ArrayField.prototype._getNewArrayField = function(key, value) {
|
||||
var output = '<div class="form-row" data-grav-array-type="subrow">' + "\n" +
|
||||
'<input data-grav-array-type="keyArraySubelement" type="text" value="" />' + "\n" +
|
||||
'<input data-grav-array-type="value" type="text" name="" value="" />';
|
||||
|
||||
output += '<span data-grav-array-action="remArrayItem" class="fa fa-minus-square"></span>' + "\n" +
|
||||
'<span data-grav-array-action="addArrayItem" class="fa fa-plus-square"></span>' + "\n" +
|
||||
'</div>';
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
root.Array = ArrayField;
|
||||
})();
|
||||
@@ -0,0 +1,100 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
root = root.FormFields = root.FormFields || {};
|
||||
|
||||
var CheckboxesField = function (el, form) {
|
||||
el = $(el);
|
||||
this.el = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']');
|
||||
|
||||
this.keys = this.el.data('grav-keys') || false;
|
||||
|
||||
this._disabled = this.el.data('grav-disabled') || false;
|
||||
this._default = this.el.data('grav-default') || '';
|
||||
};
|
||||
|
||||
CheckboxesField.getName = function () {
|
||||
return 'checkboxes';
|
||||
};
|
||||
|
||||
CheckboxesField.getTypes = function () {
|
||||
return [ 'checkboxes' ];
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.valid = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.disabled = function(state) {
|
||||
if (typeof state !== 'undefined') {
|
||||
this._disabled = state ? true : false;
|
||||
this.el.css('opacity', state ? 0.6 : 1);
|
||||
}
|
||||
|
||||
return this._disabled;
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.name = function(name) {
|
||||
if (name) {
|
||||
this.el.data('grav-field-name', name);
|
||||
return name;
|
||||
}
|
||||
|
||||
return this.el.data('grav-field-name')
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.value = function(val) {
|
||||
var useKeys = this.keys,
|
||||
values = useKeys ? {} : [];
|
||||
|
||||
if (typeof val !== 'undefined') {
|
||||
this.el.find('input').each(function () {
|
||||
var checked = false;
|
||||
|
||||
if (useKeys && typeof val[$(this).attr('name')] !== 'undefined') {
|
||||
checked = val[$(this).attr('name')];
|
||||
} else if (!useKeys && val.indexOf($(this).val()) !== -1) {
|
||||
checked = true;
|
||||
}
|
||||
|
||||
$(this).prop('checked', checked);
|
||||
});
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
this.el.find('input').each(function () {
|
||||
if (useKeys) {
|
||||
values[$(this).attr('name')] = $(this).is(':checked');
|
||||
} else if ($(this).is(':checked')) {
|
||||
values.push($(this).val());
|
||||
}
|
||||
});
|
||||
|
||||
return values;
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.reset = function() {
|
||||
this.value(this._default);
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.formValues = function() {
|
||||
var values = this.value(),
|
||||
name = this.name(),
|
||||
formValues = {};
|
||||
|
||||
for (var key in values) { if (values.hasOwnProperty(key)) {
|
||||
formValues[key] = values[key] ? '1' : '0';
|
||||
}
|
||||
}
|
||||
|
||||
return formValues;
|
||||
};
|
||||
|
||||
CheckboxesField.prototype.onChange = function(eh) {
|
||||
var self = this;
|
||||
this.el.find('input').on('change', function () { eh.call(self, self.value()); });
|
||||
};
|
||||
|
||||
root.Checkboxes = CheckboxesField;
|
||||
})();
|
||||
@@ -0,0 +1,73 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
root = root.FormFields = root.FormFields || {};
|
||||
|
||||
var Input = function (el, form) {
|
||||
el = $(el);
|
||||
|
||||
var parent = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']'),
|
||||
input = parent.prop('tagName').toUpperCase() === 'INPUT' ? parent : parent.find('input'),
|
||||
type = parent.data(form.fieldIndicator);
|
||||
|
||||
this.el = parent;
|
||||
this.input = input;
|
||||
|
||||
this._disabled = parent.data('grav-disabled') || false;
|
||||
this._default = parent.data('grav-default') || '';
|
||||
};
|
||||
|
||||
Input.getName = function () {
|
||||
return 'input';
|
||||
};
|
||||
|
||||
Input.getTypes = function () {
|
||||
return [ 'text', 'hidden' ];
|
||||
};
|
||||
|
||||
Input.prototype.valid = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
Input.prototype.disabled = function(state) {
|
||||
if (typeof state !== 'undefined') {
|
||||
this._disabled = state ? true : false;
|
||||
}
|
||||
|
||||
return this._disabled;
|
||||
};
|
||||
|
||||
Input.prototype.name = function(name) {
|
||||
if (name) {
|
||||
this.input.attr('name', name);
|
||||
return name;
|
||||
}
|
||||
|
||||
return this.input.attr('name')
|
||||
};
|
||||
|
||||
Input.prototype.value = function(val) {
|
||||
if (typeof val !== 'undefined') {
|
||||
this.input.val(val);
|
||||
}
|
||||
|
||||
return this.input.val();
|
||||
};
|
||||
|
||||
Input.prototype.reset = function() {
|
||||
this.value(this._default);
|
||||
};
|
||||
|
||||
Input.prototype.formValues = function() {
|
||||
var o = {};
|
||||
o[this.name()] = this.value();
|
||||
return o;
|
||||
};
|
||||
|
||||
Input.prototype.onChange = function(eh) {
|
||||
var self = this;
|
||||
this.input.on('keyup', function () { eh.call(self, self.value()); });
|
||||
};
|
||||
|
||||
root.Input = Input;
|
||||
})();
|
||||
@@ -0,0 +1,71 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
root = root.FormFields = root.FormFields || {};
|
||||
|
||||
var SelectizeField = function (el, form) {
|
||||
el = $(el);
|
||||
|
||||
var parent = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']'),
|
||||
tagName = parent.data('grav-field').toLowerCase() === 'select' ? 'SELECT' : 'INPUT',
|
||||
input = parent.prop('tagName').toUpperCase() === tagName ? parent : parent.find(tagName),
|
||||
type = parent.data(form.fieldIndicator);
|
||||
|
||||
input.selectize(parent.data('grav-selectize'));
|
||||
|
||||
this.el = parent;
|
||||
this.input = input;
|
||||
this.selectize = input[0].selectize;
|
||||
};
|
||||
|
||||
SelectizeField.getName = function () {
|
||||
return 'selectize';
|
||||
};
|
||||
|
||||
SelectizeField.getTypes = function () {
|
||||
return [ 'selectize', 'select'];
|
||||
};
|
||||
|
||||
SelectizeField.prototype.valid = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
SelectizeField.prototype.disabled = function() {
|
||||
return false;
|
||||
};
|
||||
|
||||
SelectizeField.prototype.name = function(name) {
|
||||
if (name) {
|
||||
this.input.attr('name', name);
|
||||
return name;
|
||||
}
|
||||
|
||||
return this.input.attr('name')
|
||||
};
|
||||
|
||||
SelectizeField.prototype.value = function(val) {
|
||||
if (typeof val !== 'undefined') {
|
||||
val = typeof val === 'string' ? val.length ? val.split(',') : [] : val;
|
||||
|
||||
for (var i = val.length - 1; i >= 0; i--) {
|
||||
this.selectize.addOption({ text: val[i], value: val[i] });
|
||||
}
|
||||
|
||||
this.selectize.setValue(val);
|
||||
}
|
||||
|
||||
return this.selectize.items;
|
||||
};
|
||||
|
||||
SelectizeField.prototype.reset = function() {
|
||||
this.value('');
|
||||
};
|
||||
|
||||
SelectizeField.prototype.formValues = function() {
|
||||
var o = {};
|
||||
o[this.name()] = this.value().join(',');
|
||||
return o;
|
||||
};
|
||||
|
||||
root.Selectize = SelectizeField;
|
||||
})();
|
||||
@@ -0,0 +1,59 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
root = root.FormFields = root.FormFields || {};
|
||||
|
||||
var ToggleField = function (el, form) {
|
||||
el = $(el);
|
||||
this.el = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']');
|
||||
|
||||
this._disabled = this.el.data('grav-disabled') || false;
|
||||
this._default = this.el.data('grav-default') || '';
|
||||
};
|
||||
|
||||
ToggleField.getName = function () {
|
||||
return 'toggle';
|
||||
};
|
||||
|
||||
ToggleField.getTypes = function () {
|
||||
return [ 'toggle' ];
|
||||
};
|
||||
|
||||
ToggleField.prototype.valid = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
ToggleField.prototype.disabled = function() {
|
||||
return false;
|
||||
};
|
||||
|
||||
ToggleField.prototype.name = function(name) {
|
||||
if (name) {
|
||||
this.el.data('grav-field-name', name);
|
||||
return name;
|
||||
}
|
||||
|
||||
return this.el.data('grav-field-name')
|
||||
};
|
||||
|
||||
ToggleField.prototype.value = function(val) {
|
||||
if (typeof val !== 'undefined') {
|
||||
this.el.find('input').prop('checked', false).filter('[value="' + val + '"]').prop('checked', true);
|
||||
return val;
|
||||
}
|
||||
|
||||
return this.el.find('input:checked').val();
|
||||
};
|
||||
|
||||
ToggleField.prototype.reset = function() {
|
||||
this.value(this._default);
|
||||
};
|
||||
|
||||
ToggleField.prototype.formValues = function() {
|
||||
var o = {};
|
||||
o[this.name()] = this.value();
|
||||
return o;
|
||||
};
|
||||
|
||||
root.Toggle = ToggleField;
|
||||
})();
|
||||
@@ -0,0 +1,335 @@
|
||||
(function () {
|
||||
var root = window || {};
|
||||
root = root.GravJS = root.GravJS || {};
|
||||
|
||||
root.clickedButton = null;
|
||||
$(document).on('click', 'button.task', function(e) {
|
||||
root.clickedButton = e.target;
|
||||
});
|
||||
|
||||
function addTypes (form, factory) {
|
||||
var name = factory.getName(),
|
||||
types = factory.getTypes();
|
||||
|
||||
for (var i = types.length - 1; i >= 0; i--) {
|
||||
form.types[types[i]] = name;
|
||||
|
||||
if (form.scanned) {
|
||||
scan(form, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function scan (form, type) {
|
||||
for (var i = form.elements.length - 1; i >= 0; i--) {
|
||||
if (!type || form.elements[i].type === type) {
|
||||
form.elements.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(form.types).length === 0 || (type && !form.types[type])) {
|
||||
return;
|
||||
}
|
||||
|
||||
form.findElements().each(function () {
|
||||
var el = $(this),
|
||||
type = el.data(form.dataIndicator);
|
||||
|
||||
if (type == 'textarea' || type == 'toggleable' || type == 'datetime') {
|
||||
var processSpan = function processSpan(element, toggleable) {
|
||||
var on = true;
|
||||
if (!toggleable) {
|
||||
on = $(element).find('input').is(':checked');
|
||||
}
|
||||
|
||||
$(element).find('label').css('opacity', on ? 1 : 0.7);
|
||||
$(element).siblings('label').css('opacity', on ? 1 : 0.7);
|
||||
|
||||
if (!on) {
|
||||
$(element).find('input').attr('checked', false).prop('value', 0);
|
||||
} else {
|
||||
$(element).find('input').attr('checked', true).prop('value', 1);
|
||||
}
|
||||
|
||||
var form_data = $(element).parent().siblings('.form-data');
|
||||
|
||||
if (on) {
|
||||
form_data.addClass('checked');
|
||||
} else {
|
||||
form_data.removeClass('checked');
|
||||
}
|
||||
|
||||
form_data.css('opacity', on ? 1 : 0.6);
|
||||
form_data.find('textarea').css('opacity', on ? 1 : 0.6);
|
||||
form_data.find('input').css('opacity', on ? 1 : 0.6);
|
||||
};
|
||||
|
||||
var processToggleables = function processToggleables(element) {
|
||||
var elType = $(element)[0].tagName.toLowerCase();
|
||||
|
||||
if (elType == 'checkbox') {
|
||||
var on = $(element).is(':checked');
|
||||
|
||||
$(element).siblings('label').css('opacity', on ? 1 : 0.7);
|
||||
$(element).parent().siblings('label').css('opacity', on ? 1 : 0.7);
|
||||
|
||||
if (!on) {
|
||||
$(element).attr('checked', false).prop('value', 0);
|
||||
} else {
|
||||
$(element).attr('checked', true).prop('value', 1);
|
||||
}
|
||||
|
||||
var form_data = $(element).parent().parent().siblings('.form-data')
|
||||
|
||||
form_data.css('opacity', on ? 1 : 0.6);
|
||||
form_data.find('textarea').css('opacity', on ? 1 : 0.6);
|
||||
form_data.find('input').css('opacity', on ? 1 : 0.6);
|
||||
}
|
||||
|
||||
if (elType == 'span') {
|
||||
processSpan(element);
|
||||
}
|
||||
};
|
||||
|
||||
el.on('change input propertychange', function() {
|
||||
processToggleables(this);
|
||||
});
|
||||
|
||||
el.find('input').on('change', function() {
|
||||
processToggleables(this);
|
||||
});
|
||||
|
||||
if ($(el)[0].className == 'checkboxes toggleable') {
|
||||
var toggles = $(el).parent().siblings('.form-data').find('label');
|
||||
toggles.on('click', function() {
|
||||
processSpan(el, true);
|
||||
});
|
||||
}
|
||||
|
||||
processToggleables(this);
|
||||
}
|
||||
|
||||
if (form.types[type]) {
|
||||
var factory = form.factories[form.types[type]],
|
||||
element = new factory(el, form),
|
||||
name = element.name();
|
||||
|
||||
if (typeof form.toggleables[name] !== 'undefined') {
|
||||
linkToggle(element, form.toggleables[name]);
|
||||
delete form.toggleables[name];
|
||||
}
|
||||
|
||||
el.data('grav-field-instance', element);
|
||||
|
||||
form.elements.push({ type: type, element: element });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function scanToggleable (form) {
|
||||
form.toggleables = {};
|
||||
form.findElements('toggleable').each(function () {
|
||||
var el = $(this);
|
||||
form.toggleables[el.data('grav-field-name')] = el;
|
||||
});
|
||||
}
|
||||
|
||||
function linkToggle (element, toggleable) {
|
||||
$(element).on('change', function (value) {
|
||||
toggleable.find('input').prop('checked', true);
|
||||
toggleable.siblings('label').css('opacity', 1);
|
||||
element.disabled(false);
|
||||
});
|
||||
|
||||
toggleable.find('input').on('change', function () {
|
||||
var el = $(this),
|
||||
on = el.is(':checked');
|
||||
|
||||
toggleable.siblings('label').css('opacity', on ? 1 : 0.7);
|
||||
element.disabled(!on);
|
||||
if (!on) {
|
||||
element.el.attr('checked', false).prop('value', 0);
|
||||
} else {
|
||||
element.el.attr('checked', true).prop('value', 1);
|
||||
}
|
||||
});
|
||||
|
||||
var on = toggleable.find('input').is(':checked');
|
||||
toggleable.siblings('label').css('opacity', on ? 1 : 0.7);
|
||||
element.disabled(!on);
|
||||
|
||||
if (!on) {
|
||||
element.el.attr('checked', false).prop('value', 0);
|
||||
} else {
|
||||
element.el.attr('checked', true).prop('value', 1);
|
||||
}
|
||||
}
|
||||
|
||||
var Form = function (el, options) {
|
||||
options = options || {};
|
||||
|
||||
this.form = $(el);
|
||||
this.form.data('grav-form-instance', this);
|
||||
this.form.on('submit', function (e) {
|
||||
this.submit(this.ajax);
|
||||
e.preventDefault();
|
||||
return false;
|
||||
}.bind(this));
|
||||
|
||||
this.scanned = false;
|
||||
|
||||
this.fieldIndicator = options.fieldIndicator || 'data-grav-field';
|
||||
this.dataIndicator = options.dataIndicator || (this.fieldIndicator.indexOf('data-') === 0 ? this.fieldIndicator.substr(5) : 'grav-field');
|
||||
this.ajax = options.ajax || false;
|
||||
|
||||
this.elements = [];
|
||||
|
||||
this.factories = {};
|
||||
this.types = {};
|
||||
|
||||
if (typeof options.globalFactories === 'undefined' || options.globalFactories) {
|
||||
for (var name in Form.factories) { if (Form.factories.hasOwnProperty(name)) {
|
||||
this.registerFactory(Form.factories[name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scanToggleable(this);
|
||||
scan(this);
|
||||
this.scanned = true;
|
||||
|
||||
//Refresh root.currentValues as toggleables have been initialized
|
||||
root.currentValues = getState();
|
||||
};
|
||||
|
||||
Form.factories = {};
|
||||
|
||||
Form.findElements = function(el, selector, notIn, notSelf) {
|
||||
el = $(el);
|
||||
notIn = notIn || selector,
|
||||
notSelf = notSelf ? true : false;
|
||||
|
||||
return el.find(selector).filter(function() {
|
||||
var parent = notSelf ? $(this) : $(this).parent();
|
||||
nearestMatch = parent.closest(notIn);
|
||||
return nearestMatch.length == 0 || el.find(nearestMatch).length == 0;
|
||||
});
|
||||
};
|
||||
|
||||
Form.registerFactory = function (factory, context) {
|
||||
context = context || Form.factories;
|
||||
context[factory.getName()] = factory;
|
||||
return true;
|
||||
};
|
||||
|
||||
Form.extendFactory = function (parentName, factory, context) {
|
||||
context = context || Form.factories;
|
||||
|
||||
if (!context[parentName]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Form.registerFactory(factory.getName(), $.extend({}, context[parentName], factory));
|
||||
};
|
||||
|
||||
Form.prototype.findElements = function(type) {
|
||||
var selector = '[' + this.fieldIndicator + (type ? '="' + type + '"' : '') + ']';
|
||||
|
||||
return Form.findElements(this.form, selector);
|
||||
};
|
||||
|
||||
Form.prototype.registerFactory = function(factory) {
|
||||
var registered = Form.registerFactory(factory, this.factories);
|
||||
|
||||
if (registered) {
|
||||
addTypes(this, this.factories[factory.getName()]);
|
||||
}
|
||||
};
|
||||
|
||||
Form.prototype.extendFactory = function(parentName, factory) {
|
||||
var registered = Form.extendFactory(parentN, factory, this.factories);
|
||||
|
||||
if (registered) {
|
||||
addTypes(this, this.factories[factory.getName()]);
|
||||
}
|
||||
};
|
||||
|
||||
Form.prototype.getElements = function() {
|
||||
if (!this.scanned) {
|
||||
scan(this);
|
||||
this.scanned = true;
|
||||
}
|
||||
|
||||
return this.elements;
|
||||
};
|
||||
|
||||
Form.prototype.getValues = function(all) {
|
||||
var elements = this.getElements(),
|
||||
values = {};
|
||||
|
||||
for (var i = elements.length - 1; i >= 0; i--) {
|
||||
var e = elements[i].element,
|
||||
isInDOM = $('body').find(e.el).length;
|
||||
|
||||
if (!all && (!isInDOM || !e.valid() || e.disabled())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$.extend(values, e.formValues());
|
||||
}
|
||||
|
||||
return values;
|
||||
};
|
||||
|
||||
Form.prototype.submit = function(ajax) {
|
||||
var action = this.form.attr('action') || document.location,
|
||||
method = this.form.attr('method') || 'POST',
|
||||
values = {};
|
||||
|
||||
// Get form values that are not handled by JS framework
|
||||
Form.findElements(this.form, 'input, textarea', '', false).each(function(input) {
|
||||
var input = $(this),
|
||||
name = input.attr('name'),
|
||||
parent = input.parent('[data-grav-disabled]'),
|
||||
value = input.val();
|
||||
|
||||
if (input.is(':disabled') || (parent && parent.data('grav-disabled') == 'true')) { return; }
|
||||
|
||||
if (name) {
|
||||
values[name] = value;
|
||||
}
|
||||
});
|
||||
|
||||
$.extend(values, this.getValues());
|
||||
|
||||
if ($(root.clickedButton).attr('name') == 'task') {
|
||||
values.task = $(root.clickedButton).attr('value');
|
||||
if (values.task == 'saveas') {
|
||||
values.lang = $(root.clickedButton).attr('lang');
|
||||
}
|
||||
if (values.task == 'switchlanguage') {
|
||||
values.lang = $(root.clickedButton).attr('lang');
|
||||
values.redirect = $(root.clickedButton).attr('redirect');
|
||||
}
|
||||
}
|
||||
|
||||
if (!values.task) {
|
||||
values.task = 'save';
|
||||
}
|
||||
|
||||
if (!ajax) {
|
||||
var form = $('<form>').attr({ method: method, action: action });
|
||||
|
||||
for (var name in values) { if (values.hasOwnProperty(name)) {
|
||||
$('<input>').attr({ type: 'hidden', name: name, value: values[name] }).appendTo(form);
|
||||
}
|
||||
}
|
||||
|
||||
return form.appendTo('body').submit();
|
||||
} else {
|
||||
return $.ajax({ method: method, url: action, data: values });
|
||||
}
|
||||
};
|
||||
|
||||
root.Form = Form;
|
||||
})();
|
||||
Reference in New Issue
Block a user